blob: 8c80ecd1e67f0f86cd47449ab19c2842758a4262 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000014
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Balazs Keri3b30d652018-10-19 13:32:20 +000074 using llvm::make_error;
75 using llvm::Error;
76 using llvm::Expected;
77 using ExpectedType = llvm::Expected<QualType>;
78 using ExpectedStmt = llvm::Expected<Stmt *>;
79 using ExpectedExpr = llvm::Expected<Expr *>;
80 using ExpectedDecl = llvm::Expected<Decl *>;
81 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000082
Balazs Keri3b30d652018-10-19 13:32:20 +000083 std::string ImportError::toString() const {
84 // FIXME: Improve error texts.
85 switch (Error) {
86 case NameConflict:
87 return "NameConflict";
88 case UnsupportedConstruct:
89 return "UnsupportedConstruct";
90 case Unknown:
91 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000092 }
Balazs Keri2a13d662018-10-19 15:16:51 +000093 llvm_unreachable("Invalid error code.");
94 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
125 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000126 }
127
Gabor Marton26f72a92018-07-12 09:42:05 +0000128 void updateFlags(const Decl *From, Decl *To) {
129 // Check if some flags or attrs are new in 'From' and copy into 'To'.
130 // FIXME: Other flags or attrs?
131 if (From->isUsed(false) && !To->isUsed(false))
132 To->setIsUsed();
133 }
134
Balazs Keri3b30d652018-10-19 13:32:20 +0000135 Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
136 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
137 "Try to get field index for non-field.");
138
139 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
140 if (!Owner)
141 return None;
142
143 unsigned Index = 0;
144 for (const auto *D : Owner->decls()) {
145 if (D == F)
146 return Index;
147
148 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
149 ++Index;
150 }
151
152 llvm_unreachable("Field was not found in its parent context.");
153
154 return None;
155 }
156
157 // FIXME: Temporary until every import returns Expected.
158 template <>
159 LLVM_NODISCARD Error
160 ASTImporter::importInto(SourceLocation &To, const SourceLocation &From) {
161 To = Import(From);
162 if (From.isValid() && To.isInvalid())
163 return llvm::make_error<ImportError>();
164 return Error::success();
165 }
166 // FIXME: Temporary until every import returns Expected.
167 template <>
168 LLVM_NODISCARD Error
169 ASTImporter::importInto(QualType &To, const QualType &From) {
170 To = Import(From);
171 if (!From.isNull() && To.isNull())
172 return llvm::make_error<ImportError>();
173 return Error::success();
174 }
175
176 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
177 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
178 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000180
Balazs Keri3b30d652018-10-19 13:32:20 +0000181 // Use this instead of Importer.importInto .
182 template <typename ImportT>
183 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
184 return Importer.importInto(To, From);
185 }
186
187 // Use this to import pointers of specific type.
188 template <typename ImportT>
189 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
190 auto ToI = Importer.Import(From);
191 if (!ToI && From)
192 return make_error<ImportError>();
193 To = cast_or_null<ImportT>(ToI);
194 return Error::success();
195 // FIXME: This should be the final code.
196 //auto ToOrErr = Importer.Import(From);
197 //if (ToOrErr) {
198 // To = cast_or_null<ImportT>(*ToOrErr);
199 //}
200 //return ToOrErr.takeError();
201 }
202
203 // Call the import function of ASTImporter for a baseclass of type `T` and
204 // cast the return value to `T`.
205 template <typename T>
206 Expected<T *> import(T *From) {
207 auto *To = Importer.Import(From);
208 if (!To && From)
209 return make_error<ImportError>();
210 return cast_or_null<T>(To);
211 // FIXME: This should be the final code.
212 //auto ToOrErr = Importer.Import(From);
213 //if (!ToOrErr)
214 // return ToOrErr.takeError();
215 //return cast_or_null<T>(*ToOrErr);
216 }
217
218 template <typename T>
219 Expected<T *> import(const T *From) {
220 return import(const_cast<T *>(From));
221 }
222
223 // Call the import function of ASTImporter for type `T`.
224 template <typename T>
225 Expected<T> import(const T &From) {
226 T To = Importer.Import(From);
227 T DefaultT;
228 if (To == DefaultT && !(From == DefaultT))
229 return make_error<ImportError>();
230 return To;
231 // FIXME: This should be the final code.
232 //return Importer.Import(From);
233 }
234
235 template <class T>
236 Expected<std::tuple<T>>
237 importSeq(const T &From) {
238 Expected<T> ToOrErr = import(From);
239 if (!ToOrErr)
240 return ToOrErr.takeError();
241 return std::make_tuple<T>(std::move(*ToOrErr));
242 }
243
244 // Import multiple objects with a single function call.
245 // This should work for every type for which a variant of `import` exists.
246 // The arguments are processed from left to right and import is stopped on
247 // first error.
248 template <class THead, class... TTail>
249 Expected<std::tuple<THead, TTail...>>
250 importSeq(const THead &FromHead, const TTail &...FromTail) {
251 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
252 if (!ToHeadOrErr)
253 return ToHeadOrErr.takeError();
254 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
255 if (!ToTailOrErr)
256 return ToTailOrErr.takeError();
257 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
258 }
259
260// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 template <typename ToDeclT> struct CallOverloadedCreateFun {
262 template <typename... Args>
263 auto operator()(Args &&... args)
264 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
265 return ToDeclT::Create(std::forward<Args>(args)...);
266 }
267 };
268
269 // Always use these functions to create a Decl during import. There are
270 // certain tasks which must be done after the Decl was created, e.g. we
271 // must immediately register that as an imported Decl. The parameter `ToD`
272 // will be set to the newly created Decl or if had been imported before
273 // then to the already imported Decl. Returns a bool value set to true if
274 // the `FromD` had been imported before.
275 template <typename ToDeclT, typename FromDeclT, typename... Args>
276 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
277 Args &&... args) {
278 // There may be several overloads of ToDeclT::Create. We must make sure
279 // to call the one which would be chosen by the arguments, thus we use a
280 // wrapper for the overload set.
281 CallOverloadedCreateFun<ToDeclT> OC;
282 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
283 std::forward<Args>(args)...);
284 }
285 // Use this overload if a special Type is needed to be created. E.g if we
286 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
287 // then:
288 // TypedefNameDecl *ToTypedef;
289 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
290 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
291 typename... Args>
292 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
293 Args &&... args) {
294 CallOverloadedCreateFun<NewDeclT> OC;
295 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
296 std::forward<Args>(args)...);
297 }
298 // Use this version if a special create function must be
299 // used, e.g. CXXRecordDecl::CreateLambda .
300 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
301 typename... Args>
302 LLVM_NODISCARD bool
303 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
304 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000305 // FIXME: This code is needed later.
306 //if (Importer.getImportDeclErrorIfAny(FromD)) {
307 // ToD = nullptr;
308 // return true; // Already imported but with error.
309 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000310 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
311 if (ToD)
312 return true; // Already imported.
313 ToD = CreateFun(std::forward<Args>(args)...);
314 InitializeImportedDecl(FromD, ToD);
315 return false; // A new Decl is created.
316 }
317
318 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
319 Importer.MapImported(FromD, ToD);
320 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
321 if (FromD->hasAttrs())
322 for (const Attr *FromAttr : FromD->getAttrs())
323 ToD->addAttr(Importer.Import(FromAttr));
324 if (FromD->isUsed())
325 ToD->setIsUsed();
326 if (FromD->isImplicit())
327 ToD->setImplicit();
328 }
329
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000331 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000332
Balazs Keri3b30d652018-10-19 13:32:20 +0000333 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
334 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
335 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336
337 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000338 ExpectedType VisitType(const Type *T);
339 ExpectedType VisitAtomicType(const AtomicType *T);
340 ExpectedType VisitBuiltinType(const BuiltinType *T);
341 ExpectedType VisitDecayedType(const DecayedType *T);
342 ExpectedType VisitComplexType(const ComplexType *T);
343 ExpectedType VisitPointerType(const PointerType *T);
344 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
345 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
346 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
347 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
348 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
349 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
350 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
351 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000352 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000353 ExpectedType VisitVectorType(const VectorType *T);
354 ExpectedType VisitExtVectorType(const ExtVectorType *T);
355 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
356 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
357 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
358 ExpectedType VisitParenType(const ParenType *T);
359 ExpectedType VisitTypedefType(const TypedefType *T);
360 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000361 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000362 ExpectedType VisitTypeOfType(const TypeOfType *T);
363 ExpectedType VisitDecltypeType(const DecltypeType *T);
364 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
365 ExpectedType VisitAutoType(const AutoType *T);
366 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000368 ExpectedType VisitRecordType(const RecordType *T);
369 ExpectedType VisitEnumType(const EnumType *T);
370 ExpectedType VisitAttributedType(const AttributedType *T);
371 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
372 ExpectedType VisitSubstTemplateTypeParmType(
373 const SubstTemplateTypeParmType *T);
374 ExpectedType VisitTemplateSpecializationType(
375 const TemplateSpecializationType *T);
376 ExpectedType VisitElaboratedType(const ElaboratedType *T);
377 ExpectedType VisitDependentNameType(const DependentNameType *T);
378 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
379 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000380 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
382 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
383 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000384
385 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000386 Error ImportDeclParts(
387 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
388 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
389 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
390 Error ImportDeclarationNameLoc(
391 const DeclarationNameInfo &From, DeclarationNameInfo &To);
392 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
393 Error ImportDeclContext(
394 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
395 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000396
Balazs Keri3b30d652018-10-19 13:32:20 +0000397 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000398
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000399 using Designator = DesignatedInitExpr::Designator;
400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000401 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000402 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000403 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000404 /// nothing (if minimal import is set) or might be everything (if minimal
405 /// import is not set).
406 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000407 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000408 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000410 /// DeclContext.
411 IDK_Basic
412 };
413
Douglas Gregor2e15c842012-02-01 21:00:38 +0000414 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
415 return IDK == IDK_Everything ||
416 (IDK == IDK_Default && !Importer.isMinimalImport());
417 }
418
Balazs Keri3b30d652018-10-19 13:32:20 +0000419 Error ImportInitializer(VarDecl *From, VarDecl *To);
420 Error ImportDefinition(
421 RecordDecl *From, RecordDecl *To,
422 ImportDefinitionKind Kind = IDK_Default);
423 Error ImportDefinition(
424 EnumDecl *From, EnumDecl *To,
425 ImportDefinitionKind Kind = IDK_Default);
426 Error ImportDefinition(
427 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
428 ImportDefinitionKind Kind = IDK_Default);
429 Error ImportDefinition(
430 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
431 ImportDefinitionKind Kind = IDK_Default);
432 Expected<TemplateParameterList *> ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000433 TemplateParameterList *Params);
Balazs Keri3b30d652018-10-19 13:32:20 +0000434 Error ImportTemplateArguments(
435 const TemplateArgument *FromArgs, unsigned NumFromArgs,
436 SmallVectorImpl<TemplateArgument> &ToArgs);
437 Expected<TemplateArgument>
438 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000439
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000440 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000441 Error ImportTemplateArgumentListInfo(
442 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000443
444 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000445 Error ImportTemplateArgumentListInfo(
446 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
447 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000448
Gabor Marton5254e642018-06-27 13:32:50 +0000449 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000450 using FunctionTemplateAndArgsTy =
451 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
452 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000453 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
454 FunctionDecl *FromFD);
455
Balazs Keri3b30d652018-10-19 13:32:20 +0000456 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000457
Gabor Marton950fb572018-07-17 12:39:27 +0000458 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000459 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
460 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000461 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
462 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000463 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000464 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000465 bool IsStructuralMatch(FunctionTemplateDecl *From,
466 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000467 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000468 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000469 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000470 ExpectedDecl VisitDecl(Decl *D);
471 ExpectedDecl VisitImportDecl(ImportDecl *D);
472 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
473 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
474 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
475 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
476 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
477 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
479 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
480 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
481 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
482 ExpectedDecl VisitLabelDecl(LabelDecl *D);
483 ExpectedDecl VisitEnumDecl(EnumDecl *D);
484 ExpectedDecl VisitRecordDecl(RecordDecl *D);
485 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
486 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
487 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
488 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
489 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
490 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
491 ExpectedDecl VisitFieldDecl(FieldDecl *D);
492 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
493 ExpectedDecl VisitFriendDecl(FriendDecl *D);
494 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
495 ExpectedDecl VisitVarDecl(VarDecl *D);
496 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
497 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
498 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
499 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
500 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
501 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
502 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
503 ExpectedDecl VisitUsingDecl(UsingDecl *D);
504 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
505 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
506 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
507 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000508
Balazs Keri3b30d652018-10-19 13:32:20 +0000509 Expected<ObjCTypeParamList *>
510 ImportObjCTypeParamList(ObjCTypeParamList *list);
511
512 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
513 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
514 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
515 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
516 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
517 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
518 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
519 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
520 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
521 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000522 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000523 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
524 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
525 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000526
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000527 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000528 ExpectedStmt VisitStmt(Stmt *S);
529 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
530 ExpectedStmt VisitDeclStmt(DeclStmt *S);
531 ExpectedStmt VisitNullStmt(NullStmt *S);
532 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
533 ExpectedStmt VisitCaseStmt(CaseStmt *S);
534 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
535 ExpectedStmt VisitLabelStmt(LabelStmt *S);
536 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
537 ExpectedStmt VisitIfStmt(IfStmt *S);
538 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
539 ExpectedStmt VisitWhileStmt(WhileStmt *S);
540 ExpectedStmt VisitDoStmt(DoStmt *S);
541 ExpectedStmt VisitForStmt(ForStmt *S);
542 ExpectedStmt VisitGotoStmt(GotoStmt *S);
543 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
544 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
545 ExpectedStmt VisitBreakStmt(BreakStmt *S);
546 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000547 // FIXME: MSAsmStmt
548 // FIXME: SEHExceptStmt
549 // FIXME: SEHFinallyStmt
550 // FIXME: SEHTryStmt
551 // FIXME: SEHLeaveStmt
552 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000553 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
554 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
555 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000556 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000557 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
558 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
559 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
560 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
561 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
562 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
563 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000564
565 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000566 ExpectedStmt VisitExpr(Expr *E);
567 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
568 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
569 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
570 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
571 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
572 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
573 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
574 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
575 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
576 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
577 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
578 ExpectedStmt VisitStringLiteral(StringLiteral *E);
579 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
580 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
581 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000582 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000583 ExpectedStmt VisitParenExpr(ParenExpr *E);
584 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
585 ExpectedStmt VisitStmtExpr(StmtExpr *E);
586 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
587 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
588 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
589 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
590 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
591 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
592 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
593 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
594 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
595 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
596 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
597 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
598 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
599 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
600 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
601 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
602 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
603 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
604 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
605 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
606 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
607 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
608 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
609 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
610 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
611 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
612 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
613 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
614 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
615 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
616 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
617 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
618 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
619 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
620 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
621 ExpectedStmt VisitMemberExpr(MemberExpr *E);
622 ExpectedStmt VisitCallExpr(CallExpr *E);
623 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
624 ExpectedStmt VisitInitListExpr(InitListExpr *E);
625 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
626 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
627 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
628 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
629 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
630 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
631 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
632 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
633 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000634
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000635 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000636 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000637 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000638 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
639 Expected<ItemT> ToOrErr = import(*Ibegin);
640 if (!ToOrErr)
641 return ToOrErr.takeError();
642 *Obegin = *ToOrErr;
643 }
644 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000645 }
646
Balazs Keri3b30d652018-10-19 13:32:20 +0000647 // Import every item from a container structure into an output container.
648 // If error occurs, stops at first error and returns the error.
649 // The output container should have space for all needed elements (it is not
650 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000651 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000652 Error ImportContainerChecked(
653 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
654 return ImportArrayChecked(
655 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000656 }
657
658 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000659 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000660 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
661 }
Lang Hames19e07e12017-06-20 21:06:00 +0000662
Lang Hames19e07e12017-06-20 21:06:00 +0000663 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000664
Balazs Keri3b30d652018-10-19 13:32:20 +0000665 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
666 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000668
Balazs Keri3b30d652018-10-19 13:32:20 +0000669// FIXME: Temporary until every import returns Expected.
670template <>
671Expected<TemplateName> ASTNodeImporter::import(const TemplateName &From) {
672 TemplateName To = Importer.Import(From);
673 if (To.isNull() && !From.isNull())
674 return make_error<ImportError>();
675 return To;
676}
677
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000678template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000679Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000680 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
681 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000682 auto ToLAngleLocOrErr = import(FromLAngleLoc);
683 if (!ToLAngleLocOrErr)
684 return ToLAngleLocOrErr.takeError();
685 auto ToRAngleLocOrErr = import(FromRAngleLoc);
686 if (!ToRAngleLocOrErr)
687 return ToRAngleLocOrErr.takeError();
688
689 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
690 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
691 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000692 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000693 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000694}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000695
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000696template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000697Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000698 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
699 return ImportTemplateArgumentListInfo(
700 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
701}
702
703template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000704Error ASTNodeImporter::ImportTemplateArgumentListInfo<
705 ASTTemplateArgumentListInfo>(
706 const ASTTemplateArgumentListInfo &From,
707 TemplateArgumentListInfo &Result) {
708 return ImportTemplateArgumentListInfo(
709 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000710}
711
Balazs Keri3b30d652018-10-19 13:32:20 +0000712Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000713ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
714 FunctionDecl *FromFD) {
715 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000716 FunctionDecl::TK_FunctionTemplateSpecialization);
717
718 FunctionTemplateAndArgsTy Result;
719
Gabor Marton5254e642018-06-27 13:32:50 +0000720 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000721 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
722 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000723
724 // Import template arguments.
725 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000726 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
727 std::get<1>(Result)))
728 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000729
Balazs Keri3b30d652018-10-19 13:32:20 +0000730 return Result;
731}
732
733template <>
734Expected<TemplateParameterList *>
735ASTNodeImporter::import(TemplateParameterList *From) {
736 SmallVector<NamedDecl *, 4> To(From->size());
737 if (Error Err = ImportContainerChecked(*From, To))
738 return std::move(Err);
739
740 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
741 if (!ToRequiresClause)
742 return ToRequiresClause.takeError();
743
744 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
745 if (!ToTemplateLocOrErr)
746 return ToTemplateLocOrErr.takeError();
747 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
748 if (!ToLAngleLocOrErr)
749 return ToLAngleLocOrErr.takeError();
750 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
751 if (!ToRAngleLocOrErr)
752 return ToRAngleLocOrErr.takeError();
753
754 return TemplateParameterList::Create(
755 Importer.getToContext(),
756 *ToTemplateLocOrErr,
757 *ToLAngleLocOrErr,
758 To,
759 *ToRAngleLocOrErr,
760 *ToRequiresClause);
761}
762
763template <>
764Expected<TemplateArgument>
765ASTNodeImporter::import(const TemplateArgument &From) {
766 switch (From.getKind()) {
767 case TemplateArgument::Null:
768 return TemplateArgument();
769
770 case TemplateArgument::Type: {
771 ExpectedType ToTypeOrErr = import(From.getAsType());
772 if (!ToTypeOrErr)
773 return ToTypeOrErr.takeError();
774 return TemplateArgument(*ToTypeOrErr);
775 }
776
777 case TemplateArgument::Integral: {
778 ExpectedType ToTypeOrErr = import(From.getIntegralType());
779 if (!ToTypeOrErr)
780 return ToTypeOrErr.takeError();
781 return TemplateArgument(From, *ToTypeOrErr);
782 }
783
784 case TemplateArgument::Declaration: {
785 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
786 if (!ToOrErr)
787 return ToOrErr.takeError();
788 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
789 if (!ToTypeOrErr)
790 return ToTypeOrErr.takeError();
791 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
792 }
793
794 case TemplateArgument::NullPtr: {
795 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
796 if (!ToTypeOrErr)
797 return ToTypeOrErr.takeError();
798 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
799 }
800
801 case TemplateArgument::Template: {
802 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
803 if (!ToTemplateOrErr)
804 return ToTemplateOrErr.takeError();
805
806 return TemplateArgument(*ToTemplateOrErr);
807 }
808
809 case TemplateArgument::TemplateExpansion: {
810 Expected<TemplateName> ToTemplateOrErr =
811 import(From.getAsTemplateOrTemplatePattern());
812 if (!ToTemplateOrErr)
813 return ToTemplateOrErr.takeError();
814
815 return TemplateArgument(
816 *ToTemplateOrErr, From.getNumTemplateExpansions());
817 }
818
819 case TemplateArgument::Expression:
820 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
821 return TemplateArgument(*ToExpr);
822 else
823 return ToExpr.takeError();
824
825 case TemplateArgument::Pack: {
826 SmallVector<TemplateArgument, 2> ToPack;
827 ToPack.reserve(From.pack_size());
828 if (Error Err = ImportTemplateArguments(
829 From.pack_begin(), From.pack_size(), ToPack))
830 return std::move(Err);
831
832 return TemplateArgument(
833 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
834 }
835 }
836
837 llvm_unreachable("Invalid template argument kind");
838}
839
840template <>
841Expected<TemplateArgumentLoc>
842ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
843 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
844 if (!ArgOrErr)
845 return ArgOrErr.takeError();
846 TemplateArgument Arg = *ArgOrErr;
847
848 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
849
850 TemplateArgumentLocInfo ToInfo;
851 if (Arg.getKind() == TemplateArgument::Expression) {
852 ExpectedExpr E = import(FromInfo.getAsExpr());
853 if (!E)
854 return E.takeError();
855 ToInfo = TemplateArgumentLocInfo(*E);
856 } else if (Arg.getKind() == TemplateArgument::Type) {
857 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
858 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
859 else
860 return TSIOrErr.takeError();
861 } else {
862 auto ToTemplateQualifierLocOrErr =
863 import(FromInfo.getTemplateQualifierLoc());
864 if (!ToTemplateQualifierLocOrErr)
865 return ToTemplateQualifierLocOrErr.takeError();
866 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
867 if (!ToTemplateNameLocOrErr)
868 return ToTemplateNameLocOrErr.takeError();
869 auto ToTemplateEllipsisLocOrErr =
870 import(FromInfo.getTemplateEllipsisLoc());
871 if (!ToTemplateEllipsisLocOrErr)
872 return ToTemplateEllipsisLocOrErr.takeError();
873
874 ToInfo = TemplateArgumentLocInfo(
875 *ToTemplateQualifierLocOrErr,
876 *ToTemplateNameLocOrErr,
877 *ToTemplateEllipsisLocOrErr);
878 }
879
880 return TemplateArgumentLoc(Arg, ToInfo);
881}
882
883template <>
884Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
885 if (DG.isNull())
886 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
887 size_t NumDecls = DG.end() - DG.begin();
888 SmallVector<Decl *, 1> ToDecls;
889 ToDecls.reserve(NumDecls);
890 for (Decl *FromD : DG) {
891 if (auto ToDOrErr = import(FromD))
892 ToDecls.push_back(*ToDOrErr);
893 else
894 return ToDOrErr.takeError();
895 }
896 return DeclGroupRef::Create(Importer.getToContext(),
897 ToDecls.begin(),
898 NumDecls);
899}
900
901template <>
902Expected<ASTNodeImporter::Designator>
903ASTNodeImporter::import(const Designator &D) {
904 if (D.isFieldDesignator()) {
905 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
906
907 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
908 if (!ToDotLocOrErr)
909 return ToDotLocOrErr.takeError();
910
911 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
912 if (!ToFieldLocOrErr)
913 return ToFieldLocOrErr.takeError();
914
915 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
916 }
917
918 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
919 if (!ToLBracketLocOrErr)
920 return ToLBracketLocOrErr.takeError();
921
922 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
923 if (!ToRBracketLocOrErr)
924 return ToRBracketLocOrErr.takeError();
925
926 if (D.isArrayDesignator())
927 return Designator(D.getFirstExprIndex(),
928 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
929
930 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
931 if (!ToEllipsisLocOrErr)
932 return ToEllipsisLocOrErr.takeError();
933
934 assert(D.isArrayRangeDesignator());
935 return Designator(
936 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
937 *ToRBracketLocOrErr);
938}
939
940template <>
941Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
942 VarDecl *Var = nullptr;
943 if (From.capturesVariable()) {
944 if (auto VarOrErr = import(From.getCapturedVar()))
945 Var = *VarOrErr;
946 else
947 return VarOrErr.takeError();
948 }
949
950 auto LocationOrErr = import(From.getLocation());
951 if (!LocationOrErr)
952 return LocationOrErr.takeError();
953
954 SourceLocation EllipsisLoc;
955 if (From.isPackExpansion())
956 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
957 return std::move(Err);
958
959 return LambdaCapture(
960 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
961 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000962}
963
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000964} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000965
Douglas Gregor3996e242010-02-15 22:01:00 +0000966//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000967// Import Types
968//----------------------------------------------------------------------------
969
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000970using namespace clang;
971
Balazs Keri3b30d652018-10-19 13:32:20 +0000972ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000973 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
974 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000975 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000976}
977
Balazs Keri3b30d652018-10-19 13:32:20 +0000978ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
979 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
980 if (!UnderlyingTypeOrErr)
981 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000982
Balazs Keri3b30d652018-10-19 13:32:20 +0000983 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000984}
985
Balazs Keri3b30d652018-10-19 13:32:20 +0000986ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000988#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
989 case BuiltinType::Id: \
990 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000991#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000992#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
993 case BuiltinType::Id: \
994 return Importer.getToContext().Id##Ty;
995#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000996#define SHARED_SINGLETON_TYPE(Expansion)
997#define BUILTIN_TYPE(Id, SingletonId) \
998 case BuiltinType::Id: return Importer.getToContext().SingletonId;
999#include "clang/AST/BuiltinTypes.def"
1000
1001 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1002 // context supports C++.
1003
1004 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1005 // context supports ObjC.
1006
Douglas Gregor96e578d2010-02-05 17:54:41 +00001007 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +00001008 // The context we're importing from has an unsigned 'char'. If we're
1009 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001010 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001011 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001012 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001013
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014 return Importer.getToContext().CharTy;
1015
Douglas Gregor96e578d2010-02-05 17:54:41 +00001016 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +00001017 // The context we're importing from has an unsigned 'char'. If we're
1018 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001019 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001020 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001021 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001022
Douglas Gregor96e578d2010-02-05 17:54:41 +00001023 return Importer.getToContext().CharTy;
1024
Chris Lattnerad3467e2010-12-25 23:25:43 +00001025 case BuiltinType::WChar_S:
1026 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001027 // FIXME: If not in C++, shall we translate to the C equivalent of
1028 // wchar_t?
1029 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001030 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001031
1032 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001033}
1034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1036 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1037 if (!ToOriginalTypeOrErr)
1038 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001039
Balazs Keri3b30d652018-10-19 13:32:20 +00001040 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001041}
1042
Balazs Keri3b30d652018-10-19 13:32:20 +00001043ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1044 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1045 if (!ToElementTypeOrErr)
1046 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001047
Balazs Keri3b30d652018-10-19 13:32:20 +00001048 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001049}
1050
Balazs Keri3b30d652018-10-19 13:32:20 +00001051ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1052 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1053 if (!ToPointeeTypeOrErr)
1054 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001055
Balazs Keri3b30d652018-10-19 13:32:20 +00001056 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001057}
1058
Balazs Keri3b30d652018-10-19 13:32:20 +00001059ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001060 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001061 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1062 if (!ToPointeeTypeOrErr)
1063 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001064
Balazs Keri3b30d652018-10-19 13:32:20 +00001065 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001066}
1067
Balazs Keri3b30d652018-10-19 13:32:20 +00001068ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001069ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001070 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001071 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1072 if (!ToPointeeTypeOrErr)
1073 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Balazs Keri3b30d652018-10-19 13:32:20 +00001075 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001076}
1077
Balazs Keri3b30d652018-10-19 13:32:20 +00001078ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001079ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001080 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001081 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1082 if (!ToPointeeTypeOrErr)
1083 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001084
Balazs Keri3b30d652018-10-19 13:32:20 +00001085 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001086}
1087
Balazs Keri3b30d652018-10-19 13:32:20 +00001088ExpectedType
1089ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001090 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001091 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1092 if (!ToPointeeTypeOrErr)
1093 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001094
Balazs Keri3b30d652018-10-19 13:32:20 +00001095 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1096 if (!ClassTypeOrErr)
1097 return ClassTypeOrErr.takeError();
1098
1099 return Importer.getToContext().getMemberPointerType(
1100 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001101}
1102
Balazs Keri3b30d652018-10-19 13:32:20 +00001103ExpectedType
1104ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1105 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1106 if (!ToElementTypeOrErr)
1107 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001108
Balazs Keri3b30d652018-10-19 13:32:20 +00001109 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001110 T->getSize(),
1111 T->getSizeModifier(),
1112 T->getIndexTypeCVRQualifiers());
1113}
1114
Balazs Keri3b30d652018-10-19 13:32:20 +00001115ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001116ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001117 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1118 if (!ToElementTypeOrErr)
1119 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001120
Balazs Keri3b30d652018-10-19 13:32:20 +00001121 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001122 T->getSizeModifier(),
1123 T->getIndexTypeCVRQualifiers());
1124}
1125
Balazs Keri3b30d652018-10-19 13:32:20 +00001126ExpectedType
1127ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1128 QualType ToElementType;
1129 Expr *ToSizeExpr;
1130 SourceRange ToBracketsRange;
1131 if (auto Imp = importSeq(
1132 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1133 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1134 else
1135 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001136
Balazs Keri3b30d652018-10-19 13:32:20 +00001137 return Importer.getToContext().getVariableArrayType(
1138 ToElementType, ToSizeExpr, T->getSizeModifier(),
1139 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001140}
1141
Balazs Keri3b30d652018-10-19 13:32:20 +00001142ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001143 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001144 QualType ToElementType;
1145 Expr *ToSizeExpr;
1146 SourceRange ToBracketsRange;
1147 if (auto Imp = importSeq(
1148 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1149 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1150 else
1151 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001152 // SizeExpr may be null if size is not specified directly.
1153 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001154
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001155 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001156 ToElementType, ToSizeExpr, T->getSizeModifier(),
1157 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001158}
1159
Balazs Keri3b30d652018-10-19 13:32:20 +00001160ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1161 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1162 if (!ToElementTypeOrErr)
1163 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001164
Balazs Keri3b30d652018-10-19 13:32:20 +00001165 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001166 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001167 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001168}
1169
Balazs Keri3b30d652018-10-19 13:32:20 +00001170ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1171 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1172 if (!ToElementTypeOrErr)
1173 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001174
Balazs Keri3b30d652018-10-19 13:32:20 +00001175 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001176 T->getNumElements());
1177}
1178
Balazs Keri3b30d652018-10-19 13:32:20 +00001179ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001180ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001181 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001182 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001183 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1184 if (!ToReturnTypeOrErr)
1185 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001186
Balazs Keri3b30d652018-10-19 13:32:20 +00001187 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001188 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001189}
1190
Balazs Keri3b30d652018-10-19 13:32:20 +00001191ExpectedType
1192ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1193 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1194 if (!ToReturnTypeOrErr)
1195 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001196
Douglas Gregor96e578d2010-02-05 17:54:41 +00001197 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001198 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001199 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001200 ExpectedType TyOrErr = import(A);
1201 if (!TyOrErr)
1202 return TyOrErr.takeError();
1203 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001204 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001205
Douglas Gregor96e578d2010-02-05 17:54:41 +00001206 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001207 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001208 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001209 ExpectedType TyOrErr = import(E);
1210 if (!TyOrErr)
1211 return TyOrErr.takeError();
1212 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001213 }
John McCalldb40c7f2010-12-14 08:05:40 +00001214
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001215 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1216 FunctionProtoType::ExtProtoInfo ToEPI;
1217
Balazs Keri3b30d652018-10-19 13:32:20 +00001218 auto Imp = importSeq(
1219 FromEPI.ExceptionSpec.NoexceptExpr,
1220 FromEPI.ExceptionSpec.SourceDecl,
1221 FromEPI.ExceptionSpec.SourceTemplate);
1222 if (!Imp)
1223 return Imp.takeError();
1224
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001225 ToEPI.ExtInfo = FromEPI.ExtInfo;
1226 ToEPI.Variadic = FromEPI.Variadic;
1227 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1228 ToEPI.TypeQuals = FromEPI.TypeQuals;
1229 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001230 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1231 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 std::tie(
1233 ToEPI.ExceptionSpec.NoexceptExpr,
1234 ToEPI.ExceptionSpec.SourceDecl,
1235 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001236
Balazs Keri3b30d652018-10-19 13:32:20 +00001237 return Importer.getToContext().getFunctionType(
1238 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001239}
1240
Balazs Keri3b30d652018-10-19 13:32:20 +00001241ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001242 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001243 UnresolvedUsingTypenameDecl *ToD;
1244 Decl *ToPrevD;
1245 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1246 std::tie(ToD, ToPrevD) = *Imp;
1247 else
1248 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001249
Balazs Keri3b30d652018-10-19 13:32:20 +00001250 return Importer.getToContext().getTypeDeclType(
1251 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001252}
1253
Balazs Keri3b30d652018-10-19 13:32:20 +00001254ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1255 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1256 if (!ToInnerTypeOrErr)
1257 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001258
Balazs Keri3b30d652018-10-19 13:32:20 +00001259 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001260}
1261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1263 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1264 if (!ToDeclOrErr)
1265 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001266
Balazs Keri3b30d652018-10-19 13:32:20 +00001267 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001268}
1269
Balazs Keri3b30d652018-10-19 13:32:20 +00001270ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1271 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1272 if (!ToExprOrErr)
1273 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001274
Balazs Keri3b30d652018-10-19 13:32:20 +00001275 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001276}
1277
Balazs Keri3b30d652018-10-19 13:32:20 +00001278ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1279 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1280 if (!ToUnderlyingTypeOrErr)
1281 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001282
Balazs Keri3b30d652018-10-19 13:32:20 +00001283 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001284}
1285
Balazs Keri3b30d652018-10-19 13:32:20 +00001286ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001287 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001288 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1289 if (!ToExprOrErr)
1290 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001291
Balazs Keri3b30d652018-10-19 13:32:20 +00001292 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1293 if (!ToUnderlyingTypeOrErr)
1294 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001295
Balazs Keri3b30d652018-10-19 13:32:20 +00001296 return Importer.getToContext().getDecltypeType(
1297 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001298}
1299
Balazs Keri3b30d652018-10-19 13:32:20 +00001300ExpectedType
1301ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1302 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1303 if (!ToBaseTypeOrErr)
1304 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001305
Balazs Keri3b30d652018-10-19 13:32:20 +00001306 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1307 if (!ToUnderlyingTypeOrErr)
1308 return ToUnderlyingTypeOrErr.takeError();
1309
1310 return Importer.getToContext().getUnaryTransformType(
1311 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001312}
1313
Balazs Keri3b30d652018-10-19 13:32:20 +00001314ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001315 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001316 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1317 if (!ToDeducedTypeOrErr)
1318 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001319
Balazs Keri3b30d652018-10-19 13:32:20 +00001320 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1321 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001322 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001323}
1324
Balazs Keri3b30d652018-10-19 13:32:20 +00001325ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001326 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001327 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1328 if (!ToDeclOrErr)
1329 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001330
Balazs Keri3b30d652018-10-19 13:32:20 +00001331 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1332 if (!ToInjTypeOrErr)
1333 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001334
1335 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1336 // See comments in InjectedClassNameType definition for details
1337 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1338 enum {
1339 TypeAlignmentInBits = 4,
1340 TypeAlignment = 1 << TypeAlignmentInBits
1341 };
1342
1343 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001344 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001345}
1346
Balazs Keri3b30d652018-10-19 13:32:20 +00001347ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1348 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1349 if (!ToDeclOrErr)
1350 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001351
Balazs Keri3b30d652018-10-19 13:32:20 +00001352 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001353}
1354
Balazs Keri3b30d652018-10-19 13:32:20 +00001355ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1356 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1357 if (!ToDeclOrErr)
1358 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001359
Balazs Keri3b30d652018-10-19 13:32:20 +00001360 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001361}
1362
Balazs Keri3b30d652018-10-19 13:32:20 +00001363ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1364 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1365 if (!ToModifiedTypeOrErr)
1366 return ToModifiedTypeOrErr.takeError();
1367 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1368 if (!ToEquivalentTypeOrErr)
1369 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001370
1371 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001372 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001373}
1374
Balazs Keri3b30d652018-10-19 13:32:20 +00001375ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001376 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001377 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1378 if (!ToDeclOrErr)
1379 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001380
1381 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001382 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001383}
1384
Balazs Keri3b30d652018-10-19 13:32:20 +00001385ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001386 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001387 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1388 if (!ReplacedOrErr)
1389 return ReplacedOrErr.takeError();
1390 const TemplateTypeParmType *Replaced =
1391 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001392
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1394 if (!ToReplacementTypeOrErr)
1395 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001396
1397 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001399}
1400
Balazs Keri3b30d652018-10-19 13:32:20 +00001401ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001402 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001403 auto ToTemplateOrErr = import(T->getTemplateName());
1404 if (!ToTemplateOrErr)
1405 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001407 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001408 if (Error Err = ImportTemplateArguments(
1409 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1410 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001411
Douglas Gregore2e50d332010-12-01 01:36:18 +00001412 QualType ToCanonType;
1413 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001414 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001415 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001416 if (ExpectedType TyOrErr = import(FromCanonType))
1417 ToCanonType = *TyOrErr;
1418 else
1419 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001420 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001421 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001422 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001423 ToCanonType);
1424}
1425
Balazs Keri3b30d652018-10-19 13:32:20 +00001426ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001427 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 auto ToQualifierOrErr = import(T->getQualifier());
1429 if (!ToQualifierOrErr)
1430 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001431
Balazs Keri3b30d652018-10-19 13:32:20 +00001432 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1433 if (!ToNamedTypeOrErr)
1434 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001435
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1437 if (!ToOwnedTagDeclOrErr)
1438 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001439
Abramo Bagnara6150c882010-05-11 21:36:43 +00001440 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001441 *ToQualifierOrErr,
1442 *ToNamedTypeOrErr,
1443 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001444}
1445
Balazs Keri3b30d652018-10-19 13:32:20 +00001446ExpectedType
1447ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1448 ExpectedType ToPatternOrErr = import(T->getPattern());
1449 if (!ToPatternOrErr)
1450 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001451
Balazs Keri3b30d652018-10-19 13:32:20 +00001452 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001453 T->getNumExpansions());
1454}
1455
Balazs Keri3b30d652018-10-19 13:32:20 +00001456ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001457 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001458 auto ToQualifierOrErr = import(T->getQualifier());
1459 if (!ToQualifierOrErr)
1460 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001461
Balazs Keri3b30d652018-10-19 13:32:20 +00001462 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001463
1464 SmallVector<TemplateArgument, 2> ToPack;
1465 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001466 if (Error Err = ImportTemplateArguments(
1467 T->getArgs(), T->getNumArgs(), ToPack))
1468 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001469
1470 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001471 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001472}
1473
Balazs Keri3b30d652018-10-19 13:32:20 +00001474ExpectedType
1475ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1476 auto ToQualifierOrErr = import(T->getQualifier());
1477 if (!ToQualifierOrErr)
1478 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001479
1480 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001481
Balazs Keri3b30d652018-10-19 13:32:20 +00001482 QualType Canon;
1483 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1484 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1485 Canon = (*TyOrErr).getCanonicalType();
1486 else
1487 return TyOrErr.takeError();
1488 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001489
Balazs Keri3b30d652018-10-19 13:32:20 +00001490 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1491 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001492 Name, Canon);
1493}
1494
Balazs Keri3b30d652018-10-19 13:32:20 +00001495ExpectedType
1496ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1497 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1498 if (!ToDeclOrErr)
1499 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500
Balazs Keri3b30d652018-10-19 13:32:20 +00001501 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001502}
1503
Balazs Keri3b30d652018-10-19 13:32:20 +00001504ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1505 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1506 if (!ToBaseTypeOrErr)
1507 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001508
Douglas Gregore9d95f12015-07-07 03:57:35 +00001509 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001510 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001511 if (ExpectedType TyOrErr = import(TypeArg))
1512 TypeArgs.push_back(*TyOrErr);
1513 else
1514 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001515 }
1516
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001517 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001518 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001519 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1520 Protocols.push_back(*ProtocolOrErr);
1521 else
1522 return ProtocolOrErr.takeError();
1523
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524 }
1525
Balazs Keri3b30d652018-10-19 13:32:20 +00001526 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001527 Protocols,
1528 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001529}
1530
Balazs Keri3b30d652018-10-19 13:32:20 +00001531ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001532ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001533 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1534 if (!ToPointeeTypeOrErr)
1535 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001536
Balazs Keri3b30d652018-10-19 13:32:20 +00001537 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001538}
1539
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001540//----------------------------------------------------------------------------
1541// Import Declarations
1542//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001543Error ASTNodeImporter::ImportDeclParts(
1544 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1545 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001546 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1547 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1548 DeclContext *OrigDC = D->getDeclContext();
1549 FunctionDecl *FunDecl;
1550 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1551 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001552 auto getLeafPointeeType = [](const Type *T) {
1553 while (T->isPointerType() || T->isArrayType()) {
1554 T = T->getPointeeOrArrayElementType();
1555 }
1556 return T;
1557 };
1558 for (const ParmVarDecl *P : FunDecl->parameters()) {
1559 const Type *LeafT =
1560 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1561 auto *RT = dyn_cast<RecordType>(LeafT);
1562 if (RT && RT->getDecl() == D) {
1563 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1564 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001565 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001566 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001567 }
1568 }
1569
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001570 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001571 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1572 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001573
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001574 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001575 if (Error Err = importInto(Name, D->getDeclName()))
1576 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001577
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001578 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001579 if (Error Err = importInto(Loc, D->getLocation()))
1580 return Err;
1581
Sean Callanan59721b32015-04-28 18:41:46 +00001582 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Balazs Keri3b30d652018-10-19 13:32:20 +00001583
1584 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001585}
1586
Balazs Keri3b30d652018-10-19 13:32:20 +00001587Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001588 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001589 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001590
Balazs Keri3b30d652018-10-19 13:32:20 +00001591 if (!ToD)
1592 if (Error Err = importInto(ToD, FromD))
1593 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001594
Balazs Keri3b30d652018-10-19 13:32:20 +00001595 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1596 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1597 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1598 !ToRecord->getDefinition()) {
1599 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1600 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001601 }
1602 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001603 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001604 }
1605
Balazs Keri3b30d652018-10-19 13:32:20 +00001606 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1607 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001608 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001609 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1610 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001611 }
1612 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001613 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001614 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001615
1616 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001617}
1618
Balazs Keri3b30d652018-10-19 13:32:20 +00001619Error
1620ASTNodeImporter::ImportDeclarationNameLoc(
1621 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001622 // NOTE: To.Name and To.Loc are already imported.
1623 // We only have to import To.LocInfo.
1624 switch (To.getName().getNameKind()) {
1625 case DeclarationName::Identifier:
1626 case DeclarationName::ObjCZeroArgSelector:
1627 case DeclarationName::ObjCOneArgSelector:
1628 case DeclarationName::ObjCMultiArgSelector:
1629 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001630 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001631 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001632
1633 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001634 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1635 To.setCXXOperatorNameRange(*ToRangeOrErr);
1636 else
1637 return ToRangeOrErr.takeError();
1638 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001639 }
1640 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001641 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1642 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1643 else
1644 return LocOrErr.takeError();
1645 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001646 }
1647 case DeclarationName::CXXConstructorName:
1648 case DeclarationName::CXXDestructorName:
1649 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001650 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1651 To.setNamedTypeInfo(*ToTInfoOrErr);
1652 else
1653 return ToTInfoOrErr.takeError();
1654 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001655 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001656 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001657 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001658}
1659
Balazs Keri3b30d652018-10-19 13:32:20 +00001660Error
1661ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001662 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001663 auto ToDCOrErr = Importer.ImportContext(FromDC);
1664 return ToDCOrErr.takeError();
1665 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001666 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001667 for (auto *From : FromDC->decls()) {
1668 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001669 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001670 // Ignore the error, continue with next Decl.
1671 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001672 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001673 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001674
Balazs Keri3b30d652018-10-19 13:32:20 +00001675 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001676}
1677
Balazs Keri3b30d652018-10-19 13:32:20 +00001678Error ASTNodeImporter::ImportDeclContext(
1679 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1680 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1681 if (!ToDCOrErr)
1682 return ToDCOrErr.takeError();
1683 ToDC = *ToDCOrErr;
1684
1685 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1686 auto ToLexicalDCOrErr = Importer.ImportContext(
1687 FromD->getLexicalDeclContext());
1688 if (!ToLexicalDCOrErr)
1689 return ToLexicalDCOrErr.takeError();
1690 ToLexicalDC = *ToLexicalDCOrErr;
1691 } else
1692 ToLexicalDC = ToDC;
1693
1694 return Error::success();
1695}
1696
1697Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001698 const CXXRecordDecl *From, CXXRecordDecl *To) {
1699 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1700 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001701
Balazs Keri1d20cc22018-07-16 12:16:39 +00001702 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001703 if (FromM->isImplicit()) {
1704 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1705 if (!ToMOrErr)
1706 return ToMOrErr.takeError();
1707 }
1708
1709 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001710}
1711
Balazs Keri3b30d652018-10-19 13:32:20 +00001712static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1713 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001714 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001715 Decl *ToTypedef = Importer.Import(FromTypedef);
1716 if (!ToTypedef)
1717 return make_error<ImportError>();
1718 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToTypedef));
1719 // FIXME: This should be the final code.
1720 //if (Expected<Decl *> ToTypedefOrErr = Importer.Import(FromTypedef))
1721 // To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1722 //else
1723 // return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001724 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001725 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001726}
1727
Balazs Keri3b30d652018-10-19 13:32:20 +00001728Error ASTNodeImporter::ImportDefinition(
1729 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001730 if (To->getDefinition() || To->isBeingDefined()) {
1731 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001732 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001733
Balazs Keri3b30d652018-10-19 13:32:20 +00001734 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001735 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001736
Douglas Gregore2e50d332010-12-01 01:36:18 +00001737 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001738
Balazs Keri3b30d652018-10-19 13:32:20 +00001739 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1740 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001741
Douglas Gregore2e50d332010-12-01 01:36:18 +00001742 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001743 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1744 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001745
1746 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1747 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1748 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001749 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001750 ToData.Aggregate = FromData.Aggregate;
1751 ToData.PlainOldData = FromData.PlainOldData;
1752 ToData.Empty = FromData.Empty;
1753 ToData.Polymorphic = FromData.Polymorphic;
1754 ToData.Abstract = FromData.Abstract;
1755 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001756 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1757 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1758 ToData.HasBasesWithNonStaticDataMembers =
1759 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001760 ToData.HasPrivateFields = FromData.HasPrivateFields;
1761 ToData.HasProtectedFields = FromData.HasProtectedFields;
1762 ToData.HasPublicFields = FromData.HasPublicFields;
1763 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001764 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001765 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001766 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001767 ToData.HasUninitializedReferenceMember
1768 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001769 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001770 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1771 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001772 ToData.NeedOverloadResolutionForCopyConstructor
1773 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001774 ToData.NeedOverloadResolutionForMoveConstructor
1775 = FromData.NeedOverloadResolutionForMoveConstructor;
1776 ToData.NeedOverloadResolutionForMoveAssignment
1777 = FromData.NeedOverloadResolutionForMoveAssignment;
1778 ToData.NeedOverloadResolutionForDestructor
1779 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001780 ToData.DefaultedCopyConstructorIsDeleted
1781 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001782 ToData.DefaultedMoveConstructorIsDeleted
1783 = FromData.DefaultedMoveConstructorIsDeleted;
1784 ToData.DefaultedMoveAssignmentIsDeleted
1785 = FromData.DefaultedMoveAssignmentIsDeleted;
1786 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001787 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1788 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001789 ToData.HasConstexprNonCopyMoveConstructor
1790 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001791 ToData.HasDefaultedDefaultConstructor
1792 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001793 ToData.DefaultedDefaultConstructorIsConstexpr
1794 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001795 ToData.HasConstexprDefaultConstructor
1796 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001797 ToData.HasNonLiteralTypeFieldsOrBases
1798 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001799 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001800 ToData.UserProvidedDefaultConstructor
1801 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001802 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001803 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1804 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1805 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1806 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001807 ToData.ImplicitCopyAssignmentHasConstParam
1808 = FromData.ImplicitCopyAssignmentHasConstParam;
1809 ToData.HasDeclaredCopyConstructorWithConstParam
1810 = FromData.HasDeclaredCopyConstructorWithConstParam;
1811 ToData.HasDeclaredCopyAssignmentWithConstParam
1812 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001813
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001814 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001815 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001816 ExpectedType TyOrErr = import(Base1.getType());
1817 if (!TyOrErr)
1818 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001819
1820 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001821 if (Base1.isPackExpansion()) {
1822 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1823 EllipsisLoc = *LocOrErr;
1824 else
1825 return LocOrErr.takeError();
1826 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001827
1828 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001829 if (Error Err =
1830 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1831 return Err;
1832
1833 auto RangeOrErr = import(Base1.getSourceRange());
1834 if (!RangeOrErr)
1835 return RangeOrErr.takeError();
1836
1837 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1838 if (!TSIOrErr)
1839 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001840
Douglas Gregore2e50d332010-12-01 01:36:18 +00001841 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001842 new (Importer.getToContext()) CXXBaseSpecifier(
1843 *RangeOrErr,
1844 Base1.isVirtual(),
1845 Base1.isBaseOfClass(),
1846 Base1.getAccessSpecifierAsWritten(),
1847 *TSIOrErr,
1848 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001849 }
1850 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001851 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001852 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001853
Douglas Gregor2e15c842012-02-01 21:00:38 +00001854 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001855 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1856 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001857
Douglas Gregore2e50d332010-12-01 01:36:18 +00001858 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001859 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001860}
1861
Balazs Keri3b30d652018-10-19 13:32:20 +00001862Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001863 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001864 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001865
Gabor Martonac3a5d62018-09-17 12:04:52 +00001866 Expr *FromInit = From->getInit();
1867 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001868 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001869
Balazs Keri3b30d652018-10-19 13:32:20 +00001870 ExpectedExpr ToInitOrErr = import(FromInit);
1871 if (!ToInitOrErr)
1872 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001873
Balazs Keri3b30d652018-10-19 13:32:20 +00001874 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001875 if (From->isInitKnownICE()) {
1876 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1877 Eval->CheckedICE = true;
1878 Eval->IsICE = From->isInitICE();
1879 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001880
1881 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001882 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001883}
1884
Balazs Keri3b30d652018-10-19 13:32:20 +00001885Error ASTNodeImporter::ImportDefinition(
1886 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001887 if (To->getDefinition() || To->isBeingDefined()) {
1888 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001889 return ImportDeclContext(From, /*ForceImport=*/true);
1890 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001891 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001892
Douglas Gregord451ea92011-07-29 23:31:30 +00001893 To->startDefinition();
1894
Balazs Keri3b30d652018-10-19 13:32:20 +00001895 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1896 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001897
Balazs Keri3b30d652018-10-19 13:32:20 +00001898 ExpectedType ToTypeOrErr =
1899 import(Importer.getFromContext().getTypeDeclType(From));
1900 if (!ToTypeOrErr)
1901 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001902
Balazs Keri3b30d652018-10-19 13:32:20 +00001903 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1904 if (!ToPromotionTypeOrErr)
1905 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001906
1907 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001908 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1909 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001910
Douglas Gregord451ea92011-07-29 23:31:30 +00001911 // FIXME: we might need to merge the number of positive or negative bits
1912 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001913 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001914 From->getNumPositiveBits(),
1915 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001916 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001917}
1918
Balazs Keri3b30d652018-10-19 13:32:20 +00001919// FIXME: Remove this, use `import` instead.
1920Expected<TemplateParameterList *> ASTNodeImporter::ImportTemplateParameterList(
1921 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001922 SmallVector<NamedDecl *, 4> ToParams(Params->size());
Balazs Keri3b30d652018-10-19 13:32:20 +00001923 if (Error Err = ImportContainerChecked(*Params, ToParams))
1924 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001925
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001926 Expr *ToRequiresClause;
1927 if (Expr *const R = Params->getRequiresClause()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001928 if (Error Err = importInto(ToRequiresClause, R))
1929 return std::move(Err);
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001930 } else {
1931 ToRequiresClause = nullptr;
1932 }
1933
Balazs Keri3b30d652018-10-19 13:32:20 +00001934 auto ToTemplateLocOrErr = import(Params->getTemplateLoc());
1935 if (!ToTemplateLocOrErr)
1936 return ToTemplateLocOrErr.takeError();
1937 auto ToLAngleLocOrErr = import(Params->getLAngleLoc());
1938 if (!ToLAngleLocOrErr)
1939 return ToLAngleLocOrErr.takeError();
1940 auto ToRAngleLocOrErr = import(Params->getRAngleLoc());
1941 if (!ToRAngleLocOrErr)
1942 return ToRAngleLocOrErr.takeError();
1943
1944 return TemplateParameterList::Create(
1945 Importer.getToContext(),
1946 *ToTemplateLocOrErr,
1947 *ToLAngleLocOrErr,
1948 ToParams,
1949 *ToRAngleLocOrErr,
1950 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001951}
1952
Balazs Keri3b30d652018-10-19 13:32:20 +00001953Error ASTNodeImporter::ImportTemplateArguments(
1954 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1955 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001956 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001957 if (auto ToOrErr = import(FromArgs[I]))
1958 ToArgs.push_back(*ToOrErr);
1959 else
1960 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001961 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001962
Balazs Keri3b30d652018-10-19 13:32:20 +00001963 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001964}
1965
Balazs Keri3b30d652018-10-19 13:32:20 +00001966// FIXME: Do not forget to remove this and use only 'import'.
1967Expected<TemplateArgument>
1968ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1969 return import(From);
1970}
1971
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001972template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001973Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001974 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1975 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001976 if (auto ToLocOrErr = import(FromLoc))
1977 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001978 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001979 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001980 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001981 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001982}
1983
Gabor Marton26f72a92018-07-12 09:42:05 +00001984static StructuralEquivalenceKind
1985getStructuralEquivalenceKind(const ASTImporter &Importer) {
1986 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1987 : StructuralEquivalenceKind::Default;
1988}
1989
Gabor Marton950fb572018-07-17 12:39:27 +00001990bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1991 StructuralEquivalenceContext Ctx(
1992 Importer.getFromContext(), Importer.getToContext(),
1993 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1994 false, Complain);
1995 return Ctx.IsEquivalent(From, To);
1996}
1997
1998bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001999 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002000 // Eliminate a potential failure point where we attempt to re-import
2001 // something we're trying to import while completing ToRecord.
2002 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2003 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002004 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002005 if (ToOriginRecord)
2006 ToRecord = ToOriginRecord;
2007 }
2008
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002009 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002010 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002011 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002012 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002013 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002014 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002015}
2016
Larisse Voufo39a1e502013-08-06 01:03:05 +00002017bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2018 bool Complain) {
2019 StructuralEquivalenceContext Ctx(
2020 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002021 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2022 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002023 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002024}
2025
Douglas Gregor98c10182010-02-12 22:17:39 +00002026bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002027 StructuralEquivalenceContext Ctx(
2028 Importer.getFromContext(), Importer.getToContext(),
2029 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002030 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002031}
2032
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002033bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2034 FunctionTemplateDecl *To) {
2035 StructuralEquivalenceContext Ctx(
2036 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002037 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2038 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002039 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002040}
2041
Balazs Keric7797c42018-07-11 09:37:24 +00002042bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2043 StructuralEquivalenceContext Ctx(
2044 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002045 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2046 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002047 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002048}
2049
Douglas Gregor91155082012-11-14 22:29:20 +00002050bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002051 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002052 const llvm::APSInt &FromVal = FromEC->getInitVal();
2053 const llvm::APSInt &ToVal = ToEC->getInitVal();
2054
2055 return FromVal.isSigned() == ToVal.isSigned() &&
2056 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2057 FromVal == ToVal;
2058}
2059
2060bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002061 ClassTemplateDecl *To) {
2062 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2063 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002064 Importer.getNonEquivalentDecls(),
2065 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002066 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002067}
2068
Larisse Voufo39a1e502013-08-06 01:03:05 +00002069bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2070 VarTemplateDecl *To) {
2071 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2072 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002073 Importer.getNonEquivalentDecls(),
2074 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002075 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002076}
2077
Balazs Keri3b30d652018-10-19 13:32:20 +00002078ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002079 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002080 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002081 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002082}
2083
Balazs Keri3b30d652018-10-19 13:32:20 +00002084ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2085 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2086 << D->getDeclKindName();
2087 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2088}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002089
Balazs Keri3b30d652018-10-19 13:32:20 +00002090ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2091 // Import the context of this declaration.
2092 DeclContext *DC, *LexicalDC;
2093 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2094 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002095
2096 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002097 ExpectedSLoc LocOrErr = import(D->getLocation());
2098 if (!LocOrErr)
2099 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002100
Gabor Marton26f72a92018-07-12 09:42:05 +00002101 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002102 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002103 return ToD;
2104
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002105 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002106 LexicalDC->addDeclInternal(ToD);
2107 return ToD;
2108}
2109
Balazs Keri3b30d652018-10-19 13:32:20 +00002110ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002111 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002112 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002113
Gabor Marton26f72a92018-07-12 09:42:05 +00002114 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002115
Sean Callanan65198272011-11-17 23:20:56 +00002116 return ToD;
2117}
2118
Balazs Keri3b30d652018-10-19 13:32:20 +00002119ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2120 ExpectedSLoc LocOrErr = import(D->getLocation());
2121 if (!LocOrErr)
2122 return LocOrErr.takeError();
2123 auto ColonLocOrErr = import(D->getColonLoc());
2124 if (!ColonLocOrErr)
2125 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002126
2127 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002128 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2129 if (!DCOrErr)
2130 return DCOrErr.takeError();
2131 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002132
Gabor Marton26f72a92018-07-12 09:42:05 +00002133 AccessSpecDecl *ToD;
2134 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002135 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002136 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002137
2138 // Lexical DeclContext and Semantic DeclContext
2139 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002140 ToD->setLexicalDeclContext(DC);
2141 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002142
Gabor Marton26f72a92018-07-12 09:42:05 +00002143 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002144}
2145
Balazs Keri3b30d652018-10-19 13:32:20 +00002146ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2147 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2148 if (!DCOrErr)
2149 return DCOrErr.takeError();
2150 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002151 DeclContext *LexicalDC = DC;
2152
Balazs Keri3b30d652018-10-19 13:32:20 +00002153 SourceLocation ToLocation, ToRParenLoc;
2154 Expr *ToAssertExpr;
2155 StringLiteral *ToMessage;
2156 if (auto Imp = importSeq(
2157 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2158 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2159 else
2160 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002161
Gabor Marton26f72a92018-07-12 09:42:05 +00002162 StaticAssertDecl *ToD;
2163 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002164 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2165 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002166 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002167
2168 ToD->setLexicalDeclContext(LexicalDC);
2169 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002170 return ToD;
2171}
2172
Balazs Keri3b30d652018-10-19 13:32:20 +00002173ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002174 // Import the major distinguishing characteristics of this namespace.
2175 DeclContext *DC, *LexicalDC;
2176 DeclarationName Name;
2177 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002178 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002179 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2180 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002181 if (ToD)
2182 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002183
2184 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002185 if (!Name) {
2186 // This is an anonymous namespace. Adopt an existing anonymous
2187 // namespace if we can.
2188 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002189 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002190 MergeWithNamespace = TU->getAnonymousNamespace();
2191 else
2192 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2193 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002194 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002195 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002196 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002197 for (auto *FoundDecl : FoundDecls) {
2198 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002199 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002200
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002201 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002202 MergeWithNamespace = FoundNS;
2203 ConflictingDecls.clear();
2204 break;
2205 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002206
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002207 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002208 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002209
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002210 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002211 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002212 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002213 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002214 if (!Name)
2215 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002216 }
2217 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002218
Balazs Keri3b30d652018-10-19 13:32:20 +00002219 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2220 if (!BeginLocOrErr)
2221 return BeginLocOrErr.takeError();
2222
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002223 // Create the "to" namespace, if needed.
2224 NamespaceDecl *ToNamespace = MergeWithNamespace;
2225 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002226 if (GetImportedOrCreateDecl(
2227 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002228 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002229 /*PrevDecl=*/nullptr))
2230 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002231 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002232 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002233
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002234 // If this is an anonymous namespace, register it as the anonymous
2235 // namespace within its context.
2236 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002237 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002238 TU->setAnonymousNamespace(ToNamespace);
2239 else
2240 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2241 }
2242 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002243 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002244
Balazs Keri3b30d652018-10-19 13:32:20 +00002245 if (Error Err = ImportDeclContext(D))
2246 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002247
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002248 return ToNamespace;
2249}
2250
Balazs Keri3b30d652018-10-19 13:32:20 +00002251ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002252 // Import the major distinguishing characteristics of this namespace.
2253 DeclContext *DC, *LexicalDC;
2254 DeclarationName Name;
2255 SourceLocation Loc;
2256 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002257 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2258 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002259 if (LookupD)
2260 return LookupD;
2261
2262 // NOTE: No conflict resolution is done for namespace aliases now.
2263
Balazs Keri3b30d652018-10-19 13:32:20 +00002264 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2265 NestedNameSpecifierLoc ToQualifierLoc;
2266 NamespaceDecl *ToNamespace;
2267 if (auto Imp = importSeq(
2268 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2269 D->getTargetNameLoc(), D->getNamespace()))
2270 std::tie(
2271 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2272 ToNamespace) = *Imp;
2273 else
2274 return Imp.takeError();
2275 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002276
Gabor Marton26f72a92018-07-12 09:42:05 +00002277 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002278 if (GetImportedOrCreateDecl(
2279 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2280 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002281 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002282
2283 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002284 LexicalDC->addDeclInternal(ToD);
2285
2286 return ToD;
2287}
2288
Balazs Keri3b30d652018-10-19 13:32:20 +00002289ExpectedDecl
2290ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002291 // Import the major distinguishing characteristics of this typedef.
2292 DeclContext *DC, *LexicalDC;
2293 DeclarationName Name;
2294 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002295 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002296 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2297 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002298 if (ToD)
2299 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002300
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002301 // If this typedef is not in block scope, determine whether we've
2302 // seen a typedef with the same name (that we can merge with) or any
2303 // other entity by that name (which name lookup could conflict with).
2304 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002305 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002306 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002307 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002308 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002309 for (auto *FoundDecl : FoundDecls) {
2310 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002311 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002312 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002313 if (Importer.IsStructurallyEquivalent(
2314 D->getUnderlyingType(), FoundTypedef->getUnderlyingType()))
2315 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002316 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002317
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002318 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002319 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002320
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002321 if (!ConflictingDecls.empty()) {
2322 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002323 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002324 ConflictingDecls.size());
2325 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002326 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002327 }
2328 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002329
Balazs Keri3b30d652018-10-19 13:32:20 +00002330 QualType ToUnderlyingType;
2331 TypeSourceInfo *ToTypeSourceInfo;
2332 SourceLocation ToBeginLoc;
2333 if (auto Imp = importSeq(
2334 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2335 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2336 else
2337 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002338
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002339 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002340 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002341 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002342 if (IsAlias) {
2343 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002344 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2345 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002346 return ToTypedef;
2347 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002348 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2349 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002350 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002351
Douglas Gregordd483172010-02-22 17:42:47 +00002352 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002353 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002354
2355 // Templated declarations should not appear in DeclContext.
2356 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2357 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2358 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002359
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002360 return ToTypedef;
2361}
2362
Balazs Keri3b30d652018-10-19 13:32:20 +00002363ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002364 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2365}
2366
Balazs Keri3b30d652018-10-19 13:32:20 +00002367ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002368 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2369}
2370
Balazs Keri3b30d652018-10-19 13:32:20 +00002371ExpectedDecl
2372ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002373 // Import the major distinguishing characteristics of this typedef.
2374 DeclContext *DC, *LexicalDC;
2375 DeclarationName Name;
2376 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002377 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002378 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2379 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002380 if (FoundD)
2381 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002382
2383 // If this typedef is not in block scope, determine whether we've
2384 // seen a typedef with the same name (that we can merge with) or any
2385 // other entity by that name (which name lookup could conflict with).
2386 if (!DC->isFunctionOrMethod()) {
2387 SmallVector<NamedDecl *, 4> ConflictingDecls;
2388 unsigned IDNS = Decl::IDNS_Ordinary;
2389 SmallVector<NamedDecl *, 2> FoundDecls;
2390 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002391 for (auto *FoundDecl : FoundDecls) {
2392 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002393 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002394 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002395 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002396 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002397 }
2398
2399 if (!ConflictingDecls.empty()) {
2400 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2401 ConflictingDecls.data(),
2402 ConflictingDecls.size());
2403 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002404 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002405 }
2406 }
2407
Balazs Keri3b30d652018-10-19 13:32:20 +00002408 TemplateParameterList *ToTemplateParameters;
2409 TypeAliasDecl *ToTemplatedDecl;
2410 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2411 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2412 else
2413 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002414
Gabor Marton26f72a92018-07-12 09:42:05 +00002415 TypeAliasTemplateDecl *ToAlias;
2416 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002417 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002418 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002419
Balazs Keri3b30d652018-10-19 13:32:20 +00002420 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002421
Gabor Horvath7a91c082017-11-14 11:30:38 +00002422 ToAlias->setAccess(D->getAccess());
2423 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002424 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002425 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002426}
2427
Balazs Keri3b30d652018-10-19 13:32:20 +00002428ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002429 // Import the major distinguishing characteristics of this label.
2430 DeclContext *DC, *LexicalDC;
2431 DeclarationName Name;
2432 SourceLocation Loc;
2433 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002434 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2435 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002436 if (ToD)
2437 return ToD;
2438
2439 assert(LexicalDC->isFunctionOrMethod());
2440
Gabor Marton26f72a92018-07-12 09:42:05 +00002441 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002442 if (D->isGnuLocal()) {
2443 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2444 if (!BeginLocOrErr)
2445 return BeginLocOrErr.takeError();
2446 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2447 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2448 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002449
Balazs Keri3b30d652018-10-19 13:32:20 +00002450 } else {
2451 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2452 Name.getAsIdentifierInfo()))
2453 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002454
Balazs Keri3b30d652018-10-19 13:32:20 +00002455 }
2456
2457 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2458 if (!ToStmtOrErr)
2459 return ToStmtOrErr.takeError();
2460
2461 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002462 ToLabel->setLexicalDeclContext(LexicalDC);
2463 LexicalDC->addDeclInternal(ToLabel);
2464 return ToLabel;
2465}
2466
Balazs Keri3b30d652018-10-19 13:32:20 +00002467ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002468 // Import the major distinguishing characteristics of this enum.
2469 DeclContext *DC, *LexicalDC;
2470 DeclarationName Name;
2471 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002472 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002473 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2474 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002475 if (ToD)
2476 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002477
Douglas Gregor98c10182010-02-12 22:17:39 +00002478 // Figure out what enum name we're looking for.
2479 unsigned IDNS = Decl::IDNS_Tag;
2480 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002481 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002482 if (Error Err = importInto(
2483 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2484 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002485 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002486 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002487 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002488
Douglas Gregor98c10182010-02-12 22:17:39 +00002489 // We may already have an enum of the same name; try to find and match it.
2490 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002491 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002492 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002493 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002494 for (auto *FoundDecl : FoundDecls) {
2495 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002496 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002497
Balazs Keri3b30d652018-10-19 13:32:20 +00002498 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002499 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002500 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002501 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002502
Balazs Keri3b30d652018-10-19 13:32:20 +00002503 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002504 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002505 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002506 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002507
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002508 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002509 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002510
Douglas Gregor98c10182010-02-12 22:17:39 +00002511 if (!ConflictingDecls.empty()) {
2512 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002513 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002514 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002515 if (!Name)
2516 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002517 }
2518 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002519
Balazs Keri3b30d652018-10-19 13:32:20 +00002520 SourceLocation ToBeginLoc;
2521 NestedNameSpecifierLoc ToQualifierLoc;
2522 QualType ToIntegerType;
2523 if (auto Imp = importSeq(
2524 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2525 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2526 else
2527 return Imp.takeError();
2528
Douglas Gregor98c10182010-02-12 22:17:39 +00002529 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002530 EnumDecl *D2;
2531 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002532 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002533 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2534 D->isScopedUsingClassTag(), D->isFixed()))
2535 return D2;
2536
Balazs Keri3b30d652018-10-19 13:32:20 +00002537 D2->setQualifierInfo(ToQualifierLoc);
2538 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002539 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002540 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002541 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002542
Douglas Gregor98c10182010-02-12 22:17:39 +00002543 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002544 if (D->isCompleteDefinition())
2545 if (Error Err = ImportDefinition(D, D2))
2546 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002547
Douglas Gregor3996e242010-02-15 22:01:00 +00002548 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002549}
2550
Balazs Keri3b30d652018-10-19 13:32:20 +00002551ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002552 bool IsFriendTemplate = false;
2553 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2554 IsFriendTemplate =
2555 DCXX->getDescribedClassTemplate() &&
2556 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2557 Decl::FOK_None;
2558 }
2559
Douglas Gregor5c73e912010-02-11 00:48:18 +00002560 // If this record has a definition in the translation unit we're coming from,
2561 // but this particular declaration is not that definition, import the
2562 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002563 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002564 if (Definition && Definition != D &&
Balazs Keri0c23dc52018-08-13 13:08:37 +00002565 // Friend template declaration must be imported on its own.
2566 !IsFriendTemplate &&
Gabor Martona3af5672018-05-23 14:24:02 +00002567 // In contrast to a normal CXXRecordDecl, the implicit
2568 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2569 // The definition of the implicit CXXRecordDecl in this case is the
2570 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2571 // condition in order to be able to import the implict Decl.
2572 !D->isImplicit()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002573 ExpectedDecl ImportedDefOrErr = import(Definition);
2574 if (!ImportedDefOrErr)
2575 return ImportedDefOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002576
Balazs Keri3b30d652018-10-19 13:32:20 +00002577 return Importer.MapImported(D, *ImportedDefOrErr);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002578 }
Gabor Martona3af5672018-05-23 14:24:02 +00002579
Douglas Gregor5c73e912010-02-11 00:48:18 +00002580 // Import the major distinguishing characteristics of this record.
2581 DeclContext *DC, *LexicalDC;
2582 DeclarationName Name;
2583 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002584 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002585 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2586 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002587 if (ToD)
2588 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002589
Douglas Gregor5c73e912010-02-11 00:48:18 +00002590 // Figure out what structure name we're looking for.
2591 unsigned IDNS = Decl::IDNS_Tag;
2592 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002593 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002594 if (Error Err = importInto(
2595 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2596 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002597 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002598 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002599 IDNS |= Decl::IDNS_Ordinary;
2600
2601 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002602 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002603 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002604 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002605 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002606 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002607 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002608
2609 if (!FoundDecls.empty()) {
2610 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2611 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2612 D->getASTContext().getExternalSource()->CompleteType(D);
2613 }
2614
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002615 for (auto *FoundDecl : FoundDecls) {
2616 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002617 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002618
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002619 Decl *Found = FoundDecl;
2620 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2621 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002622 Found = Tag->getDecl();
2623 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002624
2625 if (D->getDescribedTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002626 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Martona0df7a92018-05-30 09:19:26 +00002627 Found = Template->getTemplatedDecl();
Balazs Keri3b30d652018-10-19 13:32:20 +00002628 } else {
2629 ConflictingDecls.push_back(FoundDecl);
Gabor Martona0df7a92018-05-30 09:19:26 +00002630 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002631 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002632 }
2633
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002634 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002635 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002636 if (!IsStructuralMatch(D, FoundRecord, false))
2637 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002638 } else {
2639 if (!IsStructuralMatch(D, FoundRecord)) {
2640 ConflictingDecls.push_back(FoundDecl);
2641 continue;
2642 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002643 }
2644
Sean Callanan9092d472017-05-13 00:46:33 +00002645 PrevDecl = FoundRecord;
2646
Douglas Gregor25791052010-02-12 00:09:27 +00002647 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002648 if ((SearchName && !D->isCompleteDefinition() && !IsFriendTemplate)
Douglas Gregordd6006f2012-07-17 21:16:27 +00002649 || (D->isCompleteDefinition() &&
2650 D->isAnonymousStructOrUnion()
Balazs Keri3b30d652018-10-19 13:32:20 +00002651 == FoundDef->isAnonymousStructOrUnion())) {
Douglas Gregor25791052010-02-12 00:09:27 +00002652 // The record types structurally match, or the "from" translation
2653 // unit only had a forward declaration anyway; call it the same
2654 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002655 // FIXME: Structural equivalence check should check for same
2656 // user-defined methods.
2657 Importer.MapImported(D, FoundDef);
2658 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2659 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2660 assert(FoundCXX && "Record type mismatch");
2661
2662 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2663 // FoundDef may not have every implicit method that D has
2664 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002665 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2666 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002667 }
2668 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002669 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002670 if (IsFriendTemplate)
2671 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002672 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002673 // We have a forward declaration of this type, so adopt that forward
2674 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002675
Sean Callananc94711c2014-03-04 18:11:50 +00002676 // If one or both can be completed from external storage then try one
2677 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002678
Sean Callananc94711c2014-03-04 18:11:50 +00002679 if (FoundRecord->hasExternalLexicalStorage() &&
2680 !FoundRecord->isCompleteDefinition())
2681 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2682 if (D->hasExternalLexicalStorage())
2683 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002684
Sean Callananc94711c2014-03-04 18:11:50 +00002685 if (FoundRecord->isCompleteDefinition() &&
2686 D->isCompleteDefinition() &&
Balazs Keri3b30d652018-10-19 13:32:20 +00002687 !IsStructuralMatch(D, FoundRecord)) {
2688 ConflictingDecls.push_back(FoundDecl);
Sean Callananc94711c2014-03-04 18:11:50 +00002689 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002690 }
Balazs Keri0c23dc52018-08-13 13:08:37 +00002691
Douglas Gregor25791052010-02-12 00:09:27 +00002692 AdoptDecl = FoundRecord;
2693 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002694 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002695
2696 continue;
2697 } else if (isa<ValueDecl>(Found))
2698 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002699
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002700 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002701 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002702
Douglas Gregordd6006f2012-07-17 21:16:27 +00002703 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002704 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002705 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002706 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002707 if (!Name)
2708 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002709 }
2710 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002711
Balazs Keri3b30d652018-10-19 13:32:20 +00002712 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2713 if (!BeginLocOrErr)
2714 return BeginLocOrErr.takeError();
2715
Douglas Gregor5c73e912010-02-11 00:48:18 +00002716 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002717 RecordDecl *D2 = AdoptDecl;
2718 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002719 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002720 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002721 if (DCXX->isLambda()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002722 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2723 if (!TInfoOrErr)
2724 return TInfoOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00002725 if (GetImportedOrCreateSpecialDecl(
2726 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002727 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002728 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2729 return D2CXX;
Balazs Keri3b30d652018-10-19 13:32:20 +00002730 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2731 if (!CDeclOrErr)
2732 return CDeclOrErr.takeError();
2733 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002734 } else if (DCXX->isInjectedClassName()) {
2735 // We have to be careful to do a similar dance to the one in
2736 // Sema::ActOnStartCXXMemberDeclarations
2737 CXXRecordDecl *const PrevDecl = nullptr;
2738 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002739 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002740 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002741 Name.getAsIdentifierInfo(), PrevDecl,
2742 DelayTypeCreation))
2743 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002744 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002745 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002746 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002747 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002748 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002749 Name.getAsIdentifierInfo(),
2750 cast_or_null<CXXRecordDecl>(PrevDecl)))
2751 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002752 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002753
Douglas Gregor3996e242010-02-15 22:01:00 +00002754 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002755 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002756 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002757 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002758 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002759
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002760 if (ClassTemplateDecl *FromDescribed =
2761 DCXX->getDescribedClassTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002762 ClassTemplateDecl *ToDescribed;
2763 if (Error Err = importInto(ToDescribed, FromDescribed))
2764 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002765 D2CXX->setDescribedClassTemplate(ToDescribed);
Balazs Keri0c23dc52018-08-13 13:08:37 +00002766 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
Gabor Marton5915777e2018-06-26 13:44:24 +00002767 // In a record describing a template the type should be an
2768 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2769 // previously set type to the correct value here (ToDescribed is not
2770 // available at record create).
2771 // FIXME: The previous type is cleared but not removed from
2772 // ASTContext's internal storage.
2773 CXXRecordDecl *Injected = nullptr;
2774 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2775 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2776 if (Record && Record->isInjectedClassName()) {
2777 Injected = Record;
2778 break;
2779 }
2780 }
2781 D2CXX->setTypeForDecl(nullptr);
2782 Importer.getToContext().getInjectedClassNameType(D2CXX,
2783 ToDescribed->getInjectedClassNameSpecialization());
2784 if (Injected) {
2785 Injected->setTypeForDecl(nullptr);
2786 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2787 }
2788 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002789 } else if (MemberSpecializationInfo *MemberInfo =
2790 DCXX->getMemberSpecializationInfo()) {
2791 TemplateSpecializationKind SK =
2792 MemberInfo->getTemplateSpecializationKind();
2793 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002794
2795 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2796 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2797 else
2798 return ToInstOrErr.takeError();
2799
2800 if (ExpectedSLoc POIOrErr =
2801 import(MemberInfo->getPointOfInstantiation()))
2802 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2803 *POIOrErr);
2804 else
2805 return POIOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002806 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002807
Douglas Gregor25791052010-02-12 00:09:27 +00002808 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002809 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002810 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002811 Name.getAsIdentifierInfo(), PrevDecl))
2812 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002813 D2->setLexicalDeclContext(LexicalDC);
2814 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002815 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002816
Balazs Keri3b30d652018-10-19 13:32:20 +00002817 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2818 D2->setQualifierInfo(*QualifierLocOrErr);
2819 else
2820 return QualifierLocOrErr.takeError();
2821
Douglas Gregordd6006f2012-07-17 21:16:27 +00002822 if (D->isAnonymousStructOrUnion())
2823 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002824 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002825
2826 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002827
Balazs Keri3b30d652018-10-19 13:32:20 +00002828 if (D->isCompleteDefinition())
2829 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2830 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002831
Douglas Gregor3996e242010-02-15 22:01:00 +00002832 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002833}
2834
Balazs Keri3b30d652018-10-19 13:32:20 +00002835ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002836 // Import the major distinguishing characteristics of this enumerator.
2837 DeclContext *DC, *LexicalDC;
2838 DeclarationName Name;
2839 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002840 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002841 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2842 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002843 if (ToD)
2844 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002845
Fangrui Song6907ce22018-07-30 19:24:48 +00002846 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002847 // in the same context.
2848 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002849 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002850 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002851 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002852 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002853 for (auto *FoundDecl : FoundDecls) {
2854 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002855 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002856
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002857 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002858 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002859 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002860 }
2861
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002862 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002863 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002864
Douglas Gregor98c10182010-02-12 22:17:39 +00002865 if (!ConflictingDecls.empty()) {
2866 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002867 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002868 ConflictingDecls.size());
2869 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002870 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002871 }
2872 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002873
Balazs Keri3b30d652018-10-19 13:32:20 +00002874 ExpectedType TypeOrErr = import(D->getType());
2875 if (!TypeOrErr)
2876 return TypeOrErr.takeError();
2877
2878 ExpectedExpr InitOrErr = import(D->getInitExpr());
2879 if (!InitOrErr)
2880 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002881
Gabor Marton26f72a92018-07-12 09:42:05 +00002882 EnumConstantDecl *ToEnumerator;
2883 if (GetImportedOrCreateDecl(
2884 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002885 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002886 return ToEnumerator;
2887
Douglas Gregordd483172010-02-22 17:42:47 +00002888 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002889 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002890 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002891 return ToEnumerator;
2892}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002893
Balazs Keri3b30d652018-10-19 13:32:20 +00002894Error ASTNodeImporter::ImportTemplateInformation(
2895 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002896 switch (FromFD->getTemplatedKind()) {
2897 case FunctionDecl::TK_NonTemplate:
2898 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002899 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002900
2901 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002902 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002903
2904 if (Expected<FunctionDecl *> InstFDOrErr =
2905 import(FromFD->getInstantiatedFromMemberFunction()))
2906 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2907 else
2908 return InstFDOrErr.takeError();
2909
2910 if (ExpectedSLoc POIOrErr = import(
2911 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2912 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2913 else
2914 return POIOrErr.takeError();
2915
2916 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002917 }
2918
2919 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002920 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002921 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002922 if (!FunctionAndArgsOrErr)
2923 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002924
2925 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002926 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002927
Gabor Marton5254e642018-06-27 13:32:50 +00002928 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002929 TemplateArgumentListInfo ToTAInfo;
2930 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002931 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002932 if (Error Err = ImportTemplateArgumentListInfo(
2933 *FromTAArgsAsWritten, ToTAInfo))
2934 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002935
Balazs Keri3b30d652018-10-19 13:32:20 +00002936 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2937 if (!POIOrErr)
2938 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002939
Gabor Marton5254e642018-06-27 13:32:50 +00002940 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002941 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002942 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2943 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2944 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002945 }
2946
2947 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2948 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2949 UnresolvedSet<8> TemplDecls;
2950 unsigned NumTemplates = FromInfo->getNumTemplates();
2951 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002952 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2953 import(FromInfo->getTemplate(I)))
2954 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002955 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002956 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002957 }
2958
2959 // Import TemplateArgumentListInfo.
2960 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002961 if (Error Err = ImportTemplateArgumentListInfo(
2962 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2963 llvm::makeArrayRef(
2964 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2965 ToTAInfo))
2966 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002967
2968 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2969 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002970 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002971 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002972 }
Sam McCallfdc32072018-01-26 12:06:44 +00002973 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002974}
2975
Balazs Keri3b30d652018-10-19 13:32:20 +00002976Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002977ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002978 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002979 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002980 if (!FunctionAndArgsOrErr)
2981 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002982
Balazs Keri3b30d652018-10-19 13:32:20 +00002983 FunctionTemplateDecl *Template;
2984 TemplateArgsTy ToTemplArgs;
2985 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002986 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002987 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002988 return FoundSpec;
2989}
2990
Balazs Keri3b30d652018-10-19 13:32:20 +00002991ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002992
Balazs Keri3b30d652018-10-19 13:32:20 +00002993 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002994 auto RedeclIt = Redecls.begin();
2995 // Import the first part of the decl chain. I.e. import all previous
2996 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002997 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2998 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2999 if (!ToRedeclOrErr)
3000 return ToRedeclOrErr.takeError();
3001 }
Gabor Marton5254e642018-06-27 13:32:50 +00003002 assert(*RedeclIt == D);
3003
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003004 // Import the major distinguishing characteristics of this function.
3005 DeclContext *DC, *LexicalDC;
3006 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003007 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003008 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003009 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3010 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003011 if (ToD)
3012 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003013
Gabor Marton5254e642018-06-27 13:32:50 +00003014 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003015 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003016
Gabor Marton5254e642018-06-27 13:32:50 +00003017 // If this is a function template specialization, then try to find the same
3018 // existing specialization in the "to" context. The localUncachedLookup
3019 // below will not find any specialization, but would find the primary
3020 // template; thus, we have to skip normal lookup in case of specializations.
3021 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3022 if (D->getTemplatedKind() ==
3023 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003024 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3025 if (!FoundFunctionOrErr)
3026 return FoundFunctionOrErr.takeError();
3027 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
3028 if (D->doesThisDeclarationHaveABody() && FoundFunction->hasBody())
3029 return Importer.MapImported(D, FoundFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003030 FoundByLookup = FoundFunction;
3031 }
3032 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003033 // Try to find a function in our own ("to") context with the same name, same
3034 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003035 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003036 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003037 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003038 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003039 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003040 for (auto *FoundDecl : FoundDecls) {
3041 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003042 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003043
Balazs Keria35798d2018-07-17 09:52:41 +00003044 // If template was found, look at the templated function.
3045 if (FromFT) {
3046 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
3047 FoundDecl = Template->getTemplatedDecl();
3048 else
3049 continue;
3050 }
3051
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003052 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003053 if (FoundFunction->hasExternalFormalLinkage() &&
3054 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00003055 if (IsStructuralMatch(D, FoundFunction)) {
3056 const FunctionDecl *Definition = nullptr;
3057 if (D->doesThisDeclarationHaveABody() &&
3058 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003059 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00003060 D, const_cast<FunctionDecl *>(Definition));
3061 }
3062 FoundByLookup = FoundFunction;
3063 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003064 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003065
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003066 // FIXME: Check for overloading more carefully, e.g., by boosting
3067 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003068
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003069 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003070 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003071 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003072
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003073 // Complain about inconsistent function types.
3074 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003075 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003076 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003077 diag::note_odr_value_here)
3078 << FoundFunction->getType();
3079 }
3080 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003081
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003082 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003083 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003084
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003085 if (!ConflictingDecls.empty()) {
3086 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003087 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003088 ConflictingDecls.size());
3089 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003090 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003091 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003092 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003093
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003094 DeclarationNameInfo NameInfo(Name, Loc);
3095 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003096 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3097 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003098
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003099 QualType FromTy = D->getType();
3100 bool usedDifferentExceptionSpec = false;
3101
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003102 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003103 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3104 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3105 // FunctionDecl that we are importing the FunctionProtoType for.
3106 // To avoid an infinite recursion when importing, create the FunctionDecl
3107 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003108 if (FromEPI.ExceptionSpec.SourceDecl ||
3109 FromEPI.ExceptionSpec.SourceTemplate ||
3110 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003111 FunctionProtoType::ExtProtoInfo DefaultEPI;
3112 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003113 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003114 usedDifferentExceptionSpec = true;
3115 }
3116 }
3117
Balazs Keri3b30d652018-10-19 13:32:20 +00003118 QualType T;
3119 TypeSourceInfo *TInfo;
3120 SourceLocation ToInnerLocStart, ToEndLoc;
3121 NestedNameSpecifierLoc ToQualifierLoc;
3122 if (auto Imp = importSeq(
3123 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3124 D->getQualifierLoc(), D->getEndLoc()))
3125 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3126 else
3127 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003128
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003129 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003130 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003131 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003132 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3133 Parameters.push_back(*ToPOrErr);
3134 else
3135 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003136 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003137
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003138 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003139 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003140 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003141 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003142 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3143 ToInnerLocStart, NameInfo, T, TInfo,
3144 FromConstructor->isExplicit(),
3145 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003146 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00003147 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003148 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3149 // Import first, then allocate memory and copy if there was no error.
3150 if (Error Err = ImportContainerChecked(
3151 FromConstructor->inits(), CtorInitializers))
3152 return std::move(Err);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003153 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00003154 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3155 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003156 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00003157 ToCtor->setCtorInitializers(Memory);
3158 ToCtor->setNumCtorInitializers(NumInitializers);
3159 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003160 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003161 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003162 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3163 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3164 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003165 return ToFunction;
3166 } else if (CXXConversionDecl *FromConversion =
3167 dyn_cast<CXXConversionDecl>(D)) {
3168 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3169 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003170 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003171 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3172 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003173 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003174 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3175 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003176 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003177 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3178 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003179 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003180 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003181 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003182 D->getStorageClass(), D->isInlineSpecified(),
3183 D->hasWrittenPrototype(), D->isConstexpr()))
3184 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003185 }
John McCall3e11ebe2010-03-15 10:12:16 +00003186
Balazs Keri3b30d652018-10-19 13:32:20 +00003187 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003188 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003189 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003190 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3191 ToFunction->setTrivial(D->isTrivial());
3192 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003193 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003194
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003195 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003196 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003197 Param->setOwningFunction(ToFunction);
3198 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003199 }
David Blaikie9c70e042011-09-21 18:16:56 +00003200 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003201
Gabor Marton5254e642018-06-27 13:32:50 +00003202 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003203 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00003204 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003205 ToFunction->setPreviousDecl(Recent);
3206 }
3207
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003208 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3209 // params it refers to.
3210 if (TInfo) {
3211 if (auto ProtoLoc =
3212 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3213 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3214 ProtoLoc.setParam(I, Parameters[I]);
3215 }
3216 }
3217
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003218 if (usedDifferentExceptionSpec) {
3219 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003220 if (ExpectedType TyOrErr = import(D->getType()))
3221 ToFunction->setType(*TyOrErr);
3222 else
3223 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003224 }
3225
Balazs Keria35798d2018-07-17 09:52:41 +00003226 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003227 if (FromFT) {
3228 auto ToFTOrErr = import(FromFT);
3229 if (!ToFTOrErr)
3230 return ToFTOrErr.takeError();
3231 }
Balazs Keria35798d2018-07-17 09:52:41 +00003232
Gabor Marton5254e642018-06-27 13:32:50 +00003233 if (D->doesThisDeclarationHaveABody()) {
3234 if (Stmt *FromBody = D->getBody()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003235 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3236 ToFunction->setBody(*ToBodyOrErr);
3237 else
3238 return ToBodyOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00003239 }
3240 }
3241
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003242 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003243
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003244 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003245 if (Error Err = ImportTemplateInformation(D, ToFunction))
3246 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003247
Gabor Marton5254e642018-06-27 13:32:50 +00003248 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3249
3250 // TODO Can we generalize this approach to other AST nodes as well?
3251 if (D->getDeclContext()->containsDeclAndLoad(D))
3252 DC->addDeclInternal(ToFunction);
3253 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003254 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003255
Gabor Marton5254e642018-06-27 13:32:50 +00003256 // Friend declaration's lexical context is the befriending class, but the
3257 // semantic context is the enclosing scope of the befriending class.
3258 // We want the friend functions to be found in the semantic context by lookup.
3259 // FIXME should we handle this generically in VisitFriendDecl?
3260 // In Other cases when LexicalDC != DC we don't want it to be added,
3261 // e.g out-of-class definitions like void B::f() {} .
3262 if (LexicalDC != DC && IsFriend) {
3263 DC->makeDeclVisibleInContext(ToFunction);
3264 }
3265
Gabor Marton7a0841e2018-10-29 10:18:28 +00003266 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3267 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3268
Gabor Marton5254e642018-06-27 13:32:50 +00003269 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003270 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3271 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3272 if (!ToRedeclOrErr)
3273 return ToRedeclOrErr.takeError();
3274 }
Gabor Marton5254e642018-06-27 13:32:50 +00003275
Douglas Gregor43f54792010-02-17 02:12:47 +00003276 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003277}
3278
Balazs Keri3b30d652018-10-19 13:32:20 +00003279ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003280 return VisitFunctionDecl(D);
3281}
3282
Balazs Keri3b30d652018-10-19 13:32:20 +00003283ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003284 return VisitCXXMethodDecl(D);
3285}
3286
Balazs Keri3b30d652018-10-19 13:32:20 +00003287ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003288 return VisitCXXMethodDecl(D);
3289}
3290
Balazs Keri3b30d652018-10-19 13:32:20 +00003291ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003292 return VisitCXXMethodDecl(D);
3293}
3294
Balazs Keri3b30d652018-10-19 13:32:20 +00003295ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003296 // Import the major distinguishing characteristics of a variable.
3297 DeclContext *DC, *LexicalDC;
3298 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003299 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003300 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003301 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3302 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003303 if (ToD)
3304 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003305
Fangrui Song6907ce22018-07-30 19:24:48 +00003306 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003307 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003308 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003309 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003310 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003311 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003312 if (!Name &&
3313 ASTImporter::getFieldIndex(D) !=
3314 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003315 continue;
3316
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003317 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003318 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003319 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003320 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3321 // initializer of a FieldDecl might not had been instantiated in the
3322 // "To" context. However, the "From" context might instantiated that,
3323 // thus we have to merge that.
3324 if (Expr *FromInitializer = D->getInClassInitializer()) {
3325 // We don't have yet the initializer set.
3326 if (FoundField->hasInClassInitializer() &&
3327 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003328 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3329 FoundField->setInClassInitializer(*ToInitializerOrErr);
3330 else {
3331 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003332 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003333 // FIXME: warning message?
3334 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003335 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003336 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003337 }
3338 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003339 return FoundField;
3340 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003341
Balazs Keri3b30d652018-10-19 13:32:20 +00003342 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003343 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3344 << Name << D->getType() << FoundField->getType();
3345 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3346 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003347
3348 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003349 }
3350 }
3351
Balazs Keri3b30d652018-10-19 13:32:20 +00003352 QualType ToType;
3353 TypeSourceInfo *ToTInfo;
3354 Expr *ToBitWidth;
3355 SourceLocation ToInnerLocStart;
3356 Expr *ToInitializer;
3357 if (auto Imp = importSeq(
3358 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3359 D->getInnerLocStart(), D->getInClassInitializer()))
3360 std::tie(
3361 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3362 else
3363 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003364
Gabor Marton26f72a92018-07-12 09:42:05 +00003365 FieldDecl *ToField;
3366 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003367 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3368 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3369 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003370 return ToField;
3371
Douglas Gregordd483172010-02-22 17:42:47 +00003372 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003373 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003374 if (ToInitializer)
3375 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003376 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003377 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003378 return ToField;
3379}
3380
Balazs Keri3b30d652018-10-19 13:32:20 +00003381ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003382 // Import the major distinguishing characteristics of a variable.
3383 DeclContext *DC, *LexicalDC;
3384 DeclarationName Name;
3385 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003386 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003387 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3388 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003389 if (ToD)
3390 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003391
Fangrui Song6907ce22018-07-30 19:24:48 +00003392 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003393 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003394 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003395 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003396 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003397 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003398 if (!Name &&
3399 ASTImporter::getFieldIndex(D) !=
3400 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003401 continue;
3402
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003403 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003404 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003405 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003406 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003407 return FoundField;
3408 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003409
3410 // If there are more anonymous fields to check, continue.
3411 if (!Name && I < N-1)
3412 continue;
3413
Balazs Keri3b30d652018-10-19 13:32:20 +00003414 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003415 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3416 << Name << D->getType() << FoundField->getType();
3417 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3418 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003419
3420 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003421 }
3422 }
3423
Francois Pichet783dd6e2010-11-21 06:08:52 +00003424 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003425 auto TypeOrErr = import(D->getType());
3426 if (!TypeOrErr)
3427 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003428
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003429 auto **NamedChain =
3430 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003431
3432 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003433 for (auto *PI : D->chain())
3434 if (Expected<NamedDecl *> ToD = import(PI))
3435 NamedChain[i++] = *ToD;
3436 else
3437 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003438
Gabor Marton26f72a92018-07-12 09:42:05 +00003439 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3440 IndirectFieldDecl *ToIndirectField;
3441 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003442 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003443 // FIXME here we leak `NamedChain` which is allocated before
3444 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003445
Balazs Keri3b30d652018-10-19 13:32:20 +00003446 for (const auto *Attr : D->attrs())
3447 ToIndirectField->addAttr(Importer.Import(Attr));
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.
3462 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3463 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 Keri3b30d652018-10-19 13:32:20 +00003503 if (auto ListOrErr = ImportTemplateParameterList(FromTPLists[I]))
3504 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
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003540 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003541 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003542 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003543 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003544 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003545 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003546 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003547 return FoundIvar;
3548 }
3549
3550 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3551 << Name << D->getType() << FoundIvar->getType();
3552 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3553 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003554
3555 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003556 }
3557 }
3558
Balazs Keri3b30d652018-10-19 13:32:20 +00003559 QualType ToType;
3560 TypeSourceInfo *ToTypeSourceInfo;
3561 Expr *ToBitWidth;
3562 SourceLocation ToInnerLocStart;
3563 if (auto Imp = importSeq(
3564 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3565 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3566 else
3567 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003568
Gabor Marton26f72a92018-07-12 09:42:05 +00003569 ObjCIvarDecl *ToIvar;
3570 if (GetImportedOrCreateDecl(
3571 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003572 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3573 ToType, ToTypeSourceInfo,
3574 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003575 return ToIvar;
3576
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003577 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003578 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003579 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003580}
3581
Balazs Keri3b30d652018-10-19 13:32:20 +00003582ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003583
3584 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3585 auto RedeclIt = Redecls.begin();
3586 // Import the first part of the decl chain. I.e. import all previous
3587 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003588 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3589 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3590 if (!RedeclOrErr)
3591 return RedeclOrErr.takeError();
3592 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003593 assert(*RedeclIt == D);
3594
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003595 // Import the major distinguishing characteristics of a variable.
3596 DeclContext *DC, *LexicalDC;
3597 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003598 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003599 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003600 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3601 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003602 if (ToD)
3603 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003604
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003605 // Try to find a variable in our own ("to") context with the same name and
3606 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003607 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003608 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003609 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003610 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003611 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003612 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003613 for (auto *FoundDecl : FoundDecls) {
3614 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003615 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003616
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003617 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003618 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003619 if (FoundVar->hasExternalFormalLinkage() &&
3620 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003621 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003622 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003623
3624 // The VarDecl in the "From" context has a definition, but in the
3625 // "To" context we already have a definition.
3626 VarDecl *FoundDef = FoundVar->getDefinition();
3627 if (D->isThisDeclarationADefinition() && FoundDef)
3628 // FIXME Check for ODR error if the two definitions have
3629 // different initializers?
3630 return Importer.MapImported(D, FoundDef);
3631
3632 // The VarDecl in the "From" context has an initializer, but in the
3633 // "To" context we already have an initializer.
3634 const VarDecl *FoundDInit = nullptr;
3635 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3636 // FIXME Diagnose ODR error if the two initializers are different?
3637 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3638
3639 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003640 break;
3641 }
3642
Douglas Gregor56521c52010-02-12 17:23:39 +00003643 const ArrayType *FoundArray
3644 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3645 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003646 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003647 if (FoundArray && TArray) {
3648 if (isa<IncompleteArrayType>(FoundArray) &&
3649 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003650 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003651 if (auto TyOrErr = import(D->getType()))
3652 FoundVar->setType(*TyOrErr);
3653 else
3654 return TyOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003655
Gabor Martonac3a5d62018-09-17 12:04:52 +00003656 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003657 break;
3658 } else if (isa<IncompleteArrayType>(TArray) &&
3659 isa<ConstantArrayType>(FoundArray)) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003660 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003661 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003662 }
3663 }
3664
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003665 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003666 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003667 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3668 << FoundVar->getType();
3669 }
3670 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003671
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003672 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003673 }
3674
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003675 if (!ConflictingDecls.empty()) {
3676 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003677 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003678 ConflictingDecls.size());
3679 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003680 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003681 }
3682 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003683
Balazs Keri3b30d652018-10-19 13:32:20 +00003684 QualType ToType;
3685 TypeSourceInfo *ToTypeSourceInfo;
3686 SourceLocation ToInnerLocStart;
3687 NestedNameSpecifierLoc ToQualifierLoc;
3688 if (auto Imp = importSeq(
3689 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3690 D->getQualifierLoc()))
3691 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3692 else
3693 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003694
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003695 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003696 VarDecl *ToVar;
3697 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003698 ToInnerLocStart, Loc,
3699 Name.getAsIdentifierInfo(),
3700 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003701 D->getStorageClass()))
3702 return ToVar;
3703
Balazs Keri3b30d652018-10-19 13:32:20 +00003704 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003705 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003706 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003707
Gabor Martonac3a5d62018-09-17 12:04:52 +00003708 if (FoundByLookup) {
3709 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3710 ToVar->setPreviousDecl(Recent);
3711 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003712
Balazs Keri3b30d652018-10-19 13:32:20 +00003713 if (Error Err = ImportInitializer(D, ToVar))
3714 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003715
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003716 if (D->isConstexpr())
3717 ToVar->setConstexpr(true);
3718
Gabor Martonac3a5d62018-09-17 12:04:52 +00003719 if (D->getDeclContext()->containsDeclAndLoad(D))
3720 DC->addDeclInternal(ToVar);
3721 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3722 LexicalDC->addDeclInternal(ToVar);
3723
3724 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003725 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3726 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3727 if (!RedeclOrErr)
3728 return RedeclOrErr.takeError();
3729 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003730
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003731 return ToVar;
3732}
3733
Balazs Keri3b30d652018-10-19 13:32:20 +00003734ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003735 // Parameters are created in the translation unit's context, then moved
3736 // into the function declaration's context afterward.
3737 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003738
Balazs Keri3b30d652018-10-19 13:32:20 +00003739 DeclarationName ToDeclName;
3740 SourceLocation ToLocation;
3741 QualType ToType;
3742 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3743 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3744 else
3745 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003746
Douglas Gregor8b228d72010-02-17 21:22:52 +00003747 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003748 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003749 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3750 ToLocation, ToDeclName.getAsIdentifierInfo(),
3751 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003752 return ToParm;
3753 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003754}
3755
Balazs Keri3b30d652018-10-19 13:32:20 +00003756ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003757 // Parameters are created in the translation unit's context, then moved
3758 // into the function declaration's context afterward.
3759 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003760
Balazs Keri3b30d652018-10-19 13:32:20 +00003761 DeclarationName ToDeclName;
3762 SourceLocation ToLocation, ToInnerLocStart;
3763 QualType ToType;
3764 TypeSourceInfo *ToTypeSourceInfo;
3765 if (auto Imp = importSeq(
3766 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3767 D->getTypeSourceInfo()))
3768 std::tie(
3769 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3770 ToTypeSourceInfo) = *Imp;
3771 else
3772 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003773
Gabor Marton26f72a92018-07-12 09:42:05 +00003774 ParmVarDecl *ToParm;
3775 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003776 ToInnerLocStart, ToLocation,
3777 ToDeclName.getAsIdentifierInfo(), ToType,
3778 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003779 /*DefaultArg*/ nullptr))
3780 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003781
3782 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003783 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003784 ToParm->setKNRPromoted(D->isKNRPromoted());
3785
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003786 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003787 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3788 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3789 else
3790 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003791 } else if (D->hasUnparsedDefaultArg()) {
3792 ToParm->setUnparsedDefaultArg();
3793 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003794 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3795 ToParm->setDefaultArg(*ToDefArgOrErr);
3796 else
3797 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003798 }
Sean Callanan59721b32015-04-28 18:41:46 +00003799
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003800 if (D->isObjCMethodParameter()) {
3801 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3802 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3803 } else {
3804 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3805 D->getFunctionScopeIndex());
3806 }
3807
Gabor Marton26f72a92018-07-12 09:42:05 +00003808 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003809}
3810
Balazs Keri3b30d652018-10-19 13:32:20 +00003811ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003812 // Import the major distinguishing characteristics of a method.
3813 DeclContext *DC, *LexicalDC;
3814 DeclarationName Name;
3815 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003816 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003817 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3818 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003819 if (ToD)
3820 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003821
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003822 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003823 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003824 for (auto *FoundDecl : FoundDecls) {
3825 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003826 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3827 continue;
3828
3829 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003830 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3831 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003832 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003833 << D->isInstanceMethod() << Name << D->getReturnType()
3834 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003835 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003836 diag::note_odr_objc_method_here)
3837 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003838
3839 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003840 }
3841
3842 // Check the number of parameters.
3843 if (D->param_size() != FoundMethod->param_size()) {
3844 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3845 << D->isInstanceMethod() << Name
3846 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003847 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003848 diag::note_odr_objc_method_here)
3849 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003850
3851 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003852 }
3853
3854 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003855 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003856 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3857 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003858 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003859 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003860 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003861 diag::err_odr_objc_method_param_type_inconsistent)
3862 << D->isInstanceMethod() << Name
3863 << (*P)->getType() << (*FoundP)->getType();
3864 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3865 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003866
3867 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003868 }
3869 }
3870
3871 // Check variadic/non-variadic.
3872 // Check the number of parameters.
3873 if (D->isVariadic() != FoundMethod->isVariadic()) {
3874 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3875 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003876 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003877 diag::note_odr_objc_method_here)
3878 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003879
3880 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003881 }
3882
3883 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003884 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003885 }
3886 }
3887
Balazs Keri3b30d652018-10-19 13:32:20 +00003888 SourceLocation ToEndLoc;
3889 QualType ToReturnType;
3890 TypeSourceInfo *ToReturnTypeSourceInfo;
3891 if (auto Imp = importSeq(
3892 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3893 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3894 else
3895 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003896
Gabor Marton26f72a92018-07-12 09:42:05 +00003897 ObjCMethodDecl *ToMethod;
3898 if (GetImportedOrCreateDecl(
3899 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003900 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3901 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003902 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3903 D->getImplementationControl(), D->hasRelatedResultType()))
3904 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003905
3906 // FIXME: When we decide to merge method definitions, we'll need to
3907 // deal with implicit parameters.
3908
3909 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003910 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003911 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003912 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3913 ToParams.push_back(*ToPOrErr);
3914 else
3915 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003916 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003917
Douglas Gregor43f54792010-02-17 02:12:47 +00003918 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003919 for (auto *ToParam : ToParams) {
3920 ToParam->setOwningFunction(ToMethod);
3921 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003922 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003923
Balazs Keri3b30d652018-10-19 13:32:20 +00003924 SmallVector<SourceLocation, 12> FromSelLocs;
3925 D->getSelectorLocs(FromSelLocs);
3926 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3927 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3928 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003929
Balazs Keri3b30d652018-10-19 13:32:20 +00003930 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003931
3932 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003933 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003934 return ToMethod;
3935}
3936
Balazs Keri3b30d652018-10-19 13:32:20 +00003937ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003938 // Import the major distinguishing characteristics of a category.
3939 DeclContext *DC, *LexicalDC;
3940 DeclarationName Name;
3941 SourceLocation Loc;
3942 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003943 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3944 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003945 if (ToD)
3946 return ToD;
3947
Balazs Keri3b30d652018-10-19 13:32:20 +00003948 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3949 TypeSourceInfo *ToTypeSourceInfo;
3950 if (auto Imp = importSeq(
3951 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3952 D->getTypeSourceInfo()))
3953 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3954 else
3955 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003956
Gabor Marton26f72a92018-07-12 09:42:05 +00003957 ObjCTypeParamDecl *Result;
3958 if (GetImportedOrCreateDecl(
3959 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003960 ToVarianceLoc, D->getIndex(),
3961 ToLocation, Name.getAsIdentifierInfo(),
3962 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003963 return Result;
3964
Douglas Gregor85f3f952015-07-07 03:57:15 +00003965 Result->setLexicalDeclContext(LexicalDC);
3966 return Result;
3967}
3968
Balazs Keri3b30d652018-10-19 13:32:20 +00003969ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003970 // Import the major distinguishing characteristics of a category.
3971 DeclContext *DC, *LexicalDC;
3972 DeclarationName Name;
3973 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003974 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003975 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3976 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003977 if (ToD)
3978 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003979
Balazs Keri3b30d652018-10-19 13:32:20 +00003980 ObjCInterfaceDecl *ToInterface;
3981 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3982 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003983
Douglas Gregor84c51c32010-02-18 01:47:50 +00003984 // Determine if we've already encountered this category.
3985 ObjCCategoryDecl *MergeWithCategory
3986 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3987 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3988 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003989 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3990 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3991 if (auto Imp = importSeq(
3992 D->getAtStartLoc(), D->getCategoryNameLoc(),
3993 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3994 std::tie(
3995 ToAtStartLoc, ToCategoryNameLoc,
3996 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3997 else
3998 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003999
4000 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004001 ToAtStartLoc, Loc,
4002 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004003 Name.getAsIdentifierInfo(), ToInterface,
4004 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004005 ToIvarLBraceLoc,
4006 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004007 return ToCategory;
4008
Douglas Gregor84c51c32010-02-18 01:47:50 +00004009 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004010 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004011 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004012 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004013 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4014 ToCategory->setTypeParamList(*PListOrErr);
4015 else
4016 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004017
Douglas Gregor84c51c32010-02-18 01:47:50 +00004018 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004019 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4020 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004021 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4022 = D->protocol_loc_begin();
4023 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4024 FromProtoEnd = D->protocol_end();
4025 FromProto != FromProtoEnd;
4026 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004027 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4028 Protocols.push_back(*ToProtoOrErr);
4029 else
4030 return ToProtoOrErr.takeError();
4031
4032 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4033 ProtocolLocs.push_back(*ToProtoLocOrErr);
4034 else
4035 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004036 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004037
Douglas Gregor84c51c32010-02-18 01:47:50 +00004038 // FIXME: If we're merging, make sure that the protocol list is the same.
4039 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4040 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004041
Douglas Gregor84c51c32010-02-18 01:47:50 +00004042 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004043 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004044 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004045
Douglas Gregor84c51c32010-02-18 01:47:50 +00004046 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004047 if (Error Err = ImportDeclContext(D))
4048 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004049
Douglas Gregor84c51c32010-02-18 01:47:50 +00004050 // If we have an implementation, import it as well.
4051 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004052 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4053 import(D->getImplementation()))
4054 ToCategory->setImplementation(*ToImplOrErr);
4055 else
4056 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004057 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004058
Douglas Gregor84c51c32010-02-18 01:47:50 +00004059 return ToCategory;
4060}
4061
Balazs Keri3b30d652018-10-19 13:32:20 +00004062Error ASTNodeImporter::ImportDefinition(
4063 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004064 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004065 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004066 if (Error Err = ImportDeclContext(From))
4067 return Err;
4068 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 }
4070
4071 // Start the protocol definition
4072 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004073
Douglas Gregor2aa53772012-01-24 17:42:07 +00004074 // Import protocols
4075 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4076 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004077 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4078 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004079 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4080 FromProtoEnd = From->protocol_end();
4081 FromProto != FromProtoEnd;
4082 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004083 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4084 Protocols.push_back(*ToProtoOrErr);
4085 else
4086 return ToProtoOrErr.takeError();
4087
4088 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4089 ProtocolLocs.push_back(*ToProtoLocOrErr);
4090 else
4091 return ToProtoLocOrErr.takeError();
4092
Douglas Gregor2aa53772012-01-24 17:42:07 +00004093 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004094
Douglas Gregor2aa53772012-01-24 17:42:07 +00004095 // FIXME: If we're merging, make sure that the protocol list is the same.
4096 To->setProtocolList(Protocols.data(), Protocols.size(),
4097 ProtocolLocs.data(), Importer.getToContext());
4098
Douglas Gregor2e15c842012-02-01 21:00:38 +00004099 if (shouldForceImportDeclContext(Kind)) {
4100 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004101 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4102 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004103 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004104 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004105}
4106
Balazs Keri3b30d652018-10-19 13:32:20 +00004107ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004108 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004109 // from, but this particular declaration is not that definition, import the
4110 // definition and map to that.
4111 ObjCProtocolDecl *Definition = D->getDefinition();
4112 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004113 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4114 return Importer.MapImported(D, *ImportedDefOrErr);
4115 else
4116 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004117 }
4118
Douglas Gregor84c51c32010-02-18 01:47:50 +00004119 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004120 DeclContext *DC, *LexicalDC;
4121 DeclarationName Name;
4122 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004123 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004124 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4125 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004126 if (ToD)
4127 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004128
Craig Topper36250ad2014-05-12 05:36:57 +00004129 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004130 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004131 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004132 for (auto *FoundDecl : FoundDecls) {
4133 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004134 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004135
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004136 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004137 break;
4138 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004139
Douglas Gregor98d156a2010-02-17 16:12:00 +00004140 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004141 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004142 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4143 if (!ToAtBeginLocOrErr)
4144 return ToAtBeginLocOrErr.takeError();
4145
Gabor Marton26f72a92018-07-12 09:42:05 +00004146 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4147 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004148 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004149 /*PrevDecl=*/nullptr))
4150 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004151 ToProto->setLexicalDeclContext(LexicalDC);
4152 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004153 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004154
4155 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004156
Balazs Keri3b30d652018-10-19 13:32:20 +00004157 if (D->isThisDeclarationADefinition())
4158 if (Error Err = ImportDefinition(D, ToProto))
4159 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004160
Douglas Gregor98d156a2010-02-17 16:12:00 +00004161 return ToProto;
4162}
4163
Balazs Keri3b30d652018-10-19 13:32:20 +00004164ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4165 DeclContext *DC, *LexicalDC;
4166 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4167 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004168
Balazs Keri3b30d652018-10-19 13:32:20 +00004169 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4170 if (!ExternLocOrErr)
4171 return ExternLocOrErr.takeError();
4172
4173 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4174 if (!LangLocOrErr)
4175 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004176
4177 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004178
4179 LinkageSpecDecl *ToLinkageSpec;
4180 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004181 *ExternLocOrErr, *LangLocOrErr,
4182 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004183 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004184
4185 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004186 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4187 if (!RBraceLocOrErr)
4188 return RBraceLocOrErr.takeError();
4189 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004190 }
4191
4192 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4193 LexicalDC->addDeclInternal(ToLinkageSpec);
4194
Sean Callanan0aae0412014-12-10 00:00:37 +00004195 return ToLinkageSpec;
4196}
4197
Balazs Keri3b30d652018-10-19 13:32:20 +00004198ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004199 DeclContext *DC, *LexicalDC;
4200 DeclarationName Name;
4201 SourceLocation Loc;
4202 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004203 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4204 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004205 if (ToD)
4206 return ToD;
4207
Balazs Keri3b30d652018-10-19 13:32:20 +00004208 SourceLocation ToLoc, ToUsingLoc;
4209 NestedNameSpecifierLoc ToQualifierLoc;
4210 if (auto Imp = importSeq(
4211 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4212 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4213 else
4214 return Imp.takeError();
4215
4216 DeclarationNameInfo NameInfo(Name, ToLoc);
4217 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4218 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004219
Gabor Marton26f72a92018-07-12 09:42:05 +00004220 UsingDecl *ToUsing;
4221 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004222 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004223 D->hasTypename()))
4224 return ToUsing;
4225
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004226 ToUsing->setLexicalDeclContext(LexicalDC);
4227 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004228
4229 if (NamedDecl *FromPattern =
4230 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004231 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4232 Importer.getToContext().setInstantiatedFromUsingDecl(
4233 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004234 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004235 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004236 }
4237
Balazs Keri3b30d652018-10-19 13:32:20 +00004238 for (UsingShadowDecl *FromShadow : D->shadows()) {
4239 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4240 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004241 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004242 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004243 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004244 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004245 }
4246 return ToUsing;
4247}
4248
Balazs Keri3b30d652018-10-19 13:32:20 +00004249ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004250 DeclContext *DC, *LexicalDC;
4251 DeclarationName Name;
4252 SourceLocation Loc;
4253 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004254 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4255 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004256 if (ToD)
4257 return ToD;
4258
Balazs Keri3b30d652018-10-19 13:32:20 +00004259 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4260 if (!ToUsingOrErr)
4261 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004262
Balazs Keri3b30d652018-10-19 13:32:20 +00004263 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4264 if (!ToTargetOrErr)
4265 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004266
Gabor Marton26f72a92018-07-12 09:42:05 +00004267 UsingShadowDecl *ToShadow;
4268 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004269 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004270 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004271
4272 ToShadow->setLexicalDeclContext(LexicalDC);
4273 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004274
4275 if (UsingShadowDecl *FromPattern =
4276 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004277 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4278 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4279 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004280 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004281 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004282 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004283 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004284 }
4285
4286 LexicalDC->addDeclInternal(ToShadow);
4287
4288 return ToShadow;
4289}
4290
Balazs Keri3b30d652018-10-19 13:32:20 +00004291ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004292 DeclContext *DC, *LexicalDC;
4293 DeclarationName Name;
4294 SourceLocation Loc;
4295 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004296 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4297 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004298 if (ToD)
4299 return ToD;
4300
Balazs Keri3b30d652018-10-19 13:32:20 +00004301 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4302 if (!ToComAncestorOrErr)
4303 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004304
Balazs Keri3b30d652018-10-19 13:32:20 +00004305 NamespaceDecl *ToNominatedNamespace;
4306 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4307 NestedNameSpecifierLoc ToQualifierLoc;
4308 if (auto Imp = importSeq(
4309 D->getNominatedNamespace(), D->getUsingLoc(),
4310 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4311 D->getIdentLocation()))
4312 std::tie(
4313 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4314 ToQualifierLoc, ToIdentLocation) = *Imp;
4315 else
4316 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004317
Gabor Marton26f72a92018-07-12 09:42:05 +00004318 UsingDirectiveDecl *ToUsingDir;
4319 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004320 ToUsingLoc,
4321 ToNamespaceKeyLocation,
4322 ToQualifierLoc,
4323 ToIdentLocation,
4324 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004325 return ToUsingDir;
4326
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004327 ToUsingDir->setLexicalDeclContext(LexicalDC);
4328 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004329
4330 return ToUsingDir;
4331}
4332
Balazs Keri3b30d652018-10-19 13:32:20 +00004333ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004334 UnresolvedUsingValueDecl *D) {
4335 DeclContext *DC, *LexicalDC;
4336 DeclarationName Name;
4337 SourceLocation Loc;
4338 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004339 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4340 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004341 if (ToD)
4342 return ToD;
4343
Balazs Keri3b30d652018-10-19 13:32:20 +00004344 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4345 NestedNameSpecifierLoc ToQualifierLoc;
4346 if (auto Imp = importSeq(
4347 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4348 D->getEllipsisLoc()))
4349 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4350 else
4351 return Imp.takeError();
4352
4353 DeclarationNameInfo NameInfo(Name, ToLoc);
4354 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4355 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004356
Gabor Marton26f72a92018-07-12 09:42:05 +00004357 UnresolvedUsingValueDecl *ToUsingValue;
4358 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004359 ToUsingLoc, ToQualifierLoc, NameInfo,
4360 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004361 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004362
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004363 ToUsingValue->setAccess(D->getAccess());
4364 ToUsingValue->setLexicalDeclContext(LexicalDC);
4365 LexicalDC->addDeclInternal(ToUsingValue);
4366
4367 return ToUsingValue;
4368}
4369
Balazs Keri3b30d652018-10-19 13:32:20 +00004370ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004371 UnresolvedUsingTypenameDecl *D) {
4372 DeclContext *DC, *LexicalDC;
4373 DeclarationName Name;
4374 SourceLocation Loc;
4375 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004376 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4377 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004378 if (ToD)
4379 return ToD;
4380
Balazs Keri3b30d652018-10-19 13:32:20 +00004381 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4382 NestedNameSpecifierLoc ToQualifierLoc;
4383 if (auto Imp = importSeq(
4384 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4385 D->getEllipsisLoc()))
4386 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4387 else
4388 return Imp.takeError();
4389
Gabor Marton26f72a92018-07-12 09:42:05 +00004390 UnresolvedUsingTypenameDecl *ToUsing;
4391 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004392 ToUsingLoc, ToTypenameLoc,
4393 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004394 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004395
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004396 ToUsing->setAccess(D->getAccess());
4397 ToUsing->setLexicalDeclContext(LexicalDC);
4398 LexicalDC->addDeclInternal(ToUsing);
4399
4400 return ToUsing;
4401}
4402
Balazs Keri3b30d652018-10-19 13:32:20 +00004403
4404Error ASTNodeImporter::ImportDefinition(
4405 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004406 if (To->getDefinition()) {
4407 // Check consistency of superclass.
4408 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4409 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004410 if (auto FromSuperOrErr = import(FromSuper))
4411 FromSuper = *FromSuperOrErr;
4412 else
4413 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004414 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004415
4416 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004417 if ((bool)FromSuper != (bool)ToSuper ||
4418 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004419 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004420 diag::err_odr_objc_superclass_inconsistent)
4421 << To->getDeclName();
4422 if (ToSuper)
4423 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4424 << To->getSuperClass()->getDeclName();
4425 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004426 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004427 diag::note_odr_objc_missing_superclass);
4428 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004429 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004430 diag::note_odr_objc_superclass)
4431 << From->getSuperClass()->getDeclName();
4432 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004433 Importer.FromDiag(From->getLocation(),
4434 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004435 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004436
Douglas Gregor2e15c842012-02-01 21:00:38 +00004437 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004438 if (Error Err = ImportDeclContext(From))
4439 return Err;
4440 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004441 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004442
Douglas Gregor2aa53772012-01-24 17:42:07 +00004443 // Start the definition.
4444 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004445
Douglas Gregor2aa53772012-01-24 17:42:07 +00004446 // If this class has a superclass, import it.
4447 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004448 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4449 To->setSuperClass(*SuperTInfoOrErr);
4450 else
4451 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004452 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004453
Douglas Gregor2aa53772012-01-24 17:42:07 +00004454 // Import protocols
4455 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4456 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004457 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4458 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004459
Douglas Gregor2aa53772012-01-24 17:42:07 +00004460 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4461 FromProtoEnd = From->protocol_end();
4462 FromProto != FromProtoEnd;
4463 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004464 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4465 Protocols.push_back(*ToProtoOrErr);
4466 else
4467 return ToProtoOrErr.takeError();
4468
4469 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4470 ProtocolLocs.push_back(*ToProtoLocOrErr);
4471 else
4472 return ToProtoLocOrErr.takeError();
4473
Douglas Gregor2aa53772012-01-24 17:42:07 +00004474 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004475
Douglas Gregor2aa53772012-01-24 17:42:07 +00004476 // FIXME: If we're merging, make sure that the protocol list is the same.
4477 To->setProtocolList(Protocols.data(), Protocols.size(),
4478 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004479
Douglas Gregor2aa53772012-01-24 17:42:07 +00004480 // Import categories. When the categories themselves are imported, they'll
4481 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004482 for (auto *Cat : From->known_categories()) {
4483 auto ToCatOrErr = import(Cat);
4484 if (!ToCatOrErr)
4485 return ToCatOrErr.takeError();
4486 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004487
Douglas Gregor2aa53772012-01-24 17:42:07 +00004488 // If we have an @implementation, import it as well.
4489 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004490 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4491 import(From->getImplementation()))
4492 To->setImplementation(*ToImplOrErr);
4493 else
4494 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004495 }
4496
Douglas Gregor2e15c842012-02-01 21:00:38 +00004497 if (shouldForceImportDeclContext(Kind)) {
4498 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004499 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4500 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004501 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004502 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004503}
4504
Balazs Keri3b30d652018-10-19 13:32:20 +00004505Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004506ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4507 if (!list)
4508 return nullptr;
4509
4510 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004511 for (auto *fromTypeParam : *list) {
4512 if (auto toTypeParamOrErr = import(fromTypeParam))
4513 toTypeParams.push_back(*toTypeParamOrErr);
4514 else
4515 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004516 }
4517
Balazs Keri3b30d652018-10-19 13:32:20 +00004518 auto LAngleLocOrErr = import(list->getLAngleLoc());
4519 if (!LAngleLocOrErr)
4520 return LAngleLocOrErr.takeError();
4521
4522 auto RAngleLocOrErr = import(list->getRAngleLoc());
4523 if (!RAngleLocOrErr)
4524 return RAngleLocOrErr.takeError();
4525
Douglas Gregor85f3f952015-07-07 03:57:15 +00004526 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004527 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004528 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004529 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004530}
4531
Balazs Keri3b30d652018-10-19 13:32:20 +00004532ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004533 // If this class has a definition in the translation unit we're coming from,
4534 // but this particular declaration is not that definition, import the
4535 // definition and map to that.
4536 ObjCInterfaceDecl *Definition = D->getDefinition();
4537 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004538 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4539 return Importer.MapImported(D, *ImportedDefOrErr);
4540 else
4541 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004542 }
4543
Douglas Gregor45635322010-02-16 01:20:57 +00004544 // Import the major distinguishing characteristics of an @interface.
4545 DeclContext *DC, *LexicalDC;
4546 DeclarationName Name;
4547 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004548 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004549 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4550 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004551 if (ToD)
4552 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004553
Douglas Gregor2aa53772012-01-24 17:42:07 +00004554 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004555 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004556 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004557 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004558 for (auto *FoundDecl : FoundDecls) {
4559 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004560 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004561
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004562 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004563 break;
4564 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004565
Douglas Gregor2aa53772012-01-24 17:42:07 +00004566 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004567 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004568 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004569 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4570 if (!AtBeginLocOrErr)
4571 return AtBeginLocOrErr.takeError();
4572
Gabor Marton26f72a92018-07-12 09:42:05 +00004573 if (GetImportedOrCreateDecl(
4574 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004575 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004576 /*TypeParamList=*/nullptr,
4577 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4578 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004579 ToIface->setLexicalDeclContext(LexicalDC);
4580 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004581 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004582 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004583 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004584 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004585 if (auto ToPListOrErr =
4586 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4587 ToIface->setTypeParamList(*ToPListOrErr);
4588 else
4589 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004590
Balazs Keri3b30d652018-10-19 13:32:20 +00004591 if (D->isThisDeclarationADefinition())
4592 if (Error Err = ImportDefinition(D, ToIface))
4593 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004594
Douglas Gregor98d156a2010-02-17 16:12:00 +00004595 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004596}
4597
Balazs Keri3b30d652018-10-19 13:32:20 +00004598ExpectedDecl
4599ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4600 ObjCCategoryDecl *Category;
4601 if (Error Err = importInto(Category, D->getCategoryDecl()))
4602 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004603
Douglas Gregor4da9d682010-12-07 15:32:12 +00004604 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4605 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004606 DeclContext *DC, *LexicalDC;
4607 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4608 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004609
Balazs Keri3b30d652018-10-19 13:32:20 +00004610 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4611 if (auto Imp = importSeq(
4612 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4613 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4614 else
4615 return Imp.takeError();
4616
Gabor Marton26f72a92018-07-12 09:42:05 +00004617 if (GetImportedOrCreateDecl(
4618 ToImpl, D, Importer.getToContext(), DC,
4619 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004620 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004621 return ToImpl;
4622
Balazs Keri3b30d652018-10-19 13:32:20 +00004623 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004624 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004625 Category->setImplementation(ToImpl);
4626 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004627
Gabor Marton26f72a92018-07-12 09:42:05 +00004628 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004629 if (Error Err = ImportDeclContext(D))
4630 return std::move(Err);
4631
Douglas Gregor4da9d682010-12-07 15:32:12 +00004632 return ToImpl;
4633}
4634
Balazs Keri3b30d652018-10-19 13:32:20 +00004635ExpectedDecl
4636ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004637 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004638 ObjCInterfaceDecl *Iface;
4639 if (Error Err = importInto(Iface, D->getClassInterface()))
4640 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004641
4642 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004643 ObjCInterfaceDecl *Super;
4644 if (Error Err = importInto(Super, D->getSuperClass()))
4645 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004646
4647 ObjCImplementationDecl *Impl = Iface->getImplementation();
4648 if (!Impl) {
4649 // We haven't imported an implementation yet. Create a new @implementation
4650 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004651 DeclContext *DC, *LexicalDC;
4652 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4653 return std::move(Err);
4654
4655 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4656 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4657 if (auto Imp = importSeq(
4658 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4659 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4660 std::tie(
4661 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4662 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4663 else
4664 return Imp.takeError();
4665
Gabor Marton26f72a92018-07-12 09:42:05 +00004666 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004667 DC, Iface, Super,
4668 ToLocation,
4669 ToAtStartLoc,
4670 ToSuperClassLoc,
4671 ToIvarLBraceLoc,
4672 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004673 return Impl;
4674
Balazs Keri3b30d652018-10-19 13:32:20 +00004675 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004676
Douglas Gregorda8025c2010-12-07 01:26:03 +00004677 // Associate the implementation with the class it implements.
4678 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004679 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004680 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004681 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004682
4683 // Verify that the existing @implementation has the same superclass.
4684 if ((Super && !Impl->getSuperClass()) ||
4685 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004686 (Super && Impl->getSuperClass() &&
4687 !declaresSameEntity(Super->getCanonicalDecl(),
4688 Impl->getSuperClass()))) {
4689 Importer.ToDiag(Impl->getLocation(),
4690 diag::err_odr_objc_superclass_inconsistent)
4691 << Iface->getDeclName();
4692 // FIXME: It would be nice to have the location of the superclass
4693 // below.
4694 if (Impl->getSuperClass())
4695 Importer.ToDiag(Impl->getLocation(),
4696 diag::note_odr_objc_superclass)
4697 << Impl->getSuperClass()->getDeclName();
4698 else
4699 Importer.ToDiag(Impl->getLocation(),
4700 diag::note_odr_objc_missing_superclass);
4701 if (D->getSuperClass())
4702 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004703 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004704 << D->getSuperClass()->getDeclName();
4705 else
4706 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004707 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004708
4709 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004710 }
4711 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004712
Douglas Gregorda8025c2010-12-07 01:26:03 +00004713 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004714 if (Error Err = ImportDeclContext(D))
4715 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004716
4717 return Impl;
4718}
4719
Balazs Keri3b30d652018-10-19 13:32:20 +00004720ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004721 // Import the major distinguishing characteristics of an @property.
4722 DeclContext *DC, *LexicalDC;
4723 DeclarationName Name;
4724 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004725 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004726 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4727 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004728 if (ToD)
4729 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004730
4731 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004732 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004733 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004734 for (auto *FoundDecl : FoundDecls) {
4735 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004736 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004737 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004738 FoundProp->getType())) {
4739 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4740 << Name << D->getType() << FoundProp->getType();
4741 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4742 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004743
4744 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004745 }
4746
4747 // FIXME: Check property attributes, getters, setters, etc.?
4748
4749 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004750 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004751 return FoundProp;
4752 }
4753 }
4754
Balazs Keri3b30d652018-10-19 13:32:20 +00004755 QualType ToType;
4756 TypeSourceInfo *ToTypeSourceInfo;
4757 SourceLocation ToAtLoc, ToLParenLoc;
4758 if (auto Imp = importSeq(
4759 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4760 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4761 else
4762 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004763
4764 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004765 ObjCPropertyDecl *ToProperty;
4766 if (GetImportedOrCreateDecl(
4767 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004768 Name.getAsIdentifierInfo(), ToAtLoc,
4769 ToLParenLoc, ToType,
4770 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004771 return ToProperty;
4772
Balazs Keri3b30d652018-10-19 13:32:20 +00004773 Selector ToGetterName, ToSetterName;
4774 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4775 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4776 ObjCIvarDecl *ToPropertyIvarDecl;
4777 if (auto Imp = importSeq(
4778 D->getGetterName(), D->getSetterName(),
4779 D->getGetterNameLoc(), D->getSetterNameLoc(),
4780 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4781 D->getPropertyIvarDecl()))
4782 std::tie(
4783 ToGetterName, ToSetterName,
4784 ToGetterNameLoc, ToSetterNameLoc,
4785 ToGetterMethodDecl, ToSetterMethodDecl,
4786 ToPropertyIvarDecl) = *Imp;
4787 else
4788 return Imp.takeError();
4789
Douglas Gregora11c4582010-02-17 18:02:10 +00004790 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004791 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004792
4793 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004794 ToProperty->setPropertyAttributesAsWritten(
4795 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004796 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4797 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4798 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4799 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4800 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004801 return ToProperty;
4802}
4803
Balazs Keri3b30d652018-10-19 13:32:20 +00004804ExpectedDecl
4805ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4806 ObjCPropertyDecl *Property;
4807 if (Error Err = importInto(Property, D->getPropertyDecl()))
4808 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004809
Balazs Keri3b30d652018-10-19 13:32:20 +00004810 DeclContext *DC, *LexicalDC;
4811 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4812 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004813
Balazs Keri3b30d652018-10-19 13:32:20 +00004814 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004815
4816 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004817 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004818 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4819 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004820
4821 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004822 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4823 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004824 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004825 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4826 if (auto Imp = importSeq(
4827 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4828 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4829 else
4830 return Imp.takeError();
4831
Gabor Marton26f72a92018-07-12 09:42:05 +00004832 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004833 ToBeginLoc,
4834 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004835 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004836 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004837 return ToImpl;
4838
Douglas Gregor14a49e22010-12-07 18:32:03 +00004839 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004840 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004841 } else {
4842 // Check that we have the same kind of property implementation (@synthesize
4843 // vs. @dynamic).
4844 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004845 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004846 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004847 << Property->getDeclName()
4848 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004849 == ObjCPropertyImplDecl::Dynamic);
4850 Importer.FromDiag(D->getLocation(),
4851 diag::note_odr_objc_property_impl_kind)
4852 << D->getPropertyDecl()->getDeclName()
4853 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004854
4855 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004856 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004857
4858 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004859 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4860 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004861 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004862 diag::err_odr_objc_synthesize_ivar_inconsistent)
4863 << Property->getDeclName()
4864 << ToImpl->getPropertyIvarDecl()->getDeclName()
4865 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004866 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004867 diag::note_odr_objc_synthesize_ivar_here)
4868 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004869
4870 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004871 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004872
Douglas Gregor14a49e22010-12-07 18:32:03 +00004873 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004874 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004875 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004876
Douglas Gregor14a49e22010-12-07 18:32:03 +00004877 return ToImpl;
4878}
4879
Balazs Keri3b30d652018-10-19 13:32:20 +00004880ExpectedDecl
4881ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004882 // For template arguments, we adopt the translation unit as our declaration
4883 // context. This context will be fixed when the actual template declaration
4884 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004885
Douglas Gregora082a492010-11-30 19:14:50 +00004886 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004887
4888 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4889 if (!BeginLocOrErr)
4890 return BeginLocOrErr.takeError();
4891
4892 ExpectedSLoc LocationOrErr = import(D->getLocation());
4893 if (!LocationOrErr)
4894 return LocationOrErr.takeError();
4895
Gabor Marton26f72a92018-07-12 09:42:05 +00004896 TemplateTypeParmDecl *ToD = nullptr;
4897 (void)GetImportedOrCreateDecl(
4898 ToD, D, Importer.getToContext(),
4899 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004900 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004901 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4902 D->wasDeclaredWithTypename(), D->isParameterPack());
4903 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004904}
4905
Balazs Keri3b30d652018-10-19 13:32:20 +00004906ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004907ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004908 DeclarationName ToDeclName;
4909 SourceLocation ToLocation, ToInnerLocStart;
4910 QualType ToType;
4911 TypeSourceInfo *ToTypeSourceInfo;
4912 if (auto Imp = importSeq(
4913 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4914 D->getInnerLocStart()))
4915 std::tie(
4916 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4917 ToInnerLocStart) = *Imp;
4918 else
4919 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004920
Douglas Gregora082a492010-11-30 19:14:50 +00004921 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004922
4923 NonTypeTemplateParmDecl *ToD = nullptr;
4924 (void)GetImportedOrCreateDecl(
4925 ToD, D, Importer.getToContext(),
4926 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004927 ToInnerLocStart, ToLocation, D->getDepth(),
4928 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4929 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004930 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004931}
4932
Balazs Keri3b30d652018-10-19 13:32:20 +00004933ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004934ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4935 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004936 auto NameOrErr = import(D->getDeclName());
4937 if (!NameOrErr)
4938 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004939
Douglas Gregora082a492010-11-30 19:14:50 +00004940 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004941 ExpectedSLoc LocationOrErr = import(D->getLocation());
4942 if (!LocationOrErr)
4943 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004944
Douglas Gregora082a492010-11-30 19:14:50 +00004945 // Import template parameters.
Balazs Keri3b30d652018-10-19 13:32:20 +00004946 auto TemplateParamsOrErr = ImportTemplateParameterList(
4947 D->getTemplateParameters());
4948 if (!TemplateParamsOrErr)
4949 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004950
Douglas Gregora082a492010-11-30 19:14:50 +00004951 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004952
4953 TemplateTemplateParmDecl *ToD = nullptr;
4954 (void)GetImportedOrCreateDecl(
4955 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004956 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4957 D->getDepth(), D->getPosition(), D->isParameterPack(),
4958 (*NameOrErr).getAsIdentifierInfo(),
4959 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004960 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004961}
4962
Gabor Marton9581c332018-05-23 13:53:36 +00004963// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4964// it has any definition in the redecl chain.
4965static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4966 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4967 if (!ToTemplatedDef)
4968 return nullptr;
4969 ClassTemplateDecl *TemplateWithDef =
4970 ToTemplatedDef->getDescribedClassTemplate();
4971 return TemplateWithDef;
4972}
4973
Balazs Keri3b30d652018-10-19 13:32:20 +00004974ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004975 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4976
Douglas Gregora082a492010-11-30 19:14:50 +00004977 // If this record has a definition in the translation unit we're coming from,
4978 // but this particular declaration is not that definition, import the
4979 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004980 auto *Definition =
4981 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Balazs Keri0c23dc52018-08-13 13:08:37 +00004982 if (Definition && Definition != D->getTemplatedDecl() && !IsFriend) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004983 if (ExpectedDecl ImportedDefOrErr = import(
4984 Definition->getDescribedClassTemplate()))
4985 return Importer.MapImported(D, *ImportedDefOrErr);
4986 else
4987 return ImportedDefOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00004988 }
Gabor Marton9581c332018-05-23 13:53:36 +00004989
Douglas Gregora082a492010-11-30 19:14:50 +00004990 // Import the major distinguishing characteristics of this class template.
4991 DeclContext *DC, *LexicalDC;
4992 DeclarationName Name;
4993 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004994 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004995 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4996 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004997 if (ToD)
4998 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004999
Douglas Gregora082a492010-11-30 19:14:50 +00005000 // We may already have a template of the same name; try to find and match it.
5001 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005002 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005003 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005004 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005005 for (auto *FoundDecl : FoundDecls) {
5006 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00005007 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005008
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005009 Decl *Found = FoundDecl;
5010 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00005011
5012 // The class to be imported is a definition.
5013 if (D->isThisDeclarationADefinition()) {
5014 // Lookup will find the fwd decl only if that is more recent than the
5015 // definition. So, try to get the definition if that is available in
5016 // the redecl chain.
5017 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005018 if (TemplateWithDef)
5019 FoundTemplate = TemplateWithDef;
5020 else
Gabor Marton9581c332018-05-23 13:53:36 +00005021 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005022 }
5023
Douglas Gregora082a492010-11-30 19:14:50 +00005024 if (IsStructuralMatch(D, FoundTemplate)) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005025 if (!IsFriend) {
5026 Importer.MapImported(D->getTemplatedDecl(),
5027 FoundTemplate->getTemplatedDecl());
5028 return Importer.MapImported(D, FoundTemplate);
5029 }
Aleksei Sidorin761c2242018-05-15 11:09:07 +00005030
Balazs Keri0c23dc52018-08-13 13:08:37 +00005031 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005032 }
Douglas Gregora082a492010-11-30 19:14:50 +00005033 }
Gabor Marton9581c332018-05-23 13:53:36 +00005034
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005035 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005036 }
Gabor Marton9581c332018-05-23 13:53:36 +00005037
Douglas Gregora082a492010-11-30 19:14:50 +00005038 if (!ConflictingDecls.empty()) {
5039 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005040 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005041 ConflictingDecls.size());
5042 }
Gabor Marton9581c332018-05-23 13:53:36 +00005043
Douglas Gregora082a492010-11-30 19:14:50 +00005044 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005045 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005046 }
5047
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005048 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5049
Douglas Gregora082a492010-11-30 19:14:50 +00005050 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005051 CXXRecordDecl *ToTemplated;
5052 if (Error Err = importInto(ToTemplated, FromTemplated))
5053 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005054
Douglas Gregora082a492010-11-30 19:14:50 +00005055 // Create the class template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005056 auto TemplateParamsOrErr = ImportTemplateParameterList(
5057 D->getTemplateParameters());
5058 if (!TemplateParamsOrErr)
5059 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005060
Gabor Marton26f72a92018-07-12 09:42:05 +00005061 ClassTemplateDecl *D2;
5062 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005063 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005064 return D2;
5065
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005066 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005067
Balazs Keri0c23dc52018-08-13 13:08:37 +00005068 if (ToTemplated->getPreviousDecl()) {
5069 assert(
5070 ToTemplated->getPreviousDecl()->getDescribedClassTemplate() &&
5071 "Missing described template");
5072 D2->setPreviousDecl(
5073 ToTemplated->getPreviousDecl()->getDescribedClassTemplate());
5074 }
Douglas Gregora082a492010-11-30 19:14:50 +00005075 D2->setAccess(D->getAccess());
5076 D2->setLexicalDeclContext(LexicalDC);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005077 if (!IsFriend)
5078 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005079
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005080 if (FromTemplated->isCompleteDefinition() &&
5081 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005082 // FIXME: Import definition!
5083 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005084
Douglas Gregora082a492010-11-30 19:14:50 +00005085 return D2;
5086}
5087
Balazs Keri3b30d652018-10-19 13:32:20 +00005088ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005089 ClassTemplateSpecializationDecl *D) {
5090 // If this record has a definition in the translation unit we're coming from,
5091 // but this particular declaration is not that definition, import the
5092 // definition and map to that.
5093 TagDecl *Definition = D->getDefinition();
5094 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005095 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5096 return Importer.MapImported(D, *ImportedDefOrErr);
5097 else
5098 return ImportedDefOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005099 }
5100
Balazs Keri3b30d652018-10-19 13:32:20 +00005101 ClassTemplateDecl *ClassTemplate;
5102 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5103 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005104
Douglas Gregore2e50d332010-12-01 01:36:18 +00005105 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005106 DeclContext *DC, *LexicalDC;
5107 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5108 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005109
5110 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005111 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005112 if (Error Err = ImportTemplateArguments(
5113 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5114 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005115
Douglas Gregore2e50d332010-12-01 01:36:18 +00005116 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005117 void *InsertPos = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005118 ClassTemplateSpecializationDecl *D2 = nullptr;
5119 ClassTemplatePartialSpecializationDecl *PartialSpec =
5120 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5121 if (PartialSpec)
5122 D2 = ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
5123 else
5124 D2 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
5125 ClassTemplateSpecializationDecl * const PrevDecl = D2;
5126 RecordDecl *FoundDef = D2 ? D2->getDefinition() : nullptr;
5127 if (FoundDef) {
5128 if (!D->isCompleteDefinition()) {
5129 // The "From" translation unit only had a forward declaration; call it
5130 // the same declaration.
5131 // TODO Handle the redecl chain properly!
5132 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005133 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005134
5135 if (IsStructuralMatch(D, FoundDef)) {
5136
5137 Importer.MapImported(D, FoundDef);
5138
5139 // Import those those default field initializers which have been
5140 // instantiated in the "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005141 for (auto *FromField : D->fields()) {
5142 auto ToOrErr = import(FromField);
5143 if (!ToOrErr)
5144 // FIXME: return the error?
5145 consumeError(ToOrErr.takeError());
5146 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005147
5148 // Import those methods which have been instantiated in the
5149 // "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005150 for (CXXMethodDecl *FromM : D->methods()) {
5151 auto ToOrErr = import(FromM);
5152 if (!ToOrErr)
5153 // FIXME: return the error?
5154 consumeError(ToOrErr.takeError());
5155 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005156
5157 // TODO Import instantiated default arguments.
5158 // TODO Import instantiated exception specifications.
5159 //
5160 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint what
5161 // else could be fused during an AST merge.
5162
5163 return FoundDef;
5164 }
5165 } else { // We either couldn't find any previous specialization in the "To"
5166 // context, or we found one but without definition. Let's create a
5167 // new specialization and register that at the class template.
Balazs Keri3b30d652018-10-19 13:32:20 +00005168
5169 // Import the location of this declaration.
5170 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5171 if (!BeginLocOrErr)
5172 return BeginLocOrErr.takeError();
5173 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5174 if (!IdLocOrErr)
5175 return IdLocOrErr.takeError();
5176
Gabor Marton42e15de2018-08-22 11:52:14 +00005177 if (PartialSpec) {
5178 // Import TemplateArgumentListInfo.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005179 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005180 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Balazs Keri3b30d652018-10-19 13:32:20 +00005181 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5182 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005183
Balazs Keri3b30d652018-10-19 13:32:20 +00005184 QualType CanonInjType;
5185 if (Error Err = importInto(
5186 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5187 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005188 CanonInjType = CanonInjType.getCanonicalType();
5189
Balazs Keri3b30d652018-10-19 13:32:20 +00005190 auto ToTPListOrErr = ImportTemplateParameterList(
5191 PartialSpec->getTemplateParameters());
5192 if (!ToTPListOrErr)
5193 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005194
Gabor Marton26f72a92018-07-12 09:42:05 +00005195 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00005196 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5197 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
Gabor Marton26f72a92018-07-12 09:42:05 +00005198 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
Gabor Marton42e15de2018-08-22 11:52:14 +00005199 ToTAInfo, CanonInjType,
5200 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
Gabor Marton26f72a92018-07-12 09:42:05 +00005201 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005202
Gabor Marton42e15de2018-08-22 11:52:14 +00005203 // Update InsertPos, because preceding import calls may have invalidated
5204 // it by adding new specializations.
5205 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5206 // Add this partial specialization to the class template.
5207 ClassTemplate->AddPartialSpecialization(
5208 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
5209
5210 } else { // Not a partial specialization.
Gabor Marton26f72a92018-07-12 09:42:05 +00005211 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00005212 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5213 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5214 PrevDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00005215 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005216
5217 // Update InsertPos, because preceding import calls may have invalidated
5218 // it by adding new specializations.
5219 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5220 // Add this specialization to the class template.
5221 ClassTemplate->AddSpecialization(D2, InsertPos);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005222 }
5223
Douglas Gregore2e50d332010-12-01 01:36:18 +00005224 D2->setSpecializationKind(D->getSpecializationKind());
5225
Douglas Gregore2e50d332010-12-01 01:36:18 +00005226 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005227 if (auto LocOrErr = import(D->getQualifierLoc()))
5228 D2->setQualifierInfo(*LocOrErr);
5229 else
5230 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005231
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005232 if (auto *TSI = D->getTypeAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005233 if (auto TInfoOrErr = import(TSI))
5234 D2->setTypeAsWritten(*TInfoOrErr);
5235 else
5236 return TInfoOrErr.takeError();
5237
5238 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5239 D2->setTemplateKeywordLoc(*LocOrErr);
5240 else
5241 return LocOrErr.takeError();
5242
5243 if (auto LocOrErr = import(D->getExternLoc()))
5244 D2->setExternLoc(*LocOrErr);
5245 else
5246 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005247 }
5248
Balazs Keri3b30d652018-10-19 13:32:20 +00005249 if (D->getPointOfInstantiation().isValid()) {
5250 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5251 D2->setPointOfInstantiation(*POIOrErr);
5252 else
5253 return POIOrErr.takeError();
5254 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005255
5256 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5257
Gabor Martonb14056b2018-05-25 11:21:24 +00005258 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00005259 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00005260
5261 // Add to the DC only if it was an explicit specialization/instantiation.
5262 if (D2->isExplicitInstantiationOrSpecialization()) {
5263 LexicalDC->addDeclInternal(D2);
5264 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005265 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005266 if (D->isCompleteDefinition())
5267 if (Error Err = ImportDefinition(D, D2))
5268 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005269
Douglas Gregore2e50d332010-12-01 01:36:18 +00005270 return D2;
5271}
5272
Balazs Keri3b30d652018-10-19 13:32:20 +00005273ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005274 // If this variable has a definition in the translation unit we're coming
5275 // from,
5276 // but this particular declaration is not that definition, import the
5277 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005278 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005279 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5280 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005281 if (ExpectedDecl ImportedDefOrErr = import(
5282 Definition->getDescribedVarTemplate()))
5283 return Importer.MapImported(D, *ImportedDefOrErr);
5284 else
5285 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005286 }
5287
5288 // Import the major distinguishing characteristics of this variable template.
5289 DeclContext *DC, *LexicalDC;
5290 DeclarationName Name;
5291 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005292 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005293 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5294 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005295 if (ToD)
5296 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005297
5298 // We may already have a template of the same name; try to find and match it.
5299 assert(!DC->isFunctionOrMethod() &&
5300 "Variable templates cannot be declared at function scope");
5301 SmallVector<NamedDecl *, 4> ConflictingDecls;
5302 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005303 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005304 for (auto *FoundDecl : FoundDecls) {
5305 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005306 continue;
5307
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005308 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005309 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005310 if (IsStructuralMatch(D, FoundTemplate)) {
5311 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005312 Importer.MapImported(D->getTemplatedDecl(),
5313 FoundTemplate->getTemplatedDecl());
5314 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005315 }
5316 }
5317
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005318 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005319 }
5320
5321 if (!ConflictingDecls.empty()) {
5322 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5323 ConflictingDecls.data(),
5324 ConflictingDecls.size());
5325 }
5326
5327 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005328 // FIXME: Is it possible to get other error than name conflict?
5329 // (Put this `if` into the previous `if`?)
5330 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005331
5332 VarDecl *DTemplated = D->getTemplatedDecl();
5333
5334 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005335 // FIXME: Value not used?
5336 ExpectedType TypeOrErr = import(DTemplated->getType());
5337 if (!TypeOrErr)
5338 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005339
5340 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005341 VarDecl *ToTemplated;
5342 if (Error Err = importInto(ToTemplated, DTemplated))
5343 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005344
5345 // Create the variable template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005346 auto TemplateParamsOrErr = ImportTemplateParameterList(
5347 D->getTemplateParameters());
5348 if (!TemplateParamsOrErr)
5349 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005350
Gabor Marton26f72a92018-07-12 09:42:05 +00005351 VarTemplateDecl *ToVarTD;
5352 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005353 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005354 return ToVarTD;
5355
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005356 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005357
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005358 ToVarTD->setAccess(D->getAccess());
5359 ToVarTD->setLexicalDeclContext(LexicalDC);
5360 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005361
Larisse Voufo39a1e502013-08-06 01:03:05 +00005362 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005363 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005364 // FIXME: Import definition!
5365 }
5366
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005367 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005368}
5369
Balazs Keri3b30d652018-10-19 13:32:20 +00005370ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005371 VarTemplateSpecializationDecl *D) {
5372 // If this record has a definition in the translation unit we're coming from,
5373 // but this particular declaration is not that definition, import the
5374 // definition and map to that.
5375 VarDecl *Definition = D->getDefinition();
5376 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005377 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5378 return Importer.MapImported(D, *ImportedDefOrErr);
5379 else
5380 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005381 }
5382
Balazs Keri3b30d652018-10-19 13:32:20 +00005383 VarTemplateDecl *VarTemplate;
5384 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5385 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005386
5387 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005388 DeclContext *DC, *LexicalDC;
5389 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5390 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005391
5392 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005393 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5394 if (!BeginLocOrErr)
5395 return BeginLocOrErr.takeError();
5396
5397 auto IdLocOrErr = import(D->getLocation());
5398 if (!IdLocOrErr)
5399 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005400
5401 // Import template arguments.
5402 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005403 if (Error Err = ImportTemplateArguments(
5404 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5405 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005406
5407 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005408 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005409 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005410 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005411 if (D2) {
5412 // We already have a variable template specialization with these template
5413 // arguments.
5414
5415 // FIXME: Check for specialization vs. instantiation errors.
5416
5417 if (VarDecl *FoundDef = D2->getDefinition()) {
5418 if (!D->isThisDeclarationADefinition() ||
5419 IsStructuralMatch(D, FoundDef)) {
5420 // The record types structurally match, or the "from" translation
5421 // unit only had a forward declaration anyway; call it the same
5422 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005423 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005424 }
5425 }
5426 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005427 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005428 QualType T;
5429 if (Error Err = importInto(T, D->getType()))
5430 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005431
Balazs Keri3b30d652018-10-19 13:32:20 +00005432 auto TInfoOrErr = import(D->getTypeSourceInfo());
5433 if (!TInfoOrErr)
5434 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005435
5436 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005437 if (Error Err = ImportTemplateArgumentListInfo(
5438 D->getTemplateArgsInfo(), ToTAInfo))
5439 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005440
5441 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005442 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005443 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5444 // Import TemplateArgumentListInfo
5445 TemplateArgumentListInfo ArgInfos;
5446 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5447 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005448 if (Error Err = ImportTemplateArgumentListInfo(
5449 *FromTAArgsAsWritten, ArgInfos))
5450 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005451
Balazs Keri3b30d652018-10-19 13:32:20 +00005452 auto ToTPListOrErr = ImportTemplateParameterList(
5453 FromPartial->getTemplateParameters());
5454 if (!ToTPListOrErr)
5455 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005456
Gabor Marton26f72a92018-07-12 09:42:05 +00005457 PartVarSpecDecl *ToPartial;
5458 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005459 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5460 VarTemplate, T, *TInfoOrErr,
5461 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005462 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005463
Balazs Keri3b30d652018-10-19 13:32:20 +00005464 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5465 FromPartial->getInstantiatedFromMember()))
5466 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5467 else
5468 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005469
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005470 if (FromPartial->isMemberSpecialization())
5471 ToPartial->setMemberSpecialization();
5472
5473 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005474
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005475 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005476 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5477 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5478 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005479 D->getStorageClass(), TemplateArgs))
5480 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005481 }
5482
Balazs Keri3b30d652018-10-19 13:32:20 +00005483 if (D->getPointOfInstantiation().isValid()) {
5484 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5485 D2->setPointOfInstantiation(*POIOrErr);
5486 else
5487 return POIOrErr.takeError();
5488 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005489
Larisse Voufo39a1e502013-08-06 01:03:05 +00005490 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005491 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005492
5493 // Add this specialization to the class template.
5494 VarTemplate->AddSpecialization(D2, InsertPos);
5495
5496 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005497 if (auto LocOrErr = import(D->getQualifierLoc()))
5498 D2->setQualifierInfo(*LocOrErr);
5499 else
5500 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005501
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005502 if (D->isConstexpr())
5503 D2->setConstexpr(true);
5504
Larisse Voufo39a1e502013-08-06 01:03:05 +00005505 // Add the specialization to this context.
5506 D2->setLexicalDeclContext(LexicalDC);
5507 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005508
5509 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005510 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005511
Balazs Keri3b30d652018-10-19 13:32:20 +00005512 if (Error Err = ImportInitializer(D, D2))
5513 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005514
5515 return D2;
5516}
5517
Balazs Keri3b30d652018-10-19 13:32:20 +00005518ExpectedDecl
5519ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005520 DeclContext *DC, *LexicalDC;
5521 DeclarationName Name;
5522 SourceLocation Loc;
5523 NamedDecl *ToD;
5524
Balazs Keri3b30d652018-10-19 13:32:20 +00005525 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5526 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005527
5528 if (ToD)
5529 return ToD;
5530
5531 // Try to find a function in our own ("to") context with the same name, same
5532 // type, and in the same context as the function we're importing.
5533 if (!LexicalDC->isFunctionOrMethod()) {
5534 unsigned IDNS = Decl::IDNS_Ordinary;
5535 SmallVector<NamedDecl *, 2> FoundDecls;
5536 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005537 for (auto *FoundDecl : FoundDecls) {
5538 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005539 continue;
5540
Balazs Keri3b30d652018-10-19 13:32:20 +00005541 if (auto *FoundFunction =
5542 dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005543 if (FoundFunction->hasExternalFormalLinkage() &&
5544 D->hasExternalFormalLinkage()) {
5545 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00005546 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005547 // FIXME: Actually try to merge the body and other attributes.
5548 return FoundFunction;
5549 }
5550 }
5551 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005552 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005553 }
5554 }
5555
Balazs Keri3b30d652018-10-19 13:32:20 +00005556 auto ParamsOrErr = ImportTemplateParameterList(
5557 D->getTemplateParameters());
5558 if (!ParamsOrErr)
5559 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005560
Balazs Keri3b30d652018-10-19 13:32:20 +00005561 FunctionDecl *TemplatedFD;
5562 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5563 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005564
Gabor Marton26f72a92018-07-12 09:42:05 +00005565 FunctionTemplateDecl *ToFunc;
5566 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005567 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005568 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005569
5570 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
5571 ToFunc->setAccess(D->getAccess());
5572 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005573
5574 LexicalDC->addDeclInternal(ToFunc);
5575 return ToFunc;
5576}
5577
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005578//----------------------------------------------------------------------------
5579// Import Statements
5580//----------------------------------------------------------------------------
5581
Balazs Keri3b30d652018-10-19 13:32:20 +00005582ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005583 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5584 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005585 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005586}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005587
Balazs Keri3b30d652018-10-19 13:32:20 +00005588
5589ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005590 SmallVector<IdentifierInfo *, 4> Names;
5591 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5592 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005593 // ToII is nullptr when no symbolic name is given for output operand
5594 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005595 Names.push_back(ToII);
5596 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005597
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005598 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5599 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005600 // ToII is nullptr when no symbolic name is given for input operand
5601 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005602 Names.push_back(ToII);
5603 }
5604
5605 SmallVector<StringLiteral *, 4> Clobbers;
5606 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005607 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5608 Clobbers.push_back(*ClobberOrErr);
5609 else
5610 return ClobberOrErr.takeError();
5611
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005612 }
5613
5614 SmallVector<StringLiteral *, 4> Constraints;
5615 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005616 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5617 Constraints.push_back(*OutputOrErr);
5618 else
5619 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005620 }
5621
5622 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005623 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5624 Constraints.push_back(*InputOrErr);
5625 else
5626 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005627 }
5628
5629 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005630 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5631 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005632
Balazs Keri3b30d652018-10-19 13:32:20 +00005633 if (Error Err = ImportArrayChecked(
5634 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5635 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005636
Balazs Keri3b30d652018-10-19 13:32:20 +00005637 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5638 if (!AsmLocOrErr)
5639 return AsmLocOrErr.takeError();
5640 auto AsmStrOrErr = import(S->getAsmString());
5641 if (!AsmStrOrErr)
5642 return AsmStrOrErr.takeError();
5643 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5644 if (!RParenLocOrErr)
5645 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005646
5647 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005648 Importer.getToContext(),
5649 *AsmLocOrErr,
5650 S->isSimple(),
5651 S->isVolatile(),
5652 S->getNumOutputs(),
5653 S->getNumInputs(),
5654 Names.data(),
5655 Constraints.data(),
5656 Exprs.data(),
5657 *AsmStrOrErr,
5658 S->getNumClobbers(),
5659 Clobbers.data(),
5660 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005661}
5662
Balazs Keri3b30d652018-10-19 13:32:20 +00005663ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5664 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5665 if (!Imp)
5666 return Imp.takeError();
5667
5668 DeclGroupRef ToDG;
5669 SourceLocation ToBeginLoc, ToEndLoc;
5670 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5671
5672 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005673}
5674
Balazs Keri3b30d652018-10-19 13:32:20 +00005675ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5676 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5677 if (!ToSemiLocOrErr)
5678 return ToSemiLocOrErr.takeError();
5679 return new (Importer.getToContext()) NullStmt(
5680 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005681}
5682
Balazs Keri3b30d652018-10-19 13:32:20 +00005683ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005684 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005685
Balazs Keri3b30d652018-10-19 13:32:20 +00005686 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5687 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005688
Balazs Keri3b30d652018-10-19 13:32:20 +00005689 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5690 if (!ToLBracLocOrErr)
5691 return ToLBracLocOrErr.takeError();
5692
5693 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5694 if (!ToRBracLocOrErr)
5695 return ToRBracLocOrErr.takeError();
5696
5697 return CompoundStmt::Create(
5698 Importer.getToContext(), ToStmts,
5699 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005700}
5701
Balazs Keri3b30d652018-10-19 13:32:20 +00005702ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5703 auto Imp = importSeq(
5704 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5705 S->getEllipsisLoc(), S->getColonLoc());
5706 if (!Imp)
5707 return Imp.takeError();
5708
5709 Expr *ToLHS, *ToRHS;
5710 Stmt *ToSubStmt;
5711 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5712 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5713 *Imp;
5714
Bruno Ricci5b30571752018-10-28 12:30:53 +00005715 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5716 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005717 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005718
Gabor Horvath480892b2017-10-18 09:25:18 +00005719 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005720}
5721
Balazs Keri3b30d652018-10-19 13:32:20 +00005722ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5723 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5724 if (!Imp)
5725 return Imp.takeError();
5726
5727 SourceLocation ToDefaultLoc, ToColonLoc;
5728 Stmt *ToSubStmt;
5729 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5730
5731 return new (Importer.getToContext()) DefaultStmt(
5732 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005733}
5734
Balazs Keri3b30d652018-10-19 13:32:20 +00005735ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5736 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5737 if (!Imp)
5738 return Imp.takeError();
5739
5740 SourceLocation ToIdentLoc;
5741 LabelDecl *ToLabelDecl;
5742 Stmt *ToSubStmt;
5743 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5744
5745 return new (Importer.getToContext()) LabelStmt(
5746 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005747}
5748
Balazs Keri3b30d652018-10-19 13:32:20 +00005749ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5750 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5751 if (!ToAttrLocOrErr)
5752 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005753 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5754 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005755 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5756 return std::move(Err);
5757 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5758 if (!ToSubStmtOrErr)
5759 return ToSubStmtOrErr.takeError();
5760
5761 return AttributedStmt::Create(
5762 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005763}
5764
Balazs Keri3b30d652018-10-19 13:32:20 +00005765ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5766 auto Imp = importSeq(
5767 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5768 S->getThen(), S->getElseLoc(), S->getElse());
5769 if (!Imp)
5770 return Imp.takeError();
5771
5772 SourceLocation ToIfLoc, ToElseLoc;
5773 Stmt *ToInit, *ToThen, *ToElse;
5774 VarDecl *ToConditionVariable;
5775 Expr *ToCond;
5776 std::tie(
5777 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5778 *Imp;
5779
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005780 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5781 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5782 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005783}
5784
Balazs Keri3b30d652018-10-19 13:32:20 +00005785ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5786 auto Imp = importSeq(
5787 S->getInit(), S->getConditionVariable(), S->getCond(),
5788 S->getBody(), S->getSwitchLoc());
5789 if (!Imp)
5790 return Imp.takeError();
5791
5792 Stmt *ToInit, *ToBody;
5793 VarDecl *ToConditionVariable;
5794 Expr *ToCond;
5795 SourceLocation ToSwitchLoc;
5796 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5797
Bruno Riccie2806f82018-10-29 16:12:37 +00005798 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5799 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005800 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005801 ToStmt->setSwitchLoc(ToSwitchLoc);
5802
Sean Callanan59721b32015-04-28 18:41:46 +00005803 // Now we have to re-chain the cases.
5804 SwitchCase *LastChainedSwitchCase = nullptr;
5805 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5806 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005807 Expected<SwitchCase *> ToSCOrErr = import(SC);
5808 if (!ToSCOrErr)
5809 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005810 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005811 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005812 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005813 ToStmt->setSwitchCaseList(*ToSCOrErr);
5814 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005815 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005816
Sean Callanan59721b32015-04-28 18:41:46 +00005817 return ToStmt;
5818}
5819
Balazs Keri3b30d652018-10-19 13:32:20 +00005820ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5821 auto Imp = importSeq(
5822 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5823 if (!Imp)
5824 return Imp.takeError();
5825
5826 VarDecl *ToConditionVariable;
5827 Expr *ToCond;
5828 Stmt *ToBody;
5829 SourceLocation ToWhileLoc;
5830 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5831
Bruno Riccibacf7512018-10-30 13:42:41 +00005832 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5833 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005834}
5835
Balazs Keri3b30d652018-10-19 13:32:20 +00005836ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5837 auto Imp = importSeq(
5838 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5839 S->getRParenLoc());
5840 if (!Imp)
5841 return Imp.takeError();
5842
5843 Stmt *ToBody;
5844 Expr *ToCond;
5845 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5846 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5847
5848 return new (Importer.getToContext()) DoStmt(
5849 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005850}
5851
Balazs Keri3b30d652018-10-19 13:32:20 +00005852ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5853 auto Imp = importSeq(
5854 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5855 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5856 if (!Imp)
5857 return Imp.takeError();
5858
5859 Stmt *ToInit;
5860 Expr *ToCond, *ToInc;
5861 VarDecl *ToConditionVariable;
5862 Stmt *ToBody;
5863 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5864 std::tie(
5865 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5866 ToLParenLoc, ToRParenLoc) = *Imp;
5867
5868 return new (Importer.getToContext()) ForStmt(
5869 Importer.getToContext(),
5870 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5871 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005872}
5873
Balazs Keri3b30d652018-10-19 13:32:20 +00005874ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5875 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5876 if (!Imp)
5877 return Imp.takeError();
5878
5879 LabelDecl *ToLabel;
5880 SourceLocation ToGotoLoc, ToLabelLoc;
5881 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5882
5883 return new (Importer.getToContext()) GotoStmt(
5884 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005885}
5886
Balazs Keri3b30d652018-10-19 13:32:20 +00005887ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5888 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5889 if (!Imp)
5890 return Imp.takeError();
5891
5892 SourceLocation ToGotoLoc, ToStarLoc;
5893 Expr *ToTarget;
5894 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5895
5896 return new (Importer.getToContext()) IndirectGotoStmt(
5897 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005898}
5899
Balazs Keri3b30d652018-10-19 13:32:20 +00005900ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5901 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5902 if (!ToContinueLocOrErr)
5903 return ToContinueLocOrErr.takeError();
5904 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005905}
5906
Balazs Keri3b30d652018-10-19 13:32:20 +00005907ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5908 auto ToBreakLocOrErr = import(S->getBreakLoc());
5909 if (!ToBreakLocOrErr)
5910 return ToBreakLocOrErr.takeError();
5911 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005912}
5913
Balazs Keri3b30d652018-10-19 13:32:20 +00005914ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5915 auto Imp = importSeq(
5916 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5917 if (!Imp)
5918 return Imp.takeError();
5919
5920 SourceLocation ToReturnLoc;
5921 Expr *ToRetValue;
5922 const VarDecl *ToNRVOCandidate;
5923 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5924
Bruno Ricci023b1d12018-10-30 14:40:49 +00005925 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5926 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005927}
5928
Balazs Keri3b30d652018-10-19 13:32:20 +00005929ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5930 auto Imp = importSeq(
5931 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5932 if (!Imp)
5933 return Imp.takeError();
5934
5935 SourceLocation ToCatchLoc;
5936 VarDecl *ToExceptionDecl;
5937 Stmt *ToHandlerBlock;
5938 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5939
5940 return new (Importer.getToContext()) CXXCatchStmt (
5941 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005942}
5943
Balazs Keri3b30d652018-10-19 13:32:20 +00005944ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5945 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5946 if (!ToTryLocOrErr)
5947 return ToTryLocOrErr.takeError();
5948
5949 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5950 if (!ToTryBlockOrErr)
5951 return ToTryBlockOrErr.takeError();
5952
Sean Callanan59721b32015-04-28 18:41:46 +00005953 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5954 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5955 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005956 if (auto ToHandlerOrErr = import(FromHandler))
5957 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005958 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005959 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005960 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005961
5962 return CXXTryStmt::Create(
5963 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005964}
5965
Balazs Keri3b30d652018-10-19 13:32:20 +00005966ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5967 auto Imp1 = importSeq(
5968 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5969 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5970 if (!Imp1)
5971 return Imp1.takeError();
5972 auto Imp2 = importSeq(
5973 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5974 if (!Imp2)
5975 return Imp2.takeError();
5976
5977 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5978 Expr *ToCond, *ToInc;
5979 Stmt *ToInit, *ToBody;
5980 std::tie(
5981 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5982 ToBody) = *Imp1;
5983 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5984 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5985
5986 return new (Importer.getToContext()) CXXForRangeStmt(
5987 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5988 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005989}
5990
Balazs Keri3b30d652018-10-19 13:32:20 +00005991ExpectedStmt
5992ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5993 auto Imp = importSeq(
5994 S->getElement(), S->getCollection(), S->getBody(),
5995 S->getForLoc(), S->getRParenLoc());
5996 if (!Imp)
5997 return Imp.takeError();
5998
5999 Stmt *ToElement, *ToBody;
6000 Expr *ToCollection;
6001 SourceLocation ToForLoc, ToRParenLoc;
6002 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6003
6004 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6005 ToCollection,
6006 ToBody,
6007 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006008 ToRParenLoc);
6009}
6010
Balazs Keri3b30d652018-10-19 13:32:20 +00006011ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6012 auto Imp = importSeq(
6013 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6014 S->getCatchBody());
6015 if (!Imp)
6016 return Imp.takeError();
6017
6018 SourceLocation ToAtCatchLoc, ToRParenLoc;
6019 VarDecl *ToCatchParamDecl;
6020 Stmt *ToCatchBody;
6021 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6022
6023 return new (Importer.getToContext()) ObjCAtCatchStmt (
6024 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006025}
6026
Balazs Keri3b30d652018-10-19 13:32:20 +00006027ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6028 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6029 if (!ToAtFinallyLocOrErr)
6030 return ToAtFinallyLocOrErr.takeError();
6031 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6032 if (!ToAtFinallyStmtOrErr)
6033 return ToAtFinallyStmtOrErr.takeError();
6034 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6035 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006036}
6037
Balazs Keri3b30d652018-10-19 13:32:20 +00006038ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6039 auto Imp = importSeq(
6040 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6041 if (!Imp)
6042 return Imp.takeError();
6043
6044 SourceLocation ToAtTryLoc;
6045 Stmt *ToTryBody, *ToFinallyStmt;
6046 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6047
Sean Callanan59721b32015-04-28 18:41:46 +00006048 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6049 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6050 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006051 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6052 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006053 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006054 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006055 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006056
Sean Callanan59721b32015-04-28 18:41:46 +00006057 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006058 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006059 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006060 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006061}
6062
Balazs Keri3b30d652018-10-19 13:32:20 +00006063ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006064 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006065 auto Imp = importSeq(
6066 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6067 if (!Imp)
6068 return Imp.takeError();
6069
6070 SourceLocation ToAtSynchronizedLoc;
6071 Expr *ToSynchExpr;
6072 Stmt *ToSynchBody;
6073 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6074
Sean Callanan59721b32015-04-28 18:41:46 +00006075 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6076 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6077}
6078
Balazs Keri3b30d652018-10-19 13:32:20 +00006079ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6080 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6081 if (!ToThrowLocOrErr)
6082 return ToThrowLocOrErr.takeError();
6083 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6084 if (!ToThrowExprOrErr)
6085 return ToThrowExprOrErr.takeError();
6086 return new (Importer.getToContext()) ObjCAtThrowStmt(
6087 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006088}
6089
Balazs Keri3b30d652018-10-19 13:32:20 +00006090ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6091 ObjCAutoreleasePoolStmt *S) {
6092 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6093 if (!ToAtLocOrErr)
6094 return ToAtLocOrErr.takeError();
6095 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6096 if (!ToSubStmtOrErr)
6097 return ToSubStmtOrErr.takeError();
6098 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6099 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006100}
6101
6102//----------------------------------------------------------------------------
6103// Import Expressions
6104//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006105ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006106 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6107 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006108 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006109}
6110
Balazs Keri3b30d652018-10-19 13:32:20 +00006111ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6112 auto Imp = importSeq(
6113 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6114 E->getRParenLoc(), E->getType());
6115 if (!Imp)
6116 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006117
Balazs Keri3b30d652018-10-19 13:32:20 +00006118 SourceLocation ToBuiltinLoc, ToRParenLoc;
6119 Expr *ToSubExpr;
6120 TypeSourceInfo *ToWrittenTypeInfo;
6121 QualType ToType;
6122 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6123 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006124
6125 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006126 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6127 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006128}
6129
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006130
Balazs Keri3b30d652018-10-19 13:32:20 +00006131ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6132 ExpectedType TypeOrErr = import(E->getType());
6133 if (!TypeOrErr)
6134 return TypeOrErr.takeError();
6135
6136 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6137 if (!BeginLocOrErr)
6138 return BeginLocOrErr.takeError();
6139
6140 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006141}
6142
Balazs Keri3b30d652018-10-19 13:32:20 +00006143ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6144 auto Imp = importSeq(
6145 E->getBeginLoc(), E->getType(), E->getFunctionName());
6146 if (!Imp)
6147 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006148
Balazs Keri3b30d652018-10-19 13:32:20 +00006149 SourceLocation ToBeginLoc;
6150 QualType ToType;
6151 StringLiteral *ToFunctionName;
6152 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006153
Bruno Ricci17ff0262018-10-27 19:21:19 +00006154 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6155 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006156}
6157
Balazs Keri3b30d652018-10-19 13:32:20 +00006158ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6159 auto Imp = importSeq(
6160 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6161 E->getLocation(), E->getType());
6162 if (!Imp)
6163 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006164
Balazs Keri3b30d652018-10-19 13:32:20 +00006165 NestedNameSpecifierLoc ToQualifierLoc;
6166 SourceLocation ToTemplateKeywordLoc, ToLocation;
6167 ValueDecl *ToDecl;
6168 QualType ToType;
6169 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6170 *Imp;
6171
6172 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006173 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006174 auto FoundDOrErr = import(E->getFoundDecl());
6175 if (!FoundDOrErr)
6176 return FoundDOrErr.takeError();
6177 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006178 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006179
Aleksei Sidorina693b372016-09-28 10:16:56 +00006180 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006181 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006182 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006183 if (Error Err =
6184 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6185 return std::move(Err);
6186 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006187 }
6188
Balazs Keri3b30d652018-10-19 13:32:20 +00006189 auto *ToE = DeclRefExpr::Create(
6190 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6191 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6192 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006193 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006194 ToE->setHadMultipleCandidates(true);
6195 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006196}
6197
Balazs Keri3b30d652018-10-19 13:32:20 +00006198ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6199 ExpectedType TypeOrErr = import(E->getType());
6200 if (!TypeOrErr)
6201 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006202
Balazs Keri3b30d652018-10-19 13:32:20 +00006203 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006204}
6205
Balazs Keri3b30d652018-10-19 13:32:20 +00006206ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6207 ExpectedExpr ToInitOrErr = import(E->getInit());
6208 if (!ToInitOrErr)
6209 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006210
Balazs Keri3b30d652018-10-19 13:32:20 +00006211 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6212 if (!ToEqualOrColonLocOrErr)
6213 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006214
Balazs Keri3b30d652018-10-19 13:32:20 +00006215 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006216 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006217 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6218 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6219 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006220 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006221 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006222 }
6223
Balazs Keri3b30d652018-10-19 13:32:20 +00006224 SmallVector<Designator, 4> ToDesignators(E->size());
6225 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6226 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006227
6228 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006229 Importer.getToContext(), ToDesignators,
6230 ToIndexExprs, *ToEqualOrColonLocOrErr,
6231 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006232}
6233
Balazs Keri3b30d652018-10-19 13:32:20 +00006234ExpectedStmt
6235ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6236 ExpectedType ToTypeOrErr = import(E->getType());
6237 if (!ToTypeOrErr)
6238 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006239
Balazs Keri3b30d652018-10-19 13:32:20 +00006240 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6241 if (!ToLocationOrErr)
6242 return ToLocationOrErr.takeError();
6243
6244 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6245 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006246}
6247
Balazs Keri3b30d652018-10-19 13:32:20 +00006248ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6249 ExpectedType ToTypeOrErr = import(E->getType());
6250 if (!ToTypeOrErr)
6251 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006252
Balazs Keri3b30d652018-10-19 13:32:20 +00006253 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6254 if (!ToLocationOrErr)
6255 return ToLocationOrErr.takeError();
6256
6257 return IntegerLiteral::Create(
6258 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006259}
6260
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006261
Balazs Keri3b30d652018-10-19 13:32:20 +00006262ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6263 ExpectedType ToTypeOrErr = import(E->getType());
6264 if (!ToTypeOrErr)
6265 return ToTypeOrErr.takeError();
6266
6267 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6268 if (!ToLocationOrErr)
6269 return ToLocationOrErr.takeError();
6270
6271 return FloatingLiteral::Create(
6272 Importer.getToContext(), E->getValue(), E->isExact(),
6273 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006274}
6275
Balazs Keri3b30d652018-10-19 13:32:20 +00006276ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6277 auto ToTypeOrErr = import(E->getType());
6278 if (!ToTypeOrErr)
6279 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006280
Balazs Keri3b30d652018-10-19 13:32:20 +00006281 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6282 if (!ToSubExprOrErr)
6283 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006284
Balazs Keri3b30d652018-10-19 13:32:20 +00006285 return new (Importer.getToContext()) ImaginaryLiteral(
6286 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006287}
6288
Balazs Keri3b30d652018-10-19 13:32:20 +00006289ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6290 ExpectedType ToTypeOrErr = import(E->getType());
6291 if (!ToTypeOrErr)
6292 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006293
Balazs Keri3b30d652018-10-19 13:32:20 +00006294 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6295 if (!ToLocationOrErr)
6296 return ToLocationOrErr.takeError();
6297
6298 return new (Importer.getToContext()) CharacterLiteral(
6299 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006300}
6301
Balazs Keri3b30d652018-10-19 13:32:20 +00006302ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6303 ExpectedType ToTypeOrErr = import(E->getType());
6304 if (!ToTypeOrErr)
6305 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006306
Balazs Keri3b30d652018-10-19 13:32:20 +00006307 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6308 if (Error Err = ImportArrayChecked(
6309 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6310 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006311
Balazs Keri3b30d652018-10-19 13:32:20 +00006312 return StringLiteral::Create(
6313 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6314 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006315}
6316
Balazs Keri3b30d652018-10-19 13:32:20 +00006317ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6318 auto Imp = importSeq(
6319 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6320 E->getInitializer());
6321 if (!Imp)
6322 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006323
Balazs Keri3b30d652018-10-19 13:32:20 +00006324 SourceLocation ToLParenLoc;
6325 TypeSourceInfo *ToTypeSourceInfo;
6326 QualType ToType;
6327 Expr *ToInitializer;
6328 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006329
6330 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006331 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6332 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006333}
6334
Balazs Keri3b30d652018-10-19 13:32:20 +00006335ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6336 auto Imp = importSeq(
6337 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6338 if (!Imp)
6339 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006340
Balazs Keri3b30d652018-10-19 13:32:20 +00006341 SourceLocation ToBuiltinLoc, ToRParenLoc;
6342 QualType ToType;
6343 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6344
6345 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6346 if (Error Err = ImportArrayChecked(
6347 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6348 ToExprs.begin()))
6349 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006350
6351 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006352 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006353}
6354
Balazs Keri3b30d652018-10-19 13:32:20 +00006355ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6356 auto Imp = importSeq(
6357 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6358 if (!Imp)
6359 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006360
Balazs Keri3b30d652018-10-19 13:32:20 +00006361 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6362 LabelDecl *ToLabel;
6363 QualType ToType;
6364 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006365
6366 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006367 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006368}
6369
Bill Wendling8003edc2018-11-09 00:41:36 +00006370ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6371 auto Imp = importSeq(E->getSubExpr());
6372 if (!Imp)
6373 return Imp.takeError();
6374
6375 Expr *ToSubExpr;
6376 std::tie(ToSubExpr) = *Imp;
6377
Bill Wendling107b0e92018-11-20 08:53:30 +00006378 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006379}
6380
Balazs Keri3b30d652018-10-19 13:32:20 +00006381ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6382 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6383 if (!Imp)
6384 return Imp.takeError();
6385
6386 SourceLocation ToLParen, ToRParen;
6387 Expr *ToSubExpr;
6388 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006389
Fangrui Song6907ce22018-07-30 19:24:48 +00006390 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006391 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006392}
6393
Balazs Keri3b30d652018-10-19 13:32:20 +00006394ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6395 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6396 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6397 return std::move(Err);
6398
6399 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6400 if (!ToLParenLocOrErr)
6401 return ToLParenLocOrErr.takeError();
6402
6403 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6404 if (!ToRParenLocOrErr)
6405 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006406
6407 return new (Importer.getToContext()) ParenListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006408 Importer.getToContext(), *ToLParenLocOrErr, ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006409}
6410
Balazs Keri3b30d652018-10-19 13:32:20 +00006411ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6412 auto Imp = importSeq(
6413 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6414 if (!Imp)
6415 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006416
Balazs Keri3b30d652018-10-19 13:32:20 +00006417 CompoundStmt *ToSubStmt;
6418 QualType ToType;
6419 SourceLocation ToLParenLoc, ToRParenLoc;
6420 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006421
Balazs Keri3b30d652018-10-19 13:32:20 +00006422 return new (Importer.getToContext()) StmtExpr(
6423 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006424}
6425
Balazs Keri3b30d652018-10-19 13:32:20 +00006426ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6427 auto Imp = importSeq(
6428 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6429 if (!Imp)
6430 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006431
Balazs Keri3b30d652018-10-19 13:32:20 +00006432 Expr *ToSubExpr;
6433 QualType ToType;
6434 SourceLocation ToOperatorLoc;
6435 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006436
Aaron Ballmana5038552018-01-09 13:07:03 +00006437 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006438 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6439 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006440}
6441
Balazs Keri3b30d652018-10-19 13:32:20 +00006442ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006443ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006444 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6445 if (!Imp)
6446 return Imp.takeError();
6447
6448 QualType ToType;
6449 SourceLocation ToOperatorLoc, ToRParenLoc;
6450 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006451
Douglas Gregord8552cd2010-02-19 01:24:23 +00006452 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006453 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6454 import(E->getArgumentTypeInfo());
6455 if (!ToArgumentTypeInfoOrErr)
6456 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006457
Balazs Keri3b30d652018-10-19 13:32:20 +00006458 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6459 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6460 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006461 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006462
Balazs Keri3b30d652018-10-19 13:32:20 +00006463 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6464 if (!ToArgumentExprOrErr)
6465 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006466
Balazs Keri3b30d652018-10-19 13:32:20 +00006467 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6468 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006469}
6470
Balazs Keri3b30d652018-10-19 13:32:20 +00006471ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6472 auto Imp = importSeq(
6473 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6474 if (!Imp)
6475 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006476
Balazs Keri3b30d652018-10-19 13:32:20 +00006477 Expr *ToLHS, *ToRHS;
6478 QualType ToType;
6479 SourceLocation ToOperatorLoc;
6480 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006481
Balazs Keri3b30d652018-10-19 13:32:20 +00006482 return new (Importer.getToContext()) BinaryOperator(
6483 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6484 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006485}
6486
Balazs Keri3b30d652018-10-19 13:32:20 +00006487ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6488 auto Imp = importSeq(
6489 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6490 E->getRHS(), E->getType());
6491 if (!Imp)
6492 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006493
Balazs Keri3b30d652018-10-19 13:32:20 +00006494 Expr *ToCond, *ToLHS, *ToRHS;
6495 SourceLocation ToQuestionLoc, ToColonLoc;
6496 QualType ToType;
6497 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006498
6499 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006500 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6501 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006502}
6503
Balazs Keri3b30d652018-10-19 13:32:20 +00006504ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006505 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006506 auto Imp = importSeq(
6507 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6508 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6509 if (!Imp)
6510 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006511
Balazs Keri3b30d652018-10-19 13:32:20 +00006512 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6513 OpaqueValueExpr *ToOpaqueValue;
6514 SourceLocation ToQuestionLoc, ToColonLoc;
6515 QualType ToType;
6516 std::tie(
6517 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6518 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006519
6520 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006521 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6522 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6523 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006524}
6525
Balazs Keri3b30d652018-10-19 13:32:20 +00006526ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6527 auto Imp = importSeq(
6528 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6529 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6530 if (!Imp)
6531 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006532
Balazs Keri3b30d652018-10-19 13:32:20 +00006533 SourceLocation ToBeginLoc, ToEndLoc;
6534 TypeSourceInfo *ToQueriedTypeSourceInfo;
6535 Expr *ToDimensionExpression;
6536 QualType ToType;
6537 std::tie(
6538 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6539 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006540
6541 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006542 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6543 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006544}
6545
Balazs Keri3b30d652018-10-19 13:32:20 +00006546ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6547 auto Imp = importSeq(
6548 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6549 if (!Imp)
6550 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006551
Balazs Keri3b30d652018-10-19 13:32:20 +00006552 SourceLocation ToBeginLoc, ToEndLoc;
6553 Expr *ToQueriedExpression;
6554 QualType ToType;
6555 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006556
6557 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006558 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6559 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006560}
6561
Balazs Keri3b30d652018-10-19 13:32:20 +00006562ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6563 auto Imp = importSeq(
6564 E->getLocation(), E->getType(), E->getSourceExpr());
6565 if (!Imp)
6566 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006567
Balazs Keri3b30d652018-10-19 13:32:20 +00006568 SourceLocation ToLocation;
6569 QualType ToType;
6570 Expr *ToSourceExpr;
6571 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006572
6573 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006574 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006575}
6576
Balazs Keri3b30d652018-10-19 13:32:20 +00006577ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6578 auto Imp = importSeq(
6579 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6580 if (!Imp)
6581 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006582
Balazs Keri3b30d652018-10-19 13:32:20 +00006583 Expr *ToLHS, *ToRHS;
6584 SourceLocation ToRBracketLoc;
6585 QualType ToType;
6586 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006587
6588 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006589 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6590 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006591}
6592
Balazs Keri3b30d652018-10-19 13:32:20 +00006593ExpectedStmt
6594ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6595 auto Imp = importSeq(
6596 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6597 E->getComputationResultType(), E->getOperatorLoc());
6598 if (!Imp)
6599 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006600
Balazs Keri3b30d652018-10-19 13:32:20 +00006601 Expr *ToLHS, *ToRHS;
6602 QualType ToType, ToComputationLHSType, ToComputationResultType;
6603 SourceLocation ToOperatorLoc;
6604 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6605 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006606
Balazs Keri3b30d652018-10-19 13:32:20 +00006607 return new (Importer.getToContext()) CompoundAssignOperator(
6608 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6609 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6610 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006611}
6612
Balazs Keri3b30d652018-10-19 13:32:20 +00006613Expected<CXXCastPath>
6614ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6615 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006616 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006617 if (auto SpecOrErr = import(*I))
6618 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006619 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006620 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006621 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006622 return Path;
John McCallcf142162010-08-07 06:22:56 +00006623}
6624
Balazs Keri3b30d652018-10-19 13:32:20 +00006625ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6626 ExpectedType ToTypeOrErr = import(E->getType());
6627 if (!ToTypeOrErr)
6628 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006629
Balazs Keri3b30d652018-10-19 13:32:20 +00006630 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6631 if (!ToSubExprOrErr)
6632 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006633
Balazs Keri3b30d652018-10-19 13:32:20 +00006634 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6635 if (!ToBasePathOrErr)
6636 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006637
Balazs Keri3b30d652018-10-19 13:32:20 +00006638 return ImplicitCastExpr::Create(
6639 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6640 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006641}
6642
Balazs Keri3b30d652018-10-19 13:32:20 +00006643ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6644 auto Imp1 = importSeq(
6645 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6646 if (!Imp1)
6647 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006648
Balazs Keri3b30d652018-10-19 13:32:20 +00006649 QualType ToType;
6650 Expr *ToSubExpr;
6651 TypeSourceInfo *ToTypeInfoAsWritten;
6652 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006653
Balazs Keri3b30d652018-10-19 13:32:20 +00006654 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6655 if (!ToBasePathOrErr)
6656 return ToBasePathOrErr.takeError();
6657 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006658
Aleksei Sidorina693b372016-09-28 10:16:56 +00006659 switch (E->getStmtClass()) {
6660 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006661 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006662 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6663 if (!ToLParenLocOrErr)
6664 return ToLParenLocOrErr.takeError();
6665 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6666 if (!ToRParenLocOrErr)
6667 return ToRParenLocOrErr.takeError();
6668 return CStyleCastExpr::Create(
6669 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6670 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6671 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006672 }
6673
6674 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006675 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006676 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6677 if (!ToLParenLocOrErr)
6678 return ToLParenLocOrErr.takeError();
6679 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6680 if (!ToRParenLocOrErr)
6681 return ToRParenLocOrErr.takeError();
6682 return CXXFunctionalCastExpr::Create(
6683 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6684 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6685 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006686 }
6687
6688 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006689 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6690 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6691 if (!ToLParenLocOrErr)
6692 return ToLParenLocOrErr.takeError();
6693 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6694 if (!ToBridgeKeywordLocOrErr)
6695 return ToBridgeKeywordLocOrErr.takeError();
6696 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6697 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6698 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006699 }
6700 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006701 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006702 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006703 }
6704}
6705
Balazs Keri3b30d652018-10-19 13:32:20 +00006706ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6707 SmallVector<OffsetOfNode, 4> ToNodes;
6708 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6709 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006710
Balazs Keri3b30d652018-10-19 13:32:20 +00006711 SourceLocation ToBeginLoc, ToEndLoc;
6712 if (FromNode.getKind() != OffsetOfNode::Base) {
6713 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6714 if (!Imp)
6715 return Imp.takeError();
6716 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6717 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006718
Balazs Keri3b30d652018-10-19 13:32:20 +00006719 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006720 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006721 ToNodes.push_back(
6722 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006723 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006724 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006725 auto ToBSOrErr = import(FromNode.getBase());
6726 if (!ToBSOrErr)
6727 return ToBSOrErr.takeError();
6728 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729 break;
6730 }
6731 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006732 auto ToFieldOrErr = import(FromNode.getField());
6733 if (!ToFieldOrErr)
6734 return ToFieldOrErr.takeError();
6735 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006736 break;
6737 }
6738 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006739 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6740 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006741 break;
6742 }
6743 }
6744 }
6745
Balazs Keri3b30d652018-10-19 13:32:20 +00006746 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6747 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6748 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6749 if (!ToIndexExprOrErr)
6750 return ToIndexExprOrErr.takeError();
6751 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006752 }
6753
Balazs Keri3b30d652018-10-19 13:32:20 +00006754 auto Imp = importSeq(
6755 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6756 E->getRParenLoc());
6757 if (!Imp)
6758 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006759
Balazs Keri3b30d652018-10-19 13:32:20 +00006760 QualType ToType;
6761 TypeSourceInfo *ToTypeSourceInfo;
6762 SourceLocation ToOperatorLoc, ToRParenLoc;
6763 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6764
6765 return OffsetOfExpr::Create(
6766 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6767 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006768}
6769
Balazs Keri3b30d652018-10-19 13:32:20 +00006770ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6771 auto Imp = importSeq(
6772 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6773 if (!Imp)
6774 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006775
Balazs Keri3b30d652018-10-19 13:32:20 +00006776 QualType ToType;
6777 Expr *ToOperand;
6778 SourceLocation ToBeginLoc, ToEndLoc;
6779 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006780
Balazs Keri3b30d652018-10-19 13:32:20 +00006781 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006782 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006783 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006784 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006785 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006786
Balazs Keri3b30d652018-10-19 13:32:20 +00006787 return new (Importer.getToContext()) CXXNoexceptExpr(
6788 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006789}
6790
Balazs Keri3b30d652018-10-19 13:32:20 +00006791ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6792 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6793 if (!Imp)
6794 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006795
Balazs Keri3b30d652018-10-19 13:32:20 +00006796 Expr *ToSubExpr;
6797 QualType ToType;
6798 SourceLocation ToThrowLoc;
6799 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006800
6801 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006802 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006803}
6804
Balazs Keri3b30d652018-10-19 13:32:20 +00006805ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6806 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6807 if (!ToUsedLocOrErr)
6808 return ToUsedLocOrErr.takeError();
6809
6810 auto ToParamOrErr = import(E->getParam());
6811 if (!ToParamOrErr)
6812 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006813
6814 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006815 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006816}
6817
Balazs Keri3b30d652018-10-19 13:32:20 +00006818ExpectedStmt
6819ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6820 auto Imp = importSeq(
6821 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6822 if (!Imp)
6823 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006824
Balazs Keri3b30d652018-10-19 13:32:20 +00006825 QualType ToType;
6826 TypeSourceInfo *ToTypeSourceInfo;
6827 SourceLocation ToRParenLoc;
6828 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006829
6830 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006831 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006832}
6833
Balazs Keri3b30d652018-10-19 13:32:20 +00006834ExpectedStmt
6835ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6836 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6837 if (!ToSubExprOrErr)
6838 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006839
Balazs Keri3b30d652018-10-19 13:32:20 +00006840 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6841 if (!ToDtorOrErr)
6842 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006843
6844 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006845 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6846 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006847}
6848
Balazs Keri3b30d652018-10-19 13:32:20 +00006849ExpectedStmt
6850ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6851 auto Imp = importSeq(
6852 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6853 E->getParenOrBraceRange());
6854 if (!Imp)
6855 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006856
Balazs Keri3b30d652018-10-19 13:32:20 +00006857 CXXConstructorDecl *ToConstructor;
6858 QualType ToType;
6859 TypeSourceInfo *ToTypeSourceInfo;
6860 SourceRange ToParenOrBraceRange;
6861 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006862
Balazs Keri3b30d652018-10-19 13:32:20 +00006863 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6864 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6865 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006866
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006867 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006868 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6869 ToParenOrBraceRange, E->hadMultipleCandidates(),
6870 E->isListInitialization(), E->isStdInitListInitialization(),
6871 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006872}
6873
Balazs Keri3b30d652018-10-19 13:32:20 +00006874ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006875ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006876 auto Imp = importSeq(
6877 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6878 if (!Imp)
6879 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006880
Balazs Keri3b30d652018-10-19 13:32:20 +00006881 QualType ToType;
6882 Expr *ToTemporaryExpr;
6883 const ValueDecl *ToExtendingDecl;
6884 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006885
6886 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006887 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006888
6889 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006890 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006891 return ToMTE;
6892}
6893
Balazs Keri3b30d652018-10-19 13:32:20 +00006894ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6895 auto Imp = importSeq(
6896 E->getType(), E->getPattern(), E->getEllipsisLoc());
6897 if (!Imp)
6898 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006899
Balazs Keri3b30d652018-10-19 13:32:20 +00006900 QualType ToType;
6901 Expr *ToPattern;
6902 SourceLocation ToEllipsisLoc;
6903 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006904
6905 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006906 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006907}
6908
Balazs Keri3b30d652018-10-19 13:32:20 +00006909ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6910 auto Imp = importSeq(
6911 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6912 if (!Imp)
6913 return Imp.takeError();
6914
6915 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6916 NamedDecl *ToPack;
6917 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006918
6919 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006920 if (!E->isValueDependent())
6921 Length = E->getPackLength();
6922
Balazs Keri3b30d652018-10-19 13:32:20 +00006923 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006924 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006925 if (Error Err = ImportTemplateArguments(
6926 E->getPartialArguments().data(),
6927 E->getPartialArguments().size(),
6928 ToPartialArguments))
6929 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006930 }
6931
6932 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006933 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6934 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006935}
6936
Aleksei Sidorina693b372016-09-28 10:16:56 +00006937
Balazs Keri3b30d652018-10-19 13:32:20 +00006938ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6939 auto Imp = importSeq(
6940 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6941 E->getArraySize(), E->getInitializer(), E->getType(),
6942 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6943 E->getDirectInitRange());
6944 if (!Imp)
6945 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006946
Balazs Keri3b30d652018-10-19 13:32:20 +00006947 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6948 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6949 Expr *ToArraySize, *ToInitializer;
6950 QualType ToType;
6951 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6952 std::tie(
6953 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6954 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006955
Balazs Keri3b30d652018-10-19 13:32:20 +00006956 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6957 if (Error Err =
6958 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6959 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006960
6961 return new (Importer.getToContext()) CXXNewExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006962 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6963 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6964 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6965 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6966 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006967}
6968
Balazs Keri3b30d652018-10-19 13:32:20 +00006969ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6970 auto Imp = importSeq(
6971 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6972 if (!Imp)
6973 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006974
Balazs Keri3b30d652018-10-19 13:32:20 +00006975 QualType ToType;
6976 FunctionDecl *ToOperatorDelete;
6977 Expr *ToArgument;
6978 SourceLocation ToBeginLoc;
6979 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006980
6981 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006982 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6983 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6984 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006985}
6986
Balazs Keri3b30d652018-10-19 13:32:20 +00006987ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6988 auto Imp = importSeq(
6989 E->getType(), E->getLocation(), E->getConstructor(),
6990 E->getParenOrBraceRange());
6991 if (!Imp)
6992 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006993
Balazs Keri3b30d652018-10-19 13:32:20 +00006994 QualType ToType;
6995 SourceLocation ToLocation;
6996 CXXConstructorDecl *ToConstructor;
6997 SourceRange ToParenOrBraceRange;
6998 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006999
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007000 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007001 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7002 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007003
Balazs Keri3b30d652018-10-19 13:32:20 +00007004 return CXXConstructExpr::Create(
7005 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7006 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7007 E->isListInitialization(), E->isStdInitListInitialization(),
7008 E->requiresZeroInitialization(), E->getConstructionKind(),
7009 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007010}
7011
Balazs Keri3b30d652018-10-19 13:32:20 +00007012ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7013 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7014 if (!ToSubExprOrErr)
7015 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007016
Balazs Keri3b30d652018-10-19 13:32:20 +00007017 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7018 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7019 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007020
Balazs Keri3b30d652018-10-19 13:32:20 +00007021 return ExprWithCleanups::Create(
7022 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7023 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007024}
7025
Balazs Keri3b30d652018-10-19 13:32:20 +00007026ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7027 auto Imp = importSeq(
7028 E->getCallee(), E->getType(), E->getRParenLoc());
7029 if (!Imp)
7030 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007031
Balazs Keri3b30d652018-10-19 13:32:20 +00007032 Expr *ToCallee;
7033 QualType ToType;
7034 SourceLocation ToRParenLoc;
7035 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007036
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007037 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007038 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7039 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007040
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007041 return new (Importer.getToContext()) CXXMemberCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007042 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7043 ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007044}
7045
Balazs Keri3b30d652018-10-19 13:32:20 +00007046ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7047 ExpectedType ToTypeOrErr = import(E->getType());
7048 if (!ToTypeOrErr)
7049 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007050
Balazs Keri3b30d652018-10-19 13:32:20 +00007051 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7052 if (!ToLocationOrErr)
7053 return ToLocationOrErr.takeError();
7054
7055 return new (Importer.getToContext()) CXXThisExpr(
7056 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007057}
7058
Balazs Keri3b30d652018-10-19 13:32:20 +00007059ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7060 ExpectedType ToTypeOrErr = import(E->getType());
7061 if (!ToTypeOrErr)
7062 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007063
Balazs Keri3b30d652018-10-19 13:32:20 +00007064 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7065 if (!ToLocationOrErr)
7066 return ToLocationOrErr.takeError();
7067
7068 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7069 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007070}
7071
Balazs Keri3b30d652018-10-19 13:32:20 +00007072ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7073 auto Imp1 = importSeq(
7074 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7075 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7076 if (!Imp1)
7077 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007078
Balazs Keri3b30d652018-10-19 13:32:20 +00007079 Expr *ToBase;
7080 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7081 NestedNameSpecifierLoc ToQualifierLoc;
7082 ValueDecl *ToMemberDecl;
7083 QualType ToType;
7084 std::tie(
7085 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7086 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007087
Balazs Keri3b30d652018-10-19 13:32:20 +00007088 auto Imp2 = importSeq(
7089 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7090 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7091 if (!Imp2)
7092 return Imp2.takeError();
7093 NamedDecl *ToDecl;
7094 DeclarationName ToName;
7095 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7096 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007097
7098 DeclAccessPair ToFoundDecl =
7099 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007100
Balazs Keri3b30d652018-10-19 13:32:20 +00007101 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007102
7103 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007104 // FIXME: handle template arguments
7105 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Sean Callanan59721b32015-04-28 18:41:46 +00007106 }
7107
Balazs Keri3b30d652018-10-19 13:32:20 +00007108 return MemberExpr::Create(
7109 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7110 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
7111 ToMemberNameInfo, nullptr, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007112}
7113
Balazs Keri3b30d652018-10-19 13:32:20 +00007114ExpectedStmt
7115ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7116 auto Imp = importSeq(
7117 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7118 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7119 if (!Imp)
7120 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007121
Balazs Keri3b30d652018-10-19 13:32:20 +00007122 Expr *ToBase;
7123 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7124 NestedNameSpecifierLoc ToQualifierLoc;
7125 TypeSourceInfo *ToScopeTypeInfo;
7126 std::tie(
7127 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7128 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007129
7130 PseudoDestructorTypeStorage Storage;
7131 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7132 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007133 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7134 if (!ToDestroyedTypeLocOrErr)
7135 return ToDestroyedTypeLocOrErr.takeError();
7136 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007137 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007138 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7139 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7140 else
7141 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007142 }
7143
7144 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007145 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7146 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007147}
7148
Balazs Keri3b30d652018-10-19 13:32:20 +00007149ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007150 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007151 auto Imp = importSeq(
7152 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7153 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7154 if (!Imp)
7155 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007156
Balazs Keri3b30d652018-10-19 13:32:20 +00007157 QualType ToType;
7158 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7159 NestedNameSpecifierLoc ToQualifierLoc;
7160 NamedDecl *ToFirstQualifierFoundInScope;
7161 std::tie(
7162 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7163 ToFirstQualifierFoundInScope) = *Imp;
7164
7165 Expr *ToBase = nullptr;
7166 if (!E->isImplicitAccess()) {
7167 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7168 ToBase = *ToBaseOrErr;
7169 else
7170 return ToBaseOrErr.takeError();
7171 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007172
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007173 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007174 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007175 if (Error Err = ImportTemplateArgumentListInfo(
7176 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7177 ToTAInfo))
7178 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007179 ResInfo = &ToTAInfo;
7180 }
7181
Balazs Keri3b30d652018-10-19 13:32:20 +00007182 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7183 if (!ToMemberNameInfoOrErr)
7184 return ToMemberNameInfoOrErr.takeError();
7185 DeclarationNameInfo ToMemberNameInfo(
7186 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007187 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007188 if (Error Err = ImportDeclarationNameLoc(
7189 E->getMemberNameInfo(), ToMemberNameInfo))
7190 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007191
7192 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007193 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7194 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7195 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007196}
7197
Balazs Keri3b30d652018-10-19 13:32:20 +00007198ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007199ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007200 auto Imp = importSeq(
7201 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7202 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7203 if (!Imp)
7204 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007205
Balazs Keri3b30d652018-10-19 13:32:20 +00007206 NestedNameSpecifierLoc ToQualifierLoc;
7207 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7208 DeclarationName ToDeclName;
7209 std::tie(
7210 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7211 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007212
Balazs Keri3b30d652018-10-19 13:32:20 +00007213 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7214 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7215 return std::move(Err);
7216
7217 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007218 TemplateArgumentListInfo *ResInfo = nullptr;
7219 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007220 if (Error Err =
7221 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7222 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007223 ResInfo = &ToTAInfo;
7224 }
7225
7226 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007227 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7228 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007229}
7230
Balazs Keri3b30d652018-10-19 13:32:20 +00007231ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7232 CXXUnresolvedConstructExpr *E) {
7233 auto Imp = importSeq(
7234 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7235 if (!Imp)
7236 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007237
Balazs Keri3b30d652018-10-19 13:32:20 +00007238 SourceLocation ToLParenLoc, ToRParenLoc;
7239 TypeSourceInfo *ToTypeSourceInfo;
7240 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7241
7242 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7243 if (Error Err =
7244 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7245 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007246
7247 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007248 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7249 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007250}
7251
Balazs Keri3b30d652018-10-19 13:32:20 +00007252ExpectedStmt
7253ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7254 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7255 if (!ToNamingClassOrErr)
7256 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007257
Balazs Keri3b30d652018-10-19 13:32:20 +00007258 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7259 if (!ToQualifierLocOrErr)
7260 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007261
Balazs Keri3b30d652018-10-19 13:32:20 +00007262 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7263 if (!ToNameInfoOrErr)
7264 return ToNameInfoOrErr.takeError();
7265 DeclarationNameInfo ToNameInfo(
7266 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007267 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007268 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7269 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007270
7271 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007272 for (auto *D : E->decls())
7273 if (auto ToDOrErr = import(D))
7274 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007275 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007276 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007277
Balazs Keri3b30d652018-10-19 13:32:20 +00007278 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7279 TemplateArgumentListInfo ToTAInfo;
7280 if (Error Err = ImportTemplateArgumentListInfo(
7281 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7282 ToTAInfo))
7283 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007284
Balazs Keri3b30d652018-10-19 13:32:20 +00007285 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7286 if (!ToTemplateKeywordLocOrErr)
7287 return ToTemplateKeywordLocOrErr.takeError();
7288
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007289 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007290 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7291 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7292 ToDecls.begin(), ToDecls.end());
7293 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007294
7295 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007296 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7297 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7298 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007299}
7300
Balazs Keri3b30d652018-10-19 13:32:20 +00007301ExpectedStmt
7302ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7303 auto Imp1 = importSeq(
7304 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7305 E->getTemplateKeywordLoc());
7306 if (!Imp1)
7307 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007308
Balazs Keri3b30d652018-10-19 13:32:20 +00007309 QualType ToType;
7310 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7311 NestedNameSpecifierLoc ToQualifierLoc;
7312 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7313
7314 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7315 if (!Imp2)
7316 return Imp2.takeError();
7317 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7318 // Import additional name location/type info.
7319 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7320 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007321
7322 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007323 for (Decl *D : E->decls())
7324 if (auto ToDOrErr = import(D))
7325 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007326 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007327 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007328
7329 TemplateArgumentListInfo ToTAInfo;
7330 TemplateArgumentListInfo *ResInfo = nullptr;
7331 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007332 if (Error Err =
7333 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7334 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007335 ResInfo = &ToTAInfo;
7336 }
7337
Balazs Keri3b30d652018-10-19 13:32:20 +00007338 Expr *ToBase = nullptr;
7339 if (!E->isImplicitAccess()) {
7340 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7341 ToBase = *ToBaseOrErr;
7342 else
7343 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007344 }
7345
7346 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007347 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7348 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7349 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007350}
7351
Balazs Keri3b30d652018-10-19 13:32:20 +00007352ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7353 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7354 if (!Imp)
7355 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007356
Balazs Keri3b30d652018-10-19 13:32:20 +00007357 Expr *ToCallee;
7358 QualType ToType;
7359 SourceLocation ToRParenLoc;
7360 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007361
7362 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007363 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7364 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7365 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007366
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007367 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
7368 return new (Importer.getToContext()) CXXOperatorCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007369 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
7370 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007371 }
7372
Balazs Keri3b30d652018-10-19 13:32:20 +00007373 return new (Importer.getToContext()) CallExpr(
7374 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7375 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007376}
7377
Balazs Keri3b30d652018-10-19 13:32:20 +00007378ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7379 CXXRecordDecl *FromClass = E->getLambdaClass();
7380 auto ToClassOrErr = import(FromClass);
7381 if (!ToClassOrErr)
7382 return ToClassOrErr.takeError();
7383 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007384
7385 // NOTE: lambda classes are created with BeingDefined flag set up.
7386 // It means that ImportDefinition doesn't work for them and we should fill it
7387 // manually.
7388 if (ToClass->isBeingDefined()) {
7389 for (auto FromField : FromClass->fields()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007390 auto ToFieldOrErr = import(FromField);
7391 if (!ToFieldOrErr)
7392 return ToFieldOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007393 }
7394 }
7395
Balazs Keri3b30d652018-10-19 13:32:20 +00007396 auto ToCallOpOrErr = import(E->getCallOperator());
7397 if (!ToCallOpOrErr)
7398 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007399
7400 ToClass->completeDefinition();
7401
Balazs Keri3b30d652018-10-19 13:32:20 +00007402 SmallVector<LambdaCapture, 8> ToCaptures;
7403 ToCaptures.reserve(E->capture_size());
7404 for (const auto &FromCapture : E->captures()) {
7405 if (auto ToCaptureOrErr = import(FromCapture))
7406 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007407 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007408 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007409 }
7410
Balazs Keri3b30d652018-10-19 13:32:20 +00007411 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7412 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7413 return std::move(Err);
7414
7415 auto Imp = importSeq(
7416 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7417 if (!Imp)
7418 return Imp.takeError();
7419
7420 SourceRange ToIntroducerRange;
7421 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7422 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007423
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007424 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007425 Importer.getToContext(), ToClass, ToIntroducerRange,
7426 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7427 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7428 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007429}
7430
Sean Callanan8bca9962016-03-28 21:43:01 +00007431
Balazs Keri3b30d652018-10-19 13:32:20 +00007432ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7433 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7434 if (!Imp)
7435 return Imp.takeError();
7436
7437 SourceLocation ToLBraceLoc, ToRBraceLoc;
7438 QualType ToType;
7439 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7440
7441 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7442 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7443 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007444
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007445 ASTContext &ToCtx = Importer.getToContext();
7446 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007447 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7448 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007449
Balazs Keri3b30d652018-10-19 13:32:20 +00007450 if (E->hasArrayFiller()) {
7451 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7452 To->setArrayFiller(*ToFillerOrErr);
7453 else
7454 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007455 }
7456
Balazs Keri3b30d652018-10-19 13:32:20 +00007457 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7458 if (auto ToFDOrErr = import(FromFD))
7459 To->setInitializedFieldInUnion(*ToFDOrErr);
7460 else
7461 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007462 }
7463
Balazs Keri3b30d652018-10-19 13:32:20 +00007464 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7465 if (auto ToSyntFormOrErr = import(SyntForm))
7466 To->setSyntacticForm(*ToSyntFormOrErr);
7467 else
7468 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007469 }
7470
Gabor Martona20ce602018-09-03 13:10:53 +00007471 // Copy InitListExprBitfields, which are not handled in the ctor of
7472 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007473 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007474
7475 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007476}
7477
Balazs Keri3b30d652018-10-19 13:32:20 +00007478ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007479 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007480 ExpectedType ToTypeOrErr = import(E->getType());
7481 if (!ToTypeOrErr)
7482 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007483
Balazs Keri3b30d652018-10-19 13:32:20 +00007484 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7485 if (!ToSubExprOrErr)
7486 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007487
Balazs Keri3b30d652018-10-19 13:32:20 +00007488 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7489 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007490}
7491
Balazs Keri3b30d652018-10-19 13:32:20 +00007492ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007493 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007494 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7495 if (!Imp)
7496 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007497
Balazs Keri3b30d652018-10-19 13:32:20 +00007498 SourceLocation ToLocation;
7499 QualType ToType;
7500 CXXConstructorDecl *ToConstructor;
7501 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007502
7503 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007504 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7505 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007506}
7507
Balazs Keri3b30d652018-10-19 13:32:20 +00007508ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7509 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7510 if (!Imp)
7511 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007512
Balazs Keri3b30d652018-10-19 13:32:20 +00007513 QualType ToType;
7514 Expr *ToCommonExpr, *ToSubExpr;
7515 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007516
Balazs Keri3b30d652018-10-19 13:32:20 +00007517 return new (Importer.getToContext()) ArrayInitLoopExpr(
7518 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007519}
7520
Balazs Keri3b30d652018-10-19 13:32:20 +00007521ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7522 ExpectedType ToTypeOrErr = import(E->getType());
7523 if (!ToTypeOrErr)
7524 return ToTypeOrErr.takeError();
7525 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007526}
7527
Balazs Keri3b30d652018-10-19 13:32:20 +00007528ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7529 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7530 if (!ToBeginLocOrErr)
7531 return ToBeginLocOrErr.takeError();
7532
7533 auto ToFieldOrErr = import(E->getField());
7534 if (!ToFieldOrErr)
7535 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007536
7537 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007538 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007539}
7540
Balazs Keri3b30d652018-10-19 13:32:20 +00007541ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7542 auto Imp = importSeq(
7543 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7544 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7545 if (!Imp)
7546 return Imp.takeError();
7547
7548 QualType ToType;
7549 Expr *ToSubExpr;
7550 TypeSourceInfo *ToTypeInfoAsWritten;
7551 SourceLocation ToOperatorLoc, ToRParenLoc;
7552 SourceRange ToAngleBrackets;
7553 std::tie(
7554 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7555 ToAngleBrackets) = *Imp;
7556
Sean Callanandd2c1742016-05-16 20:48:03 +00007557 ExprValueKind VK = E->getValueKind();
7558 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007559 auto ToBasePathOrErr = ImportCastPath(E);
7560 if (!ToBasePathOrErr)
7561 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007562
Sean Callanandd2c1742016-05-16 20:48:03 +00007563 if (isa<CXXStaticCastExpr>(E)) {
7564 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007565 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7566 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007567 } else if (isa<CXXDynamicCastExpr>(E)) {
7568 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007569 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7570 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007571 } else if (isa<CXXReinterpretCastExpr>(E)) {
7572 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007573 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7574 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007575 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007576 return CXXConstCastExpr::Create(
7577 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7578 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007579 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007580 llvm_unreachable("Unknown cast type");
7581 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007582 }
7583}
7584
Balazs Keri3b30d652018-10-19 13:32:20 +00007585ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007586 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007587 auto Imp = importSeq(
7588 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7589 if (!Imp)
7590 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007591
Balazs Keri3b30d652018-10-19 13:32:20 +00007592 QualType ToType;
7593 SourceLocation ToExprLoc;
7594 NonTypeTemplateParmDecl *ToParameter;
7595 Expr *ToReplacement;
7596 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007597
7598 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007599 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007600}
7601
Balazs Keri3b30d652018-10-19 13:32:20 +00007602ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7603 auto Imp = importSeq(
7604 E->getType(), E->getBeginLoc(), E->getEndLoc());
7605 if (!Imp)
7606 return Imp.takeError();
7607
7608 QualType ToType;
7609 SourceLocation ToBeginLoc, ToEndLoc;
7610 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007611
7612 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007613 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7614 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007615
7616 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7617 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007618 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007619
7620 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007621 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7622 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007623}
7624
Balazs Keri3b30d652018-10-19 13:32:20 +00007625ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7626 ExpectedType ToTypeOrErr = import(E->getType());
7627 if (!ToTypeOrErr)
7628 return ToTypeOrErr.takeError();
7629
7630 auto ToSourceRangeOrErr = import(E->getSourceRange());
7631 if (!ToSourceRangeOrErr)
7632 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007633
7634 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007635 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7636 return new (Importer.getToContext()) CXXTypeidExpr(
7637 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7638 else
7639 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007640 }
7641
Balazs Keri3b30d652018-10-19 13:32:20 +00007642 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7643 if (!ToExprOperandOrErr)
7644 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007645
Balazs Keri3b30d652018-10-19 13:32:20 +00007646 return new (Importer.getToContext()) CXXTypeidExpr(
7647 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007648}
7649
Lang Hames19e07e12017-06-20 21:06:00 +00007650void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7651 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007652 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7653 if (auto ImportedOrErr = import(FromOverriddenMethod))
7654 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7655 (*ImportedOrErr)->getCanonicalDecl()));
7656 else
7657 consumeError(ImportedOrErr.takeError());
7658 }
Lang Hames19e07e12017-06-20 21:06:00 +00007659}
7660
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007661ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007662 ASTContext &FromContext, FileManager &FromFileManager,
7663 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007664 : ToContext(ToContext), FromContext(FromContext),
7665 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7666 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007667 ImportedDecls[FromContext.getTranslationUnitDecl()]
7668 = ToContext.getTranslationUnitDecl();
7669}
7670
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007671ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007672
7673QualType ASTImporter::Import(QualType FromT) {
7674 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007675 return {};
John McCall424cec92011-01-19 06:33:43 +00007676
Balazs Keri3b30d652018-10-19 13:32:20 +00007677 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007678
7679 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007680 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007681 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007682 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007683 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007684
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007685 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007686 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007687 ExpectedType ToTOrErr = Importer.Visit(FromTy);
7688 if (!ToTOrErr) {
7689 llvm::consumeError(ToTOrErr.takeError());
7690 return {};
7691 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007692
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007693 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007694 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007695
Balazs Keri3b30d652018-10-19 13:32:20 +00007696 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007697}
7698
Douglas Gregor62d311f2010-02-09 19:21:46 +00007699TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007700 if (!FromTSI)
7701 return FromTSI;
7702
7703 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007704 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007705 QualType T = Import(FromTSI->getType());
7706 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007707 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007708
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007709 return ToContext.getTrivialTypeSourceInfo(
7710 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007711}
7712
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007713Attr *ASTImporter::Import(const Attr *FromAttr) {
7714 Attr *ToAttr = FromAttr->clone(ToContext);
7715 ToAttr->setRange(Import(FromAttr->getRange()));
7716 return ToAttr;
7717}
7718
Sean Callanan59721b32015-04-28 18:41:46 +00007719Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7720 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7721 if (Pos != ImportedDecls.end()) {
7722 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007723 // FIXME: move this call to ImportDeclParts().
Balazs Keri3b30d652018-10-19 13:32:20 +00007724 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD))
7725 llvm::consumeError(std::move(Err));
Sean Callanan59721b32015-04-28 18:41:46 +00007726 return ToD;
7727 } else {
7728 return nullptr;
7729 }
7730}
7731
Douglas Gregor62d311f2010-02-09 19:21:46 +00007732Decl *ASTImporter::Import(Decl *FromD) {
7733 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007734 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007735
Douglas Gregord451ea92011-07-29 23:31:30 +00007736 ASTNodeImporter Importer(*this);
7737
Gabor Marton26f72a92018-07-12 09:42:05 +00007738 // Check whether we've already imported this declaration.
7739 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7740 if (ToD) {
7741 // If FromD has some updated flags after last import, apply it
7742 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007743 return ToD;
7744 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007745
7746 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007747 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
7748 if (!ToDOrErr) {
7749 llvm::consumeError(ToDOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007750 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007751 }
7752 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007753
Gabor Marton26f72a92018-07-12 09:42:05 +00007754 // Notify subclasses.
7755 Imported(FromD, ToD);
7756
Gabor Martonac3a5d62018-09-17 12:04:52 +00007757 updateFlags(FromD, ToD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007758 return ToD;
7759}
7760
Balazs Keri3b30d652018-10-19 13:32:20 +00007761Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007762 if (!FromDC)
7763 return FromDC;
7764
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007765 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007766 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007767 return nullptr;
7768
Fangrui Song6907ce22018-07-30 19:24:48 +00007769 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007770 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007771 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7772 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007773 if (ToRecord->isCompleteDefinition()) {
7774 // Do nothing.
7775 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007776 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7777 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7778 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007779 } else {
7780 CompleteDecl(ToRecord);
7781 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007782 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7783 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007784 if (ToEnum->isCompleteDefinition()) {
7785 // Do nothing.
7786 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007787 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7788 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7789 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007790 } else {
7791 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007792 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007793 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7794 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007795 if (ToClass->getDefinition()) {
7796 // Do nothing.
7797 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007798 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7799 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7800 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007801 } else {
7802 CompleteDecl(ToClass);
7803 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007804 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7805 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007806 if (ToProto->getDefinition()) {
7807 // Do nothing.
7808 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007809 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7810 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7811 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007812 } else {
7813 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007814 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007815 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007816
Douglas Gregor95d82832012-01-24 18:36:04 +00007817 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007818}
7819
7820Expr *ASTImporter::Import(Expr *FromE) {
7821 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007822 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007823
7824 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7825}
7826
7827Stmt *ASTImporter::Import(Stmt *FromS) {
7828 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007829 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007830
Fangrui Song6907ce22018-07-30 19:24:48 +00007831 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007832 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7833 if (Pos != ImportedStmts.end())
7834 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007835
Balazs Keri3b30d652018-10-19 13:32:20 +00007836 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007837 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007838 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
7839 if (!ToSOrErr) {
7840 llvm::consumeError(ToSOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007841 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007842 }
Craig Topper36250ad2014-05-12 05:36:57 +00007843
Balazs Keri3b30d652018-10-19 13:32:20 +00007844 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007845 auto *FromE = cast<Expr>(FromS);
7846 // Copy ExprBitfields, which may not be handled in Expr subclasses
7847 // constructors.
7848 ToE->setValueKind(FromE->getValueKind());
7849 ToE->setObjectKind(FromE->getObjectKind());
7850 ToE->setTypeDependent(FromE->isTypeDependent());
7851 ToE->setValueDependent(FromE->isValueDependent());
7852 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7853 ToE->setContainsUnexpandedParameterPack(
7854 FromE->containsUnexpandedParameterPack());
7855 }
7856
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007857 // Record the imported declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007858 ImportedStmts[FromS] = *ToSOrErr;
7859 return *ToSOrErr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007860}
7861
7862NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7863 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007864 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007865
Douglas Gregor90ebf252011-04-27 16:48:40 +00007866 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7867
7868 switch (FromNNS->getKind()) {
7869 case NestedNameSpecifier::Identifier:
7870 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7871 return NestedNameSpecifier::Create(ToContext, prefix, II);
7872 }
Craig Topper36250ad2014-05-12 05:36:57 +00007873 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007874
7875 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007876 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007877 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007878 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7879 }
Craig Topper36250ad2014-05-12 05:36:57 +00007880 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007881
7882 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007883 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007884 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007885 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7886 }
Craig Topper36250ad2014-05-12 05:36:57 +00007887 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007888
7889 case NestedNameSpecifier::Global:
7890 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7891
Nikola Smiljanic67860242014-09-26 00:28:20 +00007892 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007893 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007894 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007895 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7896 }
7897 return nullptr;
7898
Douglas Gregor90ebf252011-04-27 16:48:40 +00007899 case NestedNameSpecifier::TypeSpec:
7900 case NestedNameSpecifier::TypeSpecWithTemplate: {
7901 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7902 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007903 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007904 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007905 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007906 bTemplate, T.getTypePtr());
7907 }
7908 }
Craig Topper36250ad2014-05-12 05:36:57 +00007909 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007910 }
7911
7912 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007913}
7914
Douglas Gregor14454802011-02-25 02:25:35 +00007915NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007916 // Copied from NestedNameSpecifier mostly.
7917 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7918 NestedNameSpecifierLoc NNS = FromNNS;
7919
7920 // Push each of the nested-name-specifiers's onto a stack for
7921 // serialization in reverse order.
7922 while (NNS) {
7923 NestedNames.push_back(NNS);
7924 NNS = NNS.getPrefix();
7925 }
7926
7927 NestedNameSpecifierLocBuilder Builder;
7928
7929 while (!NestedNames.empty()) {
7930 NNS = NestedNames.pop_back_val();
7931 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7932 if (!Spec)
7933 return NestedNameSpecifierLoc();
7934
7935 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7936 switch (Kind) {
7937 case NestedNameSpecifier::Identifier:
7938 Builder.Extend(getToContext(),
7939 Spec->getAsIdentifier(),
7940 Import(NNS.getLocalBeginLoc()),
7941 Import(NNS.getLocalEndLoc()));
7942 break;
7943
7944 case NestedNameSpecifier::Namespace:
7945 Builder.Extend(getToContext(),
7946 Spec->getAsNamespace(),
7947 Import(NNS.getLocalBeginLoc()),
7948 Import(NNS.getLocalEndLoc()));
7949 break;
7950
7951 case NestedNameSpecifier::NamespaceAlias:
7952 Builder.Extend(getToContext(),
7953 Spec->getAsNamespaceAlias(),
7954 Import(NNS.getLocalBeginLoc()),
7955 Import(NNS.getLocalEndLoc()));
7956 break;
7957
7958 case NestedNameSpecifier::TypeSpec:
7959 case NestedNameSpecifier::TypeSpecWithTemplate: {
7960 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7961 QualType(Spec->getAsType(), 0));
7962 Builder.Extend(getToContext(),
7963 Import(NNS.getLocalBeginLoc()),
7964 TSI->getTypeLoc(),
7965 Import(NNS.getLocalEndLoc()));
7966 break;
7967 }
7968
7969 case NestedNameSpecifier::Global:
7970 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7971 break;
7972
7973 case NestedNameSpecifier::Super: {
7974 SourceRange ToRange = Import(NNS.getSourceRange());
7975 Builder.MakeSuper(getToContext(),
7976 Spec->getAsRecordDecl(),
7977 ToRange.getBegin(),
7978 ToRange.getEnd());
7979 }
7980 }
7981 }
7982
7983 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007984}
7985
Douglas Gregore2e50d332010-12-01 01:36:18 +00007986TemplateName ASTImporter::Import(TemplateName From) {
7987 switch (From.getKind()) {
7988 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007989 if (auto *ToTemplate =
7990 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007991 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007992
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007993 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007994
Douglas Gregore2e50d332010-12-01 01:36:18 +00007995 case TemplateName::OverloadedTemplate: {
7996 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7997 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007998 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007999 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00008000 ToTemplates.addDecl(To);
8001 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008002 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008003 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008004 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008005 ToTemplates.end());
8006 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008007
Douglas Gregore2e50d332010-12-01 01:36:18 +00008008 case TemplateName::QualifiedTemplate: {
8009 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
8010 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
8011 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008012 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008013
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008014 if (auto *ToTemplate =
8015 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00008016 return ToContext.getQualifiedTemplateName(Qualifier,
8017 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008018 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008019
8020 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008021 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008022
Douglas Gregore2e50d332010-12-01 01:36:18 +00008023 case TemplateName::DependentTemplate: {
8024 DependentTemplateName *DTN = From.getAsDependentTemplateName();
8025 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
8026 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008027 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008028
Douglas Gregore2e50d332010-12-01 01:36:18 +00008029 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008030 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008031 Import(DTN->getIdentifier()));
8032 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008033
Douglas Gregore2e50d332010-12-01 01:36:18 +00008034 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
8035 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008036
8037 case TemplateName::SubstTemplateTemplateParm: {
8038 SubstTemplateTemplateParmStorage *subst
8039 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008040 auto *param =
8041 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00008042 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008043 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00008044
8045 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008046 if (replacement.isNull())
8047 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008048
John McCalld9dfe3a2011-06-30 08:33:18 +00008049 return ToContext.getSubstTemplateTemplateParm(param, replacement);
8050 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008051
Douglas Gregor5590be02011-01-15 06:45:20 +00008052 case TemplateName::SubstTemplateTemplateParmPack: {
8053 SubstTemplateTemplateParmPackStorage *SubstPack
8054 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008055 auto *Param =
8056 cast_or_null<TemplateTemplateParmDecl>(
8057 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00008058 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008059 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008060
Douglas Gregor5590be02011-01-15 06:45:20 +00008061 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008062 Expected<TemplateArgument> ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00008063 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
Balazs Keri3b30d652018-10-19 13:32:20 +00008064 if (!ArgPack) {
8065 llvm::consumeError(ArgPack.takeError());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008066 return {};
Balazs Keri3b30d652018-10-19 13:32:20 +00008067 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008068
Balazs Keri3b30d652018-10-19 13:32:20 +00008069 return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00008070 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008071 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008072
Douglas Gregore2e50d332010-12-01 01:36:18 +00008073 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008074}
8075
Douglas Gregor62d311f2010-02-09 19:21:46 +00008076SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
8077 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008078 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008079
Douglas Gregor811663e2010-02-10 00:15:17 +00008080 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008081
Douglas Gregor811663e2010-02-10 00:15:17 +00008082 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00008083 FileID ToFileID = Import(Decomposed.first);
8084 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008085 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008086 SourceManager &ToSM = ToContext.getSourceManager();
8087 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00008088}
8089
8090SourceRange ASTImporter::Import(SourceRange FromRange) {
8091 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
8092}
8093
Douglas Gregor811663e2010-02-10 00:15:17 +00008094FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008095 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008096 if (Pos != ImportedFileIDs.end())
8097 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008098
Douglas Gregor811663e2010-02-10 00:15:17 +00008099 SourceManager &FromSM = FromContext.getSourceManager();
8100 SourceManager &ToSM = ToContext.getSourceManager();
8101 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008102
8103 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008104 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008105 if (FromSLoc.isExpansion()) {
8106 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
8107 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
8108 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
8109 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8110 SourceLocation MLoc;
8111 if (FromEx.isMacroArgExpansion()) {
8112 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
8113 } else {
8114 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
8115 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
8116 FromEx.isExpansionTokenRange());
8117 }
8118 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008119 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008120 // Include location of this file.
8121 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
8122
8123 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
8124 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8125 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8126 // disk again
8127 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8128 // than mmap the files several times.
8129 const FileEntry *Entry =
8130 ToFileManager.getFile(Cache->OrigEntry->getName());
8131 if (!Entry)
8132 return {};
8133 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
8134 FromSLoc.getFile().getFileCharacteristic());
8135 } else {
8136 // FIXME: We want to re-use the existing MemoryBuffer!
8137 const llvm::MemoryBuffer *FromBuf =
8138 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
8139 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8140 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8141 FromBuf->getBufferIdentifier());
8142 ToID = ToSM.createFileID(std::move(ToBuf),
8143 FromSLoc.getFile().getFileCharacteristic());
8144 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008145 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008146
Sebastian Redl99219f12010-09-30 01:03:06 +00008147 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008148 return ToID;
8149}
8150
Sean Callanandd2c1742016-05-16 20:48:03 +00008151CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8152 Expr *ToExpr = Import(From->getInit());
8153 if (!ToExpr && From->getInit())
8154 return nullptr;
8155
8156 if (From->isBaseInitializer()) {
8157 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8158 if (!ToTInfo && From->getTypeSourceInfo())
8159 return nullptr;
8160
8161 return new (ToContext) CXXCtorInitializer(
8162 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
8163 ToExpr, Import(From->getRParenLoc()),
8164 From->isPackExpansion() ? Import(From->getEllipsisLoc())
8165 : SourceLocation());
8166 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008167 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008168 if (!ToField && From->getMember())
8169 return nullptr;
8170
8171 return new (ToContext) CXXCtorInitializer(
8172 ToContext, ToField, Import(From->getMemberLocation()),
8173 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8174 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008175 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00008176 Import(From->getIndirectMember()));
8177 if (!ToIField && From->getIndirectMember())
8178 return nullptr;
8179
8180 return new (ToContext) CXXCtorInitializer(
8181 ToContext, ToIField, Import(From->getMemberLocation()),
8182 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8183 } else if (From->isDelegatingInitializer()) {
8184 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8185 if (!ToTInfo && From->getTypeSourceInfo())
8186 return nullptr;
8187
8188 return new (ToContext)
8189 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
8190 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008191 } else {
8192 return nullptr;
8193 }
8194}
8195
Aleksei Sidorina693b372016-09-28 10:16:56 +00008196CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
8197 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8198 if (Pos != ImportedCXXBaseSpecifiers.end())
8199 return Pos->second;
8200
8201 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
8202 Import(BaseSpec->getSourceRange()),
8203 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8204 BaseSpec->getAccessSpecifierAsWritten(),
8205 Import(BaseSpec->getTypeSourceInfo()),
8206 Import(BaseSpec->getEllipsisLoc()));
8207 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8208 return Imported;
8209}
8210
Balazs Keri3b30d652018-10-19 13:32:20 +00008211Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008212 Decl *To = Import(From);
8213 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008214 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008215
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008216 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008217 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008218
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008219 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00008220 if (!ToRecord->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008221 return Importer.ImportDefinition(
8222 cast<RecordDecl>(FromDC), ToRecord,
8223 ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008224 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00008225 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008226
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008227 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00008228 if (!ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008229 return Importer.ImportDefinition(
8230 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008231 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008232 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008233
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008234 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008235 if (!ToIFace->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008236 return Importer.ImportDefinition(
8237 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8238 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008239 }
8240 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008241
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008242 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008243 if (!ToProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008244 return Importer.ImportDefinition(
8245 cast<ObjCProtocolDecl>(FromDC), ToProto,
8246 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008247 }
8248 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008249
Balazs Keri3b30d652018-10-19 13:32:20 +00008250 return Importer.ImportDeclContext(FromDC, true);
Douglas Gregor0a791672011-01-18 03:11:38 +00008251 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008252
8253 return Error::success();
8254}
8255
8256void ASTImporter::ImportDefinition(Decl *From) {
8257 Error Err = ImportDefinition_New(From);
8258 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008259}
8260
Douglas Gregor96e578d2010-02-05 17:54:41 +00008261DeclarationName ASTImporter::Import(DeclarationName FromName) {
8262 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008263 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008264
8265 switch (FromName.getNameKind()) {
8266 case DeclarationName::Identifier:
8267 return Import(FromName.getAsIdentifierInfo());
8268
8269 case DeclarationName::ObjCZeroArgSelector:
8270 case DeclarationName::ObjCOneArgSelector:
8271 case DeclarationName::ObjCMultiArgSelector:
8272 return Import(FromName.getObjCSelector());
8273
8274 case DeclarationName::CXXConstructorName: {
8275 QualType T = Import(FromName.getCXXNameType());
8276 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008277 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008278
8279 return ToContext.DeclarationNames.getCXXConstructorName(
8280 ToContext.getCanonicalType(T));
8281 }
8282
8283 case DeclarationName::CXXDestructorName: {
8284 QualType T = Import(FromName.getCXXNameType());
8285 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008286 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008287
8288 return ToContext.DeclarationNames.getCXXDestructorName(
8289 ToContext.getCanonicalType(T));
8290 }
8291
Richard Smith35845152017-02-07 01:37:30 +00008292 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008293 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00008294 Import(FromName.getCXXDeductionGuideTemplate()));
8295 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008296 return {};
Richard Smith35845152017-02-07 01:37:30 +00008297 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
8298 }
8299
Douglas Gregor96e578d2010-02-05 17:54:41 +00008300 case DeclarationName::CXXConversionFunctionName: {
8301 QualType T = Import(FromName.getCXXNameType());
8302 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008303 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008304
8305 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8306 ToContext.getCanonicalType(T));
8307 }
8308
8309 case DeclarationName::CXXOperatorName:
8310 return ToContext.DeclarationNames.getCXXOperatorName(
8311 FromName.getCXXOverloadedOperator());
8312
8313 case DeclarationName::CXXLiteralOperatorName:
8314 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
8315 Import(FromName.getCXXLiteralIdentifier()));
8316
8317 case DeclarationName::CXXUsingDirective:
8318 // FIXME: STATICS!
8319 return DeclarationName::getUsingDirectiveName();
8320 }
8321
David Blaikiee4d798f2012-01-20 21:50:17 +00008322 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008323}
8324
Douglas Gregore2e50d332010-12-01 01:36:18 +00008325IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008326 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008327 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008328
Sean Callananf94ef1d2016-05-14 06:11:19 +00008329 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8330
8331 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8332 ToId->setBuiltinID(FromId->getBuiltinID());
8333
8334 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008335}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008336
Douglas Gregor43f54792010-02-17 02:12:47 +00008337Selector ASTImporter::Import(Selector FromSel) {
8338 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008339 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00008340
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008341 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008342 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8343 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8344 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8345 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8346}
8347
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008348DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8349 DeclContext *DC,
8350 unsigned IDNS,
8351 NamedDecl **Decls,
8352 unsigned NumDecls) {
8353 return Name;
8354}
8355
8356DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008357 if (LastDiagFromFrom)
8358 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8359 FromContext.getDiagnostics());
8360 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008361 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008362}
8363
8364DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008365 if (!LastDiagFromFrom)
8366 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8367 ToContext.getDiagnostics());
8368 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008369 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008370}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008371
Douglas Gregor2e15c842012-02-01 21:00:38 +00008372void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008373 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008374 if (!ID->getDefinition())
8375 ID->startDefinition();
8376 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008377 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008378 if (!PD->getDefinition())
8379 PD->startDefinition();
8380 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008381 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008382 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8383 TD->startDefinition();
8384 TD->setCompleteDefinition(true);
8385 }
8386 }
8387 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008388 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008389 }
8390}
8391
Gabor Marton26f72a92018-07-12 09:42:05 +00008392Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8393 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8394 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8395 "Try to import an already imported Decl");
8396 if (Pos != ImportedDecls.end())
8397 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008398 ImportedDecls[From] = To;
8399 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008400}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008401
Douglas Gregordd6006f2012-07-17 21:16:27 +00008402bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8403 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00008404 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00008405 = ImportedTypes.find(From.getTypePtr());
8406 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8407 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008408
Douglas Gregordd6006f2012-07-17 21:16:27 +00008409 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008410 getStructuralEquivalenceKind(*this), false,
8411 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008412 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008413}