blob: 7d69ed9c98f977469ad6e616bc5732ce6834f304 [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"
Gabor Marton17c3eaf2019-07-01 12:44:39 +000060#include "llvm/ADT/ScopeExit.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000061#include "llvm/ADT/STLExtras.h"
62#include "llvm/ADT/SmallVector.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000065#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000066#include <algorithm>
67#include <cassert>
68#include <cstddef>
69#include <memory>
70#include <type_traits>
71#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000072
Douglas Gregor3c2404b2011-11-03 18:07:07 +000073namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000074
Balazs Keri3b30d652018-10-19 13:32:20 +000075 using llvm::make_error;
76 using llvm::Error;
77 using llvm::Expected;
78 using ExpectedType = llvm::Expected<QualType>;
79 using ExpectedStmt = llvm::Expected<Stmt *>;
80 using ExpectedExpr = llvm::Expected<Expr *>;
81 using ExpectedDecl = llvm::Expected<Decl *>;
82 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000083
Balazs Keri3b30d652018-10-19 13:32:20 +000084 std::string ImportError::toString() const {
85 // FIXME: Improve error texts.
86 switch (Error) {
87 case NameConflict:
88 return "NameConflict";
89 case UnsupportedConstruct:
90 return "UnsupportedConstruct";
91 case Unknown:
92 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000093 }
Balazs Keri2a13d662018-10-19 15:16:51 +000094 llvm_unreachable("Invalid error code.");
95 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000096 }
97
Balazs Keri3b30d652018-10-19 13:32:20 +000098 void ImportError::log(raw_ostream &OS) const {
99 OS << toString();
100 }
101
102 std::error_code ImportError::convertToErrorCode() const {
103 llvm_unreachable("Function not implemented.");
104 }
105
106 char ImportError::ID;
107
Gabor Marton5254e642018-06-27 13:32:50 +0000108 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000109 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000110 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000111 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000112 for (auto *R : D->getFirstDecl()->redecls()) {
113 if (R != D->getFirstDecl())
114 Redecls.push_back(R);
115 }
116 Redecls.push_back(D->getFirstDecl());
117 std::reverse(Redecls.begin(), Redecls.end());
118 return Redecls;
119 }
120
121 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000122 if (auto *FD = dyn_cast<FunctionDecl>(D))
123 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
124 if (auto *VD = dyn_cast<VarDecl>(D))
125 return getCanonicalForwardRedeclChain<VarDecl>(VD);
Gabor Marton7df342a2018-12-17 12:42:12 +0000126 if (auto *TD = dyn_cast<TagDecl>(D))
127 return getCanonicalForwardRedeclChain<TagDecl>(TD);
Gabor Martonac3a5d62018-09-17 12:04:52 +0000128 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000129 }
130
Gabor Marton26f72a92018-07-12 09:42:05 +0000131 void updateFlags(const Decl *From, Decl *To) {
132 // Check if some flags or attrs are new in 'From' and copy into 'To'.
133 // FIXME: Other flags or attrs?
134 if (From->isUsed(false) && !To->isUsed(false))
135 To->setIsUsed();
136 }
137
Balazs Keri3b30d652018-10-19 13:32:20 +0000138 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
139 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
140 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000141 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000142
Balazs Keri3b30d652018-10-19 13:32:20 +0000143 // Use this instead of Importer.importInto .
144 template <typename ImportT>
145 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
146 return Importer.importInto(To, From);
147 }
148
149 // Use this to import pointers of specific type.
150 template <typename ImportT>
151 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000152 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000153 if (ToOrErr)
154 To = cast_or_null<ImportT>(*ToOrErr);
155 return ToOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +0000156 }
157
158 // Call the import function of ASTImporter for a baseclass of type `T` and
159 // cast the return value to `T`.
160 template <typename T>
161 Expected<T *> import(T *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000162 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000163 if (!ToOrErr)
164 return ToOrErr.takeError();
165 return cast_or_null<T>(*ToOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +0000166 }
167
168 template <typename T>
169 Expected<T *> import(const T *From) {
170 return import(const_cast<T *>(From));
171 }
172
173 // Call the import function of ASTImporter for type `T`.
174 template <typename T>
175 Expected<T> import(const T &From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000176 return Importer.Import(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000177 }
178
Richard Smithb9fb1212019-05-06 03:47:15 +0000179 // Import an Optional<T> by importing the contained T, if any.
180 template<typename T>
181 Expected<Optional<T>> import(Optional<T> From) {
182 if (!From)
183 return Optional<T>();
184 return import(*From);
185 }
186
Balazs Keri3b30d652018-10-19 13:32:20 +0000187 template <class T>
188 Expected<std::tuple<T>>
189 importSeq(const T &From) {
190 Expected<T> ToOrErr = import(From);
191 if (!ToOrErr)
192 return ToOrErr.takeError();
193 return std::make_tuple<T>(std::move(*ToOrErr));
194 }
195
196 // Import multiple objects with a single function call.
197 // This should work for every type for which a variant of `import` exists.
198 // The arguments are processed from left to right and import is stopped on
199 // first error.
200 template <class THead, class... TTail>
201 Expected<std::tuple<THead, TTail...>>
202 importSeq(const THead &FromHead, const TTail &...FromTail) {
203 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
204 if (!ToHeadOrErr)
205 return ToHeadOrErr.takeError();
206 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
207 if (!ToTailOrErr)
208 return ToTailOrErr.takeError();
209 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
210 }
211
212// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000213 template <typename ToDeclT> struct CallOverloadedCreateFun {
214 template <typename... Args>
215 auto operator()(Args &&... args)
216 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
217 return ToDeclT::Create(std::forward<Args>(args)...);
218 }
219 };
220
221 // Always use these functions to create a Decl during import. There are
222 // certain tasks which must be done after the Decl was created, e.g. we
223 // must immediately register that as an imported Decl. The parameter `ToD`
224 // will be set to the newly created Decl or if had been imported before
225 // then to the already imported Decl. Returns a bool value set to true if
226 // the `FromD` had been imported before.
227 template <typename ToDeclT, typename FromDeclT, typename... Args>
228 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
229 Args &&... args) {
230 // There may be several overloads of ToDeclT::Create. We must make sure
231 // to call the one which would be chosen by the arguments, thus we use a
232 // wrapper for the overload set.
233 CallOverloadedCreateFun<ToDeclT> OC;
234 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
235 std::forward<Args>(args)...);
236 }
237 // Use this overload if a special Type is needed to be created. E.g if we
238 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
239 // then:
240 // TypedefNameDecl *ToTypedef;
241 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
242 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
243 typename... Args>
244 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
245 Args &&... args) {
246 CallOverloadedCreateFun<NewDeclT> OC;
247 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
248 std::forward<Args>(args)...);
249 }
250 // Use this version if a special create function must be
251 // used, e.g. CXXRecordDecl::CreateLambda .
252 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
253 typename... Args>
254 LLVM_NODISCARD bool
255 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
256 FromDeclT *FromD, Args &&... args) {
Gabor Marton303c98612019-06-25 08:00:51 +0000257 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 }
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001635
1636 // We use strict error handling in case of records and enums, but not
1637 // with e.g. namespaces.
1638 //
1639 // FIXME Clients of the ASTImporter should be able to choose an
1640 // appropriate error handling strategy for their needs. For instance,
1641 // they may not want to mark an entire namespace as erroneous merely
1642 // because there is an ODR error with two typedefs. As another example,
1643 // the client may allow EnumConstantDecls with same names but with
1644 // different values in two distinct translation units.
1645 bool AccumulateChildErrors = isa<TagDecl>(FromDC);
1646
1647 Error ChildErrors = Error::success();
Davide Italiano93a64ef2018-10-30 20:46:29 +00001648 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001649 for (auto *From : FromDC->decls()) {
1650 ExpectedDecl ImportedOrErr = import(From);
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001651 if (!ImportedOrErr) {
1652 if (AccumulateChildErrors)
1653 ChildErrors =
1654 joinErrors(std::move(ChildErrors), ImportedOrErr.takeError());
1655 else
1656 consumeError(ImportedOrErr.takeError());
1657 }
Douglas Gregor0a791672011-01-18 03:11:38 +00001658 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001659
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001660 return ChildErrors;
Douglas Gregor968d6332010-02-21 18:24:45 +00001661}
1662
Balazs Keri3b30d652018-10-19 13:32:20 +00001663Error ASTNodeImporter::ImportDeclContext(
1664 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1665 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1666 if (!ToDCOrErr)
1667 return ToDCOrErr.takeError();
1668 ToDC = *ToDCOrErr;
1669
1670 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1671 auto ToLexicalDCOrErr = Importer.ImportContext(
1672 FromD->getLexicalDeclContext());
1673 if (!ToLexicalDCOrErr)
1674 return ToLexicalDCOrErr.takeError();
1675 ToLexicalDC = *ToLexicalDCOrErr;
1676 } else
1677 ToLexicalDC = ToDC;
1678
1679 return Error::success();
1680}
1681
1682Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001683 const CXXRecordDecl *From, CXXRecordDecl *To) {
1684 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1685 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001686
Balazs Keri1d20cc22018-07-16 12:16:39 +00001687 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001688 if (FromM->isImplicit()) {
1689 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1690 if (!ToMOrErr)
1691 return ToMOrErr.takeError();
1692 }
1693
1694 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001695}
1696
Balazs Keri3b30d652018-10-19 13:32:20 +00001697static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1698 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001699 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00001700 if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef))
Balazs Keri57949eb2019-03-25 09:16:39 +00001701 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1702 else
1703 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001704 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001705 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001706}
1707
Balazs Keri3b30d652018-10-19 13:32:20 +00001708Error ASTNodeImporter::ImportDefinition(
1709 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001710 if (To->getDefinition() || To->isBeingDefined()) {
1711 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001712 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001713
Balazs Keri3b30d652018-10-19 13:32:20 +00001714 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001715 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001716
Douglas Gregore2e50d332010-12-01 01:36:18 +00001717 To->startDefinition();
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001718 // Complete the definition even if error is returned.
1719 // The RecordDecl may be already part of the AST so it is better to
1720 // have it in complete state even if something is wrong with it.
1721 auto DefinitionCompleter =
1722 llvm::make_scope_exit([To]() { To->completeDefinition(); });
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001723
Balazs Keri3b30d652018-10-19 13:32:20 +00001724 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1725 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001726
Douglas Gregore2e50d332010-12-01 01:36:18 +00001727 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001728 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1729 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1730 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001731
1732 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1733 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1734 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001735 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001736 ToData.Aggregate = FromData.Aggregate;
1737 ToData.PlainOldData = FromData.PlainOldData;
1738 ToData.Empty = FromData.Empty;
1739 ToData.Polymorphic = FromData.Polymorphic;
1740 ToData.Abstract = FromData.Abstract;
1741 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001742 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1743 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1744 ToData.HasBasesWithNonStaticDataMembers =
1745 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001746 ToData.HasPrivateFields = FromData.HasPrivateFields;
1747 ToData.HasProtectedFields = FromData.HasProtectedFields;
1748 ToData.HasPublicFields = FromData.HasPublicFields;
1749 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001750 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001751 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001752 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001753 ToData.HasUninitializedReferenceMember
1754 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001755 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001756 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1757 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001758 ToData.NeedOverloadResolutionForCopyConstructor
1759 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001760 ToData.NeedOverloadResolutionForMoveConstructor
1761 = FromData.NeedOverloadResolutionForMoveConstructor;
1762 ToData.NeedOverloadResolutionForMoveAssignment
1763 = FromData.NeedOverloadResolutionForMoveAssignment;
1764 ToData.NeedOverloadResolutionForDestructor
1765 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001766 ToData.DefaultedCopyConstructorIsDeleted
1767 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001768 ToData.DefaultedMoveConstructorIsDeleted
1769 = FromData.DefaultedMoveConstructorIsDeleted;
1770 ToData.DefaultedMoveAssignmentIsDeleted
1771 = FromData.DefaultedMoveAssignmentIsDeleted;
1772 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001773 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1774 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001775 ToData.HasConstexprNonCopyMoveConstructor
1776 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001777 ToData.HasDefaultedDefaultConstructor
1778 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001779 ToData.DefaultedDefaultConstructorIsConstexpr
1780 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001781 ToData.HasConstexprDefaultConstructor
1782 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001783 ToData.HasNonLiteralTypeFieldsOrBases
1784 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001785 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001786 ToData.UserProvidedDefaultConstructor
1787 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001788 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001789 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1790 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1791 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1792 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001793 ToData.ImplicitCopyAssignmentHasConstParam
1794 = FromData.ImplicitCopyAssignmentHasConstParam;
1795 ToData.HasDeclaredCopyConstructorWithConstParam
1796 = FromData.HasDeclaredCopyConstructorWithConstParam;
1797 ToData.HasDeclaredCopyAssignmentWithConstParam
1798 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001799
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001800 // Copy over the data stored in RecordDeclBits
1801 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1802
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001803 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001804 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001805 ExpectedType TyOrErr = import(Base1.getType());
1806 if (!TyOrErr)
1807 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001808
1809 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001810 if (Base1.isPackExpansion()) {
1811 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1812 EllipsisLoc = *LocOrErr;
1813 else
1814 return LocOrErr.takeError();
1815 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001816
1817 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001818 if (Error Err =
1819 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1820 return Err;
1821
1822 auto RangeOrErr = import(Base1.getSourceRange());
1823 if (!RangeOrErr)
1824 return RangeOrErr.takeError();
1825
1826 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1827 if (!TSIOrErr)
1828 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001829
Douglas Gregore2e50d332010-12-01 01:36:18 +00001830 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001831 new (Importer.getToContext()) CXXBaseSpecifier(
1832 *RangeOrErr,
1833 Base1.isVirtual(),
1834 Base1.isBaseOfClass(),
1835 Base1.getAccessSpecifierAsWritten(),
1836 *TSIOrErr,
1837 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001838 }
1839 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001840 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001841 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001842
Douglas Gregor2e15c842012-02-01 21:00:38 +00001843 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001844 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1845 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001846
Balazs Keri3b30d652018-10-19 13:32:20 +00001847 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001848}
1849
Balazs Keri3b30d652018-10-19 13:32:20 +00001850Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001851 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001852 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001853
Gabor Martonac3a5d62018-09-17 12:04:52 +00001854 Expr *FromInit = From->getInit();
1855 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001856 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001857
Balazs Keri3b30d652018-10-19 13:32:20 +00001858 ExpectedExpr ToInitOrErr = import(FromInit);
1859 if (!ToInitOrErr)
1860 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001861
Balazs Keri3b30d652018-10-19 13:32:20 +00001862 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001863 if (From->isInitKnownICE()) {
1864 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1865 Eval->CheckedICE = true;
1866 Eval->IsICE = From->isInitICE();
1867 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001868
1869 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001870 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001871}
1872
Balazs Keri3b30d652018-10-19 13:32:20 +00001873Error ASTNodeImporter::ImportDefinition(
1874 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001875 if (To->getDefinition() || To->isBeingDefined()) {
1876 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001877 return ImportDeclContext(From, /*ForceImport=*/true);
1878 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001879 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001880
Douglas Gregord451ea92011-07-29 23:31:30 +00001881 To->startDefinition();
1882
Balazs Keri3b30d652018-10-19 13:32:20 +00001883 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1884 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001885
Balazs Keri3b30d652018-10-19 13:32:20 +00001886 ExpectedType ToTypeOrErr =
1887 import(Importer.getFromContext().getTypeDeclType(From));
1888 if (!ToTypeOrErr)
1889 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001890
Balazs Keri3b30d652018-10-19 13:32:20 +00001891 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1892 if (!ToPromotionTypeOrErr)
1893 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001894
1895 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001896 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1897 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001898
Douglas Gregord451ea92011-07-29 23:31:30 +00001899 // FIXME: we might need to merge the number of positive or negative bits
1900 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001901 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001902 From->getNumPositiveBits(),
1903 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001904 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001905}
1906
Balazs Keri3b30d652018-10-19 13:32:20 +00001907Error ASTNodeImporter::ImportTemplateArguments(
1908 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1909 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001910 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001911 if (auto ToOrErr = import(FromArgs[I]))
1912 ToArgs.push_back(*ToOrErr);
1913 else
1914 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001915 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001916
Balazs Keri3b30d652018-10-19 13:32:20 +00001917 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001918}
1919
Balazs Keri3b30d652018-10-19 13:32:20 +00001920// FIXME: Do not forget to remove this and use only 'import'.
1921Expected<TemplateArgument>
1922ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1923 return import(From);
1924}
1925
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001926template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001927Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001928 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1929 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001930 if (auto ToLocOrErr = import(FromLoc))
1931 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001932 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001933 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001934 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001935 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001936}
1937
Gabor Marton26f72a92018-07-12 09:42:05 +00001938static StructuralEquivalenceKind
1939getStructuralEquivalenceKind(const ASTImporter &Importer) {
1940 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1941 : StructuralEquivalenceKind::Default;
1942}
1943
Gabor Marton950fb572018-07-17 12:39:27 +00001944bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1945 StructuralEquivalenceContext Ctx(
1946 Importer.getFromContext(), Importer.getToContext(),
1947 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1948 false, Complain);
1949 return Ctx.IsEquivalent(From, To);
1950}
1951
1952bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001953 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001954 // Eliminate a potential failure point where we attempt to re-import
1955 // something we're trying to import while completing ToRecord.
1956 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1957 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001958 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001959 if (ToOriginRecord)
1960 ToRecord = ToOriginRecord;
1961 }
1962
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001963 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001964 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001965 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001966 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001967 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001968 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001969}
1970
Larisse Voufo39a1e502013-08-06 01:03:05 +00001971bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1972 bool Complain) {
1973 StructuralEquivalenceContext Ctx(
1974 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001975 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1976 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001977 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001978}
1979
Douglas Gregor98c10182010-02-12 22:17:39 +00001980bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001981 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00001982 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001983 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
1984 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
1985 ToEnum = ToOriginEnum;
1986
Gabor Marton26f72a92018-07-12 09:42:05 +00001987 StructuralEquivalenceContext Ctx(
1988 Importer.getFromContext(), Importer.getToContext(),
1989 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001990 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001991}
1992
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001993bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1994 FunctionTemplateDecl *To) {
1995 StructuralEquivalenceContext Ctx(
1996 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001997 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1998 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001999 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002000}
2001
Balazs Keric7797c42018-07-11 09:37:24 +00002002bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2003 StructuralEquivalenceContext Ctx(
2004 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002005 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2006 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002007 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002008}
2009
Douglas Gregor91155082012-11-14 22:29:20 +00002010bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002011 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002012 const llvm::APSInt &FromVal = FromEC->getInitVal();
2013 const llvm::APSInt &ToVal = ToEC->getInitVal();
2014
2015 return FromVal.isSigned() == ToVal.isSigned() &&
2016 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2017 FromVal == ToVal;
2018}
2019
2020bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002021 ClassTemplateDecl *To) {
2022 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2023 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002024 Importer.getNonEquivalentDecls(),
2025 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002026 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002027}
2028
Larisse Voufo39a1e502013-08-06 01:03:05 +00002029bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2030 VarTemplateDecl *To) {
2031 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2032 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002033 Importer.getNonEquivalentDecls(),
2034 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002035 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002036}
2037
Balazs Keri3b30d652018-10-19 13:32:20 +00002038ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002039 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002040 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002041 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002042}
2043
Balazs Keri3b30d652018-10-19 13:32:20 +00002044ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2045 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2046 << D->getDeclKindName();
2047 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2048}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002049
Balazs Keri3b30d652018-10-19 13:32:20 +00002050ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2051 // Import the context of this declaration.
2052 DeclContext *DC, *LexicalDC;
2053 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2054 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002055
2056 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002057 ExpectedSLoc LocOrErr = import(D->getLocation());
2058 if (!LocOrErr)
2059 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002060
Gabor Marton26f72a92018-07-12 09:42:05 +00002061 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002062 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002063 return ToD;
2064
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002065 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002066 LexicalDC->addDeclInternal(ToD);
2067 return ToD;
2068}
2069
Balazs Keri3b30d652018-10-19 13:32:20 +00002070ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002071 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002072 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002073
Gabor Marton26f72a92018-07-12 09:42:05 +00002074 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002075
Sean Callanan65198272011-11-17 23:20:56 +00002076 return ToD;
2077}
2078
Balazs Keri3b30d652018-10-19 13:32:20 +00002079ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2080 ExpectedSLoc LocOrErr = import(D->getLocation());
2081 if (!LocOrErr)
2082 return LocOrErr.takeError();
2083 auto ColonLocOrErr = import(D->getColonLoc());
2084 if (!ColonLocOrErr)
2085 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002086
2087 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002088 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2089 if (!DCOrErr)
2090 return DCOrErr.takeError();
2091 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002092
Gabor Marton26f72a92018-07-12 09:42:05 +00002093 AccessSpecDecl *ToD;
2094 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002095 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002096 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002097
2098 // Lexical DeclContext and Semantic DeclContext
2099 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002100 ToD->setLexicalDeclContext(DC);
2101 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002102
Gabor Marton26f72a92018-07-12 09:42:05 +00002103 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002104}
2105
Balazs Keri3b30d652018-10-19 13:32:20 +00002106ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2107 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2108 if (!DCOrErr)
2109 return DCOrErr.takeError();
2110 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002111 DeclContext *LexicalDC = DC;
2112
Balazs Keri3b30d652018-10-19 13:32:20 +00002113 SourceLocation ToLocation, ToRParenLoc;
2114 Expr *ToAssertExpr;
2115 StringLiteral *ToMessage;
2116 if (auto Imp = importSeq(
2117 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2118 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2119 else
2120 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002121
Gabor Marton26f72a92018-07-12 09:42:05 +00002122 StaticAssertDecl *ToD;
2123 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002124 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2125 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002126 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002127
2128 ToD->setLexicalDeclContext(LexicalDC);
2129 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002130 return ToD;
2131}
2132
Balazs Keri3b30d652018-10-19 13:32:20 +00002133ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002134 // Import the major distinguishing characteristics of this namespace.
2135 DeclContext *DC, *LexicalDC;
2136 DeclarationName Name;
2137 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002138 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002139 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2140 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002141 if (ToD)
2142 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002143
2144 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002145 if (!Name) {
2146 // This is an anonymous namespace. Adopt an existing anonymous
2147 // namespace if we can.
2148 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002149 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002150 MergeWithNamespace = TU->getAnonymousNamespace();
2151 else
2152 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2153 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002154 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002155 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002156 for (auto *FoundDecl : FoundDecls) {
2157 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002158 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002159
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002160 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002161 MergeWithNamespace = FoundNS;
2162 ConflictingDecls.clear();
2163 break;
2164 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002165
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002166 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002167 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002168
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002169 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002170 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002171 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002172 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002173 if (!Name)
2174 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002175 }
2176 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002177
Balazs Keri3b30d652018-10-19 13:32:20 +00002178 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2179 if (!BeginLocOrErr)
2180 return BeginLocOrErr.takeError();
2181
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002182 // Create the "to" namespace, if needed.
2183 NamespaceDecl *ToNamespace = MergeWithNamespace;
2184 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002185 if (GetImportedOrCreateDecl(
2186 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002187 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002188 /*PrevDecl=*/nullptr))
2189 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002190 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002191 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002192
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002193 // If this is an anonymous namespace, register it as the anonymous
2194 // namespace within its context.
2195 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002196 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002197 TU->setAnonymousNamespace(ToNamespace);
2198 else
2199 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2200 }
2201 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002202 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002203
Balazs Keri3b30d652018-10-19 13:32:20 +00002204 if (Error Err = ImportDeclContext(D))
2205 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002206
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002207 return ToNamespace;
2208}
2209
Balazs Keri3b30d652018-10-19 13:32:20 +00002210ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002211 // Import the major distinguishing characteristics of this namespace.
2212 DeclContext *DC, *LexicalDC;
2213 DeclarationName Name;
2214 SourceLocation Loc;
2215 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002216 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2217 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002218 if (LookupD)
2219 return LookupD;
2220
2221 // NOTE: No conflict resolution is done for namespace aliases now.
2222
Balazs Keri3b30d652018-10-19 13:32:20 +00002223 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2224 NestedNameSpecifierLoc ToQualifierLoc;
2225 NamespaceDecl *ToNamespace;
2226 if (auto Imp = importSeq(
2227 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2228 D->getTargetNameLoc(), D->getNamespace()))
2229 std::tie(
2230 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2231 ToNamespace) = *Imp;
2232 else
2233 return Imp.takeError();
2234 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002235
Gabor Marton26f72a92018-07-12 09:42:05 +00002236 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002237 if (GetImportedOrCreateDecl(
2238 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2239 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002240 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002241
2242 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002243 LexicalDC->addDeclInternal(ToD);
2244
2245 return ToD;
2246}
2247
Balazs Keri3b30d652018-10-19 13:32:20 +00002248ExpectedDecl
2249ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002250 // Import the major distinguishing characteristics of this typedef.
2251 DeclContext *DC, *LexicalDC;
2252 DeclarationName Name;
2253 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002254 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002255 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2256 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002257 if (ToD)
2258 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002259
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002260 // If this typedef is not in block scope, determine whether we've
2261 // seen a typedef with the same name (that we can merge with) or any
2262 // other entity by that name (which name lookup could conflict with).
2263 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002264 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002265 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002266 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002267 for (auto *FoundDecl : FoundDecls) {
2268 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002269 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002270 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002271 QualType FromUT = D->getUnderlyingType();
2272 QualType FoundUT = FoundTypedef->getUnderlyingType();
2273 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2274 // If the "From" context has a complete underlying type but we
2275 // already have a complete underlying type then return with that.
2276 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002277 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002278 }
Gabor Martondd2b76e2019-06-11 13:35:25 +00002279 // FIXME Handle redecl chain. When you do that make consistent changes
2280 // in ASTImporterLookupTable too.
Gabor Martonb93baf62018-11-27 09:51:36 +00002281 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002282 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002283
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002284 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002285 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002286
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002287 if (!ConflictingDecls.empty()) {
2288 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002289 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002290 ConflictingDecls.size());
2291 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002292 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002293 }
2294 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002295
Balazs Keri3b30d652018-10-19 13:32:20 +00002296 QualType ToUnderlyingType;
2297 TypeSourceInfo *ToTypeSourceInfo;
2298 SourceLocation ToBeginLoc;
2299 if (auto Imp = importSeq(
2300 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2301 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2302 else
2303 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002304
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002305 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002306 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002307 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002308 if (IsAlias) {
2309 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002310 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2311 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002312 return ToTypedef;
2313 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002314 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2315 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002316 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002317
Douglas Gregordd483172010-02-22 17:42:47 +00002318 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002319 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002320
2321 // Templated declarations should not appear in DeclContext.
2322 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2323 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2324 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002325
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002326 return ToTypedef;
2327}
2328
Balazs Keri3b30d652018-10-19 13:32:20 +00002329ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002330 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2331}
2332
Balazs Keri3b30d652018-10-19 13:32:20 +00002333ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002334 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2335}
2336
Balazs Keri3b30d652018-10-19 13:32:20 +00002337ExpectedDecl
2338ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002339 // Import the major distinguishing characteristics of this typedef.
2340 DeclContext *DC, *LexicalDC;
2341 DeclarationName Name;
2342 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002343 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002344 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2345 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002346 if (FoundD)
2347 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002348
2349 // If this typedef is not in block scope, determine whether we've
2350 // seen a typedef with the same name (that we can merge with) or any
2351 // other entity by that name (which name lookup could conflict with).
2352 if (!DC->isFunctionOrMethod()) {
2353 SmallVector<NamedDecl *, 4> ConflictingDecls;
2354 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002355 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002356 for (auto *FoundDecl : FoundDecls) {
2357 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002358 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002359 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002360 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002361 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002362 }
2363
2364 if (!ConflictingDecls.empty()) {
2365 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2366 ConflictingDecls.data(),
2367 ConflictingDecls.size());
2368 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002369 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002370 }
2371 }
2372
Balazs Keri3b30d652018-10-19 13:32:20 +00002373 TemplateParameterList *ToTemplateParameters;
2374 TypeAliasDecl *ToTemplatedDecl;
2375 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2376 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2377 else
2378 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002379
Gabor Marton26f72a92018-07-12 09:42:05 +00002380 TypeAliasTemplateDecl *ToAlias;
2381 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002382 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002383 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002384
Balazs Keri3b30d652018-10-19 13:32:20 +00002385 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002386
Gabor Horvath7a91c082017-11-14 11:30:38 +00002387 ToAlias->setAccess(D->getAccess());
2388 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002389 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002390 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002391}
2392
Balazs Keri3b30d652018-10-19 13:32:20 +00002393ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002394 // Import the major distinguishing characteristics of this label.
2395 DeclContext *DC, *LexicalDC;
2396 DeclarationName Name;
2397 SourceLocation Loc;
2398 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002399 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2400 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002401 if (ToD)
2402 return ToD;
2403
2404 assert(LexicalDC->isFunctionOrMethod());
2405
Gabor Marton26f72a92018-07-12 09:42:05 +00002406 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002407 if (D->isGnuLocal()) {
2408 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2409 if (!BeginLocOrErr)
2410 return BeginLocOrErr.takeError();
2411 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2412 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2413 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002414
Balazs Keri3b30d652018-10-19 13:32:20 +00002415 } else {
2416 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2417 Name.getAsIdentifierInfo()))
2418 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002419
Balazs Keri3b30d652018-10-19 13:32:20 +00002420 }
2421
2422 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2423 if (!ToStmtOrErr)
2424 return ToStmtOrErr.takeError();
2425
2426 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002427 ToLabel->setLexicalDeclContext(LexicalDC);
2428 LexicalDC->addDeclInternal(ToLabel);
2429 return ToLabel;
2430}
2431
Balazs Keri3b30d652018-10-19 13:32:20 +00002432ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002433 // Import the major distinguishing characteristics of this enum.
2434 DeclContext *DC, *LexicalDC;
2435 DeclarationName Name;
2436 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002437 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002438 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2439 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002440 if (ToD)
2441 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002442
Douglas Gregor98c10182010-02-12 22:17:39 +00002443 // Figure out what enum name we're looking for.
2444 unsigned IDNS = Decl::IDNS_Tag;
2445 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002446 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002447 if (Error Err = importInto(
2448 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2449 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002450 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002451 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002452 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002453
Douglas Gregor98c10182010-02-12 22:17:39 +00002454 // We may already have an enum of the same name; try to find and match it.
2455 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002456 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002457 auto FoundDecls =
2458 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002459 for (auto *FoundDecl : FoundDecls) {
2460 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002461 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002462
Balazs Keri3b30d652018-10-19 13:32:20 +00002463 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002464 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002465 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002466 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002467
Balazs Keri3b30d652018-10-19 13:32:20 +00002468 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002469 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002470 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002471 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002472
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002473 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002474 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002475
Douglas Gregor98c10182010-02-12 22:17:39 +00002476 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002477 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002478 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002479 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002480 if (!Name)
2481 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002482 }
2483 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002484
Balazs Keri3b30d652018-10-19 13:32:20 +00002485 SourceLocation ToBeginLoc;
2486 NestedNameSpecifierLoc ToQualifierLoc;
2487 QualType ToIntegerType;
2488 if (auto Imp = importSeq(
2489 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2490 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2491 else
2492 return Imp.takeError();
2493
Douglas Gregor98c10182010-02-12 22:17:39 +00002494 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002495 EnumDecl *D2;
2496 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002497 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002498 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2499 D->isScopedUsingClassTag(), D->isFixed()))
2500 return D2;
2501
Balazs Keri3b30d652018-10-19 13:32:20 +00002502 D2->setQualifierInfo(ToQualifierLoc);
2503 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002504 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002505 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002506 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002507
Douglas Gregor98c10182010-02-12 22:17:39 +00002508 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002509 if (D->isCompleteDefinition())
2510 if (Error Err = ImportDefinition(D, D2))
2511 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002512
Douglas Gregor3996e242010-02-15 22:01:00 +00002513 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002514}
2515
Balazs Keri3b30d652018-10-19 13:32:20 +00002516ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002517 bool IsFriendTemplate = false;
2518 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2519 IsFriendTemplate =
2520 DCXX->getDescribedClassTemplate() &&
2521 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2522 Decl::FOK_None;
2523 }
2524
Douglas Gregor5c73e912010-02-11 00:48:18 +00002525 // Import the major distinguishing characteristics of this record.
2526 DeclContext *DC, *LexicalDC;
2527 DeclarationName Name;
2528 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002529 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002530 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2531 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002532 if (ToD)
2533 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002534
Douglas Gregor5c73e912010-02-11 00:48:18 +00002535 // Figure out what structure name we're looking for.
2536 unsigned IDNS = Decl::IDNS_Tag;
2537 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002538 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002539 if (Error Err = importInto(
2540 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2541 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002542 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002543 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002544 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002545
2546 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002547 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002548 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002549 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002550 auto FoundDecls =
2551 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002552 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002553 // We're going to have to compare D against potentially conflicting Decls,
2554 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002555 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2556 D->getASTContext().getExternalSource()->CompleteType(D);
2557 }
2558
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002559 for (auto *FoundDecl : FoundDecls) {
2560 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002561 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002562
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002563 Decl *Found = FoundDecl;
2564 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2565 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002566 Found = Tag->getDecl();
2567 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002568
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002569 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002570 // Do not emit false positive diagnostic in case of unnamed
2571 // struct/union and in case of anonymous structs. Would be false
2572 // because there may be several anonymous/unnamed structs in a class.
2573 // E.g. these are both valid:
2574 // struct A { // unnamed structs
2575 // struct { struct A *next; } entry0;
2576 // struct { struct A *next; } entry1;
2577 // };
2578 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2579 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002580 if (!IsStructuralMatch(D, FoundRecord, false))
2581 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002582
Balazs Keric8272192019-05-27 09:36:00 +00002583 if (!hasSameVisibilityContext(FoundRecord, D))
2584 continue;
2585
Gabor Marton7df342a2018-12-17 12:42:12 +00002586 if (IsStructuralMatch(D, FoundRecord)) {
2587 RecordDecl *FoundDef = FoundRecord->getDefinition();
2588 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002589 // FIXME: Structural equivalence check should check for same
2590 // user-defined methods.
2591 Importer.MapImported(D, FoundDef);
2592 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2593 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2594 assert(FoundCXX && "Record type mismatch");
2595
Gabor Marton7df342a2018-12-17 12:42:12 +00002596 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002597 // FoundDef may not have every implicit method that D has
2598 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002599 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2600 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002601 }
Douglas Gregor25791052010-02-12 00:09:27 +00002602 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002603 PrevDecl = FoundRecord->getMostRecentDecl();
2604 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002605 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002606 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002607
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002608 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002609 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002610
Douglas Gregordd6006f2012-07-17 21:16:27 +00002611 if (!ConflictingDecls.empty() && SearchName) {
Shafik Yaghmour468724e2019-05-24 16:53:44 +00002612 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002613 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002614 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002615 if (!Name)
2616 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002617 }
2618 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002619
Balazs Keri3b30d652018-10-19 13:32:20 +00002620 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2621 if (!BeginLocOrErr)
2622 return BeginLocOrErr.takeError();
2623
Douglas Gregor5c73e912010-02-11 00:48:18 +00002624 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002625 RecordDecl *D2 = nullptr;
2626 CXXRecordDecl *D2CXX = nullptr;
2627 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2628 if (DCXX->isLambda()) {
2629 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2630 if (!TInfoOrErr)
2631 return TInfoOrErr.takeError();
2632 if (GetImportedOrCreateSpecialDecl(
2633 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2634 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2635 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2636 return D2CXX;
2637 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2638 if (!CDeclOrErr)
2639 return CDeclOrErr.takeError();
2640 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2641 } else if (DCXX->isInjectedClassName()) {
2642 // We have to be careful to do a similar dance to the one in
2643 // Sema::ActOnStartCXXMemberDeclarations
2644 const bool DelayTypeCreation = true;
2645 if (GetImportedOrCreateDecl(
2646 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2647 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2648 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2649 return D2CXX;
2650 Importer.getToContext().getTypeDeclType(
2651 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2652 } else {
2653 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2654 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2655 Name.getAsIdentifierInfo(),
2656 cast_or_null<CXXRecordDecl>(PrevDecl)))
2657 return D2CXX;
2658 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002659
Gabor Marton7df342a2018-12-17 12:42:12 +00002660 D2 = D2CXX;
2661 D2->setAccess(D->getAccess());
2662 D2->setLexicalDeclContext(LexicalDC);
2663 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2664 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002665
Gabor Marton7df342a2018-12-17 12:42:12 +00002666 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2667 DC->makeDeclVisibleInContext(D2);
2668
2669 if (ClassTemplateDecl *FromDescribed =
2670 DCXX->getDescribedClassTemplate()) {
2671 ClassTemplateDecl *ToDescribed;
2672 if (Error Err = importInto(ToDescribed, FromDescribed))
2673 return std::move(Err);
2674 D2CXX->setDescribedClassTemplate(ToDescribed);
2675 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2676 // In a record describing a template the type should be an
2677 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2678 // previously set type to the correct value here (ToDescribed is not
2679 // available at record create).
2680 // FIXME: The previous type is cleared but not removed from
2681 // ASTContext's internal storage.
2682 CXXRecordDecl *Injected = nullptr;
2683 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2684 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2685 if (Record && Record->isInjectedClassName()) {
2686 Injected = Record;
2687 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002688 }
2689 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002690 // Create an injected type for the whole redecl chain.
2691 SmallVector<Decl *, 2> Redecls =
2692 getCanonicalForwardRedeclChain(D2CXX);
2693 for (auto *R : Redecls) {
2694 auto *RI = cast<CXXRecordDecl>(R);
2695 RI->setTypeForDecl(nullptr);
2696 // Below we create a new injected type and assign that to the
2697 // canonical decl, subsequent declarations in the chain will reuse
2698 // that type.
2699 Importer.getToContext().getInjectedClassNameType(
2700 RI, ToDescribed->getInjectedClassNameSpecialization());
2701 }
2702 // Set the new type for the previous injected decl too.
2703 if (Injected) {
2704 Injected->setTypeForDecl(nullptr);
2705 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2706 }
2707 }
2708 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002709 DCXX->getMemberSpecializationInfo()) {
2710 TemplateSpecializationKind SK =
2711 MemberInfo->getTemplateSpecializationKind();
2712 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002713
2714 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2715 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2716 else
2717 return ToInstOrErr.takeError();
2718
2719 if (ExpectedSLoc POIOrErr =
2720 import(MemberInfo->getPointOfInstantiation()))
2721 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2722 *POIOrErr);
2723 else
2724 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002725 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002726
Gabor Marton7df342a2018-12-17 12:42:12 +00002727 } else {
2728 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2729 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2730 Name.getAsIdentifierInfo(), PrevDecl))
2731 return D2;
2732 D2->setLexicalDeclContext(LexicalDC);
2733 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002734 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002735
Gabor Marton7df342a2018-12-17 12:42:12 +00002736 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2737 D2->setQualifierInfo(*QualifierLocOrErr);
2738 else
2739 return QualifierLocOrErr.takeError();
2740
2741 if (D->isAnonymousStructOrUnion())
2742 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002743
Balazs Keri3b30d652018-10-19 13:32:20 +00002744 if (D->isCompleteDefinition())
2745 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2746 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002747
Douglas Gregor3996e242010-02-15 22:01:00 +00002748 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002749}
2750
Balazs Keri3b30d652018-10-19 13:32:20 +00002751ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002752 // Import the major distinguishing characteristics of this enumerator.
2753 DeclContext *DC, *LexicalDC;
2754 DeclarationName Name;
2755 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002756 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002757 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2758 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002759 if (ToD)
2760 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002761
Fangrui Song6907ce22018-07-30 19:24:48 +00002762 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002763 // in the same context.
2764 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002765 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002766 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002767 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002768 for (auto *FoundDecl : FoundDecls) {
2769 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002770 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002771
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002772 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002773 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002774 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002775 }
2776
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002777 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002778 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002779
Douglas Gregor98c10182010-02-12 22:17:39 +00002780 if (!ConflictingDecls.empty()) {
2781 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002782 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002783 ConflictingDecls.size());
2784 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002785 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002786 }
2787 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002788
Balazs Keri3b30d652018-10-19 13:32:20 +00002789 ExpectedType TypeOrErr = import(D->getType());
2790 if (!TypeOrErr)
2791 return TypeOrErr.takeError();
2792
2793 ExpectedExpr InitOrErr = import(D->getInitExpr());
2794 if (!InitOrErr)
2795 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002796
Gabor Marton26f72a92018-07-12 09:42:05 +00002797 EnumConstantDecl *ToEnumerator;
2798 if (GetImportedOrCreateDecl(
2799 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002800 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002801 return ToEnumerator;
2802
Douglas Gregordd483172010-02-22 17:42:47 +00002803 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002804 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002805 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002806 return ToEnumerator;
2807}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002808
Balazs Keri1efc9742019-05-07 10:55:11 +00002809Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2810 DeclaratorDecl *ToD) {
2811 unsigned int Num = FromD->getNumTemplateParameterLists();
2812 if (Num == 0)
2813 return Error::success();
2814 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2815 for (unsigned int I = 0; I < Num; ++I)
2816 if (Expected<TemplateParameterList *> ToTPListOrErr =
2817 import(FromD->getTemplateParameterList(I)))
2818 ToTPLists[I] = *ToTPListOrErr;
2819 else
2820 return ToTPListOrErr.takeError();
2821 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2822 return Error::success();
2823}
2824
Balazs Keri3b30d652018-10-19 13:32:20 +00002825Error ASTNodeImporter::ImportTemplateInformation(
2826 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002827 switch (FromFD->getTemplatedKind()) {
2828 case FunctionDecl::TK_NonTemplate:
2829 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002830 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002831
2832 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002833 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002834
2835 if (Expected<FunctionDecl *> InstFDOrErr =
2836 import(FromFD->getInstantiatedFromMemberFunction()))
2837 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2838 else
2839 return InstFDOrErr.takeError();
2840
2841 if (ExpectedSLoc POIOrErr = import(
2842 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2843 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2844 else
2845 return POIOrErr.takeError();
2846
2847 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002848 }
2849
2850 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002851 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002852 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002853 if (!FunctionAndArgsOrErr)
2854 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002855
2856 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002857 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002858
Gabor Marton5254e642018-06-27 13:32:50 +00002859 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002860 TemplateArgumentListInfo ToTAInfo;
2861 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002862 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002863 if (Error Err = ImportTemplateArgumentListInfo(
2864 *FromTAArgsAsWritten, ToTAInfo))
2865 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002866
Balazs Keri3b30d652018-10-19 13:32:20 +00002867 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2868 if (!POIOrErr)
2869 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002870
Balazs Keri1efc9742019-05-07 10:55:11 +00002871 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
2872 return Err;
2873
Gabor Marton5254e642018-06-27 13:32:50 +00002874 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002875 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002876 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2877 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2878 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002879 }
2880
2881 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2882 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2883 UnresolvedSet<8> TemplDecls;
2884 unsigned NumTemplates = FromInfo->getNumTemplates();
2885 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002886 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2887 import(FromInfo->getTemplate(I)))
2888 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002889 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002890 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002891 }
2892
2893 // Import TemplateArgumentListInfo.
2894 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002895 if (Error Err = ImportTemplateArgumentListInfo(
2896 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2897 llvm::makeArrayRef(
2898 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2899 ToTAInfo))
2900 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002901
2902 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2903 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002904 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002905 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002906 }
Sam McCallfdc32072018-01-26 12:06:44 +00002907 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002908}
2909
Balazs Keri3b30d652018-10-19 13:32:20 +00002910Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002911ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002912 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002913 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002914 if (!FunctionAndArgsOrErr)
2915 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002916
Balazs Keri3b30d652018-10-19 13:32:20 +00002917 FunctionTemplateDecl *Template;
2918 TemplateArgsTy ToTemplArgs;
2919 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002920 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002921 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002922 return FoundSpec;
2923}
2924
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002925Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2926 FunctionDecl *ToFD) {
2927 if (Stmt *FromBody = FromFD->getBody()) {
2928 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2929 ToFD->setBody(*ToBodyOrErr);
2930 else
2931 return ToBodyOrErr.takeError();
2932 }
2933 return Error::success();
2934}
2935
Gabor Marton458d1452019-02-14 13:07:03 +00002936template <typename T>
2937bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2938 if (From->hasExternalFormalLinkage())
2939 return Found->hasExternalFormalLinkage();
2940 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2941 return false;
2942 if (From->isInAnonymousNamespace())
2943 return Found->isInAnonymousNamespace();
2944 else
2945 return !Found->isInAnonymousNamespace() &&
2946 !Found->hasExternalFormalLinkage();
2947}
2948
Balazs Keri3b30d652018-10-19 13:32:20 +00002949ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002950
Balazs Keri3b30d652018-10-19 13:32:20 +00002951 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002952 auto RedeclIt = Redecls.begin();
2953 // Import the first part of the decl chain. I.e. import all previous
2954 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002955 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2956 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2957 if (!ToRedeclOrErr)
2958 return ToRedeclOrErr.takeError();
2959 }
Gabor Marton5254e642018-06-27 13:32:50 +00002960 assert(*RedeclIt == D);
2961
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002962 // Import the major distinguishing characteristics of this function.
2963 DeclContext *DC, *LexicalDC;
2964 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002965 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002966 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002967 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2968 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002969 if (ToD)
2970 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002971
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002972 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002973 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002974
Gabor Marton5254e642018-06-27 13:32:50 +00002975 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002976 // existing specialization in the "to" context. The lookup below will not
2977 // find any specialization, but would find the primary template; thus, we
2978 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00002979 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2980 if (D->getTemplatedKind() ==
2981 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002982 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
2983 if (!FoundFunctionOrErr)
2984 return FoundFunctionOrErr.takeError();
2985 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00002986 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2987 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00002988 FoundByLookup = FoundFunction;
2989 }
2990 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002991 // Try to find a function in our own ("to") context with the same name, same
2992 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002993 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002994 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002995 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00002996 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002997 for (auto *FoundDecl : FoundDecls) {
2998 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002999 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003000
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003001 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003002 if (!hasSameVisibilityContext(FoundFunction, D))
3003 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003004
Gabor Marton458d1452019-02-14 13:07:03 +00003005 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00003006 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3007 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00003008 FoundByLookup = FoundFunction;
3009 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003010 }
Gabor Marton458d1452019-02-14 13:07:03 +00003011 // FIXME: Check for overloading more carefully, e.g., by boosting
3012 // Sema::IsOverload out to the AST library.
3013
3014 // Function overloading is okay in C++.
3015 if (Importer.getToContext().getLangOpts().CPlusPlus)
3016 continue;
3017
3018 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00003019 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003020 << Name << D->getType() << FoundFunction->getType();
3021 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
3022 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003023 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003024
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003025 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003026 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003027
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003028 if (!ConflictingDecls.empty()) {
3029 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003030 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003031 ConflictingDecls.size());
3032 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003033 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003034 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003035 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003036
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003037 // We do not allow more than one in-class declaration of a function. This is
3038 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3039 // assumes there is only one in-class declaration. Building a redecl
3040 // chain would result in more than one in-class declaration for
3041 // overrides (even if they are part of the same redecl chain inside the
3042 // derived class.)
3043 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003044 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003045 if (D->getLexicalDeclContext() == D->getDeclContext()) {
3046 if (!D->doesThisDeclarationHaveABody())
3047 return Importer.MapImported(D, FoundByLookup);
3048 else {
3049 // Let's continue and build up the redecl chain in this case.
3050 // FIXME Merge the functions into one decl.
3051 }
3052 }
3053 }
3054 }
3055
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003056 DeclarationNameInfo NameInfo(Name, Loc);
3057 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003058 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3059 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003060
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003061 QualType FromTy = D->getType();
3062 bool usedDifferentExceptionSpec = false;
3063
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003064 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003065 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3066 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3067 // FunctionDecl that we are importing the FunctionProtoType for.
3068 // To avoid an infinite recursion when importing, create the FunctionDecl
3069 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003070 if (FromEPI.ExceptionSpec.SourceDecl ||
3071 FromEPI.ExceptionSpec.SourceTemplate ||
3072 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003073 FunctionProtoType::ExtProtoInfo DefaultEPI;
3074 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003075 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003076 usedDifferentExceptionSpec = true;
3077 }
3078 }
3079
Balazs Keri3b30d652018-10-19 13:32:20 +00003080 QualType T;
3081 TypeSourceInfo *TInfo;
3082 SourceLocation ToInnerLocStart, ToEndLoc;
3083 NestedNameSpecifierLoc ToQualifierLoc;
3084 if (auto Imp = importSeq(
3085 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3086 D->getQualifierLoc(), D->getEndLoc()))
3087 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3088 else
3089 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003090
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003091 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003092 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003093 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003094 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3095 Parameters.push_back(*ToPOrErr);
3096 else
3097 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003098 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003099
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003100 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003101 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003102 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003103 Expr *ExplicitExpr = nullptr;
3104 if (FromConstructor->getExplicitSpecifier().getExpr()) {
3105 auto Imp = importSeq(FromConstructor->getExplicitSpecifier().getExpr());
3106 if (!Imp)
3107 return Imp.takeError();
3108 std::tie(ExplicitExpr) = *Imp;
3109 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003110 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Richard Smith76b90272019-05-09 03:59:21 +00003111 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3112 ToInnerLocStart, NameInfo, T, TInfo,
3113 ExplicitSpecifier(
3114 ExplicitExpr,
3115 FromConstructor->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003116 D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003117 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003118 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3119
3120 auto Imp =
3121 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3122 FromDtor->getOperatorDeleteThisArg());
3123
3124 if (!Imp)
3125 return Imp.takeError();
3126
3127 FunctionDecl *ToOperatorDelete;
3128 Expr *ToThisArg;
3129 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3130
Gabor Marton26f72a92018-07-12 09:42:05 +00003131 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003132 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3133 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3134 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003135 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003136
3137 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3138
3139 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003140 } else if (CXXConversionDecl *FromConversion =
3141 dyn_cast<CXXConversionDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003142 Expr *ExplicitExpr = nullptr;
3143 if (FromConversion->getExplicitSpecifier().getExpr()) {
3144 auto Imp = importSeq(FromConversion->getExplicitSpecifier().getExpr());
3145 if (!Imp)
3146 return Imp.takeError();
3147 std::tie(ExplicitExpr) = *Imp;
3148 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003149 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3150 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003151 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith76b90272019-05-09 03:59:21 +00003152 ExplicitSpecifier(ExplicitExpr,
3153 FromConversion->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003154 D->getConstexprKind(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003155 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003156 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003157 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3158 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003159 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003160 Method->isInlineSpecified(), D->getConstexprKind(),
3161 SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003162 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003163 } else {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003164 if (GetImportedOrCreateDecl(
3165 ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart,
3166 NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(),
3167 D->hasWrittenPrototype(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003168 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003169 }
John McCall3e11ebe2010-03-15 10:12:16 +00003170
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003171 // Connect the redecl chain.
3172 if (FoundByLookup) {
3173 auto *Recent = const_cast<FunctionDecl *>(
3174 FoundByLookup->getMostRecentDecl());
3175 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003176 // FIXME Probably we should merge exception specifications. E.g. In the
3177 // "To" context the existing function may have exception specification with
3178 // noexcept-unevaluated, while the newly imported function may have an
3179 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3180 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003181 }
3182
3183 // Import Ctor initializers.
3184 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3185 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3186 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3187 // Import first, then allocate memory and copy if there was no error.
3188 if (Error Err = ImportContainerChecked(
3189 FromConstructor->inits(), CtorInitializers))
3190 return std::move(Err);
3191 auto **Memory =
3192 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3193 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3194 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3195 ToCtor->setCtorInitializers(Memory);
3196 ToCtor->setNumCtorInitializers(NumInitializers);
3197 }
3198 }
3199
Balazs Keri3b30d652018-10-19 13:32:20 +00003200 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003201 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003202 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003203 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3204 ToFunction->setTrivial(D->isTrivial());
3205 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003206 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003207
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003208 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003209 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003210 Param->setOwningFunction(ToFunction);
3211 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003212 }
David Blaikie9c70e042011-09-21 18:16:56 +00003213 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003214
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003215 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3216 // params it refers to.
3217 if (TInfo) {
3218 if (auto ProtoLoc =
3219 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3220 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3221 ProtoLoc.setParam(I, Parameters[I]);
3222 }
3223 }
3224
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003225 if (usedDifferentExceptionSpec) {
3226 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003227 if (ExpectedType TyOrErr = import(D->getType()))
3228 ToFunction->setType(*TyOrErr);
3229 else
3230 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003231 }
3232
Balazs Keria35798d2018-07-17 09:52:41 +00003233 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003234 if (FromFT) {
3235 auto ToFTOrErr = import(FromFT);
3236 if (!ToFTOrErr)
3237 return ToFTOrErr.takeError();
3238 }
Balazs Keria35798d2018-07-17 09:52:41 +00003239
Gabor Marton5254e642018-06-27 13:32:50 +00003240 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003241 Error Err = ImportFunctionDeclBody(D, ToFunction);
3242
3243 if (Err)
3244 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003245 }
3246
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003247 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003248
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003249 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003250 if (Error Err = ImportTemplateInformation(D, ToFunction))
3251 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003252
Gabor Marton5254e642018-06-27 13:32:50 +00003253 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3254
3255 // TODO Can we generalize this approach to other AST nodes as well?
3256 if (D->getDeclContext()->containsDeclAndLoad(D))
3257 DC->addDeclInternal(ToFunction);
3258 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003259 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003260
Gabor Marton5254e642018-06-27 13:32:50 +00003261 // Friend declaration's lexical context is the befriending class, but the
3262 // semantic context is the enclosing scope of the befriending class.
3263 // We want the friend functions to be found in the semantic context by lookup.
3264 // FIXME should we handle this generically in VisitFriendDecl?
3265 // In Other cases when LexicalDC != DC we don't want it to be added,
3266 // e.g out-of-class definitions like void B::f() {} .
3267 if (LexicalDC != DC && IsFriend) {
3268 DC->makeDeclVisibleInContext(ToFunction);
3269 }
3270
Gabor Marton7a0841e2018-10-29 10:18:28 +00003271 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3272 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3273
Gabor Marton5254e642018-06-27 13:32:50 +00003274 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003275 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3276 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3277 if (!ToRedeclOrErr)
3278 return ToRedeclOrErr.takeError();
3279 }
Gabor Marton5254e642018-06-27 13:32:50 +00003280
Douglas Gregor43f54792010-02-17 02:12:47 +00003281 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003282}
3283
Balazs Keri3b30d652018-10-19 13:32:20 +00003284ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003285 return VisitFunctionDecl(D);
3286}
3287
Balazs Keri3b30d652018-10-19 13:32:20 +00003288ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003289 return VisitCXXMethodDecl(D);
3290}
3291
Balazs Keri3b30d652018-10-19 13:32:20 +00003292ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003293 return VisitCXXMethodDecl(D);
3294}
3295
Balazs Keri3b30d652018-10-19 13:32:20 +00003296ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003297 return VisitCXXMethodDecl(D);
3298}
3299
Balazs Keri3b30d652018-10-19 13:32:20 +00003300ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003301 // Import the major distinguishing characteristics of a variable.
3302 DeclContext *DC, *LexicalDC;
3303 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003304 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003305 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003306 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3307 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003308 if (ToD)
3309 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003310
Fangrui Song6907ce22018-07-30 19:24:48 +00003311 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003312 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003313 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003314 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003315 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003316 if (!Name &&
3317 ASTImporter::getFieldIndex(D) !=
3318 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003319 continue;
3320
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003321 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003322 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003323 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003324 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3325 // initializer of a FieldDecl might not had been instantiated in the
3326 // "To" context. However, the "From" context might instantiated that,
3327 // thus we have to merge that.
3328 if (Expr *FromInitializer = D->getInClassInitializer()) {
3329 // We don't have yet the initializer set.
3330 if (FoundField->hasInClassInitializer() &&
3331 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003332 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3333 FoundField->setInClassInitializer(*ToInitializerOrErr);
3334 else {
3335 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003336 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003337 // FIXME: warning message?
3338 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003339 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003340 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003341 }
3342 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003343 return FoundField;
3344 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003345
Balazs Keri3b30d652018-10-19 13:32:20 +00003346 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003347 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003348 << Name << D->getType() << FoundField->getType();
3349 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3350 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003351
3352 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003353 }
3354 }
3355
Balazs Keri3b30d652018-10-19 13:32:20 +00003356 QualType ToType;
3357 TypeSourceInfo *ToTInfo;
3358 Expr *ToBitWidth;
3359 SourceLocation ToInnerLocStart;
3360 Expr *ToInitializer;
3361 if (auto Imp = importSeq(
3362 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3363 D->getInnerLocStart(), D->getInClassInitializer()))
3364 std::tie(
3365 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3366 else
3367 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003368
Gabor Marton26f72a92018-07-12 09:42:05 +00003369 FieldDecl *ToField;
3370 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003371 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3372 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3373 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003374 return ToField;
3375
Douglas Gregordd483172010-02-22 17:42:47 +00003376 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003377 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003378 if (ToInitializer)
3379 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003380 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003381 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003382 return ToField;
3383}
3384
Balazs Keri3b30d652018-10-19 13:32:20 +00003385ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003386 // Import the major distinguishing characteristics of a variable.
3387 DeclContext *DC, *LexicalDC;
3388 DeclarationName Name;
3389 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003390 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003391 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3392 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003393 if (ToD)
3394 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003395
Fangrui Song6907ce22018-07-30 19:24:48 +00003396 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003397 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003398 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003399 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003400 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003401 if (!Name &&
3402 ASTImporter::getFieldIndex(D) !=
3403 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003404 continue;
3405
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003406 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003407 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003408 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003409 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003410 return FoundField;
3411 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003412
3413 // If there are more anonymous fields to check, continue.
3414 if (!Name && I < N-1)
3415 continue;
3416
Balazs Keri3b30d652018-10-19 13:32:20 +00003417 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003418 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003419 << Name << D->getType() << FoundField->getType();
3420 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3421 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003422
3423 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003424 }
3425 }
3426
Francois Pichet783dd6e2010-11-21 06:08:52 +00003427 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003428 auto TypeOrErr = import(D->getType());
3429 if (!TypeOrErr)
3430 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003431
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003432 auto **NamedChain =
3433 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003434
3435 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003436 for (auto *PI : D->chain())
3437 if (Expected<NamedDecl *> ToD = import(PI))
3438 NamedChain[i++] = *ToD;
3439 else
3440 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003441
Gabor Marton26f72a92018-07-12 09:42:05 +00003442 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3443 IndirectFieldDecl *ToIndirectField;
3444 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003445 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003446 // FIXME here we leak `NamedChain` which is allocated before
3447 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003448
Francois Pichet783dd6e2010-11-21 06:08:52 +00003449 ToIndirectField->setAccess(D->getAccess());
3450 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003451 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003452 return ToIndirectField;
3453}
3454
Balazs Keri3b30d652018-10-19 13:32:20 +00003455ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003456 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003457 DeclContext *DC, *LexicalDC;
3458 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3459 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003460
3461 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003462 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003463 auto *RD = cast<CXXRecordDecl>(DC);
3464 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003465
3466 while (ImportedFriend) {
3467 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003468 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3469 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003470 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003471
3472 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3473 if (Importer.IsStructurallyEquivalent(
3474 D->getFriendType()->getType(),
3475 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003476 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003477 }
3478 ImportedFriend = ImportedFriend->getNextFriend();
3479 }
3480
3481 // Not found. Create it.
3482 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003483 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003484 NamedDecl *ToFriendD;
3485 if (Error Err = importInto(ToFriendD, FriendD))
3486 return std::move(Err);
3487
3488 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003489 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3490 ToFriendD->setObjectOfFriendDecl(false);
3491
3492 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003493 } else { // The friend is a type, not a decl.
3494 if (auto TSIOrErr = import(D->getFriendType()))
3495 ToFU = *TSIOrErr;
3496 else
3497 return TSIOrErr.takeError();
3498 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003499
3500 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003501 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003502 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003503 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003504 ToTPLists[I] = *ListOrErr;
3505 else
3506 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003507 }
3508
Balazs Keri3b30d652018-10-19 13:32:20 +00003509 auto LocationOrErr = import(D->getLocation());
3510 if (!LocationOrErr)
3511 return LocationOrErr.takeError();
3512 auto FriendLocOrErr = import(D->getFriendLoc());
3513 if (!FriendLocOrErr)
3514 return FriendLocOrErr.takeError();
3515
Gabor Marton26f72a92018-07-12 09:42:05 +00003516 FriendDecl *FrD;
3517 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003518 *LocationOrErr, ToFU,
3519 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003520 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003521
3522 FrD->setAccess(D->getAccess());
3523 FrD->setLexicalDeclContext(LexicalDC);
3524 LexicalDC->addDeclInternal(FrD);
3525 return FrD;
3526}
3527
Balazs Keri3b30d652018-10-19 13:32:20 +00003528ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003529 // Import the major distinguishing characteristics of an ivar.
3530 DeclContext *DC, *LexicalDC;
3531 DeclarationName Name;
3532 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003533 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003534 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3535 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003536 if (ToD)
3537 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003538
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003539 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003540 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003541 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003542 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003543 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003544 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003545 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003546 return FoundIvar;
3547 }
3548
Gabor Marton410f32c2019-04-01 15:29:55 +00003549 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003550 << Name << D->getType() << FoundIvar->getType();
3551 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3552 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003553
3554 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003555 }
3556 }
3557
Balazs Keri3b30d652018-10-19 13:32:20 +00003558 QualType ToType;
3559 TypeSourceInfo *ToTypeSourceInfo;
3560 Expr *ToBitWidth;
3561 SourceLocation ToInnerLocStart;
3562 if (auto Imp = importSeq(
3563 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3564 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3565 else
3566 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003567
Gabor Marton26f72a92018-07-12 09:42:05 +00003568 ObjCIvarDecl *ToIvar;
3569 if (GetImportedOrCreateDecl(
3570 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003571 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3572 ToType, ToTypeSourceInfo,
3573 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003574 return ToIvar;
3575
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003576 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003577 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003578 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003579}
3580
Balazs Keri3b30d652018-10-19 13:32:20 +00003581ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003582
3583 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3584 auto RedeclIt = Redecls.begin();
3585 // Import the first part of the decl chain. I.e. import all previous
3586 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003587 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3588 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3589 if (!RedeclOrErr)
3590 return RedeclOrErr.takeError();
3591 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003592 assert(*RedeclIt == D);
3593
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003594 // Import the major distinguishing characteristics of a variable.
3595 DeclContext *DC, *LexicalDC;
3596 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003597 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003598 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003599 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3600 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003601 if (ToD)
3602 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003603
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003604 // Try to find a variable in our own ("to") context with the same name and
3605 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003606 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003607 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003608 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003609 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003610 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003611 for (auto *FoundDecl : FoundDecls) {
3612 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003613 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003614
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003615 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003616 if (!hasSameVisibilityContext(FoundVar, D))
3617 continue;
3618 if (Importer.IsStructurallyEquivalent(D->getType(),
3619 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003620
Gabor Marton458d1452019-02-14 13:07:03 +00003621 // The VarDecl in the "From" context has a definition, but in the
3622 // "To" context we already have a definition.
3623 VarDecl *FoundDef = FoundVar->getDefinition();
3624 if (D->isThisDeclarationADefinition() && FoundDef)
3625 // FIXME Check for ODR error if the two definitions have
3626 // different initializers?
3627 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003628
Gabor Marton458d1452019-02-14 13:07:03 +00003629 // The VarDecl in the "From" context has an initializer, but in the
3630 // "To" context we already have an initializer.
3631 const VarDecl *FoundDInit = nullptr;
3632 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3633 // FIXME Diagnose ODR error if the two initializers are different?
3634 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3635
3636 FoundByLookup = FoundVar;
3637 break;
3638 }
3639
3640 const ArrayType *FoundArray
3641 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3642 const ArrayType *TArray
3643 = Importer.getToContext().getAsArrayType(D->getType());
3644 if (FoundArray && TArray) {
3645 if (isa<IncompleteArrayType>(FoundArray) &&
3646 isa<ConstantArrayType>(TArray)) {
3647 // Import the type.
3648 if (auto TyOrErr = import(D->getType()))
3649 FoundVar->setType(*TyOrErr);
3650 else
3651 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003652
3653 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003654 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003655 } else if (isa<IncompleteArrayType>(TArray) &&
3656 isa<ConstantArrayType>(FoundArray)) {
3657 FoundByLookup = FoundVar;
3658 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003659 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003660 }
Gabor Marton458d1452019-02-14 13:07:03 +00003661
Gabor Marton410f32c2019-04-01 15:29:55 +00003662 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003663 << Name << D->getType() << FoundVar->getType();
3664 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3665 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003666 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003667
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003668 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003669 }
3670
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003671 if (!ConflictingDecls.empty()) {
3672 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003673 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003674 ConflictingDecls.size());
3675 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003676 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003677 }
3678 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003679
Balazs Keri3b30d652018-10-19 13:32:20 +00003680 QualType ToType;
3681 TypeSourceInfo *ToTypeSourceInfo;
3682 SourceLocation ToInnerLocStart;
3683 NestedNameSpecifierLoc ToQualifierLoc;
3684 if (auto Imp = importSeq(
3685 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3686 D->getQualifierLoc()))
3687 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3688 else
3689 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003690
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003691 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003692 VarDecl *ToVar;
3693 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003694 ToInnerLocStart, Loc,
3695 Name.getAsIdentifierInfo(),
3696 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003697 D->getStorageClass()))
3698 return ToVar;
3699
Balazs Keri3b30d652018-10-19 13:32:20 +00003700 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003701 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003702 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003703
Gabor Martonac3a5d62018-09-17 12:04:52 +00003704 if (FoundByLookup) {
3705 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3706 ToVar->setPreviousDecl(Recent);
3707 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003708
Balazs Keri3b30d652018-10-19 13:32:20 +00003709 if (Error Err = ImportInitializer(D, ToVar))
3710 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003711
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003712 if (D->isConstexpr())
3713 ToVar->setConstexpr(true);
3714
Gabor Martonac3a5d62018-09-17 12:04:52 +00003715 if (D->getDeclContext()->containsDeclAndLoad(D))
3716 DC->addDeclInternal(ToVar);
3717 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3718 LexicalDC->addDeclInternal(ToVar);
3719
3720 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003721 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3722 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3723 if (!RedeclOrErr)
3724 return RedeclOrErr.takeError();
3725 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003726
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003727 return ToVar;
3728}
3729
Balazs Keri3b30d652018-10-19 13:32:20 +00003730ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003731 // Parameters are created in the translation unit's context, then moved
3732 // into the function declaration's context afterward.
3733 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003734
Balazs Keri3b30d652018-10-19 13:32:20 +00003735 DeclarationName ToDeclName;
3736 SourceLocation ToLocation;
3737 QualType ToType;
3738 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3739 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3740 else
3741 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003742
Douglas Gregor8b228d72010-02-17 21:22:52 +00003743 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003744 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003745 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3746 ToLocation, ToDeclName.getAsIdentifierInfo(),
3747 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003748 return ToParm;
3749 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003750}
3751
Balazs Keri3b30d652018-10-19 13:32:20 +00003752ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003753 // Parameters are created in the translation unit's context, then moved
3754 // into the function declaration's context afterward.
3755 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003756
Balazs Keri3b30d652018-10-19 13:32:20 +00003757 DeclarationName ToDeclName;
3758 SourceLocation ToLocation, ToInnerLocStart;
3759 QualType ToType;
3760 TypeSourceInfo *ToTypeSourceInfo;
3761 if (auto Imp = importSeq(
3762 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3763 D->getTypeSourceInfo()))
3764 std::tie(
3765 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3766 ToTypeSourceInfo) = *Imp;
3767 else
3768 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003769
Gabor Marton26f72a92018-07-12 09:42:05 +00003770 ParmVarDecl *ToParm;
3771 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003772 ToInnerLocStart, ToLocation,
3773 ToDeclName.getAsIdentifierInfo(), ToType,
3774 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003775 /*DefaultArg*/ nullptr))
3776 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003777
3778 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003779 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003780 ToParm->setKNRPromoted(D->isKNRPromoted());
3781
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003782 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003783 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3784 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3785 else
3786 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003787 } else if (D->hasUnparsedDefaultArg()) {
3788 ToParm->setUnparsedDefaultArg();
3789 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003790 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3791 ToParm->setDefaultArg(*ToDefArgOrErr);
3792 else
3793 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003794 }
Sean Callanan59721b32015-04-28 18:41:46 +00003795
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003796 if (D->isObjCMethodParameter()) {
3797 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3798 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3799 } else {
3800 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3801 D->getFunctionScopeIndex());
3802 }
3803
Gabor Marton26f72a92018-07-12 09:42:05 +00003804 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003805}
3806
Balazs Keri3b30d652018-10-19 13:32:20 +00003807ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003808 // Import the major distinguishing characteristics of a method.
3809 DeclContext *DC, *LexicalDC;
3810 DeclarationName Name;
3811 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003812 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003813 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3814 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003815 if (ToD)
3816 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003817
Gabor Marton54058b52018-12-17 13:53:12 +00003818 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003819 for (auto *FoundDecl : FoundDecls) {
3820 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003821 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3822 continue;
3823
3824 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003825 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3826 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003827 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003828 << D->isInstanceMethod() << Name << D->getReturnType()
3829 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003830 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003831 diag::note_odr_objc_method_here)
3832 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003833
3834 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003835 }
3836
3837 // Check the number of parameters.
3838 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003839 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003840 << D->isInstanceMethod() << Name
3841 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003842 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003843 diag::note_odr_objc_method_here)
3844 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003845
3846 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003847 }
3848
3849 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003850 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003851 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3852 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003853 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003854 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003855 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003856 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003857 << D->isInstanceMethod() << Name
3858 << (*P)->getType() << (*FoundP)->getType();
3859 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3860 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003861
3862 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003863 }
3864 }
3865
3866 // Check variadic/non-variadic.
3867 // Check the number of parameters.
3868 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003869 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003870 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003871 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003872 diag::note_odr_objc_method_here)
3873 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003874
3875 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003876 }
3877
3878 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003879 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003880 }
3881 }
3882
Balazs Keri3b30d652018-10-19 13:32:20 +00003883 SourceLocation ToEndLoc;
3884 QualType ToReturnType;
3885 TypeSourceInfo *ToReturnTypeSourceInfo;
3886 if (auto Imp = importSeq(
3887 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3888 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3889 else
3890 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003891
Gabor Marton26f72a92018-07-12 09:42:05 +00003892 ObjCMethodDecl *ToMethod;
3893 if (GetImportedOrCreateDecl(
3894 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003895 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3896 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003897 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3898 D->getImplementationControl(), D->hasRelatedResultType()))
3899 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003900
3901 // FIXME: When we decide to merge method definitions, we'll need to
3902 // deal with implicit parameters.
3903
3904 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003905 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003906 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003907 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3908 ToParams.push_back(*ToPOrErr);
3909 else
3910 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003911 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003912
Douglas Gregor43f54792010-02-17 02:12:47 +00003913 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003914 for (auto *ToParam : ToParams) {
3915 ToParam->setOwningFunction(ToMethod);
3916 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003917 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003918
Balazs Keri3b30d652018-10-19 13:32:20 +00003919 SmallVector<SourceLocation, 12> FromSelLocs;
3920 D->getSelectorLocs(FromSelLocs);
3921 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3922 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3923 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003924
Balazs Keri3b30d652018-10-19 13:32:20 +00003925 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003926
3927 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003928 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003929 return ToMethod;
3930}
3931
Balazs Keri3b30d652018-10-19 13:32:20 +00003932ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003933 // Import the major distinguishing characteristics of a category.
3934 DeclContext *DC, *LexicalDC;
3935 DeclarationName Name;
3936 SourceLocation Loc;
3937 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003938 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3939 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003940 if (ToD)
3941 return ToD;
3942
Balazs Keri3b30d652018-10-19 13:32:20 +00003943 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3944 TypeSourceInfo *ToTypeSourceInfo;
3945 if (auto Imp = importSeq(
3946 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3947 D->getTypeSourceInfo()))
3948 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3949 else
3950 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003951
Gabor Marton26f72a92018-07-12 09:42:05 +00003952 ObjCTypeParamDecl *Result;
3953 if (GetImportedOrCreateDecl(
3954 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003955 ToVarianceLoc, D->getIndex(),
3956 ToLocation, Name.getAsIdentifierInfo(),
3957 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003958 return Result;
3959
Douglas Gregor85f3f952015-07-07 03:57:15 +00003960 Result->setLexicalDeclContext(LexicalDC);
3961 return Result;
3962}
3963
Balazs Keri3b30d652018-10-19 13:32:20 +00003964ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003965 // Import the major distinguishing characteristics of a category.
3966 DeclContext *DC, *LexicalDC;
3967 DeclarationName Name;
3968 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003969 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003970 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3971 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003972 if (ToD)
3973 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003974
Balazs Keri3b30d652018-10-19 13:32:20 +00003975 ObjCInterfaceDecl *ToInterface;
3976 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3977 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003978
Douglas Gregor84c51c32010-02-18 01:47:50 +00003979 // Determine if we've already encountered this category.
3980 ObjCCategoryDecl *MergeWithCategory
3981 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3982 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3983 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003984 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3985 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3986 if (auto Imp = importSeq(
3987 D->getAtStartLoc(), D->getCategoryNameLoc(),
3988 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3989 std::tie(
3990 ToAtStartLoc, ToCategoryNameLoc,
3991 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3992 else
3993 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003994
3995 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003996 ToAtStartLoc, Loc,
3997 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003998 Name.getAsIdentifierInfo(), ToInterface,
3999 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004000 ToIvarLBraceLoc,
4001 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004002 return ToCategory;
4003
Douglas Gregor84c51c32010-02-18 01:47:50 +00004004 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004005 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004006 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004007 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004008 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4009 ToCategory->setTypeParamList(*PListOrErr);
4010 else
4011 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004012
Douglas Gregor84c51c32010-02-18 01:47:50 +00004013 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004014 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4015 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004016 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4017 = D->protocol_loc_begin();
4018 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4019 FromProtoEnd = D->protocol_end();
4020 FromProto != FromProtoEnd;
4021 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004022 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4023 Protocols.push_back(*ToProtoOrErr);
4024 else
4025 return ToProtoOrErr.takeError();
4026
4027 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4028 ProtocolLocs.push_back(*ToProtoLocOrErr);
4029 else
4030 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004031 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004032
Douglas Gregor84c51c32010-02-18 01:47:50 +00004033 // FIXME: If we're merging, make sure that the protocol list is the same.
4034 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4035 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004036
Douglas Gregor84c51c32010-02-18 01:47:50 +00004037 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004038 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004039 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004040
Douglas Gregor84c51c32010-02-18 01:47:50 +00004041 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004042 if (Error Err = ImportDeclContext(D))
4043 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004044
Douglas Gregor84c51c32010-02-18 01:47:50 +00004045 // If we have an implementation, import it as well.
4046 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004047 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4048 import(D->getImplementation()))
4049 ToCategory->setImplementation(*ToImplOrErr);
4050 else
4051 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004052 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004053
Douglas Gregor84c51c32010-02-18 01:47:50 +00004054 return ToCategory;
4055}
4056
Balazs Keri3b30d652018-10-19 13:32:20 +00004057Error ASTNodeImporter::ImportDefinition(
4058 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004059 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004060 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004061 if (Error Err = ImportDeclContext(From))
4062 return Err;
4063 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004064 }
4065
4066 // Start the protocol definition
4067 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004068
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 // Import protocols
4070 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4071 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004072 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4073 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004074 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4075 FromProtoEnd = From->protocol_end();
4076 FromProto != FromProtoEnd;
4077 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004078 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4079 Protocols.push_back(*ToProtoOrErr);
4080 else
4081 return ToProtoOrErr.takeError();
4082
4083 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4084 ProtocolLocs.push_back(*ToProtoLocOrErr);
4085 else
4086 return ToProtoLocOrErr.takeError();
4087
Douglas Gregor2aa53772012-01-24 17:42:07 +00004088 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004089
Douglas Gregor2aa53772012-01-24 17:42:07 +00004090 // FIXME: If we're merging, make sure that the protocol list is the same.
4091 To->setProtocolList(Protocols.data(), Protocols.size(),
4092 ProtocolLocs.data(), Importer.getToContext());
4093
Douglas Gregor2e15c842012-02-01 21:00:38 +00004094 if (shouldForceImportDeclContext(Kind)) {
4095 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004096 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4097 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004098 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004099 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004100}
4101
Balazs Keri3b30d652018-10-19 13:32:20 +00004102ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004103 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004104 // from, but this particular declaration is not that definition, import the
4105 // definition and map to that.
4106 ObjCProtocolDecl *Definition = D->getDefinition();
4107 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004108 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4109 return Importer.MapImported(D, *ImportedDefOrErr);
4110 else
4111 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004112 }
4113
Douglas Gregor84c51c32010-02-18 01:47:50 +00004114 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004115 DeclContext *DC, *LexicalDC;
4116 DeclarationName Name;
4117 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004118 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004119 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4120 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004121 if (ToD)
4122 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004123
Craig Topper36250ad2014-05-12 05:36:57 +00004124 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004125 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004126 for (auto *FoundDecl : FoundDecls) {
4127 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004128 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004129
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004130 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004131 break;
4132 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004133
Douglas Gregor98d156a2010-02-17 16:12:00 +00004134 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004135 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004136 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4137 if (!ToAtBeginLocOrErr)
4138 return ToAtBeginLocOrErr.takeError();
4139
Gabor Marton26f72a92018-07-12 09:42:05 +00004140 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4141 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004142 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004143 /*PrevDecl=*/nullptr))
4144 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004145 ToProto->setLexicalDeclContext(LexicalDC);
4146 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004147 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004148
4149 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004150
Balazs Keri3b30d652018-10-19 13:32:20 +00004151 if (D->isThisDeclarationADefinition())
4152 if (Error Err = ImportDefinition(D, ToProto))
4153 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004154
Douglas Gregor98d156a2010-02-17 16:12:00 +00004155 return ToProto;
4156}
4157
Balazs Keri3b30d652018-10-19 13:32:20 +00004158ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4159 DeclContext *DC, *LexicalDC;
4160 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4161 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004162
Balazs Keri3b30d652018-10-19 13:32:20 +00004163 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4164 if (!ExternLocOrErr)
4165 return ExternLocOrErr.takeError();
4166
4167 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4168 if (!LangLocOrErr)
4169 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004170
4171 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004172
4173 LinkageSpecDecl *ToLinkageSpec;
4174 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004175 *ExternLocOrErr, *LangLocOrErr,
4176 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004177 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004178
4179 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004180 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4181 if (!RBraceLocOrErr)
4182 return RBraceLocOrErr.takeError();
4183 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004184 }
4185
4186 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4187 LexicalDC->addDeclInternal(ToLinkageSpec);
4188
Sean Callanan0aae0412014-12-10 00:00:37 +00004189 return ToLinkageSpec;
4190}
4191
Balazs Keri3b30d652018-10-19 13:32:20 +00004192ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004193 DeclContext *DC, *LexicalDC;
4194 DeclarationName Name;
4195 SourceLocation Loc;
4196 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004197 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4198 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004199 if (ToD)
4200 return ToD;
4201
Balazs Keri3b30d652018-10-19 13:32:20 +00004202 SourceLocation ToLoc, ToUsingLoc;
4203 NestedNameSpecifierLoc ToQualifierLoc;
4204 if (auto Imp = importSeq(
4205 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4206 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4207 else
4208 return Imp.takeError();
4209
4210 DeclarationNameInfo NameInfo(Name, ToLoc);
4211 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4212 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004213
Gabor Marton26f72a92018-07-12 09:42:05 +00004214 UsingDecl *ToUsing;
4215 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004216 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004217 D->hasTypename()))
4218 return ToUsing;
4219
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004220 ToUsing->setLexicalDeclContext(LexicalDC);
4221 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004222
4223 if (NamedDecl *FromPattern =
4224 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004225 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4226 Importer.getToContext().setInstantiatedFromUsingDecl(
4227 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004228 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004229 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004230 }
4231
Balazs Keri3b30d652018-10-19 13:32:20 +00004232 for (UsingShadowDecl *FromShadow : D->shadows()) {
4233 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4234 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004235 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004236 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004237 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004238 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004239 }
4240 return ToUsing;
4241}
4242
Balazs Keri3b30d652018-10-19 13:32:20 +00004243ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004244 DeclContext *DC, *LexicalDC;
4245 DeclarationName Name;
4246 SourceLocation Loc;
4247 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004248 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4249 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004250 if (ToD)
4251 return ToD;
4252
Balazs Keri3b30d652018-10-19 13:32:20 +00004253 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4254 if (!ToUsingOrErr)
4255 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004256
Balazs Keri3b30d652018-10-19 13:32:20 +00004257 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4258 if (!ToTargetOrErr)
4259 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004260
Gabor Marton26f72a92018-07-12 09:42:05 +00004261 UsingShadowDecl *ToShadow;
4262 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004263 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004264 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004265
4266 ToShadow->setLexicalDeclContext(LexicalDC);
4267 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004268
4269 if (UsingShadowDecl *FromPattern =
4270 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004271 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4272 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4273 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004274 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004275 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004276 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004277 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004278 }
4279
4280 LexicalDC->addDeclInternal(ToShadow);
4281
4282 return ToShadow;
4283}
4284
Balazs Keri3b30d652018-10-19 13:32:20 +00004285ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004286 DeclContext *DC, *LexicalDC;
4287 DeclarationName Name;
4288 SourceLocation Loc;
4289 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004290 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4291 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004292 if (ToD)
4293 return ToD;
4294
Balazs Keri3b30d652018-10-19 13:32:20 +00004295 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4296 if (!ToComAncestorOrErr)
4297 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004298
Balazs Keri3b30d652018-10-19 13:32:20 +00004299 NamespaceDecl *ToNominatedNamespace;
4300 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4301 NestedNameSpecifierLoc ToQualifierLoc;
4302 if (auto Imp = importSeq(
4303 D->getNominatedNamespace(), D->getUsingLoc(),
4304 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4305 D->getIdentLocation()))
4306 std::tie(
4307 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4308 ToQualifierLoc, ToIdentLocation) = *Imp;
4309 else
4310 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004311
Gabor Marton26f72a92018-07-12 09:42:05 +00004312 UsingDirectiveDecl *ToUsingDir;
4313 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004314 ToUsingLoc,
4315 ToNamespaceKeyLocation,
4316 ToQualifierLoc,
4317 ToIdentLocation,
4318 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004319 return ToUsingDir;
4320
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004321 ToUsingDir->setLexicalDeclContext(LexicalDC);
4322 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004323
4324 return ToUsingDir;
4325}
4326
Balazs Keri3b30d652018-10-19 13:32:20 +00004327ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004328 UnresolvedUsingValueDecl *D) {
4329 DeclContext *DC, *LexicalDC;
4330 DeclarationName Name;
4331 SourceLocation Loc;
4332 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004333 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4334 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004335 if (ToD)
4336 return ToD;
4337
Balazs Keri3b30d652018-10-19 13:32:20 +00004338 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4339 NestedNameSpecifierLoc ToQualifierLoc;
4340 if (auto Imp = importSeq(
4341 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4342 D->getEllipsisLoc()))
4343 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4344 else
4345 return Imp.takeError();
4346
4347 DeclarationNameInfo NameInfo(Name, ToLoc);
4348 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4349 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004350
Gabor Marton26f72a92018-07-12 09:42:05 +00004351 UnresolvedUsingValueDecl *ToUsingValue;
4352 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004353 ToUsingLoc, ToQualifierLoc, NameInfo,
4354 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004355 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004356
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004357 ToUsingValue->setAccess(D->getAccess());
4358 ToUsingValue->setLexicalDeclContext(LexicalDC);
4359 LexicalDC->addDeclInternal(ToUsingValue);
4360
4361 return ToUsingValue;
4362}
4363
Balazs Keri3b30d652018-10-19 13:32:20 +00004364ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004365 UnresolvedUsingTypenameDecl *D) {
4366 DeclContext *DC, *LexicalDC;
4367 DeclarationName Name;
4368 SourceLocation Loc;
4369 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004370 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4371 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004372 if (ToD)
4373 return ToD;
4374
Balazs Keri3b30d652018-10-19 13:32:20 +00004375 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4376 NestedNameSpecifierLoc ToQualifierLoc;
4377 if (auto Imp = importSeq(
4378 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4379 D->getEllipsisLoc()))
4380 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4381 else
4382 return Imp.takeError();
4383
Gabor Marton26f72a92018-07-12 09:42:05 +00004384 UnresolvedUsingTypenameDecl *ToUsing;
4385 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004386 ToUsingLoc, ToTypenameLoc,
4387 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004388 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004389
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004390 ToUsing->setAccess(D->getAccess());
4391 ToUsing->setLexicalDeclContext(LexicalDC);
4392 LexicalDC->addDeclInternal(ToUsing);
4393
4394 return ToUsing;
4395}
4396
Balazs Keri3b30d652018-10-19 13:32:20 +00004397
4398Error ASTNodeImporter::ImportDefinition(
4399 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004400 if (To->getDefinition()) {
4401 // Check consistency of superclass.
4402 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4403 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004404 if (auto FromSuperOrErr = import(FromSuper))
4405 FromSuper = *FromSuperOrErr;
4406 else
4407 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004408 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004409
4410 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004411 if ((bool)FromSuper != (bool)ToSuper ||
4412 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004413 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004414 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004415 << To->getDeclName();
4416 if (ToSuper)
4417 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4418 << To->getSuperClass()->getDeclName();
4419 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004420 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004421 diag::note_odr_objc_missing_superclass);
4422 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004423 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004424 diag::note_odr_objc_superclass)
4425 << From->getSuperClass()->getDeclName();
4426 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004427 Importer.FromDiag(From->getLocation(),
4428 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004429 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004430
Douglas Gregor2e15c842012-02-01 21:00:38 +00004431 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004432 if (Error Err = ImportDeclContext(From))
4433 return Err;
4434 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004435 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004436
Douglas Gregor2aa53772012-01-24 17:42:07 +00004437 // Start the definition.
4438 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004439
Douglas Gregor2aa53772012-01-24 17:42:07 +00004440 // If this class has a superclass, import it.
4441 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004442 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4443 To->setSuperClass(*SuperTInfoOrErr);
4444 else
4445 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004446 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004447
Douglas Gregor2aa53772012-01-24 17:42:07 +00004448 // Import protocols
4449 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4450 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004451 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4452 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004453
Douglas Gregor2aa53772012-01-24 17:42:07 +00004454 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4455 FromProtoEnd = From->protocol_end();
4456 FromProto != FromProtoEnd;
4457 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004458 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4459 Protocols.push_back(*ToProtoOrErr);
4460 else
4461 return ToProtoOrErr.takeError();
4462
4463 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4464 ProtocolLocs.push_back(*ToProtoLocOrErr);
4465 else
4466 return ToProtoLocOrErr.takeError();
4467
Douglas Gregor2aa53772012-01-24 17:42:07 +00004468 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004469
Douglas Gregor2aa53772012-01-24 17:42:07 +00004470 // FIXME: If we're merging, make sure that the protocol list is the same.
4471 To->setProtocolList(Protocols.data(), Protocols.size(),
4472 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004473
Douglas Gregor2aa53772012-01-24 17:42:07 +00004474 // Import categories. When the categories themselves are imported, they'll
4475 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004476 for (auto *Cat : From->known_categories()) {
4477 auto ToCatOrErr = import(Cat);
4478 if (!ToCatOrErr)
4479 return ToCatOrErr.takeError();
4480 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004481
Douglas Gregor2aa53772012-01-24 17:42:07 +00004482 // If we have an @implementation, import it as well.
4483 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004484 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4485 import(From->getImplementation()))
4486 To->setImplementation(*ToImplOrErr);
4487 else
4488 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004489 }
4490
Douglas Gregor2e15c842012-02-01 21:00:38 +00004491 if (shouldForceImportDeclContext(Kind)) {
4492 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004493 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4494 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004495 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004496 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004497}
4498
Balazs Keri3b30d652018-10-19 13:32:20 +00004499Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004500ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4501 if (!list)
4502 return nullptr;
4503
4504 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004505 for (auto *fromTypeParam : *list) {
4506 if (auto toTypeParamOrErr = import(fromTypeParam))
4507 toTypeParams.push_back(*toTypeParamOrErr);
4508 else
4509 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004510 }
4511
Balazs Keri3b30d652018-10-19 13:32:20 +00004512 auto LAngleLocOrErr = import(list->getLAngleLoc());
4513 if (!LAngleLocOrErr)
4514 return LAngleLocOrErr.takeError();
4515
4516 auto RAngleLocOrErr = import(list->getRAngleLoc());
4517 if (!RAngleLocOrErr)
4518 return RAngleLocOrErr.takeError();
4519
Douglas Gregor85f3f952015-07-07 03:57:15 +00004520 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004521 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004522 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004523 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004524}
4525
Balazs Keri3b30d652018-10-19 13:32:20 +00004526ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004527 // If this class has a definition in the translation unit we're coming from,
4528 // but this particular declaration is not that definition, import the
4529 // definition and map to that.
4530 ObjCInterfaceDecl *Definition = D->getDefinition();
4531 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004532 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4533 return Importer.MapImported(D, *ImportedDefOrErr);
4534 else
4535 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004536 }
4537
Douglas Gregor45635322010-02-16 01:20:57 +00004538 // Import the major distinguishing characteristics of an @interface.
4539 DeclContext *DC, *LexicalDC;
4540 DeclarationName Name;
4541 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004542 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004543 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4544 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004545 if (ToD)
4546 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004547
Douglas Gregor2aa53772012-01-24 17:42:07 +00004548 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004549 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004550 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004551 for (auto *FoundDecl : FoundDecls) {
4552 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004553 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004554
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004555 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004556 break;
4557 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004558
Douglas Gregor2aa53772012-01-24 17:42:07 +00004559 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004560 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004561 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004562 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4563 if (!AtBeginLocOrErr)
4564 return AtBeginLocOrErr.takeError();
4565
Gabor Marton26f72a92018-07-12 09:42:05 +00004566 if (GetImportedOrCreateDecl(
4567 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004568 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004569 /*TypeParamList=*/nullptr,
4570 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4571 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004572 ToIface->setLexicalDeclContext(LexicalDC);
4573 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004574 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004575 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004576 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004577 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004578 if (auto ToPListOrErr =
4579 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4580 ToIface->setTypeParamList(*ToPListOrErr);
4581 else
4582 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004583
Balazs Keri3b30d652018-10-19 13:32:20 +00004584 if (D->isThisDeclarationADefinition())
4585 if (Error Err = ImportDefinition(D, ToIface))
4586 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004587
Douglas Gregor98d156a2010-02-17 16:12:00 +00004588 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004589}
4590
Balazs Keri3b30d652018-10-19 13:32:20 +00004591ExpectedDecl
4592ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4593 ObjCCategoryDecl *Category;
4594 if (Error Err = importInto(Category, D->getCategoryDecl()))
4595 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004596
Douglas Gregor4da9d682010-12-07 15:32:12 +00004597 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4598 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004599 DeclContext *DC, *LexicalDC;
4600 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4601 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004602
Balazs Keri3b30d652018-10-19 13:32:20 +00004603 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4604 if (auto Imp = importSeq(
4605 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4606 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4607 else
4608 return Imp.takeError();
4609
Gabor Marton26f72a92018-07-12 09:42:05 +00004610 if (GetImportedOrCreateDecl(
4611 ToImpl, D, Importer.getToContext(), DC,
4612 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004613 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004614 return ToImpl;
4615
Balazs Keri3b30d652018-10-19 13:32:20 +00004616 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004617 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004618 Category->setImplementation(ToImpl);
4619 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004620
Gabor Marton26f72a92018-07-12 09:42:05 +00004621 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004622 if (Error Err = ImportDeclContext(D))
4623 return std::move(Err);
4624
Douglas Gregor4da9d682010-12-07 15:32:12 +00004625 return ToImpl;
4626}
4627
Balazs Keri3b30d652018-10-19 13:32:20 +00004628ExpectedDecl
4629ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004630 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004631 ObjCInterfaceDecl *Iface;
4632 if (Error Err = importInto(Iface, D->getClassInterface()))
4633 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004634
4635 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004636 ObjCInterfaceDecl *Super;
4637 if (Error Err = importInto(Super, D->getSuperClass()))
4638 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004639
4640 ObjCImplementationDecl *Impl = Iface->getImplementation();
4641 if (!Impl) {
4642 // We haven't imported an implementation yet. Create a new @implementation
4643 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004644 DeclContext *DC, *LexicalDC;
4645 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4646 return std::move(Err);
4647
4648 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4649 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4650 if (auto Imp = importSeq(
4651 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4652 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4653 std::tie(
4654 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4655 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4656 else
4657 return Imp.takeError();
4658
Gabor Marton26f72a92018-07-12 09:42:05 +00004659 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004660 DC, Iface, Super,
4661 ToLocation,
4662 ToAtStartLoc,
4663 ToSuperClassLoc,
4664 ToIvarLBraceLoc,
4665 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004666 return Impl;
4667
Balazs Keri3b30d652018-10-19 13:32:20 +00004668 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004669
Douglas Gregorda8025c2010-12-07 01:26:03 +00004670 // Associate the implementation with the class it implements.
4671 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004672 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004673 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004674 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004675
4676 // Verify that the existing @implementation has the same superclass.
4677 if ((Super && !Impl->getSuperClass()) ||
4678 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004679 (Super && Impl->getSuperClass() &&
4680 !declaresSameEntity(Super->getCanonicalDecl(),
4681 Impl->getSuperClass()))) {
4682 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004683 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004684 << Iface->getDeclName();
4685 // FIXME: It would be nice to have the location of the superclass
4686 // below.
4687 if (Impl->getSuperClass())
4688 Importer.ToDiag(Impl->getLocation(),
4689 diag::note_odr_objc_superclass)
4690 << Impl->getSuperClass()->getDeclName();
4691 else
4692 Importer.ToDiag(Impl->getLocation(),
4693 diag::note_odr_objc_missing_superclass);
4694 if (D->getSuperClass())
4695 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004696 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004697 << D->getSuperClass()->getDeclName();
4698 else
4699 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004700 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004701
4702 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004703 }
4704 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004705
Douglas Gregorda8025c2010-12-07 01:26:03 +00004706 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004707 if (Error Err = ImportDeclContext(D))
4708 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004709
4710 return Impl;
4711}
4712
Balazs Keri3b30d652018-10-19 13:32:20 +00004713ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004714 // Import the major distinguishing characteristics of an @property.
4715 DeclContext *DC, *LexicalDC;
4716 DeclarationName Name;
4717 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004718 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004719 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4720 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004721 if (ToD)
4722 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004723
4724 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004725 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004726 for (auto *FoundDecl : FoundDecls) {
4727 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004728 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004729 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004730 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004731 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004732 << Name << D->getType() << FoundProp->getType();
4733 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4734 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004735
4736 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004737 }
4738
4739 // FIXME: Check property attributes, getters, setters, etc.?
4740
4741 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004742 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004743 return FoundProp;
4744 }
4745 }
4746
Balazs Keri3b30d652018-10-19 13:32:20 +00004747 QualType ToType;
4748 TypeSourceInfo *ToTypeSourceInfo;
4749 SourceLocation ToAtLoc, ToLParenLoc;
4750 if (auto Imp = importSeq(
4751 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4752 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4753 else
4754 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004755
4756 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004757 ObjCPropertyDecl *ToProperty;
4758 if (GetImportedOrCreateDecl(
4759 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004760 Name.getAsIdentifierInfo(), ToAtLoc,
4761 ToLParenLoc, ToType,
4762 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004763 return ToProperty;
4764
Balazs Keri3b30d652018-10-19 13:32:20 +00004765 Selector ToGetterName, ToSetterName;
4766 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4767 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4768 ObjCIvarDecl *ToPropertyIvarDecl;
4769 if (auto Imp = importSeq(
4770 D->getGetterName(), D->getSetterName(),
4771 D->getGetterNameLoc(), D->getSetterNameLoc(),
4772 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4773 D->getPropertyIvarDecl()))
4774 std::tie(
4775 ToGetterName, ToSetterName,
4776 ToGetterNameLoc, ToSetterNameLoc,
4777 ToGetterMethodDecl, ToSetterMethodDecl,
4778 ToPropertyIvarDecl) = *Imp;
4779 else
4780 return Imp.takeError();
4781
Douglas Gregora11c4582010-02-17 18:02:10 +00004782 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004783 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004784
4785 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004786 ToProperty->setPropertyAttributesAsWritten(
4787 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004788 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4789 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4790 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4791 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4792 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004793 return ToProperty;
4794}
4795
Balazs Keri3b30d652018-10-19 13:32:20 +00004796ExpectedDecl
4797ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4798 ObjCPropertyDecl *Property;
4799 if (Error Err = importInto(Property, D->getPropertyDecl()))
4800 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004801
Balazs Keri3b30d652018-10-19 13:32:20 +00004802 DeclContext *DC, *LexicalDC;
4803 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4804 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004805
Balazs Keri3b30d652018-10-19 13:32:20 +00004806 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004807
4808 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004809 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004810 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4811 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004812
4813 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004814 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4815 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004816 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004817 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4818 if (auto Imp = importSeq(
4819 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4820 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4821 else
4822 return Imp.takeError();
4823
Gabor Marton26f72a92018-07-12 09:42:05 +00004824 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004825 ToBeginLoc,
4826 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004827 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004828 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004829 return ToImpl;
4830
Douglas Gregor14a49e22010-12-07 18:32:03 +00004831 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004832 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004833 } else {
4834 // Check that we have the same kind of property implementation (@synthesize
4835 // vs. @dynamic).
4836 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004837 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004838 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004839 << Property->getDeclName()
4840 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004841 == ObjCPropertyImplDecl::Dynamic);
4842 Importer.FromDiag(D->getLocation(),
4843 diag::note_odr_objc_property_impl_kind)
4844 << D->getPropertyDecl()->getDeclName()
4845 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004846
4847 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004848 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004849
4850 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004851 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4852 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004853 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004854 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004855 << Property->getDeclName()
4856 << ToImpl->getPropertyIvarDecl()->getDeclName()
4857 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004858 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004859 diag::note_odr_objc_synthesize_ivar_here)
4860 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004861
4862 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004863 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004864
Douglas Gregor14a49e22010-12-07 18:32:03 +00004865 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004866 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004867 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004868
Douglas Gregor14a49e22010-12-07 18:32:03 +00004869 return ToImpl;
4870}
4871
Balazs Keri3b30d652018-10-19 13:32:20 +00004872ExpectedDecl
4873ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004874 // For template arguments, we adopt the translation unit as our declaration
4875 // context. This context will be fixed when the actual template declaration
4876 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004877
Douglas Gregora082a492010-11-30 19:14:50 +00004878 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004879
4880 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4881 if (!BeginLocOrErr)
4882 return BeginLocOrErr.takeError();
4883
4884 ExpectedSLoc LocationOrErr = import(D->getLocation());
4885 if (!LocationOrErr)
4886 return LocationOrErr.takeError();
4887
Gabor Marton26f72a92018-07-12 09:42:05 +00004888 TemplateTypeParmDecl *ToD = nullptr;
4889 (void)GetImportedOrCreateDecl(
4890 ToD, D, Importer.getToContext(),
4891 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004892 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004893 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4894 D->wasDeclaredWithTypename(), D->isParameterPack());
4895 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004896}
4897
Balazs Keri3b30d652018-10-19 13:32:20 +00004898ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004899ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004900 DeclarationName ToDeclName;
4901 SourceLocation ToLocation, ToInnerLocStart;
4902 QualType ToType;
4903 TypeSourceInfo *ToTypeSourceInfo;
4904 if (auto Imp = importSeq(
4905 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4906 D->getInnerLocStart()))
4907 std::tie(
4908 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4909 ToInnerLocStart) = *Imp;
4910 else
4911 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004912
Douglas Gregora082a492010-11-30 19:14:50 +00004913 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004914
4915 NonTypeTemplateParmDecl *ToD = nullptr;
4916 (void)GetImportedOrCreateDecl(
4917 ToD, D, Importer.getToContext(),
4918 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004919 ToInnerLocStart, ToLocation, D->getDepth(),
4920 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4921 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004922 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004923}
4924
Balazs Keri3b30d652018-10-19 13:32:20 +00004925ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004926ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4927 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004928 auto NameOrErr = import(D->getDeclName());
4929 if (!NameOrErr)
4930 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004931
Douglas Gregora082a492010-11-30 19:14:50 +00004932 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004933 ExpectedSLoc LocationOrErr = import(D->getLocation());
4934 if (!LocationOrErr)
4935 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004936
Douglas Gregora082a492010-11-30 19:14:50 +00004937 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004938 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004939 if (!TemplateParamsOrErr)
4940 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004941
Douglas Gregora082a492010-11-30 19:14:50 +00004942 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004943
4944 TemplateTemplateParmDecl *ToD = nullptr;
4945 (void)GetImportedOrCreateDecl(
4946 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004947 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4948 D->getDepth(), D->getPosition(), D->isParameterPack(),
4949 (*NameOrErr).getAsIdentifierInfo(),
4950 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004951 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004952}
4953
Gabor Marton16d98c22019-03-07 13:01:51 +00004954// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004955// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004956template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4957 assert(D->getTemplatedDecl() && "Should be called on templates only");
4958 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004959 if (!ToTemplatedDef)
4960 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004961 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4962 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004963}
4964
Balazs Keri3b30d652018-10-19 13:32:20 +00004965ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004966 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4967
Douglas Gregora082a492010-11-30 19:14:50 +00004968 // Import the major distinguishing characteristics of this class template.
4969 DeclContext *DC, *LexicalDC;
4970 DeclarationName Name;
4971 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004972 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004973 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4974 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004975 if (ToD)
4976 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004977
Gabor Marton7df342a2018-12-17 12:42:12 +00004978 ClassTemplateDecl *FoundByLookup = nullptr;
4979
Douglas Gregora082a492010-11-30 19:14:50 +00004980 // We may already have a template of the same name; try to find and match it.
4981 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004982 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004983 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004984 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004985 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4986 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004987 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004988
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004989 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004990 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4991 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004992
Douglas Gregora082a492010-11-30 19:14:50 +00004993 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004994 ClassTemplateDecl *TemplateWithDef =
4995 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004996 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4997 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004998 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004999 FoundByLookup = FoundTemplate;
5000 break;
Gabor Marton9581c332018-05-23 13:53:36 +00005001 }
Douglas Gregora082a492010-11-30 19:14:50 +00005002 }
Gabor Marton9581c332018-05-23 13:53:36 +00005003
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005004 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005005 }
Gabor Marton9581c332018-05-23 13:53:36 +00005006
Douglas Gregora082a492010-11-30 19:14:50 +00005007 if (!ConflictingDecls.empty()) {
5008 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005009 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005010 ConflictingDecls.size());
5011 }
Gabor Marton9581c332018-05-23 13:53:36 +00005012
Douglas Gregora082a492010-11-30 19:14:50 +00005013 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005014 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005015 }
5016
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005017 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5018
Douglas Gregora082a492010-11-30 19:14:50 +00005019 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005020 CXXRecordDecl *ToTemplated;
5021 if (Error Err = importInto(ToTemplated, FromTemplated))
5022 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005023
Douglas Gregora082a492010-11-30 19:14:50 +00005024 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005025 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005026 if (!TemplateParamsOrErr)
5027 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005028
Gabor Marton26f72a92018-07-12 09:42:05 +00005029 ClassTemplateDecl *D2;
5030 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005031 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005032 return D2;
5033
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005034 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005035
Douglas Gregora082a492010-11-30 19:14:50 +00005036 D2->setAccess(D->getAccess());
5037 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00005038
5039 if (D->getDeclContext()->containsDeclAndLoad(D))
5040 DC->addDeclInternal(D2);
5041 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00005042 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005043
Gabor Marton7df342a2018-12-17 12:42:12 +00005044 if (FoundByLookup) {
5045 auto *Recent =
5046 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5047
5048 // It is possible that during the import of the class template definition
5049 // we start the import of a fwd friend decl of the very same class template
5050 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5051 // had been created earlier and by that time the lookup could not find
5052 // anything existing, so it has no previous decl. Later, (still during the
5053 // import of the fwd friend decl) we start to import the definition again
5054 // and this time the lookup finds the previous fwd friend class template.
5055 // In this case we must set up the previous decl for the templated decl.
5056 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005057 assert(FoundByLookup->getTemplatedDecl() &&
5058 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005059 CXXRecordDecl *PrevTemplated =
5060 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5061 if (ToTemplated != PrevTemplated)
5062 ToTemplated->setPreviousDecl(PrevTemplated);
5063 }
5064
5065 D2->setPreviousDecl(Recent);
5066 }
5067
5068 if (LexicalDC != DC && IsFriend)
5069 DC->makeDeclVisibleInContext(D2);
5070
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005071 if (FromTemplated->isCompleteDefinition() &&
5072 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005073 // FIXME: Import definition!
5074 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005075
Douglas Gregora082a492010-11-30 19:14:50 +00005076 return D2;
5077}
5078
Balazs Keri3b30d652018-10-19 13:32:20 +00005079ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005080 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005081 ClassTemplateDecl *ClassTemplate;
5082 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5083 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005084
Douglas Gregore2e50d332010-12-01 01:36:18 +00005085 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005086 DeclContext *DC, *LexicalDC;
5087 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5088 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005089
5090 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005091 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005092 if (Error Err = ImportTemplateArguments(
5093 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5094 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005095
Douglas Gregore2e50d332010-12-01 01:36:18 +00005096 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005097 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005098 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005099 ClassTemplatePartialSpecializationDecl *PartialSpec =
5100 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5101 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005102 PrevDecl =
5103 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005104 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005105 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005106
Gabor Marton7f8c4002019-03-19 13:34:10 +00005107 if (PrevDecl) {
5108 if (IsStructuralMatch(D, PrevDecl)) {
5109 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5110 Importer.MapImported(D, PrevDecl->getDefinition());
5111 // Import those default field initializers which have been
5112 // instantiated in the "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005113 for (auto *FromField : D->fields()) {
5114 auto ToOrErr = import(FromField);
5115 if (!ToOrErr)
5116 return ToOrErr.takeError();
5117 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005118
Gabor Marton7f8c4002019-03-19 13:34:10 +00005119 // Import those methods which have been instantiated in the
5120 // "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005121 for (CXXMethodDecl *FromM : D->methods()) {
5122 auto ToOrErr = import(FromM);
5123 if (!ToOrErr)
5124 return ToOrErr.takeError();
5125 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005126
Gabor Marton7f8c4002019-03-19 13:34:10 +00005127 // TODO Import instantiated default arguments.
5128 // TODO Import instantiated exception specifications.
5129 //
5130 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5131 // what else could be fused during an AST merge.
5132 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005133 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005134 } else { // ODR violation.
5135 // FIXME HandleNameConflict
Gabor Marton303c98612019-06-25 08:00:51 +00005136 return make_error<ImportError>(ImportError::NameConflict);
Gabor Marton42e15de2018-08-22 11:52:14 +00005137 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005138 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005139
Gabor Marton7f8c4002019-03-19 13:34:10 +00005140 // Import the location of this declaration.
5141 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5142 if (!BeginLocOrErr)
5143 return BeginLocOrErr.takeError();
5144 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5145 if (!IdLocOrErr)
5146 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005147
Gabor Marton7f8c4002019-03-19 13:34:10 +00005148 // Create the specialization.
5149 ClassTemplateSpecializationDecl *D2 = nullptr;
5150 if (PartialSpec) {
5151 // Import TemplateArgumentListInfo.
5152 TemplateArgumentListInfo ToTAInfo;
5153 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5154 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5155 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005156
Gabor Marton7f8c4002019-03-19 13:34:10 +00005157 QualType CanonInjType;
5158 if (Error Err = importInto(
5159 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5160 return std::move(Err);
5161 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005162
Balazs Keridec09162019-03-20 15:42:42 +00005163 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005164 if (!ToTPListOrErr)
5165 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005166
Gabor Marton7f8c4002019-03-19 13:34:10 +00005167 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5168 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5169 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5170 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5171 ToTAInfo, CanonInjType,
5172 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5173 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005174
Gabor Marton7f8c4002019-03-19 13:34:10 +00005175 // Update InsertPos, because preceding import calls may have invalidated
5176 // it by adding new specializations.
5177 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5178 // Add this partial specialization to the class template.
5179 ClassTemplate->AddPartialSpecialization(
5180 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005181
Gabor Marton7f8c4002019-03-19 13:34:10 +00005182 } else { // Not a partial specialization.
5183 if (GetImportedOrCreateDecl(
5184 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5185 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5186 PrevDecl))
5187 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005188
Gabor Marton7f8c4002019-03-19 13:34:10 +00005189 // Update InsertPos, because preceding import calls may have invalidated
5190 // it by adding new specializations.
5191 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5192 // Add this specialization to the class template.
5193 ClassTemplate->AddSpecialization(D2, InsertPos);
5194 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005195
Gabor Marton7f8c4002019-03-19 13:34:10 +00005196 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005197
Gabor Marton7f8c4002019-03-19 13:34:10 +00005198 // Set the context of this specialization/instantiation.
5199 D2->setLexicalDeclContext(LexicalDC);
5200
5201 // Add to the DC only if it was an explicit specialization/instantiation.
5202 if (D2->isExplicitInstantiationOrSpecialization()) {
5203 LexicalDC->addDeclInternal(D2);
5204 }
5205
5206 // Import the qualifier, if any.
5207 if (auto LocOrErr = import(D->getQualifierLoc()))
5208 D2->setQualifierInfo(*LocOrErr);
5209 else
5210 return LocOrErr.takeError();
5211
5212 if (auto *TSI = D->getTypeAsWritten()) {
5213 if (auto TInfoOrErr = import(TSI))
5214 D2->setTypeAsWritten(*TInfoOrErr);
5215 else
5216 return TInfoOrErr.takeError();
5217
5218 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5219 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005220 else
5221 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005222
Gabor Marton7f8c4002019-03-19 13:34:10 +00005223 if (auto LocOrErr = import(D->getExternLoc()))
5224 D2->setExternLoc(*LocOrErr);
5225 else
5226 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005227 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005228
5229 if (D->getPointOfInstantiation().isValid()) {
5230 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5231 D2->setPointOfInstantiation(*POIOrErr);
5232 else
5233 return POIOrErr.takeError();
5234 }
5235
5236 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5237
Balazs Keri3b30d652018-10-19 13:32:20 +00005238 if (D->isCompleteDefinition())
5239 if (Error Err = ImportDefinition(D, D2))
5240 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005241
Douglas Gregore2e50d332010-12-01 01:36:18 +00005242 return D2;
5243}
5244
Balazs Keri3b30d652018-10-19 13:32:20 +00005245ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005246 // If this variable has a definition in the translation unit we're coming
5247 // from,
5248 // but this particular declaration is not that definition, import the
5249 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005250 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005251 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5252 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005253 if (ExpectedDecl ImportedDefOrErr = import(
5254 Definition->getDescribedVarTemplate()))
5255 return Importer.MapImported(D, *ImportedDefOrErr);
5256 else
5257 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005258 }
5259
5260 // Import the major distinguishing characteristics of this variable template.
5261 DeclContext *DC, *LexicalDC;
5262 DeclarationName Name;
5263 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005264 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005265 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5266 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005267 if (ToD)
5268 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005269
5270 // We may already have a template of the same name; try to find and match it.
5271 assert(!DC->isFunctionOrMethod() &&
5272 "Variable templates cannot be declared at function scope");
5273 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005274 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005275 for (auto *FoundDecl : FoundDecls) {
5276 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005277 continue;
5278
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005279 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005280 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005281 if (IsStructuralMatch(D, FoundTemplate)) {
5282 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005283 Importer.MapImported(D->getTemplatedDecl(),
5284 FoundTemplate->getTemplatedDecl());
5285 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005286 }
5287 }
5288
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005289 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005290 }
5291
5292 if (!ConflictingDecls.empty()) {
5293 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5294 ConflictingDecls.data(),
5295 ConflictingDecls.size());
5296 }
5297
5298 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005299 // FIXME: Is it possible to get other error than name conflict?
5300 // (Put this `if` into the previous `if`?)
5301 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005302
5303 VarDecl *DTemplated = D->getTemplatedDecl();
5304
5305 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005306 // FIXME: Value not used?
5307 ExpectedType TypeOrErr = import(DTemplated->getType());
5308 if (!TypeOrErr)
5309 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005310
5311 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005312 VarDecl *ToTemplated;
5313 if (Error Err = importInto(ToTemplated, DTemplated))
5314 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005315
5316 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005317 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005318 if (!TemplateParamsOrErr)
5319 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005320
Gabor Marton26f72a92018-07-12 09:42:05 +00005321 VarTemplateDecl *ToVarTD;
5322 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005323 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005324 return ToVarTD;
5325
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005326 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005327
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005328 ToVarTD->setAccess(D->getAccess());
5329 ToVarTD->setLexicalDeclContext(LexicalDC);
5330 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005331
Larisse Voufo39a1e502013-08-06 01:03:05 +00005332 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005333 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005334 // FIXME: Import definition!
5335 }
5336
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005337 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005338}
5339
Balazs Keri3b30d652018-10-19 13:32:20 +00005340ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005341 VarTemplateSpecializationDecl *D) {
5342 // If this record has a definition in the translation unit we're coming from,
5343 // but this particular declaration is not that definition, import the
5344 // definition and map to that.
5345 VarDecl *Definition = D->getDefinition();
5346 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005347 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5348 return Importer.MapImported(D, *ImportedDefOrErr);
5349 else
5350 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005351 }
5352
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00005353 VarTemplateDecl *VarTemplate = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005354 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5355 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005356
5357 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005358 DeclContext *DC, *LexicalDC;
5359 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5360 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005361
5362 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005363 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5364 if (!BeginLocOrErr)
5365 return BeginLocOrErr.takeError();
5366
5367 auto IdLocOrErr = import(D->getLocation());
5368 if (!IdLocOrErr)
5369 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005370
5371 // Import template arguments.
5372 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005373 if (Error Err = ImportTemplateArguments(
5374 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5375 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005376
5377 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005378 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005379 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005380 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005381 if (D2) {
5382 // We already have a variable template specialization with these template
5383 // arguments.
5384
5385 // FIXME: Check for specialization vs. instantiation errors.
5386
5387 if (VarDecl *FoundDef = D2->getDefinition()) {
5388 if (!D->isThisDeclarationADefinition() ||
5389 IsStructuralMatch(D, FoundDef)) {
5390 // The record types structurally match, or the "from" translation
5391 // unit only had a forward declaration anyway; call it the same
5392 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005393 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005394 }
5395 }
5396 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005397 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005398 QualType T;
5399 if (Error Err = importInto(T, D->getType()))
5400 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005401
Balazs Keri3b30d652018-10-19 13:32:20 +00005402 auto TInfoOrErr = import(D->getTypeSourceInfo());
5403 if (!TInfoOrErr)
5404 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005405
5406 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005407 if (Error Err = ImportTemplateArgumentListInfo(
5408 D->getTemplateArgsInfo(), ToTAInfo))
5409 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005410
5411 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005412 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005413 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5414 // Import TemplateArgumentListInfo
5415 TemplateArgumentListInfo ArgInfos;
5416 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5417 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005418 if (Error Err = ImportTemplateArgumentListInfo(
5419 *FromTAArgsAsWritten, ArgInfos))
5420 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005421
Balazs Keridec09162019-03-20 15:42:42 +00005422 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005423 if (!ToTPListOrErr)
5424 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005425
Gabor Marton26f72a92018-07-12 09:42:05 +00005426 PartVarSpecDecl *ToPartial;
5427 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005428 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5429 VarTemplate, T, *TInfoOrErr,
5430 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005431 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005432
Balazs Keri3b30d652018-10-19 13:32:20 +00005433 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5434 FromPartial->getInstantiatedFromMember()))
5435 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5436 else
5437 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005438
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005439 if (FromPartial->isMemberSpecialization())
5440 ToPartial->setMemberSpecialization();
5441
5442 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005443
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005444 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005445 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5446 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5447 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005448 D->getStorageClass(), TemplateArgs))
5449 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005450 }
5451
Balazs Keri3b30d652018-10-19 13:32:20 +00005452 if (D->getPointOfInstantiation().isValid()) {
5453 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5454 D2->setPointOfInstantiation(*POIOrErr);
5455 else
5456 return POIOrErr.takeError();
5457 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005458
Larisse Voufo39a1e502013-08-06 01:03:05 +00005459 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005460 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005461
5462 // Add this specialization to the class template.
5463 VarTemplate->AddSpecialization(D2, InsertPos);
5464
5465 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005466 if (auto LocOrErr = import(D->getQualifierLoc()))
5467 D2->setQualifierInfo(*LocOrErr);
5468 else
5469 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005470
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005471 if (D->isConstexpr())
5472 D2->setConstexpr(true);
5473
Larisse Voufo39a1e502013-08-06 01:03:05 +00005474 // Add the specialization to this context.
5475 D2->setLexicalDeclContext(LexicalDC);
5476 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005477
5478 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005479 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005480
Balazs Keri3b30d652018-10-19 13:32:20 +00005481 if (Error Err = ImportInitializer(D, D2))
5482 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005483
5484 return D2;
5485}
5486
Balazs Keri3b30d652018-10-19 13:32:20 +00005487ExpectedDecl
5488ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005489 DeclContext *DC, *LexicalDC;
5490 DeclarationName Name;
5491 SourceLocation Loc;
5492 NamedDecl *ToD;
5493
Balazs Keri3b30d652018-10-19 13:32:20 +00005494 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5495 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005496
5497 if (ToD)
5498 return ToD;
5499
Gabor Marton16d98c22019-03-07 13:01:51 +00005500 const FunctionTemplateDecl *FoundByLookup = nullptr;
5501
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005502 // Try to find a function in our own ("to") context with the same name, same
5503 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005504 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005505 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005506 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005507 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005508 for (auto *FoundDecl : FoundDecls) {
5509 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005510 continue;
5511
Gabor Marton16d98c22019-03-07 13:01:51 +00005512 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5513 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005514 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005515 if (IsStructuralMatch(D, FoundTemplate)) {
5516 FunctionTemplateDecl *TemplateWithDef =
5517 getTemplateDefinition(FoundTemplate);
5518 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5519 return Importer.MapImported(D, TemplateWithDef);
5520 }
5521 FoundByLookup = FoundTemplate;
5522 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005523 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005524 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005525 }
5526 }
5527 }
5528 }
5529
Balazs Keridec09162019-03-20 15:42:42 +00005530 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005531 if (!ParamsOrErr)
5532 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005533
Balazs Keri3b30d652018-10-19 13:32:20 +00005534 FunctionDecl *TemplatedFD;
5535 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5536 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005537
Gabor Marton26f72a92018-07-12 09:42:05 +00005538 FunctionTemplateDecl *ToFunc;
5539 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005540 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005541 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005542
5543 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005544
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005545 ToFunc->setAccess(D->getAccess());
5546 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005547 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005548
5549 if (FoundByLookup) {
5550 auto *Recent =
5551 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5552 if (!TemplatedFD->getPreviousDecl()) {
5553 assert(FoundByLookup->getTemplatedDecl() &&
5554 "Found decl must have its templated decl set");
5555 auto *PrevTemplated =
5556 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5557 if (TemplatedFD != PrevTemplated)
5558 TemplatedFD->setPreviousDecl(PrevTemplated);
5559 }
5560 ToFunc->setPreviousDecl(Recent);
5561 }
5562
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005563 return ToFunc;
5564}
5565
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005566//----------------------------------------------------------------------------
5567// Import Statements
5568//----------------------------------------------------------------------------
5569
Balazs Keri3b30d652018-10-19 13:32:20 +00005570ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005571 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5572 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005573 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005574}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005575
Balazs Keri3b30d652018-10-19 13:32:20 +00005576
5577ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Gabor Marton303c98612019-06-25 08:00:51 +00005578 if (Importer.returnWithErrorInTest())
5579 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005580 SmallVector<IdentifierInfo *, 4> Names;
5581 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5582 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005583 // ToII is nullptr when no symbolic name is given for output operand
5584 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005585 Names.push_back(ToII);
5586 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005587
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005588 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5589 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005590 // ToII is nullptr when no symbolic name is given for input operand
5591 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005592 Names.push_back(ToII);
5593 }
5594
5595 SmallVector<StringLiteral *, 4> Clobbers;
5596 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005597 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5598 Clobbers.push_back(*ClobberOrErr);
5599 else
5600 return ClobberOrErr.takeError();
5601
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005602 }
5603
5604 SmallVector<StringLiteral *, 4> Constraints;
5605 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005606 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5607 Constraints.push_back(*OutputOrErr);
5608 else
5609 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005610 }
5611
5612 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005613 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5614 Constraints.push_back(*InputOrErr);
5615 else
5616 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005617 }
5618
Jennifer Yub8fee672019-06-03 15:57:25 +00005619 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() +
5620 S->getNumLabels());
Balazs Keri3b30d652018-10-19 13:32:20 +00005621 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5622 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005623
Jennifer Yub8fee672019-06-03 15:57:25 +00005624 if (Error Err =
5625 ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
5626 return std::move(Err);
5627
Balazs Keri3b30d652018-10-19 13:32:20 +00005628 if (Error Err = ImportArrayChecked(
Jennifer Yub8fee672019-06-03 15:57:25 +00005629 S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs()))
Balazs Keri3b30d652018-10-19 13:32:20 +00005630 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005631
Balazs Keri3b30d652018-10-19 13:32:20 +00005632 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5633 if (!AsmLocOrErr)
5634 return AsmLocOrErr.takeError();
5635 auto AsmStrOrErr = import(S->getAsmString());
5636 if (!AsmStrOrErr)
5637 return AsmStrOrErr.takeError();
5638 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5639 if (!RParenLocOrErr)
5640 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005641
5642 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005643 Importer.getToContext(),
5644 *AsmLocOrErr,
5645 S->isSimple(),
5646 S->isVolatile(),
5647 S->getNumOutputs(),
5648 S->getNumInputs(),
5649 Names.data(),
5650 Constraints.data(),
5651 Exprs.data(),
5652 *AsmStrOrErr,
5653 S->getNumClobbers(),
5654 Clobbers.data(),
Jennifer Yub8fee672019-06-03 15:57:25 +00005655 S->getNumLabels(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005656 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005657}
5658
Balazs Keri3b30d652018-10-19 13:32:20 +00005659ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5660 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5661 if (!Imp)
5662 return Imp.takeError();
5663
5664 DeclGroupRef ToDG;
5665 SourceLocation ToBeginLoc, ToEndLoc;
5666 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5667
5668 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005669}
5670
Balazs Keri3b30d652018-10-19 13:32:20 +00005671ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5672 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5673 if (!ToSemiLocOrErr)
5674 return ToSemiLocOrErr.takeError();
5675 return new (Importer.getToContext()) NullStmt(
5676 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005677}
5678
Balazs Keri3b30d652018-10-19 13:32:20 +00005679ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005680 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005681
Balazs Keri3b30d652018-10-19 13:32:20 +00005682 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5683 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005684
Balazs Keri3b30d652018-10-19 13:32:20 +00005685 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5686 if (!ToLBracLocOrErr)
5687 return ToLBracLocOrErr.takeError();
5688
5689 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5690 if (!ToRBracLocOrErr)
5691 return ToRBracLocOrErr.takeError();
5692
5693 return CompoundStmt::Create(
5694 Importer.getToContext(), ToStmts,
5695 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005696}
5697
Balazs Keri3b30d652018-10-19 13:32:20 +00005698ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5699 auto Imp = importSeq(
5700 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5701 S->getEllipsisLoc(), S->getColonLoc());
5702 if (!Imp)
5703 return Imp.takeError();
5704
5705 Expr *ToLHS, *ToRHS;
5706 Stmt *ToSubStmt;
5707 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5708 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5709 *Imp;
5710
Bruno Ricci5b30571752018-10-28 12:30:53 +00005711 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5712 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005713 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005714
Gabor Horvath480892b2017-10-18 09:25:18 +00005715 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005716}
5717
Balazs Keri3b30d652018-10-19 13:32:20 +00005718ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5719 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5720 if (!Imp)
5721 return Imp.takeError();
5722
5723 SourceLocation ToDefaultLoc, ToColonLoc;
5724 Stmt *ToSubStmt;
5725 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5726
5727 return new (Importer.getToContext()) DefaultStmt(
5728 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005729}
5730
Balazs Keri3b30d652018-10-19 13:32:20 +00005731ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5732 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5733 if (!Imp)
5734 return Imp.takeError();
5735
5736 SourceLocation ToIdentLoc;
5737 LabelDecl *ToLabelDecl;
5738 Stmt *ToSubStmt;
5739 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5740
5741 return new (Importer.getToContext()) LabelStmt(
5742 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005743}
5744
Balazs Keri3b30d652018-10-19 13:32:20 +00005745ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5746 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5747 if (!ToAttrLocOrErr)
5748 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005749 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5750 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005751 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5752 return std::move(Err);
5753 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5754 if (!ToSubStmtOrErr)
5755 return ToSubStmtOrErr.takeError();
5756
5757 return AttributedStmt::Create(
5758 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005759}
5760
Balazs Keri3b30d652018-10-19 13:32:20 +00005761ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5762 auto Imp = importSeq(
5763 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5764 S->getThen(), S->getElseLoc(), S->getElse());
5765 if (!Imp)
5766 return Imp.takeError();
5767
5768 SourceLocation ToIfLoc, ToElseLoc;
5769 Stmt *ToInit, *ToThen, *ToElse;
5770 VarDecl *ToConditionVariable;
5771 Expr *ToCond;
5772 std::tie(
5773 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5774 *Imp;
5775
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005776 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5777 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5778 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005779}
5780
Balazs Keri3b30d652018-10-19 13:32:20 +00005781ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5782 auto Imp = importSeq(
5783 S->getInit(), S->getConditionVariable(), S->getCond(),
5784 S->getBody(), S->getSwitchLoc());
5785 if (!Imp)
5786 return Imp.takeError();
5787
5788 Stmt *ToInit, *ToBody;
5789 VarDecl *ToConditionVariable;
5790 Expr *ToCond;
5791 SourceLocation ToSwitchLoc;
5792 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5793
Bruno Riccie2806f82018-10-29 16:12:37 +00005794 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5795 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005796 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005797 ToStmt->setSwitchLoc(ToSwitchLoc);
5798
Sean Callanan59721b32015-04-28 18:41:46 +00005799 // Now we have to re-chain the cases.
5800 SwitchCase *LastChainedSwitchCase = nullptr;
5801 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5802 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005803 Expected<SwitchCase *> ToSCOrErr = import(SC);
5804 if (!ToSCOrErr)
5805 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005806 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005807 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005808 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005809 ToStmt->setSwitchCaseList(*ToSCOrErr);
5810 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005811 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005812
Sean Callanan59721b32015-04-28 18:41:46 +00005813 return ToStmt;
5814}
5815
Balazs Keri3b30d652018-10-19 13:32:20 +00005816ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5817 auto Imp = importSeq(
5818 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5819 if (!Imp)
5820 return Imp.takeError();
5821
5822 VarDecl *ToConditionVariable;
5823 Expr *ToCond;
5824 Stmt *ToBody;
5825 SourceLocation ToWhileLoc;
5826 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5827
Bruno Riccibacf7512018-10-30 13:42:41 +00005828 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5829 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005830}
5831
Balazs Keri3b30d652018-10-19 13:32:20 +00005832ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5833 auto Imp = importSeq(
5834 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5835 S->getRParenLoc());
5836 if (!Imp)
5837 return Imp.takeError();
5838
5839 Stmt *ToBody;
5840 Expr *ToCond;
5841 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5842 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5843
5844 return new (Importer.getToContext()) DoStmt(
5845 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005846}
5847
Balazs Keri3b30d652018-10-19 13:32:20 +00005848ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5849 auto Imp = importSeq(
5850 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5851 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5852 if (!Imp)
5853 return Imp.takeError();
5854
5855 Stmt *ToInit;
5856 Expr *ToCond, *ToInc;
5857 VarDecl *ToConditionVariable;
5858 Stmt *ToBody;
5859 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5860 std::tie(
5861 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5862 ToLParenLoc, ToRParenLoc) = *Imp;
5863
5864 return new (Importer.getToContext()) ForStmt(
5865 Importer.getToContext(),
5866 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5867 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005868}
5869
Balazs Keri3b30d652018-10-19 13:32:20 +00005870ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5871 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5872 if (!Imp)
5873 return Imp.takeError();
5874
5875 LabelDecl *ToLabel;
5876 SourceLocation ToGotoLoc, ToLabelLoc;
5877 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5878
5879 return new (Importer.getToContext()) GotoStmt(
5880 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005881}
5882
Balazs Keri3b30d652018-10-19 13:32:20 +00005883ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5884 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5885 if (!Imp)
5886 return Imp.takeError();
5887
5888 SourceLocation ToGotoLoc, ToStarLoc;
5889 Expr *ToTarget;
5890 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5891
5892 return new (Importer.getToContext()) IndirectGotoStmt(
5893 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005894}
5895
Balazs Keri3b30d652018-10-19 13:32:20 +00005896ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5897 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5898 if (!ToContinueLocOrErr)
5899 return ToContinueLocOrErr.takeError();
5900 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005901}
5902
Balazs Keri3b30d652018-10-19 13:32:20 +00005903ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5904 auto ToBreakLocOrErr = import(S->getBreakLoc());
5905 if (!ToBreakLocOrErr)
5906 return ToBreakLocOrErr.takeError();
5907 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005908}
5909
Balazs Keri3b30d652018-10-19 13:32:20 +00005910ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5911 auto Imp = importSeq(
5912 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5913 if (!Imp)
5914 return Imp.takeError();
5915
5916 SourceLocation ToReturnLoc;
5917 Expr *ToRetValue;
5918 const VarDecl *ToNRVOCandidate;
5919 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5920
Bruno Ricci023b1d12018-10-30 14:40:49 +00005921 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5922 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005923}
5924
Balazs Keri3b30d652018-10-19 13:32:20 +00005925ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5926 auto Imp = importSeq(
5927 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5928 if (!Imp)
5929 return Imp.takeError();
5930
5931 SourceLocation ToCatchLoc;
5932 VarDecl *ToExceptionDecl;
5933 Stmt *ToHandlerBlock;
5934 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5935
5936 return new (Importer.getToContext()) CXXCatchStmt (
5937 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005938}
5939
Balazs Keri3b30d652018-10-19 13:32:20 +00005940ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5941 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5942 if (!ToTryLocOrErr)
5943 return ToTryLocOrErr.takeError();
5944
5945 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5946 if (!ToTryBlockOrErr)
5947 return ToTryBlockOrErr.takeError();
5948
Sean Callanan59721b32015-04-28 18:41:46 +00005949 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5950 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5951 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005952 if (auto ToHandlerOrErr = import(FromHandler))
5953 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005954 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005955 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005956 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005957
5958 return CXXTryStmt::Create(
5959 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005960}
5961
Balazs Keri3b30d652018-10-19 13:32:20 +00005962ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5963 auto Imp1 = importSeq(
5964 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5965 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5966 if (!Imp1)
5967 return Imp1.takeError();
5968 auto Imp2 = importSeq(
5969 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5970 if (!Imp2)
5971 return Imp2.takeError();
5972
5973 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5974 Expr *ToCond, *ToInc;
5975 Stmt *ToInit, *ToBody;
5976 std::tie(
5977 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5978 ToBody) = *Imp1;
5979 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5980 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5981
5982 return new (Importer.getToContext()) CXXForRangeStmt(
5983 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5984 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005985}
5986
Balazs Keri3b30d652018-10-19 13:32:20 +00005987ExpectedStmt
5988ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5989 auto Imp = importSeq(
5990 S->getElement(), S->getCollection(), S->getBody(),
5991 S->getForLoc(), S->getRParenLoc());
5992 if (!Imp)
5993 return Imp.takeError();
5994
5995 Stmt *ToElement, *ToBody;
5996 Expr *ToCollection;
5997 SourceLocation ToForLoc, ToRParenLoc;
5998 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5999
6000 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6001 ToCollection,
6002 ToBody,
6003 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006004 ToRParenLoc);
6005}
6006
Balazs Keri3b30d652018-10-19 13:32:20 +00006007ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6008 auto Imp = importSeq(
6009 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6010 S->getCatchBody());
6011 if (!Imp)
6012 return Imp.takeError();
6013
6014 SourceLocation ToAtCatchLoc, ToRParenLoc;
6015 VarDecl *ToCatchParamDecl;
6016 Stmt *ToCatchBody;
6017 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6018
6019 return new (Importer.getToContext()) ObjCAtCatchStmt (
6020 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006021}
6022
Balazs Keri3b30d652018-10-19 13:32:20 +00006023ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6024 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6025 if (!ToAtFinallyLocOrErr)
6026 return ToAtFinallyLocOrErr.takeError();
6027 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6028 if (!ToAtFinallyStmtOrErr)
6029 return ToAtFinallyStmtOrErr.takeError();
6030 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6031 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006032}
6033
Balazs Keri3b30d652018-10-19 13:32:20 +00006034ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6035 auto Imp = importSeq(
6036 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6037 if (!Imp)
6038 return Imp.takeError();
6039
6040 SourceLocation ToAtTryLoc;
6041 Stmt *ToTryBody, *ToFinallyStmt;
6042 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6043
Sean Callanan59721b32015-04-28 18:41:46 +00006044 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6045 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6046 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006047 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6048 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006049 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006050 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006051 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006052
Sean Callanan59721b32015-04-28 18:41:46 +00006053 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006054 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006055 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006056 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006057}
6058
Balazs Keri3b30d652018-10-19 13:32:20 +00006059ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006060 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006061 auto Imp = importSeq(
6062 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6063 if (!Imp)
6064 return Imp.takeError();
6065
6066 SourceLocation ToAtSynchronizedLoc;
6067 Expr *ToSynchExpr;
6068 Stmt *ToSynchBody;
6069 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6070
Sean Callanan59721b32015-04-28 18:41:46 +00006071 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6072 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6073}
6074
Balazs Keri3b30d652018-10-19 13:32:20 +00006075ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6076 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6077 if (!ToThrowLocOrErr)
6078 return ToThrowLocOrErr.takeError();
6079 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6080 if (!ToThrowExprOrErr)
6081 return ToThrowExprOrErr.takeError();
6082 return new (Importer.getToContext()) ObjCAtThrowStmt(
6083 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006084}
6085
Balazs Keri3b30d652018-10-19 13:32:20 +00006086ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6087 ObjCAutoreleasePoolStmt *S) {
6088 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6089 if (!ToAtLocOrErr)
6090 return ToAtLocOrErr.takeError();
6091 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6092 if (!ToSubStmtOrErr)
6093 return ToSubStmtOrErr.takeError();
6094 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6095 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006096}
6097
6098//----------------------------------------------------------------------------
6099// Import Expressions
6100//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006101ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006102 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6103 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006104 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006105}
6106
Balazs Keri3b30d652018-10-19 13:32:20 +00006107ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6108 auto Imp = importSeq(
6109 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6110 E->getRParenLoc(), E->getType());
6111 if (!Imp)
6112 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006113
Balazs Keri3b30d652018-10-19 13:32:20 +00006114 SourceLocation ToBuiltinLoc, ToRParenLoc;
6115 Expr *ToSubExpr;
6116 TypeSourceInfo *ToWrittenTypeInfo;
6117 QualType ToType;
6118 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6119 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006120
6121 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006122 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6123 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006124}
6125
Tom Roeder521f0042019-02-26 19:26:41 +00006126ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6127 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6128 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6129 if (!Imp)
6130 return Imp.takeError();
6131
6132 Expr *ToCond;
6133 Expr *ToLHS;
6134 Expr *ToRHS;
6135 SourceLocation ToBuiltinLoc, ToRParenLoc;
6136 QualType ToType;
6137 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6138
6139 ExprValueKind VK = E->getValueKind();
6140 ExprObjectKind OK = E->getObjectKind();
6141
6142 bool TypeDependent = ToCond->isTypeDependent();
6143 bool ValueDependent = ToCond->isValueDependent();
6144
6145 // The value of CondIsTrue only matters if the value is not
6146 // condition-dependent.
6147 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6148
6149 return new (Importer.getToContext())
6150 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6151 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6152}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006153
Balazs Keri3b30d652018-10-19 13:32:20 +00006154ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6155 ExpectedType TypeOrErr = import(E->getType());
6156 if (!TypeOrErr)
6157 return TypeOrErr.takeError();
6158
6159 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6160 if (!BeginLocOrErr)
6161 return BeginLocOrErr.takeError();
6162
6163 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006164}
6165
Balazs Keri3b30d652018-10-19 13:32:20 +00006166ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6167 auto Imp = importSeq(
6168 E->getBeginLoc(), E->getType(), E->getFunctionName());
6169 if (!Imp)
6170 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006171
Balazs Keri3b30d652018-10-19 13:32:20 +00006172 SourceLocation ToBeginLoc;
6173 QualType ToType;
6174 StringLiteral *ToFunctionName;
6175 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006176
Bruno Ricci17ff0262018-10-27 19:21:19 +00006177 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6178 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006179}
6180
Balazs Keri3b30d652018-10-19 13:32:20 +00006181ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6182 auto Imp = importSeq(
6183 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6184 E->getLocation(), E->getType());
6185 if (!Imp)
6186 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006187
Balazs Keri3b30d652018-10-19 13:32:20 +00006188 NestedNameSpecifierLoc ToQualifierLoc;
6189 SourceLocation ToTemplateKeywordLoc, ToLocation;
6190 ValueDecl *ToDecl;
6191 QualType ToType;
6192 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6193 *Imp;
6194
6195 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006196 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006197 auto FoundDOrErr = import(E->getFoundDecl());
6198 if (!FoundDOrErr)
6199 return FoundDOrErr.takeError();
6200 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006201 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006202
Aleksei Sidorina693b372016-09-28 10:16:56 +00006203 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006204 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006205 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006206 if (Error Err =
6207 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6208 return std::move(Err);
6209 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006210 }
6211
Balazs Keri3b30d652018-10-19 13:32:20 +00006212 auto *ToE = DeclRefExpr::Create(
6213 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6214 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
Richard Smith715f7a12019-06-11 17:50:32 +00006215 E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse());
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006216 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006217 ToE->setHadMultipleCandidates(true);
6218 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006219}
6220
Balazs Keri3b30d652018-10-19 13:32:20 +00006221ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6222 ExpectedType TypeOrErr = import(E->getType());
6223 if (!TypeOrErr)
6224 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006225
Balazs Keri3b30d652018-10-19 13:32:20 +00006226 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006227}
6228
Balazs Keri3b30d652018-10-19 13:32:20 +00006229ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6230 ExpectedExpr ToInitOrErr = import(E->getInit());
6231 if (!ToInitOrErr)
6232 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006233
Balazs Keri3b30d652018-10-19 13:32:20 +00006234 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6235 if (!ToEqualOrColonLocOrErr)
6236 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006237
Balazs Keri3b30d652018-10-19 13:32:20 +00006238 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006239 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006240 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6241 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6242 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006243 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006244 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006245 }
6246
Balazs Keri3b30d652018-10-19 13:32:20 +00006247 SmallVector<Designator, 4> ToDesignators(E->size());
6248 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6249 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006250
6251 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006252 Importer.getToContext(), ToDesignators,
6253 ToIndexExprs, *ToEqualOrColonLocOrErr,
6254 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006255}
6256
Balazs Keri3b30d652018-10-19 13:32:20 +00006257ExpectedStmt
6258ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6259 ExpectedType ToTypeOrErr = import(E->getType());
6260 if (!ToTypeOrErr)
6261 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006262
Balazs Keri3b30d652018-10-19 13:32:20 +00006263 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6264 if (!ToLocationOrErr)
6265 return ToLocationOrErr.takeError();
6266
6267 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6268 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006269}
6270
Balazs Keri3b30d652018-10-19 13:32:20 +00006271ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6272 ExpectedType ToTypeOrErr = import(E->getType());
6273 if (!ToTypeOrErr)
6274 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006275
Balazs Keri3b30d652018-10-19 13:32:20 +00006276 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6277 if (!ToLocationOrErr)
6278 return ToLocationOrErr.takeError();
6279
6280 return IntegerLiteral::Create(
6281 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006282}
6283
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006284
Balazs Keri3b30d652018-10-19 13:32:20 +00006285ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6286 ExpectedType ToTypeOrErr = import(E->getType());
6287 if (!ToTypeOrErr)
6288 return ToTypeOrErr.takeError();
6289
6290 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6291 if (!ToLocationOrErr)
6292 return ToLocationOrErr.takeError();
6293
6294 return FloatingLiteral::Create(
6295 Importer.getToContext(), E->getValue(), E->isExact(),
6296 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006297}
6298
Balazs Keri3b30d652018-10-19 13:32:20 +00006299ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6300 auto ToTypeOrErr = import(E->getType());
6301 if (!ToTypeOrErr)
6302 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006303
Balazs Keri3b30d652018-10-19 13:32:20 +00006304 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6305 if (!ToSubExprOrErr)
6306 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006307
Balazs Keri3b30d652018-10-19 13:32:20 +00006308 return new (Importer.getToContext()) ImaginaryLiteral(
6309 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006310}
6311
Balazs Keri3b30d652018-10-19 13:32:20 +00006312ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6313 ExpectedType ToTypeOrErr = import(E->getType());
6314 if (!ToTypeOrErr)
6315 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006316
Balazs Keri3b30d652018-10-19 13:32:20 +00006317 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6318 if (!ToLocationOrErr)
6319 return ToLocationOrErr.takeError();
6320
6321 return new (Importer.getToContext()) CharacterLiteral(
6322 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006323}
6324
Balazs Keri3b30d652018-10-19 13:32:20 +00006325ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6326 ExpectedType ToTypeOrErr = import(E->getType());
6327 if (!ToTypeOrErr)
6328 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006329
Balazs Keri3b30d652018-10-19 13:32:20 +00006330 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6331 if (Error Err = ImportArrayChecked(
6332 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6333 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006334
Balazs Keri3b30d652018-10-19 13:32:20 +00006335 return StringLiteral::Create(
6336 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6337 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006338}
6339
Balazs Keri3b30d652018-10-19 13:32:20 +00006340ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6341 auto Imp = importSeq(
6342 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6343 E->getInitializer());
6344 if (!Imp)
6345 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006346
Balazs Keri3b30d652018-10-19 13:32:20 +00006347 SourceLocation ToLParenLoc;
6348 TypeSourceInfo *ToTypeSourceInfo;
6349 QualType ToType;
6350 Expr *ToInitializer;
6351 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006352
6353 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006354 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6355 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006356}
6357
Balazs Keri3b30d652018-10-19 13:32:20 +00006358ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6359 auto Imp = importSeq(
6360 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6361 if (!Imp)
6362 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006363
Balazs Keri3b30d652018-10-19 13:32:20 +00006364 SourceLocation ToBuiltinLoc, ToRParenLoc;
6365 QualType ToType;
6366 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6367
6368 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6369 if (Error Err = ImportArrayChecked(
6370 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6371 ToExprs.begin()))
6372 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006373
6374 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006375 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006376}
6377
Balazs Keri3b30d652018-10-19 13:32:20 +00006378ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6379 auto Imp = importSeq(
6380 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6381 if (!Imp)
6382 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006383
Balazs Keri3b30d652018-10-19 13:32:20 +00006384 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6385 LabelDecl *ToLabel;
6386 QualType ToType;
6387 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006388
6389 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006390 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006391}
6392
Bill Wendling8003edc2018-11-09 00:41:36 +00006393ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6394 auto Imp = importSeq(E->getSubExpr());
6395 if (!Imp)
6396 return Imp.takeError();
6397
6398 Expr *ToSubExpr;
6399 std::tie(ToSubExpr) = *Imp;
6400
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00006401 // TODO : Handle APValue::ValueKind that require importing.
6402 APValue::ValueKind Kind = E->getResultAPValueKind();
6403 if (Kind == APValue::Int || Kind == APValue::Float ||
6404 Kind == APValue::FixedPoint || Kind == APValue::ComplexFloat ||
6405 Kind == APValue::ComplexInt)
6406 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr,
6407 E->getAPValueResult());
Fangrui Song407659a2018-11-30 23:41:18 +00006408 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006409}
6410
Balazs Keri3b30d652018-10-19 13:32:20 +00006411ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6412 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6413 if (!Imp)
6414 return Imp.takeError();
6415
6416 SourceLocation ToLParen, ToRParen;
6417 Expr *ToSubExpr;
6418 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006419
Fangrui Song6907ce22018-07-30 19:24:48 +00006420 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006421 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006422}
6423
Balazs Keri3b30d652018-10-19 13:32:20 +00006424ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6425 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6426 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6427 return std::move(Err);
6428
6429 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6430 if (!ToLParenLocOrErr)
6431 return ToLParenLocOrErr.takeError();
6432
6433 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6434 if (!ToRParenLocOrErr)
6435 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006436
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006437 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6438 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006439}
6440
Balazs Keri3b30d652018-10-19 13:32:20 +00006441ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6442 auto Imp = importSeq(
6443 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6444 if (!Imp)
6445 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006446
Balazs Keri3b30d652018-10-19 13:32:20 +00006447 CompoundStmt *ToSubStmt;
6448 QualType ToType;
6449 SourceLocation ToLParenLoc, ToRParenLoc;
6450 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006451
Balazs Keri3b30d652018-10-19 13:32:20 +00006452 return new (Importer.getToContext()) StmtExpr(
6453 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006454}
6455
Balazs Keri3b30d652018-10-19 13:32:20 +00006456ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6457 auto Imp = importSeq(
6458 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6459 if (!Imp)
6460 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006461
Balazs Keri3b30d652018-10-19 13:32:20 +00006462 Expr *ToSubExpr;
6463 QualType ToType;
6464 SourceLocation ToOperatorLoc;
6465 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006466
Aaron Ballmana5038552018-01-09 13:07:03 +00006467 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006468 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6469 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006470}
6471
Balazs Keri3b30d652018-10-19 13:32:20 +00006472ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006473ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006474 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6475 if (!Imp)
6476 return Imp.takeError();
6477
6478 QualType ToType;
6479 SourceLocation ToOperatorLoc, ToRParenLoc;
6480 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006481
Douglas Gregord8552cd2010-02-19 01:24:23 +00006482 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006483 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6484 import(E->getArgumentTypeInfo());
6485 if (!ToArgumentTypeInfoOrErr)
6486 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006487
Balazs Keri3b30d652018-10-19 13:32:20 +00006488 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6489 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6490 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006491 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006492
Balazs Keri3b30d652018-10-19 13:32:20 +00006493 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6494 if (!ToArgumentExprOrErr)
6495 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006496
Balazs Keri3b30d652018-10-19 13:32:20 +00006497 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6498 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006499}
6500
Balazs Keri3b30d652018-10-19 13:32:20 +00006501ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6502 auto Imp = importSeq(
6503 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6504 if (!Imp)
6505 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006506
Balazs Keri3b30d652018-10-19 13:32:20 +00006507 Expr *ToLHS, *ToRHS;
6508 QualType ToType;
6509 SourceLocation ToOperatorLoc;
6510 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006511
Balazs Keri3b30d652018-10-19 13:32:20 +00006512 return new (Importer.getToContext()) BinaryOperator(
6513 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6514 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006515}
6516
Balazs Keri3b30d652018-10-19 13:32:20 +00006517ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6518 auto Imp = importSeq(
6519 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6520 E->getRHS(), E->getType());
6521 if (!Imp)
6522 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006523
Balazs Keri3b30d652018-10-19 13:32:20 +00006524 Expr *ToCond, *ToLHS, *ToRHS;
6525 SourceLocation ToQuestionLoc, ToColonLoc;
6526 QualType ToType;
6527 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006528
6529 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006530 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6531 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006532}
6533
Balazs Keri3b30d652018-10-19 13:32:20 +00006534ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006535 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006536 auto Imp = importSeq(
6537 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6538 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6539 if (!Imp)
6540 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006541
Balazs Keri3b30d652018-10-19 13:32:20 +00006542 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6543 OpaqueValueExpr *ToOpaqueValue;
6544 SourceLocation ToQuestionLoc, ToColonLoc;
6545 QualType ToType;
6546 std::tie(
6547 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6548 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006549
6550 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006551 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6552 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6553 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006554}
6555
Balazs Keri3b30d652018-10-19 13:32:20 +00006556ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6557 auto Imp = importSeq(
6558 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6559 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6560 if (!Imp)
6561 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006562
Balazs Keri3b30d652018-10-19 13:32:20 +00006563 SourceLocation ToBeginLoc, ToEndLoc;
6564 TypeSourceInfo *ToQueriedTypeSourceInfo;
6565 Expr *ToDimensionExpression;
6566 QualType ToType;
6567 std::tie(
6568 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6569 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006570
6571 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006572 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6573 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006574}
6575
Balazs Keri3b30d652018-10-19 13:32:20 +00006576ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6577 auto Imp = importSeq(
6578 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6579 if (!Imp)
6580 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006581
Balazs Keri3b30d652018-10-19 13:32:20 +00006582 SourceLocation ToBeginLoc, ToEndLoc;
6583 Expr *ToQueriedExpression;
6584 QualType ToType;
6585 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006586
6587 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006588 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6589 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006590}
6591
Balazs Keri3b30d652018-10-19 13:32:20 +00006592ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6593 auto Imp = importSeq(
6594 E->getLocation(), E->getType(), E->getSourceExpr());
6595 if (!Imp)
6596 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006597
Balazs Keri3b30d652018-10-19 13:32:20 +00006598 SourceLocation ToLocation;
6599 QualType ToType;
6600 Expr *ToSourceExpr;
6601 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006602
6603 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006604 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006605}
6606
Balazs Keri3b30d652018-10-19 13:32:20 +00006607ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6608 auto Imp = importSeq(
6609 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6610 if (!Imp)
6611 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006612
Balazs Keri3b30d652018-10-19 13:32:20 +00006613 Expr *ToLHS, *ToRHS;
6614 SourceLocation ToRBracketLoc;
6615 QualType ToType;
6616 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006617
6618 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006619 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6620 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006621}
6622
Balazs Keri3b30d652018-10-19 13:32:20 +00006623ExpectedStmt
6624ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6625 auto Imp = importSeq(
6626 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6627 E->getComputationResultType(), E->getOperatorLoc());
6628 if (!Imp)
6629 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006630
Balazs Keri3b30d652018-10-19 13:32:20 +00006631 Expr *ToLHS, *ToRHS;
6632 QualType ToType, ToComputationLHSType, ToComputationResultType;
6633 SourceLocation ToOperatorLoc;
6634 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6635 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006636
Balazs Keri3b30d652018-10-19 13:32:20 +00006637 return new (Importer.getToContext()) CompoundAssignOperator(
6638 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6639 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6640 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006641}
6642
Balazs Keri3b30d652018-10-19 13:32:20 +00006643Expected<CXXCastPath>
6644ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6645 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006646 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006647 if (auto SpecOrErr = import(*I))
6648 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006649 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006650 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006651 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006652 return Path;
John McCallcf142162010-08-07 06:22:56 +00006653}
6654
Balazs Keri3b30d652018-10-19 13:32:20 +00006655ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6656 ExpectedType ToTypeOrErr = import(E->getType());
6657 if (!ToTypeOrErr)
6658 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006659
Balazs Keri3b30d652018-10-19 13:32:20 +00006660 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6661 if (!ToSubExprOrErr)
6662 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006663
Balazs Keri3b30d652018-10-19 13:32:20 +00006664 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6665 if (!ToBasePathOrErr)
6666 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006667
Balazs Keri3b30d652018-10-19 13:32:20 +00006668 return ImplicitCastExpr::Create(
6669 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6670 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006671}
6672
Balazs Keri3b30d652018-10-19 13:32:20 +00006673ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6674 auto Imp1 = importSeq(
6675 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6676 if (!Imp1)
6677 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006678
Balazs Keri3b30d652018-10-19 13:32:20 +00006679 QualType ToType;
6680 Expr *ToSubExpr;
6681 TypeSourceInfo *ToTypeInfoAsWritten;
6682 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006683
Balazs Keri3b30d652018-10-19 13:32:20 +00006684 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6685 if (!ToBasePathOrErr)
6686 return ToBasePathOrErr.takeError();
6687 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006688
Aleksei Sidorina693b372016-09-28 10:16:56 +00006689 switch (E->getStmtClass()) {
6690 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006691 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006692 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6693 if (!ToLParenLocOrErr)
6694 return ToLParenLocOrErr.takeError();
6695 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6696 if (!ToRParenLocOrErr)
6697 return ToRParenLocOrErr.takeError();
6698 return CStyleCastExpr::Create(
6699 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6700 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6701 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006702 }
6703
6704 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006705 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006706 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6707 if (!ToLParenLocOrErr)
6708 return ToLParenLocOrErr.takeError();
6709 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6710 if (!ToRParenLocOrErr)
6711 return ToRParenLocOrErr.takeError();
6712 return CXXFunctionalCastExpr::Create(
6713 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6714 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6715 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006716 }
6717
6718 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006719 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6720 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6721 if (!ToLParenLocOrErr)
6722 return ToLParenLocOrErr.takeError();
6723 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6724 if (!ToBridgeKeywordLocOrErr)
6725 return ToBridgeKeywordLocOrErr.takeError();
6726 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6727 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6728 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729 }
6730 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006731 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006732 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006733 }
6734}
6735
Balazs Keri3b30d652018-10-19 13:32:20 +00006736ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6737 SmallVector<OffsetOfNode, 4> ToNodes;
6738 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6739 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006740
Balazs Keri3b30d652018-10-19 13:32:20 +00006741 SourceLocation ToBeginLoc, ToEndLoc;
6742 if (FromNode.getKind() != OffsetOfNode::Base) {
6743 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6744 if (!Imp)
6745 return Imp.takeError();
6746 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6747 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006748
Balazs Keri3b30d652018-10-19 13:32:20 +00006749 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006750 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006751 ToNodes.push_back(
6752 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006753 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006754 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006755 auto ToBSOrErr = import(FromNode.getBase());
6756 if (!ToBSOrErr)
6757 return ToBSOrErr.takeError();
6758 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006759 break;
6760 }
6761 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006762 auto ToFieldOrErr = import(FromNode.getField());
6763 if (!ToFieldOrErr)
6764 return ToFieldOrErr.takeError();
6765 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006766 break;
6767 }
6768 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006769 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6770 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006771 break;
6772 }
6773 }
6774 }
6775
Balazs Keri3b30d652018-10-19 13:32:20 +00006776 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6777 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6778 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6779 if (!ToIndexExprOrErr)
6780 return ToIndexExprOrErr.takeError();
6781 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006782 }
6783
Balazs Keri3b30d652018-10-19 13:32:20 +00006784 auto Imp = importSeq(
6785 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6786 E->getRParenLoc());
6787 if (!Imp)
6788 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006789
Balazs Keri3b30d652018-10-19 13:32:20 +00006790 QualType ToType;
6791 TypeSourceInfo *ToTypeSourceInfo;
6792 SourceLocation ToOperatorLoc, ToRParenLoc;
6793 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6794
6795 return OffsetOfExpr::Create(
6796 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6797 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006798}
6799
Balazs Keri3b30d652018-10-19 13:32:20 +00006800ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6801 auto Imp = importSeq(
6802 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6803 if (!Imp)
6804 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006805
Balazs Keri3b30d652018-10-19 13:32:20 +00006806 QualType ToType;
6807 Expr *ToOperand;
6808 SourceLocation ToBeginLoc, ToEndLoc;
6809 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006810
Balazs Keri3b30d652018-10-19 13:32:20 +00006811 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006812 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006813 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006814 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006815 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006816
Balazs Keri3b30d652018-10-19 13:32:20 +00006817 return new (Importer.getToContext()) CXXNoexceptExpr(
6818 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006819}
6820
Balazs Keri3b30d652018-10-19 13:32:20 +00006821ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6822 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6823 if (!Imp)
6824 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006825
Balazs Keri3b30d652018-10-19 13:32:20 +00006826 Expr *ToSubExpr;
6827 QualType ToType;
6828 SourceLocation ToThrowLoc;
6829 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006830
6831 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006832 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006833}
6834
Balazs Keri3b30d652018-10-19 13:32:20 +00006835ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6836 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6837 if (!ToUsedLocOrErr)
6838 return ToUsedLocOrErr.takeError();
6839
6840 auto ToParamOrErr = import(E->getParam());
6841 if (!ToParamOrErr)
6842 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006843
Eric Fiselier708afb52019-05-16 21:04:15 +00006844 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
6845 if (!UsedContextOrErr)
6846 return UsedContextOrErr.takeError();
6847
Aleksei Sidorina693b372016-09-28 10:16:56 +00006848 return CXXDefaultArgExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00006849 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr, *UsedContextOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006850}
6851
Balazs Keri3b30d652018-10-19 13:32:20 +00006852ExpectedStmt
6853ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6854 auto Imp = importSeq(
6855 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6856 if (!Imp)
6857 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006858
Balazs Keri3b30d652018-10-19 13:32:20 +00006859 QualType ToType;
6860 TypeSourceInfo *ToTypeSourceInfo;
6861 SourceLocation ToRParenLoc;
6862 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006863
6864 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006865 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006866}
6867
Balazs Keri3b30d652018-10-19 13:32:20 +00006868ExpectedStmt
6869ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6870 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6871 if (!ToSubExprOrErr)
6872 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006873
Balazs Keri3b30d652018-10-19 13:32:20 +00006874 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6875 if (!ToDtorOrErr)
6876 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006877
6878 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006879 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6880 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006881}
6882
Balazs Keri3b30d652018-10-19 13:32:20 +00006883ExpectedStmt
6884ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6885 auto Imp = importSeq(
6886 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6887 E->getParenOrBraceRange());
6888 if (!Imp)
6889 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006890
Balazs Keri3b30d652018-10-19 13:32:20 +00006891 CXXConstructorDecl *ToConstructor;
6892 QualType ToType;
6893 TypeSourceInfo *ToTypeSourceInfo;
6894 SourceRange ToParenOrBraceRange;
6895 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006896
Balazs Keri3b30d652018-10-19 13:32:20 +00006897 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6898 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6899 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006900
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006901 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006902 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6903 ToParenOrBraceRange, E->hadMultipleCandidates(),
6904 E->isListInitialization(), E->isStdInitListInitialization(),
6905 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006906}
6907
Balazs Keri3b30d652018-10-19 13:32:20 +00006908ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006909ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006910 auto Imp = importSeq(
6911 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6912 if (!Imp)
6913 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006914
Balazs Keri3b30d652018-10-19 13:32:20 +00006915 QualType ToType;
6916 Expr *ToTemporaryExpr;
6917 const ValueDecl *ToExtendingDecl;
6918 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006919
6920 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006921 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006922
6923 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006924 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006925 return ToMTE;
6926}
6927
Balazs Keri3b30d652018-10-19 13:32:20 +00006928ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6929 auto Imp = importSeq(
6930 E->getType(), E->getPattern(), E->getEllipsisLoc());
6931 if (!Imp)
6932 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006933
Balazs Keri3b30d652018-10-19 13:32:20 +00006934 QualType ToType;
6935 Expr *ToPattern;
6936 SourceLocation ToEllipsisLoc;
6937 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006938
6939 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006940 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006941}
6942
Balazs Keri3b30d652018-10-19 13:32:20 +00006943ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6944 auto Imp = importSeq(
6945 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6946 if (!Imp)
6947 return Imp.takeError();
6948
6949 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6950 NamedDecl *ToPack;
6951 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006952
6953 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006954 if (!E->isValueDependent())
6955 Length = E->getPackLength();
6956
Balazs Keri3b30d652018-10-19 13:32:20 +00006957 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006958 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006959 if (Error Err = ImportTemplateArguments(
6960 E->getPartialArguments().data(),
6961 E->getPartialArguments().size(),
6962 ToPartialArguments))
6963 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006964 }
6965
6966 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006967 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6968 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006969}
6970
Aleksei Sidorina693b372016-09-28 10:16:56 +00006971
Balazs Keri3b30d652018-10-19 13:32:20 +00006972ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6973 auto Imp = importSeq(
6974 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6975 E->getArraySize(), E->getInitializer(), E->getType(),
6976 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6977 E->getDirectInitRange());
6978 if (!Imp)
6979 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006980
Balazs Keri3b30d652018-10-19 13:32:20 +00006981 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6982 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00006983 Optional<Expr *> ToArraySize;
6984 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00006985 QualType ToType;
6986 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6987 std::tie(
6988 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6989 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006990
Balazs Keri3b30d652018-10-19 13:32:20 +00006991 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6992 if (Error Err =
6993 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6994 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006995
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006996 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006997 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6998 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6999 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
7000 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
7001 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007002}
7003
Balazs Keri3b30d652018-10-19 13:32:20 +00007004ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
7005 auto Imp = importSeq(
7006 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
7007 if (!Imp)
7008 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007009
Balazs Keri3b30d652018-10-19 13:32:20 +00007010 QualType ToType;
7011 FunctionDecl *ToOperatorDelete;
7012 Expr *ToArgument;
7013 SourceLocation ToBeginLoc;
7014 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007015
7016 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007017 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
7018 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
7019 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00007020}
7021
Balazs Keri3b30d652018-10-19 13:32:20 +00007022ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
7023 auto Imp = importSeq(
7024 E->getType(), E->getLocation(), E->getConstructor(),
7025 E->getParenOrBraceRange());
7026 if (!Imp)
7027 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007028
Balazs Keri3b30d652018-10-19 13:32:20 +00007029 QualType ToType;
7030 SourceLocation ToLocation;
7031 CXXConstructorDecl *ToConstructor;
7032 SourceRange ToParenOrBraceRange;
7033 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007034
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007035 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007036 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7037 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007038
Balazs Keri3b30d652018-10-19 13:32:20 +00007039 return CXXConstructExpr::Create(
7040 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7041 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7042 E->isListInitialization(), E->isStdInitListInitialization(),
7043 E->requiresZeroInitialization(), E->getConstructionKind(),
7044 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007045}
7046
Balazs Keri3b30d652018-10-19 13:32:20 +00007047ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7048 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7049 if (!ToSubExprOrErr)
7050 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007051
Balazs Keri3b30d652018-10-19 13:32:20 +00007052 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7053 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7054 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007055
Balazs Keri3b30d652018-10-19 13:32:20 +00007056 return ExprWithCleanups::Create(
7057 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7058 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007059}
7060
Balazs Keri3b30d652018-10-19 13:32:20 +00007061ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7062 auto Imp = importSeq(
7063 E->getCallee(), E->getType(), E->getRParenLoc());
7064 if (!Imp)
7065 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007066
Balazs Keri3b30d652018-10-19 13:32:20 +00007067 Expr *ToCallee;
7068 QualType ToType;
7069 SourceLocation ToRParenLoc;
7070 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007071
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007072 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007073 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7074 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007075
Bruno Riccic5885cf2018-12-21 15:20:32 +00007076 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7077 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007078}
7079
Balazs Keri3b30d652018-10-19 13:32:20 +00007080ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7081 ExpectedType ToTypeOrErr = import(E->getType());
7082 if (!ToTypeOrErr)
7083 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007084
Balazs Keri3b30d652018-10-19 13:32:20 +00007085 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7086 if (!ToLocationOrErr)
7087 return ToLocationOrErr.takeError();
7088
7089 return new (Importer.getToContext()) CXXThisExpr(
7090 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007091}
7092
Balazs Keri3b30d652018-10-19 13:32:20 +00007093ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7094 ExpectedType ToTypeOrErr = import(E->getType());
7095 if (!ToTypeOrErr)
7096 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007097
Balazs Keri3b30d652018-10-19 13:32:20 +00007098 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7099 if (!ToLocationOrErr)
7100 return ToLocationOrErr.takeError();
7101
7102 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7103 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007104}
7105
Balazs Keri3b30d652018-10-19 13:32:20 +00007106ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7107 auto Imp1 = importSeq(
7108 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7109 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7110 if (!Imp1)
7111 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007112
Balazs Keri3b30d652018-10-19 13:32:20 +00007113 Expr *ToBase;
7114 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7115 NestedNameSpecifierLoc ToQualifierLoc;
7116 ValueDecl *ToMemberDecl;
7117 QualType ToType;
7118 std::tie(
7119 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7120 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007121
Balazs Keri3b30d652018-10-19 13:32:20 +00007122 auto Imp2 = importSeq(
7123 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7124 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7125 if (!Imp2)
7126 return Imp2.takeError();
7127 NamedDecl *ToDecl;
7128 DeclarationName ToName;
7129 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7130 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007131
7132 DeclAccessPair ToFoundDecl =
7133 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007134
Balazs Keri3b30d652018-10-19 13:32:20 +00007135 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007136
Gabor Marton5caba302019-03-07 13:38:20 +00007137 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007138 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007139 if (Error Err =
7140 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7141 E->template_arguments(), ToTAInfo))
7142 return std::move(Err);
7143 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007144 }
7145
Richard Smith1bbad592019-06-11 17:50:36 +00007146 return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(),
7147 ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7148 ToMemberDecl, ToFoundDecl, ToMemberNameInfo,
7149 ResInfo, ToType, E->getValueKind(),
7150 E->getObjectKind(), E->isNonOdrUse());
Sean Callanan59721b32015-04-28 18:41:46 +00007151}
7152
Balazs Keri3b30d652018-10-19 13:32:20 +00007153ExpectedStmt
7154ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7155 auto Imp = importSeq(
7156 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7157 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7158 if (!Imp)
7159 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007160
Balazs Keri3b30d652018-10-19 13:32:20 +00007161 Expr *ToBase;
7162 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7163 NestedNameSpecifierLoc ToQualifierLoc;
7164 TypeSourceInfo *ToScopeTypeInfo;
7165 std::tie(
7166 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7167 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007168
7169 PseudoDestructorTypeStorage Storage;
7170 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7171 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007172 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7173 if (!ToDestroyedTypeLocOrErr)
7174 return ToDestroyedTypeLocOrErr.takeError();
7175 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007176 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007177 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7178 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7179 else
7180 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007181 }
7182
7183 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007184 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7185 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007186}
7187
Balazs Keri3b30d652018-10-19 13:32:20 +00007188ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007189 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007190 auto Imp = importSeq(
7191 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7192 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7193 if (!Imp)
7194 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007195
Balazs Keri3b30d652018-10-19 13:32:20 +00007196 QualType ToType;
7197 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7198 NestedNameSpecifierLoc ToQualifierLoc;
7199 NamedDecl *ToFirstQualifierFoundInScope;
7200 std::tie(
7201 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7202 ToFirstQualifierFoundInScope) = *Imp;
7203
7204 Expr *ToBase = nullptr;
7205 if (!E->isImplicitAccess()) {
7206 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7207 ToBase = *ToBaseOrErr;
7208 else
7209 return ToBaseOrErr.takeError();
7210 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007211
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007212 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007213 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007214 if (Error Err = ImportTemplateArgumentListInfo(
7215 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7216 ToTAInfo))
7217 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007218 ResInfo = &ToTAInfo;
7219 }
7220
Balazs Keri3b30d652018-10-19 13:32:20 +00007221 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7222 if (!ToMemberNameInfoOrErr)
7223 return ToMemberNameInfoOrErr.takeError();
7224 DeclarationNameInfo ToMemberNameInfo(
7225 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007226 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007227 if (Error Err = ImportDeclarationNameLoc(
7228 E->getMemberNameInfo(), ToMemberNameInfo))
7229 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007230
7231 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007232 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7233 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7234 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007235}
7236
Balazs Keri3b30d652018-10-19 13:32:20 +00007237ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007238ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007239 auto Imp = importSeq(
7240 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7241 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7242 if (!Imp)
7243 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007244
Balazs Keri3b30d652018-10-19 13:32:20 +00007245 NestedNameSpecifierLoc ToQualifierLoc;
7246 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7247 DeclarationName ToDeclName;
7248 std::tie(
7249 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7250 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007251
Balazs Keri3b30d652018-10-19 13:32:20 +00007252 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7253 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7254 return std::move(Err);
7255
7256 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007257 TemplateArgumentListInfo *ResInfo = nullptr;
7258 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007259 if (Error Err =
7260 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7261 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007262 ResInfo = &ToTAInfo;
7263 }
7264
7265 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007266 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7267 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007268}
7269
Balazs Keri3b30d652018-10-19 13:32:20 +00007270ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7271 CXXUnresolvedConstructExpr *E) {
7272 auto Imp = importSeq(
7273 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7274 if (!Imp)
7275 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007276
Balazs Keri3b30d652018-10-19 13:32:20 +00007277 SourceLocation ToLParenLoc, ToRParenLoc;
7278 TypeSourceInfo *ToTypeSourceInfo;
7279 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7280
7281 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7282 if (Error Err =
7283 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7284 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007285
7286 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007287 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7288 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007289}
7290
Balazs Keri3b30d652018-10-19 13:32:20 +00007291ExpectedStmt
7292ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7293 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7294 if (!ToNamingClassOrErr)
7295 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007296
Balazs Keri3b30d652018-10-19 13:32:20 +00007297 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7298 if (!ToQualifierLocOrErr)
7299 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007300
Balazs Keri3b30d652018-10-19 13:32:20 +00007301 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7302 if (!ToNameInfoOrErr)
7303 return ToNameInfoOrErr.takeError();
7304 DeclarationNameInfo ToNameInfo(
7305 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007306 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007307 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7308 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007309
7310 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007311 for (auto *D : E->decls())
7312 if (auto ToDOrErr = import(D))
7313 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007314 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007315 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007316
Balazs Keri3b30d652018-10-19 13:32:20 +00007317 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7318 TemplateArgumentListInfo ToTAInfo;
7319 if (Error Err = ImportTemplateArgumentListInfo(
7320 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7321 ToTAInfo))
7322 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007323
Balazs Keri3b30d652018-10-19 13:32:20 +00007324 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7325 if (!ToTemplateKeywordLocOrErr)
7326 return ToTemplateKeywordLocOrErr.takeError();
7327
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007328 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007329 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7330 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7331 ToDecls.begin(), ToDecls.end());
7332 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007333
7334 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007335 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7336 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7337 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007338}
7339
Balazs Keri3b30d652018-10-19 13:32:20 +00007340ExpectedStmt
7341ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7342 auto Imp1 = importSeq(
7343 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7344 E->getTemplateKeywordLoc());
7345 if (!Imp1)
7346 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007347
Balazs Keri3b30d652018-10-19 13:32:20 +00007348 QualType ToType;
7349 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7350 NestedNameSpecifierLoc ToQualifierLoc;
7351 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7352
7353 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7354 if (!Imp2)
7355 return Imp2.takeError();
7356 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7357 // Import additional name location/type info.
7358 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7359 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007360
7361 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007362 for (Decl *D : E->decls())
7363 if (auto ToDOrErr = import(D))
7364 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007365 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007366 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007367
7368 TemplateArgumentListInfo ToTAInfo;
7369 TemplateArgumentListInfo *ResInfo = nullptr;
7370 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007371 if (Error Err =
7372 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7373 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007374 ResInfo = &ToTAInfo;
7375 }
7376
Balazs Keri3b30d652018-10-19 13:32:20 +00007377 Expr *ToBase = nullptr;
7378 if (!E->isImplicitAccess()) {
7379 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7380 ToBase = *ToBaseOrErr;
7381 else
7382 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007383 }
7384
7385 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007386 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7387 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7388 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007389}
7390
Balazs Keri3b30d652018-10-19 13:32:20 +00007391ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7392 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7393 if (!Imp)
7394 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007395
Balazs Keri3b30d652018-10-19 13:32:20 +00007396 Expr *ToCallee;
7397 QualType ToType;
7398 SourceLocation ToRParenLoc;
7399 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007400
7401 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007402 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7403 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7404 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007405
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007406 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007407 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007408 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007409 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7410 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007411 }
7412
Bruno Riccic5885cf2018-12-21 15:20:32 +00007413 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7414 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7415 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007416}
7417
Balazs Keri3b30d652018-10-19 13:32:20 +00007418ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7419 CXXRecordDecl *FromClass = E->getLambdaClass();
7420 auto ToClassOrErr = import(FromClass);
7421 if (!ToClassOrErr)
7422 return ToClassOrErr.takeError();
7423 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007424
7425 // NOTE: lambda classes are created with BeingDefined flag set up.
7426 // It means that ImportDefinition doesn't work for them and we should fill it
7427 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007428 if (ToClass->isBeingDefined())
7429 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7430 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007431
Balazs Keri3b30d652018-10-19 13:32:20 +00007432 auto ToCallOpOrErr = import(E->getCallOperator());
7433 if (!ToCallOpOrErr)
7434 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007435
7436 ToClass->completeDefinition();
7437
Balazs Keri3b30d652018-10-19 13:32:20 +00007438 SmallVector<LambdaCapture, 8> ToCaptures;
7439 ToCaptures.reserve(E->capture_size());
7440 for (const auto &FromCapture : E->captures()) {
7441 if (auto ToCaptureOrErr = import(FromCapture))
7442 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007443 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007444 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007445 }
7446
Balazs Keri3b30d652018-10-19 13:32:20 +00007447 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7448 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7449 return std::move(Err);
7450
7451 auto Imp = importSeq(
7452 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7453 if (!Imp)
7454 return Imp.takeError();
7455
7456 SourceRange ToIntroducerRange;
7457 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7458 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007459
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007460 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007461 Importer.getToContext(), ToClass, ToIntroducerRange,
7462 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7463 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7464 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007465}
7466
Sean Callanan8bca9962016-03-28 21:43:01 +00007467
Balazs Keri3b30d652018-10-19 13:32:20 +00007468ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7469 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7470 if (!Imp)
7471 return Imp.takeError();
7472
7473 SourceLocation ToLBraceLoc, ToRBraceLoc;
7474 QualType ToType;
7475 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7476
7477 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7478 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7479 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007480
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007481 ASTContext &ToCtx = Importer.getToContext();
7482 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007483 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7484 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007485
Balazs Keri3b30d652018-10-19 13:32:20 +00007486 if (E->hasArrayFiller()) {
7487 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7488 To->setArrayFiller(*ToFillerOrErr);
7489 else
7490 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007491 }
7492
Balazs Keri3b30d652018-10-19 13:32:20 +00007493 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7494 if (auto ToFDOrErr = import(FromFD))
7495 To->setInitializedFieldInUnion(*ToFDOrErr);
7496 else
7497 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007498 }
7499
Balazs Keri3b30d652018-10-19 13:32:20 +00007500 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7501 if (auto ToSyntFormOrErr = import(SyntForm))
7502 To->setSyntacticForm(*ToSyntFormOrErr);
7503 else
7504 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007505 }
7506
Gabor Martona20ce602018-09-03 13:10:53 +00007507 // Copy InitListExprBitfields, which are not handled in the ctor of
7508 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007509 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007510
7511 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007512}
7513
Balazs Keri3b30d652018-10-19 13:32:20 +00007514ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007515 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007516 ExpectedType ToTypeOrErr = import(E->getType());
7517 if (!ToTypeOrErr)
7518 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007519
Balazs Keri3b30d652018-10-19 13:32:20 +00007520 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7521 if (!ToSubExprOrErr)
7522 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007523
Balazs Keri3b30d652018-10-19 13:32:20 +00007524 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7525 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007526}
7527
Balazs Keri3b30d652018-10-19 13:32:20 +00007528ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007529 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007530 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7531 if (!Imp)
7532 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007533
Balazs Keri3b30d652018-10-19 13:32:20 +00007534 SourceLocation ToLocation;
7535 QualType ToType;
7536 CXXConstructorDecl *ToConstructor;
7537 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007538
7539 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007540 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7541 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007542}
7543
Balazs Keri3b30d652018-10-19 13:32:20 +00007544ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7545 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7546 if (!Imp)
7547 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007548
Balazs Keri3b30d652018-10-19 13:32:20 +00007549 QualType ToType;
7550 Expr *ToCommonExpr, *ToSubExpr;
7551 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007552
Balazs Keri3b30d652018-10-19 13:32:20 +00007553 return new (Importer.getToContext()) ArrayInitLoopExpr(
7554 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007555}
7556
Balazs Keri3b30d652018-10-19 13:32:20 +00007557ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7558 ExpectedType ToTypeOrErr = import(E->getType());
7559 if (!ToTypeOrErr)
7560 return ToTypeOrErr.takeError();
7561 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007562}
7563
Balazs Keri3b30d652018-10-19 13:32:20 +00007564ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7565 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7566 if (!ToBeginLocOrErr)
7567 return ToBeginLocOrErr.takeError();
7568
7569 auto ToFieldOrErr = import(E->getField());
7570 if (!ToFieldOrErr)
7571 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007572
Eric Fiselier708afb52019-05-16 21:04:15 +00007573 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7574 if (!UsedContextOrErr)
7575 return UsedContextOrErr.takeError();
7576
Sean Callanandd2c1742016-05-16 20:48:03 +00007577 return CXXDefaultInitExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00007578 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007579}
7580
Balazs Keri3b30d652018-10-19 13:32:20 +00007581ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7582 auto Imp = importSeq(
7583 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7584 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7585 if (!Imp)
7586 return Imp.takeError();
7587
7588 QualType ToType;
7589 Expr *ToSubExpr;
7590 TypeSourceInfo *ToTypeInfoAsWritten;
7591 SourceLocation ToOperatorLoc, ToRParenLoc;
7592 SourceRange ToAngleBrackets;
7593 std::tie(
7594 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7595 ToAngleBrackets) = *Imp;
7596
Sean Callanandd2c1742016-05-16 20:48:03 +00007597 ExprValueKind VK = E->getValueKind();
7598 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007599 auto ToBasePathOrErr = ImportCastPath(E);
7600 if (!ToBasePathOrErr)
7601 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007602
Sean Callanandd2c1742016-05-16 20:48:03 +00007603 if (isa<CXXStaticCastExpr>(E)) {
7604 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007605 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7606 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007607 } else if (isa<CXXDynamicCastExpr>(E)) {
7608 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007609 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7610 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007611 } else if (isa<CXXReinterpretCastExpr>(E)) {
7612 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007613 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7614 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007615 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007616 return CXXConstCastExpr::Create(
7617 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7618 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007619 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007620 llvm_unreachable("Unknown cast type");
7621 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007622 }
7623}
7624
Balazs Keri3b30d652018-10-19 13:32:20 +00007625ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007626 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007627 auto Imp = importSeq(
7628 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7629 if (!Imp)
7630 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007631
Balazs Keri3b30d652018-10-19 13:32:20 +00007632 QualType ToType;
7633 SourceLocation ToExprLoc;
7634 NonTypeTemplateParmDecl *ToParameter;
7635 Expr *ToReplacement;
7636 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007637
7638 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007639 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007640}
7641
Balazs Keri3b30d652018-10-19 13:32:20 +00007642ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7643 auto Imp = importSeq(
7644 E->getType(), E->getBeginLoc(), E->getEndLoc());
7645 if (!Imp)
7646 return Imp.takeError();
7647
7648 QualType ToType;
7649 SourceLocation ToBeginLoc, ToEndLoc;
7650 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007651
7652 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007653 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7654 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007655
7656 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7657 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007658 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007659
7660 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007661 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7662 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007663}
7664
Balazs Keri3b30d652018-10-19 13:32:20 +00007665ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7666 ExpectedType ToTypeOrErr = import(E->getType());
7667 if (!ToTypeOrErr)
7668 return ToTypeOrErr.takeError();
7669
7670 auto ToSourceRangeOrErr = import(E->getSourceRange());
7671 if (!ToSourceRangeOrErr)
7672 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007673
7674 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007675 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7676 return new (Importer.getToContext()) CXXTypeidExpr(
7677 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7678 else
7679 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007680 }
7681
Balazs Keri3b30d652018-10-19 13:32:20 +00007682 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7683 if (!ToExprOperandOrErr)
7684 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007685
Balazs Keri3b30d652018-10-19 13:32:20 +00007686 return new (Importer.getToContext()) CXXTypeidExpr(
7687 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007688}
7689
Lang Hames19e07e12017-06-20 21:06:00 +00007690void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7691 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007692 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7693 if (auto ImportedOrErr = import(FromOverriddenMethod))
7694 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7695 (*ImportedOrErr)->getCanonicalDecl()));
7696 else
7697 consumeError(ImportedOrErr.takeError());
7698 }
Lang Hames19e07e12017-06-20 21:06:00 +00007699}
7700
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007701ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007702 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007703 bool MinimalImport,
7704 ASTImporterLookupTable *LookupTable)
7705 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007706 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7707 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007708
7709 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7710 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007711}
7712
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007713ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007714
Gabor Marton54058b52018-12-17 13:53:12 +00007715Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7716 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7717 "Try to get field index for non-field.");
7718
7719 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7720 if (!Owner)
7721 return None;
7722
7723 unsigned Index = 0;
7724 for (const auto *D : Owner->decls()) {
7725 if (D == F)
7726 return Index;
7727
7728 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7729 ++Index;
7730 }
7731
7732 llvm_unreachable("Field was not found in its parent context.");
7733
7734 return None;
7735}
7736
7737ASTImporter::FoundDeclsTy
7738ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7739 // We search in the redecl context because of transparent contexts.
7740 // E.g. a simple C language enum is a transparent context:
7741 // enum E { A, B };
7742 // Now if we had a global variable in the TU
7743 // int A;
7744 // then the enum constant 'A' and the variable 'A' violates ODR.
7745 // We can diagnose this only if we search in the redecl context.
7746 DeclContext *ReDC = DC->getRedeclContext();
7747 if (LookupTable) {
7748 ASTImporterLookupTable::LookupResult LookupResult =
7749 LookupTable->lookup(ReDC, Name);
7750 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7751 } else {
7752 // FIXME Can we remove this kind of lookup?
7753 // Or lldb really needs this C/C++ lookup?
7754 FoundDeclsTy Result;
7755 ReDC->localUncachedLookup(Name, Result);
7756 return Result;
7757 }
7758}
7759
7760void ASTImporter::AddToLookupTable(Decl *ToD) {
7761 if (LookupTable)
7762 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7763 LookupTable->add(ToND);
7764}
7765
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007766Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7767 // Import the decl using ASTNodeImporter.
7768 ASTNodeImporter Importer(*this);
7769 return Importer.Visit(FromD);
7770}
7771
7772void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7773 MapImported(FromD, ToD);
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007774}
7775
Gabor Marton5ac6d492019-05-15 10:29:48 +00007776Expected<QualType> ASTImporter::Import(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007777 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007778 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007779
Balazs Keri3b30d652018-10-19 13:32:20 +00007780 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007781
7782 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007783 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007784 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007785 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007786 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007787
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007788 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007789 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007790 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007791 if (!ToTOrErr)
7792 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007793
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007794 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007795 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007796
Balazs Keri3b30d652018-10-19 13:32:20 +00007797 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007798}
7799
Gabor Marton5ac6d492019-05-15 10:29:48 +00007800Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007801 if (!FromTSI)
7802 return FromTSI;
7803
7804 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007805 // on the type and a single location. Implement a real version of this.
Gabor Marton5ac6d492019-05-15 10:29:48 +00007806 ExpectedType TOrErr = Import(FromTSI->getType());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007807 if (!TOrErr)
7808 return TOrErr.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00007809 ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007810 if (!BeginLocOrErr)
7811 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007812
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007813 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7814}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007815
Gabor Marton5ac6d492019-05-15 10:29:48 +00007816Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007817 Attr *ToAttr = FromAttr->clone(ToContext);
Gabor Marton5ac6d492019-05-15 10:29:48 +00007818 if (auto ToRangeOrErr = Import(FromAttr->getRange()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007819 ToAttr->setRange(*ToRangeOrErr);
7820 else
7821 return ToRangeOrErr.takeError();
7822
Davide Italianofaee83d2018-11-28 19:15:23 +00007823 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007824}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007825
Gabor Martonbe77a982018-12-12 11:22:55 +00007826Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7827 auto Pos = ImportedDecls.find(FromD);
7828 if (Pos != ImportedDecls.end())
7829 return Pos->second;
7830 else
Sean Callanan59721b32015-04-28 18:41:46 +00007831 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007832}
7833
Gabor Marton458d1452019-02-14 13:07:03 +00007834TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7835 auto FromDPos = ImportedFromDecls.find(ToD);
7836 if (FromDPos == ImportedFromDecls.end())
7837 return nullptr;
7838 return FromDPos->second->getTranslationUnitDecl();
7839}
7840
Gabor Marton5ac6d492019-05-15 10:29:48 +00007841Expected<Decl *> ASTImporter::Import(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007842 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007843 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007844
Gabor Marton1ad4b992019-07-01 14:19:53 +00007845 // Push FromD to the stack, and remove that when we return.
7846 ImportPath.push(FromD);
7847 auto ImportPathBuilder =
7848 llvm::make_scope_exit([this]() { ImportPath.pop(); });
Douglas Gregord451ea92011-07-29 23:31:30 +00007849
Gabor Marton303c98612019-06-25 08:00:51 +00007850 // Check whether there was a previous failed import.
7851 // If yes return the existing error.
7852 if (auto Error = getImportDeclErrorIfAny(FromD))
7853 return make_error<ImportError>(*Error);
7854
Gabor Marton26f72a92018-07-12 09:42:05 +00007855 // Check whether we've already imported this declaration.
7856 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7857 if (ToD) {
7858 // If FromD has some updated flags after last import, apply it
7859 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00007860 // If we encounter a cycle during an import then we save the relevant part
7861 // of the import path associated to the Decl.
7862 if (ImportPath.hasCycleAtBack())
7863 SavedImportPaths[FromD].push_back(ImportPath.copyCycleAtBack());
Douglas Gregord451ea92011-07-29 23:31:30 +00007864 return ToD;
7865 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007866
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007867 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007868 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Gabor Marton303c98612019-06-25 08:00:51 +00007869 if (!ToDOrErr) {
7870 // Failed to import.
7871
7872 auto Pos = ImportedDecls.find(FromD);
7873 if (Pos != ImportedDecls.end()) {
7874 // Import failed after the object was created.
7875 // Remove all references to it.
7876 auto *ToD = Pos->second;
7877 ImportedDecls.erase(Pos);
7878
7879 // ImportedDecls and ImportedFromDecls are not symmetric. It may happen
7880 // (e.g. with namespaces) that several decls from the 'from' context are
7881 // mapped to the same decl in the 'to' context. If we removed entries
7882 // from the LookupTable here then we may end up removing them multiple
7883 // times.
7884
7885 // The Lookuptable contains decls only which are in the 'to' context.
7886 // Remove from the Lookuptable only if it is *imported* into the 'to'
7887 // context (and do not remove it if it was added during the initial
7888 // traverse of the 'to' context).
7889 auto PosF = ImportedFromDecls.find(ToD);
7890 if (PosF != ImportedFromDecls.end()) {
7891 if (LookupTable)
7892 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7893 LookupTable->remove(ToND);
7894 ImportedFromDecls.erase(PosF);
7895 }
7896
7897 // FIXME: AST may contain remaining references to the failed object.
7898 }
7899
Gabor Marton1ad4b992019-07-01 14:19:53 +00007900 // After takeError the error is not usable anymore in ToDOrErr.
Gabor Marton303c98612019-06-25 08:00:51 +00007901 // Get a copy of the error object (any more simple solution for this?).
7902 ImportError ErrOut;
7903 handleAllErrors(ToDOrErr.takeError(),
7904 [&ErrOut](const ImportError &E) { ErrOut = E; });
7905 setImportDeclError(FromD, ErrOut);
Gabor Marton1ad4b992019-07-01 14:19:53 +00007906
7907 // Set the error for all nodes which have been created before we
7908 // recognized the error.
7909 for (const auto &Path : SavedImportPaths[FromD])
7910 for (Decl *Di : Path)
7911 setImportDeclError(Di, ErrOut);
7912 SavedImportPaths[FromD].clear();
7913
Gabor Marton303c98612019-06-25 08:00:51 +00007914 // Do not return ToDOrErr, error was taken out of it.
7915 return make_error<ImportError>(ErrOut);
7916 }
7917
Balazs Keri3b30d652018-10-19 13:32:20 +00007918 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007919
Gabor Marton303c98612019-06-25 08:00:51 +00007920 // FIXME: Handle the "already imported with error" case. We can get here
7921 // nullptr only if GetImportedOrCreateDecl returned nullptr (after a
7922 // previously failed create was requested).
7923 // Later GetImportedOrCreateDecl can be updated to return the error.
Gabor Marton7f8c4002019-03-19 13:34:10 +00007924 if (!ToD) {
Gabor Marton303c98612019-06-25 08:00:51 +00007925 auto Err = getImportDeclErrorIfAny(FromD);
7926 assert(Err);
7927 return make_error<ImportError>(*Err);
Gabor Marton7f8c4002019-03-19 13:34:10 +00007928 }
7929
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007930 // Make sure that ImportImpl registered the imported decl.
7931 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
Gabor Marton26f72a92018-07-12 09:42:05 +00007932 // Notify subclasses.
7933 Imported(FromD, ToD);
7934
Gabor Martonac3a5d62018-09-17 12:04:52 +00007935 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00007936 SavedImportPaths[FromD].clear();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007937 return ToDOrErr;
7938}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007939
Balazs Keri3b30d652018-10-19 13:32:20 +00007940Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007941 if (!FromDC)
7942 return FromDC;
7943
Gabor Marton5ac6d492019-05-15 10:29:48 +00007944 ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC));
Balazs Keria1f6b102019-04-08 13:59:15 +00007945 if (!ToDCOrErr)
7946 return ToDCOrErr.takeError();
7947 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00007948
Fangrui Song6907ce22018-07-30 19:24:48 +00007949 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007950 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007951 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7952 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007953 if (ToRecord->isCompleteDefinition()) {
7954 // Do nothing.
7955 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007956 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7957 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7958 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007959 } else {
7960 CompleteDecl(ToRecord);
7961 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007962 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7963 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007964 if (ToEnum->isCompleteDefinition()) {
7965 // Do nothing.
7966 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007967 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7968 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7969 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007970 } else {
7971 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007972 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007973 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7974 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007975 if (ToClass->getDefinition()) {
7976 // Do nothing.
7977 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007978 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7979 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7980 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007981 } else {
7982 CompleteDecl(ToClass);
7983 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007984 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7985 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007986 if (ToProto->getDefinition()) {
7987 // Do nothing.
7988 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007989 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7990 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7991 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007992 } else {
7993 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007994 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007995 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007996
Douglas Gregor95d82832012-01-24 18:36:04 +00007997 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007998}
7999
Gabor Marton5ac6d492019-05-15 10:29:48 +00008000Expected<Expr *> ASTImporter::Import(Expr *FromE) {
8001 if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE)))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008002 return cast_or_null<Expr>(*ToSOrErr);
8003 else
8004 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00008005}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008006
Gabor Marton5ac6d492019-05-15 10:29:48 +00008007Expected<Stmt *> ASTImporter::Import(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008008 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00008009 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008010
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008011 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008012 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
8013 if (Pos != ImportedStmts.end())
8014 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00008015
Balazs Keri3b30d652018-10-19 13:32:20 +00008016 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008017 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008018 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008019 if (!ToSOrErr)
8020 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008021
Balazs Keri3b30d652018-10-19 13:32:20 +00008022 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00008023 auto *FromE = cast<Expr>(FromS);
8024 // Copy ExprBitfields, which may not be handled in Expr subclasses
8025 // constructors.
8026 ToE->setValueKind(FromE->getValueKind());
8027 ToE->setObjectKind(FromE->getObjectKind());
8028 ToE->setTypeDependent(FromE->isTypeDependent());
8029 ToE->setValueDependent(FromE->isValueDependent());
8030 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
8031 ToE->setContainsUnexpandedParameterPack(
8032 FromE->containsUnexpandedParameterPack());
8033 }
8034
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008035 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00008036 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008037 return ToSOrErr;
8038}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008039
Balazs Keri4a3d7582018-11-27 18:36:31 +00008040Expected<NestedNameSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008041ASTImporter::Import(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008042 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00008043 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008044
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008045 NestedNameSpecifier *Prefix;
8046 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
8047 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00008048
8049 switch (FromNNS->getKind()) {
8050 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008051 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
8052 return NestedNameSpecifier::Create(ToContext, Prefix,
8053 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00008054
8055 case NestedNameSpecifier::Namespace:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008056 if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008057 return NestedNameSpecifier::Create(ToContext, Prefix,
8058 cast<NamespaceDecl>(*NSOrErr));
8059 } else
8060 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008061
8062 case NestedNameSpecifier::NamespaceAlias:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008063 if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008064 return NestedNameSpecifier::Create(ToContext, Prefix,
8065 cast<NamespaceAliasDecl>(*NSADOrErr));
8066 else
8067 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008068
8069 case NestedNameSpecifier::Global:
8070 return NestedNameSpecifier::GlobalSpecifier(ToContext);
8071
Nikola Smiljanic67860242014-09-26 00:28:20 +00008072 case NestedNameSpecifier::Super:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008073 if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008074 return NestedNameSpecifier::SuperSpecifier(ToContext,
8075 cast<CXXRecordDecl>(*RDOrErr));
8076 else
8077 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008078
Douglas Gregor90ebf252011-04-27 16:48:40 +00008079 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008080 case NestedNameSpecifier::TypeSpecWithTemplate:
8081 if (Expected<QualType> TyOrErr =
Gabor Marton5ac6d492019-05-15 10:29:48 +00008082 Import(QualType(FromNNS->getAsType(), 0u))) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008083 bool TSTemplate =
8084 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
8085 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
8086 TyOrErr->getTypePtr());
8087 } else {
8088 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008089 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008090 }
8091
8092 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008093}
8094
Balazs Keri4a3d7582018-11-27 18:36:31 +00008095Expected<NestedNameSpecifierLoc>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008096ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008097 // Copied from NestedNameSpecifier mostly.
8098 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8099 NestedNameSpecifierLoc NNS = FromNNS;
8100
8101 // Push each of the nested-name-specifiers's onto a stack for
8102 // serialization in reverse order.
8103 while (NNS) {
8104 NestedNames.push_back(NNS);
8105 NNS = NNS.getPrefix();
8106 }
8107
8108 NestedNameSpecifierLocBuilder Builder;
8109
8110 while (!NestedNames.empty()) {
8111 NNS = NestedNames.pop_back_val();
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00008112 NestedNameSpecifier *Spec = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008113 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8114 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008115
8116 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008117
8118 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8119 if (Kind != NestedNameSpecifier::Super) {
8120 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8121 return std::move(Err);
8122
8123 if (Kind != NestedNameSpecifier::Global)
8124 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8125 return std::move(Err);
8126 }
8127
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008128 switch (Kind) {
8129 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008130 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8131 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008132 break;
8133
8134 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008135 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8136 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008137 break;
8138
8139 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008140 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8141 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008142 break;
8143
8144 case NestedNameSpecifier::TypeSpec:
8145 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008146 SourceLocation ToTLoc;
8147 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8148 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008149 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008150 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008151 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8152 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008153 break;
8154 }
8155
8156 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008157 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008158 break;
8159
8160 case NestedNameSpecifier::Super: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008161 auto ToSourceRangeOrErr = Import(NNS.getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008162 if (!ToSourceRangeOrErr)
8163 return ToSourceRangeOrErr.takeError();
8164
8165 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8166 ToSourceRangeOrErr->getBegin(),
8167 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008168 }
8169 }
8170 }
8171
8172 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008173}
8174
Gabor Marton5ac6d492019-05-15 10:29:48 +00008175Expected<TemplateName> ASTImporter::Import(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008176 switch (From.getKind()) {
8177 case TemplateName::Template:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008178 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008179 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8180 else
8181 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008182
Douglas Gregore2e50d332010-12-01 01:36:18 +00008183 case TemplateName::OverloadedTemplate: {
8184 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8185 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008186 for (auto *I : *FromStorage) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008187 if (auto ToOrErr = Import(I))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008188 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008189 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008190 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008191 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008192 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008193 ToTemplates.end());
8194 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008195
Richard Smithb23c5e82019-05-09 03:31:27 +00008196 case TemplateName::AssumedTemplate: {
8197 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008198 auto DeclNameOrErr = Import(FromStorage->getDeclName());
Richard Smithb23c5e82019-05-09 03:31:27 +00008199 if (!DeclNameOrErr)
8200 return DeclNameOrErr.takeError();
8201 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8202 }
8203
Douglas Gregore2e50d332010-12-01 01:36:18 +00008204 case TemplateName::QualifiedTemplate: {
8205 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008206 auto QualifierOrErr = Import(QTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008207 if (!QualifierOrErr)
8208 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008209
Gabor Marton5ac6d492019-05-15 10:29:48 +00008210 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008211 return ToContext.getQualifiedTemplateName(
8212 *QualifierOrErr, QTN->hasTemplateKeyword(),
8213 cast<TemplateDecl>(*ToTemplateOrErr));
8214 else
8215 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008216 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008217
Douglas Gregore2e50d332010-12-01 01:36:18 +00008218 case TemplateName::DependentTemplate: {
8219 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008220 auto QualifierOrErr = Import(DTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008221 if (!QualifierOrErr)
8222 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008223
Douglas Gregore2e50d332010-12-01 01:36:18 +00008224 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008225 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008226 Import(DTN->getIdentifier()));
8227 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008228
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008229 return ToContext.getDependentTemplateName(*QualifierOrErr,
8230 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008231 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008232
8233 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008234 SubstTemplateTemplateParmStorage *Subst =
8235 From.getAsSubstTemplateTemplateParm();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008236 ExpectedDecl ParamOrErr = Import(Subst->getParameter());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008237 if (!ParamOrErr)
8238 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008239
Gabor Marton5ac6d492019-05-15 10:29:48 +00008240 auto ReplacementOrErr = Import(Subst->getReplacement());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008241 if (!ReplacementOrErr)
8242 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008243
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008244 return ToContext.getSubstTemplateTemplateParm(
8245 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008246 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008247
Douglas Gregor5590be02011-01-15 06:45:20 +00008248 case TemplateName::SubstTemplateTemplateParmPack: {
8249 SubstTemplateTemplateParmPackStorage *SubstPack
8250 = From.getAsSubstTemplateTemplateParmPack();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008251 ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008252 if (!ParamOrErr)
8253 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008254
Douglas Gregor5590be02011-01-15 06:45:20 +00008255 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008256 auto ArgPackOrErr =
8257 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8258 if (!ArgPackOrErr)
8259 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008260
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008261 return ToContext.getSubstTemplateTemplateParmPack(
8262 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008263 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008264 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008265
Douglas Gregore2e50d332010-12-01 01:36:18 +00008266 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008267}
8268
Gabor Marton5ac6d492019-05-15 10:29:48 +00008269Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008270 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008271 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008272
Douglas Gregor811663e2010-02-10 00:15:17 +00008273 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008274 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008275
Douglas Gregor811663e2010-02-10 00:15:17 +00008276 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008277 Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008278 if (!ToFileIDOrErr)
8279 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008280 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008281 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8282}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008283
Gabor Marton5ac6d492019-05-15 10:29:48 +00008284Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008285 SourceLocation ToBegin, ToEnd;
8286 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8287 return std::move(Err);
8288 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8289 return std::move(Err);
8290
8291 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008292}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008293
Gabor Marton5ac6d492019-05-15 10:29:48 +00008294Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008295 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008296 if (Pos != ImportedFileIDs.end())
8297 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008298
Douglas Gregor811663e2010-02-10 00:15:17 +00008299 SourceManager &FromSM = FromContext.getSourceManager();
8300 SourceManager &ToSM = ToContext.getSourceManager();
8301 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008302
8303 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008304 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008305 if (FromSLoc.isExpansion()) {
8306 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008307 ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008308 if (!ToSpLoc)
8309 return ToSpLoc.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008310 ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008311 if (!ToExLocS)
8312 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008313 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8314 SourceLocation MLoc;
8315 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008316 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008317 } else {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008318 if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008319 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8320 FromEx.isExpansionTokenRange());
8321 else
8322 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008323 }
8324 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008325 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008326 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008327
8328 if (!IsBuiltin) {
8329 // Include location of this file.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008330 ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008331 if (!ToIncludeLoc)
8332 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008333
8334 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8335 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8336 // disk again
8337 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8338 // than mmap the files several times.
8339 const FileEntry *Entry =
8340 ToFileManager.getFile(Cache->OrigEntry->getName());
8341 // FIXME: The filename may be a virtual name that does probably not
8342 // point to a valid file and we get no Entry here. In this case try with
8343 // the memory buffer below.
8344 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008345 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008346 FromSLoc.getFile().getFileCharacteristic());
8347 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008348 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008349
8350 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008351 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008352 bool Invalid = true;
8353 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8354 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8355 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008356 // FIXME: Use a new error kind?
8357 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008358
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008359 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8360 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8361 FromBuf->getBufferIdentifier());
8362 ToID = ToSM.createFileID(std::move(ToBuf),
8363 FromSLoc.getFile().getFileCharacteristic());
8364 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008365 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008366
Balazs Keri9cf39df2019-02-27 16:31:48 +00008367 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8368
Sebastian Redl99219f12010-09-30 01:03:06 +00008369 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008370 return ToID;
8371}
8372
Gabor Marton5ac6d492019-05-15 10:29:48 +00008373Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) {
8374 ExpectedExpr ToExprOrErr = Import(From->getInit());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008375 if (!ToExprOrErr)
8376 return ToExprOrErr.takeError();
8377
Gabor Marton5ac6d492019-05-15 10:29:48 +00008378 auto LParenLocOrErr = Import(From->getLParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008379 if (!LParenLocOrErr)
8380 return LParenLocOrErr.takeError();
8381
Gabor Marton5ac6d492019-05-15 10:29:48 +00008382 auto RParenLocOrErr = Import(From->getRParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008383 if (!RParenLocOrErr)
8384 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008385
8386 if (From->isBaseInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008387 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008388 if (!ToTInfoOrErr)
8389 return ToTInfoOrErr.takeError();
8390
8391 SourceLocation EllipsisLoc;
8392 if (From->isPackExpansion())
8393 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8394 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008395
8396 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008397 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8398 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008399 } else if (From->isMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008400 ExpectedDecl ToFieldOrErr = Import(From->getMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008401 if (!ToFieldOrErr)
8402 return ToFieldOrErr.takeError();
8403
Gabor Marton5ac6d492019-05-15 10:29:48 +00008404 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008405 if (!MemberLocOrErr)
8406 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008407
8408 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008409 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8410 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008411 } else if (From->isIndirectMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008412 ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008413 if (!ToIFieldOrErr)
8414 return ToIFieldOrErr.takeError();
8415
Gabor Marton5ac6d492019-05-15 10:29:48 +00008416 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008417 if (!MemberLocOrErr)
8418 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008419
8420 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008421 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8422 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008423 } else if (From->isDelegatingInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008424 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008425 if (!ToTInfoOrErr)
8426 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008427
8428 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008429 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8430 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008431 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008432 // FIXME: assert?
8433 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008434 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008435}
Sean Callanandd2c1742016-05-16 20:48:03 +00008436
Balazs Keri4a3d7582018-11-27 18:36:31 +00008437Expected<CXXBaseSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008438ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008439 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8440 if (Pos != ImportedCXXBaseSpecifiers.end())
8441 return Pos->second;
8442
Gabor Marton5ac6d492019-05-15 10:29:48 +00008443 Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008444 if (!ToSourceRange)
8445 return ToSourceRange.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008446 Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008447 if (!ToTSI)
8448 return ToTSI.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008449 ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008450 if (!ToEllipsisLoc)
8451 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008452 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008453 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8454 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008455 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8456 return Imported;
8457}
8458
Gabor Marton5ac6d492019-05-15 10:29:48 +00008459Error ASTImporter::ImportDefinition(Decl *From) {
8460 ExpectedDecl ToOrErr = Import(From);
8461 if (!ToOrErr)
8462 return ToOrErr.takeError();
8463 Decl *To = *ToOrErr;
Fangrui Song6907ce22018-07-30 19:24:48 +00008464
Don Hintonf170dff2019-03-19 06:14:14 +00008465 auto *FromDC = cast<DeclContext>(From);
8466 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008467
Don Hintonf170dff2019-03-19 06:14:14 +00008468 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8469 if (!ToRecord->getDefinition()) {
8470 return Importer.ImportDefinition(
8471 cast<RecordDecl>(FromDC), ToRecord,
8472 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008473 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008474 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008475
Don Hintonf170dff2019-03-19 06:14:14 +00008476 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8477 if (!ToEnum->getDefinition()) {
8478 return Importer.ImportDefinition(
8479 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8480 }
8481 }
8482
8483 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8484 if (!ToIFace->getDefinition()) {
8485 return Importer.ImportDefinition(
8486 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8487 ASTNodeImporter::IDK_Everything);
8488 }
8489 }
8490
8491 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8492 if (!ToProto->getDefinition()) {
8493 return Importer.ImportDefinition(
8494 cast<ObjCProtocolDecl>(FromDC), ToProto,
8495 ASTNodeImporter::IDK_Everything);
8496 }
8497 }
8498
8499 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008500}
8501
Gabor Marton5ac6d492019-05-15 10:29:48 +00008502Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008503 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008504 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008505
8506 switch (FromName.getNameKind()) {
8507 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008508 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008509
8510 case DeclarationName::ObjCZeroArgSelector:
8511 case DeclarationName::ObjCOneArgSelector:
8512 case DeclarationName::ObjCMultiArgSelector:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008513 if (auto ToSelOrErr = Import(FromName.getObjCSelector()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008514 return DeclarationName(*ToSelOrErr);
8515 else
8516 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008517
8518 case DeclarationName::CXXConstructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008519 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008520 return ToContext.DeclarationNames.getCXXConstructorName(
8521 ToContext.getCanonicalType(*ToTyOrErr));
8522 else
8523 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008524 }
8525
8526 case DeclarationName::CXXDestructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008527 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008528 return ToContext.DeclarationNames.getCXXDestructorName(
8529 ToContext.getCanonicalType(*ToTyOrErr));
8530 else
8531 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008532 }
8533
Richard Smith35845152017-02-07 01:37:30 +00008534 case DeclarationName::CXXDeductionGuideName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008535 if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008536 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8537 cast<TemplateDecl>(*ToTemplateOrErr));
8538 else
8539 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008540 }
8541
Douglas Gregor96e578d2010-02-05 17:54:41 +00008542 case DeclarationName::CXXConversionFunctionName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008543 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008544 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8545 ToContext.getCanonicalType(*ToTyOrErr));
8546 else
8547 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008548 }
8549
8550 case DeclarationName::CXXOperatorName:
8551 return ToContext.DeclarationNames.getCXXOperatorName(
8552 FromName.getCXXOverloadedOperator());
8553
8554 case DeclarationName::CXXLiteralOperatorName:
8555 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008556 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008557
8558 case DeclarationName::CXXUsingDirective:
8559 // FIXME: STATICS!
8560 return DeclarationName::getUsingDirectiveName();
8561 }
8562
David Blaikiee4d798f2012-01-20 21:50:17 +00008563 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008564}
8565
Douglas Gregore2e50d332010-12-01 01:36:18 +00008566IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008567 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008568 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008569
Sean Callananf94ef1d2016-05-14 06:11:19 +00008570 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8571
8572 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8573 ToId->setBuiltinID(FromId->getBuiltinID());
8574
8575 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008576}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008577
Gabor Marton5ac6d492019-05-15 10:29:48 +00008578Expected<Selector> ASTImporter::Import(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008579 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008580 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008581
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008582 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008583 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8584 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8585 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8586 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8587}
8588
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008589DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8590 DeclContext *DC,
8591 unsigned IDNS,
8592 NamedDecl **Decls,
8593 unsigned NumDecls) {
8594 return Name;
8595}
8596
8597DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008598 if (LastDiagFromFrom)
8599 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8600 FromContext.getDiagnostics());
8601 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008602 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008603}
8604
8605DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008606 if (!LastDiagFromFrom)
8607 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8608 ToContext.getDiagnostics());
8609 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008610 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008611}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008612
Douglas Gregor2e15c842012-02-01 21:00:38 +00008613void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008614 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008615 if (!ID->getDefinition())
8616 ID->startDefinition();
8617 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008618 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008619 if (!PD->getDefinition())
8620 PD->startDefinition();
8621 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008622 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008623 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8624 TD->startDefinition();
8625 TD->setCompleteDefinition(true);
8626 }
8627 }
8628 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008629 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008630 }
8631}
8632
Gabor Marton26f72a92018-07-12 09:42:05 +00008633Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8634 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8635 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8636 "Try to import an already imported Decl");
8637 if (Pos != ImportedDecls.end())
8638 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008639 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008640 // This mapping should be maintained only in this function. Therefore do not
8641 // check for additional consistency.
8642 ImportedFromDecls[To] = From;
Gabor Marton303c98612019-06-25 08:00:51 +00008643 AddToLookupTable(To);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008644 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008645}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008646
Gabor Marton303c98612019-06-25 08:00:51 +00008647llvm::Optional<ImportError>
8648ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const {
8649 auto Pos = ImportDeclErrors.find(FromD);
8650 if (Pos != ImportDeclErrors.end())
8651 return Pos->second;
8652 else
8653 return Optional<ImportError>();
8654}
8655
8656void ASTImporter::setImportDeclError(Decl *From, ImportError Error) {
Gabor Marton1ad4b992019-07-01 14:19:53 +00008657 auto InsertRes = ImportDeclErrors.insert({From, Error});
Benjamin Kramer4f769362019-07-01 14:33:26 +00008658 (void)InsertRes;
Gabor Marton1ad4b992019-07-01 14:19:53 +00008659 // Either we set the error for the first time, or we already had set one and
8660 // now we want to set the same error.
8661 assert(InsertRes.second || InsertRes.first->second.Error == Error.Error);
Gabor Marton303c98612019-06-25 08:00:51 +00008662}
8663
Douglas Gregordd6006f2012-07-17 21:16:27 +00008664bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8665 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008666 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8667 ImportedTypes.find(From.getTypePtr());
8668 if (Pos != ImportedTypes.end()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008669 if (ExpectedType ToFromOrErr = Import(From)) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008670 if (ToContext.hasSameType(*ToFromOrErr, To))
8671 return true;
8672 } else {
8673 llvm::consumeError(ToFromOrErr.takeError());
8674 }
8675 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008676
Douglas Gregordd6006f2012-07-17 21:16:27 +00008677 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008678 getStructuralEquivalenceKind(*this), false,
8679 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008680 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008681}