blob: 53568f1169aac97e815bfa28e23a7df4e86e6e65 [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);
582 ExpectedStmt VisitParenExpr(ParenExpr *E);
583 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
584 ExpectedStmt VisitStmtExpr(StmtExpr *E);
585 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
586 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
587 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
588 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
589 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
590 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
591 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
592 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
593 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
594 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
595 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
596 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
597 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
598 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
599 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
600 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
601 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
602 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
603 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
604 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
605 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
606 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
607 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
608 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
609 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
610 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
611 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
612 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
613 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
614 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
615 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
616 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
617 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
618 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
619 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
620 ExpectedStmt VisitMemberExpr(MemberExpr *E);
621 ExpectedStmt VisitCallExpr(CallExpr *E);
622 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
623 ExpectedStmt VisitInitListExpr(InitListExpr *E);
624 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
625 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
626 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
627 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
628 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
629 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
630 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
631 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
632 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000633
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000634 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000635 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000636 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000637 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
638 Expected<ItemT> ToOrErr = import(*Ibegin);
639 if (!ToOrErr)
640 return ToOrErr.takeError();
641 *Obegin = *ToOrErr;
642 }
643 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000644 }
645
Balazs Keri3b30d652018-10-19 13:32:20 +0000646 // Import every item from a container structure into an output container.
647 // If error occurs, stops at first error and returns the error.
648 // The output container should have space for all needed elements (it is not
649 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000650 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000651 Error ImportContainerChecked(
652 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
653 return ImportArrayChecked(
654 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000655 }
656
657 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000658 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000659 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
660 }
Lang Hames19e07e12017-06-20 21:06:00 +0000661
Lang Hames19e07e12017-06-20 21:06:00 +0000662 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000663
Balazs Keri3b30d652018-10-19 13:32:20 +0000664 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
665 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000666 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000667
Balazs Keri3b30d652018-10-19 13:32:20 +0000668// FIXME: Temporary until every import returns Expected.
669template <>
670Expected<TemplateName> ASTNodeImporter::import(const TemplateName &From) {
671 TemplateName To = Importer.Import(From);
672 if (To.isNull() && !From.isNull())
673 return make_error<ImportError>();
674 return To;
675}
676
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000677template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000678Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000679 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
680 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000681 auto ToLAngleLocOrErr = import(FromLAngleLoc);
682 if (!ToLAngleLocOrErr)
683 return ToLAngleLocOrErr.takeError();
684 auto ToRAngleLocOrErr = import(FromRAngleLoc);
685 if (!ToRAngleLocOrErr)
686 return ToRAngleLocOrErr.takeError();
687
688 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
689 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
690 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000691 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000692 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000693}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000694
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000695template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000696Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000697 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
698 return ImportTemplateArgumentListInfo(
699 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
700}
701
702template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000703Error ASTNodeImporter::ImportTemplateArgumentListInfo<
704 ASTTemplateArgumentListInfo>(
705 const ASTTemplateArgumentListInfo &From,
706 TemplateArgumentListInfo &Result) {
707 return ImportTemplateArgumentListInfo(
708 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000709}
710
Balazs Keri3b30d652018-10-19 13:32:20 +0000711Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000712ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
713 FunctionDecl *FromFD) {
714 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000715 FunctionDecl::TK_FunctionTemplateSpecialization);
716
717 FunctionTemplateAndArgsTy Result;
718
Gabor Marton5254e642018-06-27 13:32:50 +0000719 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000720 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
721 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000722
723 // Import template arguments.
724 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000725 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
726 std::get<1>(Result)))
727 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000728
Balazs Keri3b30d652018-10-19 13:32:20 +0000729 return Result;
730}
731
732template <>
733Expected<TemplateParameterList *>
734ASTNodeImporter::import(TemplateParameterList *From) {
735 SmallVector<NamedDecl *, 4> To(From->size());
736 if (Error Err = ImportContainerChecked(*From, To))
737 return std::move(Err);
738
739 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
740 if (!ToRequiresClause)
741 return ToRequiresClause.takeError();
742
743 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
744 if (!ToTemplateLocOrErr)
745 return ToTemplateLocOrErr.takeError();
746 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
747 if (!ToLAngleLocOrErr)
748 return ToLAngleLocOrErr.takeError();
749 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
750 if (!ToRAngleLocOrErr)
751 return ToRAngleLocOrErr.takeError();
752
753 return TemplateParameterList::Create(
754 Importer.getToContext(),
755 *ToTemplateLocOrErr,
756 *ToLAngleLocOrErr,
757 To,
758 *ToRAngleLocOrErr,
759 *ToRequiresClause);
760}
761
762template <>
763Expected<TemplateArgument>
764ASTNodeImporter::import(const TemplateArgument &From) {
765 switch (From.getKind()) {
766 case TemplateArgument::Null:
767 return TemplateArgument();
768
769 case TemplateArgument::Type: {
770 ExpectedType ToTypeOrErr = import(From.getAsType());
771 if (!ToTypeOrErr)
772 return ToTypeOrErr.takeError();
773 return TemplateArgument(*ToTypeOrErr);
774 }
775
776 case TemplateArgument::Integral: {
777 ExpectedType ToTypeOrErr = import(From.getIntegralType());
778 if (!ToTypeOrErr)
779 return ToTypeOrErr.takeError();
780 return TemplateArgument(From, *ToTypeOrErr);
781 }
782
783 case TemplateArgument::Declaration: {
784 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
785 if (!ToOrErr)
786 return ToOrErr.takeError();
787 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
788 if (!ToTypeOrErr)
789 return ToTypeOrErr.takeError();
790 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
791 }
792
793 case TemplateArgument::NullPtr: {
794 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
795 if (!ToTypeOrErr)
796 return ToTypeOrErr.takeError();
797 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
798 }
799
800 case TemplateArgument::Template: {
801 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
802 if (!ToTemplateOrErr)
803 return ToTemplateOrErr.takeError();
804
805 return TemplateArgument(*ToTemplateOrErr);
806 }
807
808 case TemplateArgument::TemplateExpansion: {
809 Expected<TemplateName> ToTemplateOrErr =
810 import(From.getAsTemplateOrTemplatePattern());
811 if (!ToTemplateOrErr)
812 return ToTemplateOrErr.takeError();
813
814 return TemplateArgument(
815 *ToTemplateOrErr, From.getNumTemplateExpansions());
816 }
817
818 case TemplateArgument::Expression:
819 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
820 return TemplateArgument(*ToExpr);
821 else
822 return ToExpr.takeError();
823
824 case TemplateArgument::Pack: {
825 SmallVector<TemplateArgument, 2> ToPack;
826 ToPack.reserve(From.pack_size());
827 if (Error Err = ImportTemplateArguments(
828 From.pack_begin(), From.pack_size(), ToPack))
829 return std::move(Err);
830
831 return TemplateArgument(
832 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
833 }
834 }
835
836 llvm_unreachable("Invalid template argument kind");
837}
838
839template <>
840Expected<TemplateArgumentLoc>
841ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
842 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
843 if (!ArgOrErr)
844 return ArgOrErr.takeError();
845 TemplateArgument Arg = *ArgOrErr;
846
847 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
848
849 TemplateArgumentLocInfo ToInfo;
850 if (Arg.getKind() == TemplateArgument::Expression) {
851 ExpectedExpr E = import(FromInfo.getAsExpr());
852 if (!E)
853 return E.takeError();
854 ToInfo = TemplateArgumentLocInfo(*E);
855 } else if (Arg.getKind() == TemplateArgument::Type) {
856 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
857 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
858 else
859 return TSIOrErr.takeError();
860 } else {
861 auto ToTemplateQualifierLocOrErr =
862 import(FromInfo.getTemplateQualifierLoc());
863 if (!ToTemplateQualifierLocOrErr)
864 return ToTemplateQualifierLocOrErr.takeError();
865 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
866 if (!ToTemplateNameLocOrErr)
867 return ToTemplateNameLocOrErr.takeError();
868 auto ToTemplateEllipsisLocOrErr =
869 import(FromInfo.getTemplateEllipsisLoc());
870 if (!ToTemplateEllipsisLocOrErr)
871 return ToTemplateEllipsisLocOrErr.takeError();
872
873 ToInfo = TemplateArgumentLocInfo(
874 *ToTemplateQualifierLocOrErr,
875 *ToTemplateNameLocOrErr,
876 *ToTemplateEllipsisLocOrErr);
877 }
878
879 return TemplateArgumentLoc(Arg, ToInfo);
880}
881
882template <>
883Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
884 if (DG.isNull())
885 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
886 size_t NumDecls = DG.end() - DG.begin();
887 SmallVector<Decl *, 1> ToDecls;
888 ToDecls.reserve(NumDecls);
889 for (Decl *FromD : DG) {
890 if (auto ToDOrErr = import(FromD))
891 ToDecls.push_back(*ToDOrErr);
892 else
893 return ToDOrErr.takeError();
894 }
895 return DeclGroupRef::Create(Importer.getToContext(),
896 ToDecls.begin(),
897 NumDecls);
898}
899
900template <>
901Expected<ASTNodeImporter::Designator>
902ASTNodeImporter::import(const Designator &D) {
903 if (D.isFieldDesignator()) {
904 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
905
906 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
907 if (!ToDotLocOrErr)
908 return ToDotLocOrErr.takeError();
909
910 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
911 if (!ToFieldLocOrErr)
912 return ToFieldLocOrErr.takeError();
913
914 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
915 }
916
917 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
918 if (!ToLBracketLocOrErr)
919 return ToLBracketLocOrErr.takeError();
920
921 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
922 if (!ToRBracketLocOrErr)
923 return ToRBracketLocOrErr.takeError();
924
925 if (D.isArrayDesignator())
926 return Designator(D.getFirstExprIndex(),
927 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
928
929 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
930 if (!ToEllipsisLocOrErr)
931 return ToEllipsisLocOrErr.takeError();
932
933 assert(D.isArrayRangeDesignator());
934 return Designator(
935 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
936 *ToRBracketLocOrErr);
937}
938
939template <>
940Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
941 VarDecl *Var = nullptr;
942 if (From.capturesVariable()) {
943 if (auto VarOrErr = import(From.getCapturedVar()))
944 Var = *VarOrErr;
945 else
946 return VarOrErr.takeError();
947 }
948
949 auto LocationOrErr = import(From.getLocation());
950 if (!LocationOrErr)
951 return LocationOrErr.takeError();
952
953 SourceLocation EllipsisLoc;
954 if (From.isPackExpansion())
955 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
956 return std::move(Err);
957
958 return LambdaCapture(
959 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
960 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000961}
962
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000963} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000964
Douglas Gregor3996e242010-02-15 22:01:00 +0000965//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000966// Import Types
967//----------------------------------------------------------------------------
968
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000969using namespace clang;
970
Balazs Keri3b30d652018-10-19 13:32:20 +0000971ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000972 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
973 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000974 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000975}
976
Balazs Keri3b30d652018-10-19 13:32:20 +0000977ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
978 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
979 if (!UnderlyingTypeOrErr)
980 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000981
Balazs Keri3b30d652018-10-19 13:32:20 +0000982 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000983}
984
Balazs Keri3b30d652018-10-19 13:32:20 +0000985ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000986 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000987#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
988 case BuiltinType::Id: \
989 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000990#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000991#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
992 case BuiltinType::Id: \
993 return Importer.getToContext().Id##Ty;
994#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000995#define SHARED_SINGLETON_TYPE(Expansion)
996#define BUILTIN_TYPE(Id, SingletonId) \
997 case BuiltinType::Id: return Importer.getToContext().SingletonId;
998#include "clang/AST/BuiltinTypes.def"
999
1000 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1001 // context supports C++.
1002
1003 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1004 // context supports ObjC.
1005
Douglas Gregor96e578d2010-02-05 17:54:41 +00001006 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +00001007 // The context we're importing from has an unsigned 'char'. If we're
1008 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001009 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001010 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001011 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001012
Douglas Gregor96e578d2010-02-05 17:54:41 +00001013 return Importer.getToContext().CharTy;
1014
Douglas Gregor96e578d2010-02-05 17:54:41 +00001015 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +00001016 // The context we're importing from has an unsigned 'char'. If we're
1017 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001018 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001019 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001020 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001021
Douglas Gregor96e578d2010-02-05 17:54:41 +00001022 return Importer.getToContext().CharTy;
1023
Chris Lattnerad3467e2010-12-25 23:25:43 +00001024 case BuiltinType::WChar_S:
1025 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001026 // FIXME: If not in C++, shall we translate to the C equivalent of
1027 // wchar_t?
1028 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001029 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001030
1031 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001032}
1033
Balazs Keri3b30d652018-10-19 13:32:20 +00001034ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1035 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1036 if (!ToOriginalTypeOrErr)
1037 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001038
Balazs Keri3b30d652018-10-19 13:32:20 +00001039 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001040}
1041
Balazs Keri3b30d652018-10-19 13:32:20 +00001042ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1043 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1044 if (!ToElementTypeOrErr)
1045 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001046
Balazs Keri3b30d652018-10-19 13:32:20 +00001047 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001048}
1049
Balazs Keri3b30d652018-10-19 13:32:20 +00001050ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1051 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1052 if (!ToPointeeTypeOrErr)
1053 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001054
Balazs Keri3b30d652018-10-19 13:32:20 +00001055 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001056}
1057
Balazs Keri3b30d652018-10-19 13:32:20 +00001058ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001059 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001060 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1061 if (!ToPointeeTypeOrErr)
1062 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001063
Balazs Keri3b30d652018-10-19 13:32:20 +00001064 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001065}
1066
Balazs Keri3b30d652018-10-19 13:32:20 +00001067ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001068ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001069 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001070 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1071 if (!ToPointeeTypeOrErr)
1072 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001073
Balazs Keri3b30d652018-10-19 13:32:20 +00001074 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001075}
1076
Balazs Keri3b30d652018-10-19 13:32:20 +00001077ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001078ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001079 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001080 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1081 if (!ToPointeeTypeOrErr)
1082 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001083
Balazs Keri3b30d652018-10-19 13:32:20 +00001084 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001085}
1086
Balazs Keri3b30d652018-10-19 13:32:20 +00001087ExpectedType
1088ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001089 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001090 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1091 if (!ToPointeeTypeOrErr)
1092 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001093
Balazs Keri3b30d652018-10-19 13:32:20 +00001094 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1095 if (!ClassTypeOrErr)
1096 return ClassTypeOrErr.takeError();
1097
1098 return Importer.getToContext().getMemberPointerType(
1099 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001100}
1101
Balazs Keri3b30d652018-10-19 13:32:20 +00001102ExpectedType
1103ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1104 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1105 if (!ToElementTypeOrErr)
1106 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001107
Balazs Keri3b30d652018-10-19 13:32:20 +00001108 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001109 T->getSize(),
1110 T->getSizeModifier(),
1111 T->getIndexTypeCVRQualifiers());
1112}
1113
Balazs Keri3b30d652018-10-19 13:32:20 +00001114ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001115ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001116 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1117 if (!ToElementTypeOrErr)
1118 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001119
Balazs Keri3b30d652018-10-19 13:32:20 +00001120 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001121 T->getSizeModifier(),
1122 T->getIndexTypeCVRQualifiers());
1123}
1124
Balazs Keri3b30d652018-10-19 13:32:20 +00001125ExpectedType
1126ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1127 QualType ToElementType;
1128 Expr *ToSizeExpr;
1129 SourceRange ToBracketsRange;
1130 if (auto Imp = importSeq(
1131 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1132 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1133 else
1134 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001135
Balazs Keri3b30d652018-10-19 13:32:20 +00001136 return Importer.getToContext().getVariableArrayType(
1137 ToElementType, ToSizeExpr, T->getSizeModifier(),
1138 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001139}
1140
Balazs Keri3b30d652018-10-19 13:32:20 +00001141ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001142 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001143 QualType ToElementType;
1144 Expr *ToSizeExpr;
1145 SourceRange ToBracketsRange;
1146 if (auto Imp = importSeq(
1147 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1148 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1149 else
1150 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001151 // SizeExpr may be null if size is not specified directly.
1152 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001153
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001154 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001155 ToElementType, ToSizeExpr, T->getSizeModifier(),
1156 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001157}
1158
Balazs Keri3b30d652018-10-19 13:32:20 +00001159ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1160 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1161 if (!ToElementTypeOrErr)
1162 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001163
Balazs Keri3b30d652018-10-19 13:32:20 +00001164 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001165 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001166 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001167}
1168
Balazs Keri3b30d652018-10-19 13:32:20 +00001169ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1170 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1171 if (!ToElementTypeOrErr)
1172 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001173
Balazs Keri3b30d652018-10-19 13:32:20 +00001174 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001175 T->getNumElements());
1176}
1177
Balazs Keri3b30d652018-10-19 13:32:20 +00001178ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001179ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001180 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001181 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001182 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1183 if (!ToReturnTypeOrErr)
1184 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001185
Balazs Keri3b30d652018-10-19 13:32:20 +00001186 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001187 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001188}
1189
Balazs Keri3b30d652018-10-19 13:32:20 +00001190ExpectedType
1191ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1192 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1193 if (!ToReturnTypeOrErr)
1194 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001195
Douglas Gregor96e578d2010-02-05 17:54:41 +00001196 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001197 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001198 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001199 ExpectedType TyOrErr = import(A);
1200 if (!TyOrErr)
1201 return TyOrErr.takeError();
1202 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001203 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001204
Douglas Gregor96e578d2010-02-05 17:54:41 +00001205 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001206 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001207 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001208 ExpectedType TyOrErr = import(E);
1209 if (!TyOrErr)
1210 return TyOrErr.takeError();
1211 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001212 }
John McCalldb40c7f2010-12-14 08:05:40 +00001213
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001214 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1215 FunctionProtoType::ExtProtoInfo ToEPI;
1216
Balazs Keri3b30d652018-10-19 13:32:20 +00001217 auto Imp = importSeq(
1218 FromEPI.ExceptionSpec.NoexceptExpr,
1219 FromEPI.ExceptionSpec.SourceDecl,
1220 FromEPI.ExceptionSpec.SourceTemplate);
1221 if (!Imp)
1222 return Imp.takeError();
1223
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001224 ToEPI.ExtInfo = FromEPI.ExtInfo;
1225 ToEPI.Variadic = FromEPI.Variadic;
1226 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1227 ToEPI.TypeQuals = FromEPI.TypeQuals;
1228 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001229 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1230 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001231 std::tie(
1232 ToEPI.ExceptionSpec.NoexceptExpr,
1233 ToEPI.ExceptionSpec.SourceDecl,
1234 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001235
Balazs Keri3b30d652018-10-19 13:32:20 +00001236 return Importer.getToContext().getFunctionType(
1237 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001238}
1239
Balazs Keri3b30d652018-10-19 13:32:20 +00001240ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001241 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001242 UnresolvedUsingTypenameDecl *ToD;
1243 Decl *ToPrevD;
1244 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1245 std::tie(ToD, ToPrevD) = *Imp;
1246 else
1247 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001248
Balazs Keri3b30d652018-10-19 13:32:20 +00001249 return Importer.getToContext().getTypeDeclType(
1250 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001251}
1252
Balazs Keri3b30d652018-10-19 13:32:20 +00001253ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1254 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1255 if (!ToInnerTypeOrErr)
1256 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001257
Balazs Keri3b30d652018-10-19 13:32:20 +00001258 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001259}
1260
Balazs Keri3b30d652018-10-19 13:32:20 +00001261ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1262 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1263 if (!ToDeclOrErr)
1264 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001265
Balazs Keri3b30d652018-10-19 13:32:20 +00001266 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001267}
1268
Balazs Keri3b30d652018-10-19 13:32:20 +00001269ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1270 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1271 if (!ToExprOrErr)
1272 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001273
Balazs Keri3b30d652018-10-19 13:32:20 +00001274 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001275}
1276
Balazs Keri3b30d652018-10-19 13:32:20 +00001277ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1278 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1279 if (!ToUnderlyingTypeOrErr)
1280 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001281
Balazs Keri3b30d652018-10-19 13:32:20 +00001282 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001283}
1284
Balazs Keri3b30d652018-10-19 13:32:20 +00001285ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001286 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001287 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1288 if (!ToExprOrErr)
1289 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001290
Balazs Keri3b30d652018-10-19 13:32:20 +00001291 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1292 if (!ToUnderlyingTypeOrErr)
1293 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001294
Balazs Keri3b30d652018-10-19 13:32:20 +00001295 return Importer.getToContext().getDecltypeType(
1296 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001297}
1298
Balazs Keri3b30d652018-10-19 13:32:20 +00001299ExpectedType
1300ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1301 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1302 if (!ToBaseTypeOrErr)
1303 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001304
Balazs Keri3b30d652018-10-19 13:32:20 +00001305 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1306 if (!ToUnderlyingTypeOrErr)
1307 return ToUnderlyingTypeOrErr.takeError();
1308
1309 return Importer.getToContext().getUnaryTransformType(
1310 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001311}
1312
Balazs Keri3b30d652018-10-19 13:32:20 +00001313ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001314 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001315 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1316 if (!ToDeducedTypeOrErr)
1317 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001318
Balazs Keri3b30d652018-10-19 13:32:20 +00001319 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1320 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001321 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001322}
1323
Balazs Keri3b30d652018-10-19 13:32:20 +00001324ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001325 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001326 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1327 if (!ToDeclOrErr)
1328 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001329
Balazs Keri3b30d652018-10-19 13:32:20 +00001330 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1331 if (!ToInjTypeOrErr)
1332 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001333
1334 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1335 // See comments in InjectedClassNameType definition for details
1336 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1337 enum {
1338 TypeAlignmentInBits = 4,
1339 TypeAlignment = 1 << TypeAlignmentInBits
1340 };
1341
1342 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001343 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001344}
1345
Balazs Keri3b30d652018-10-19 13:32:20 +00001346ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1347 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1348 if (!ToDeclOrErr)
1349 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001350
Balazs Keri3b30d652018-10-19 13:32:20 +00001351 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001352}
1353
Balazs Keri3b30d652018-10-19 13:32:20 +00001354ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1355 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1356 if (!ToDeclOrErr)
1357 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001358
Balazs Keri3b30d652018-10-19 13:32:20 +00001359 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001360}
1361
Balazs Keri3b30d652018-10-19 13:32:20 +00001362ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1363 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1364 if (!ToModifiedTypeOrErr)
1365 return ToModifiedTypeOrErr.takeError();
1366 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1367 if (!ToEquivalentTypeOrErr)
1368 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001369
1370 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001371 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001372}
1373
Balazs Keri3b30d652018-10-19 13:32:20 +00001374ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001375 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001376 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1377 if (!ToDeclOrErr)
1378 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001379
1380 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001381 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001382}
1383
Balazs Keri3b30d652018-10-19 13:32:20 +00001384ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001385 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001386 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1387 if (!ReplacedOrErr)
1388 return ReplacedOrErr.takeError();
1389 const TemplateTypeParmType *Replaced =
1390 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001391
Balazs Keri3b30d652018-10-19 13:32:20 +00001392 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1393 if (!ToReplacementTypeOrErr)
1394 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001395
1396 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001397 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001398}
1399
Balazs Keri3b30d652018-10-19 13:32:20 +00001400ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001401 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001402 auto ToTemplateOrErr = import(T->getTemplateName());
1403 if (!ToTemplateOrErr)
1404 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001405
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001406 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001407 if (Error Err = ImportTemplateArguments(
1408 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1409 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001410
Douglas Gregore2e50d332010-12-01 01:36:18 +00001411 QualType ToCanonType;
1412 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001413 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001414 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001415 if (ExpectedType TyOrErr = import(FromCanonType))
1416 ToCanonType = *TyOrErr;
1417 else
1418 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001419 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001420 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001421 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001422 ToCanonType);
1423}
1424
Balazs Keri3b30d652018-10-19 13:32:20 +00001425ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001426 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001427 auto ToQualifierOrErr = import(T->getQualifier());
1428 if (!ToQualifierOrErr)
1429 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001430
Balazs Keri3b30d652018-10-19 13:32:20 +00001431 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1432 if (!ToNamedTypeOrErr)
1433 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001434
Balazs Keri3b30d652018-10-19 13:32:20 +00001435 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1436 if (!ToOwnedTagDeclOrErr)
1437 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001438
Abramo Bagnara6150c882010-05-11 21:36:43 +00001439 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001440 *ToQualifierOrErr,
1441 *ToNamedTypeOrErr,
1442 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001443}
1444
Balazs Keri3b30d652018-10-19 13:32:20 +00001445ExpectedType
1446ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1447 ExpectedType ToPatternOrErr = import(T->getPattern());
1448 if (!ToPatternOrErr)
1449 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001450
Balazs Keri3b30d652018-10-19 13:32:20 +00001451 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001452 T->getNumExpansions());
1453}
1454
Balazs Keri3b30d652018-10-19 13:32:20 +00001455ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001456 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001457 auto ToQualifierOrErr = import(T->getQualifier());
1458 if (!ToQualifierOrErr)
1459 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001460
Balazs Keri3b30d652018-10-19 13:32:20 +00001461 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001462
1463 SmallVector<TemplateArgument, 2> ToPack;
1464 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001465 if (Error Err = ImportTemplateArguments(
1466 T->getArgs(), T->getNumArgs(), ToPack))
1467 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001468
1469 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001470 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001471}
1472
Balazs Keri3b30d652018-10-19 13:32:20 +00001473ExpectedType
1474ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1475 auto ToQualifierOrErr = import(T->getQualifier());
1476 if (!ToQualifierOrErr)
1477 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001478
1479 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001480
Balazs Keri3b30d652018-10-19 13:32:20 +00001481 QualType Canon;
1482 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1483 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1484 Canon = (*TyOrErr).getCanonicalType();
1485 else
1486 return TyOrErr.takeError();
1487 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001488
Balazs Keri3b30d652018-10-19 13:32:20 +00001489 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1490 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001491 Name, Canon);
1492}
1493
Balazs Keri3b30d652018-10-19 13:32:20 +00001494ExpectedType
1495ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1496 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1497 if (!ToDeclOrErr)
1498 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001499
Balazs Keri3b30d652018-10-19 13:32:20 +00001500 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001501}
1502
Balazs Keri3b30d652018-10-19 13:32:20 +00001503ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1504 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1505 if (!ToBaseTypeOrErr)
1506 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001507
Douglas Gregore9d95f12015-07-07 03:57:35 +00001508 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001509 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001510 if (ExpectedType TyOrErr = import(TypeArg))
1511 TypeArgs.push_back(*TyOrErr);
1512 else
1513 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001514 }
1515
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001516 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001517 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001518 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1519 Protocols.push_back(*ProtocolOrErr);
1520 else
1521 return ProtocolOrErr.takeError();
1522
Douglas Gregor96e578d2010-02-05 17:54:41 +00001523 }
1524
Balazs Keri3b30d652018-10-19 13:32:20 +00001525 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001526 Protocols,
1527 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001528}
1529
Balazs Keri3b30d652018-10-19 13:32:20 +00001530ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001531ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001532 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1533 if (!ToPointeeTypeOrErr)
1534 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001535
Balazs Keri3b30d652018-10-19 13:32:20 +00001536 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001537}
1538
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001539//----------------------------------------------------------------------------
1540// Import Declarations
1541//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001542Error ASTNodeImporter::ImportDeclParts(
1543 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1544 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001545 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1546 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1547 DeclContext *OrigDC = D->getDeclContext();
1548 FunctionDecl *FunDecl;
1549 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1550 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001551 auto getLeafPointeeType = [](const Type *T) {
1552 while (T->isPointerType() || T->isArrayType()) {
1553 T = T->getPointeeOrArrayElementType();
1554 }
1555 return T;
1556 };
1557 for (const ParmVarDecl *P : FunDecl->parameters()) {
1558 const Type *LeafT =
1559 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1560 auto *RT = dyn_cast<RecordType>(LeafT);
1561 if (RT && RT->getDecl() == D) {
1562 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1563 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001564 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001565 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001566 }
1567 }
1568
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001569 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001570 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1571 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001572
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001573 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001574 if (Error Err = importInto(Name, D->getDeclName()))
1575 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001576
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001577 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001578 if (Error Err = importInto(Loc, D->getLocation()))
1579 return Err;
1580
Sean Callanan59721b32015-04-28 18:41:46 +00001581 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Balazs Keri3b30d652018-10-19 13:32:20 +00001582
1583 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001584}
1585
Balazs Keri3b30d652018-10-19 13:32:20 +00001586Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001587 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001588 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001589
Balazs Keri3b30d652018-10-19 13:32:20 +00001590 if (!ToD)
1591 if (Error Err = importInto(ToD, FromD))
1592 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001593
Balazs Keri3b30d652018-10-19 13:32:20 +00001594 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1595 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1596 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1597 !ToRecord->getDefinition()) {
1598 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1599 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001600 }
1601 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001602 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001603 }
1604
Balazs Keri3b30d652018-10-19 13:32:20 +00001605 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1606 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001607 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001608 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1609 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001610 }
1611 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001612 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001613 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001614
1615 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001616}
1617
Balazs Keri3b30d652018-10-19 13:32:20 +00001618Error
1619ASTNodeImporter::ImportDeclarationNameLoc(
1620 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001621 // NOTE: To.Name and To.Loc are already imported.
1622 // We only have to import To.LocInfo.
1623 switch (To.getName().getNameKind()) {
1624 case DeclarationName::Identifier:
1625 case DeclarationName::ObjCZeroArgSelector:
1626 case DeclarationName::ObjCOneArgSelector:
1627 case DeclarationName::ObjCMultiArgSelector:
1628 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001629 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001630 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001631
1632 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001633 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1634 To.setCXXOperatorNameRange(*ToRangeOrErr);
1635 else
1636 return ToRangeOrErr.takeError();
1637 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001638 }
1639 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001640 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1641 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1642 else
1643 return LocOrErr.takeError();
1644 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001645 }
1646 case DeclarationName::CXXConstructorName:
1647 case DeclarationName::CXXDestructorName:
1648 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001649 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1650 To.setNamedTypeInfo(*ToTInfoOrErr);
1651 else
1652 return ToTInfoOrErr.takeError();
1653 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001654 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001655 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001656 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001657}
1658
Balazs Keri3b30d652018-10-19 13:32:20 +00001659Error
1660ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001661 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001662 auto ToDCOrErr = Importer.ImportContext(FromDC);
1663 return ToDCOrErr.takeError();
1664 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001665 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001666 for (auto *From : FromDC->decls()) {
1667 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001668 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001669 // Ignore the error, continue with next Decl.
1670 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001671 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001672 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001673
Balazs Keri3b30d652018-10-19 13:32:20 +00001674 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001675}
1676
Balazs Keri3b30d652018-10-19 13:32:20 +00001677Error ASTNodeImporter::ImportDeclContext(
1678 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1679 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1680 if (!ToDCOrErr)
1681 return ToDCOrErr.takeError();
1682 ToDC = *ToDCOrErr;
1683
1684 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1685 auto ToLexicalDCOrErr = Importer.ImportContext(
1686 FromD->getLexicalDeclContext());
1687 if (!ToLexicalDCOrErr)
1688 return ToLexicalDCOrErr.takeError();
1689 ToLexicalDC = *ToLexicalDCOrErr;
1690 } else
1691 ToLexicalDC = ToDC;
1692
1693 return Error::success();
1694}
1695
1696Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001697 const CXXRecordDecl *From, CXXRecordDecl *To) {
1698 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1699 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001700
Balazs Keri1d20cc22018-07-16 12:16:39 +00001701 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001702 if (FromM->isImplicit()) {
1703 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1704 if (!ToMOrErr)
1705 return ToMOrErr.takeError();
1706 }
1707
1708 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001709}
1710
Balazs Keri3b30d652018-10-19 13:32:20 +00001711static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1712 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001713 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001714 Decl *ToTypedef = Importer.Import(FromTypedef);
1715 if (!ToTypedef)
1716 return make_error<ImportError>();
1717 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToTypedef));
1718 // FIXME: This should be the final code.
1719 //if (Expected<Decl *> ToTypedefOrErr = Importer.Import(FromTypedef))
1720 // To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1721 //else
1722 // return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001723 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001724 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001725}
1726
Balazs Keri3b30d652018-10-19 13:32:20 +00001727Error ASTNodeImporter::ImportDefinition(
1728 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001729 if (To->getDefinition() || To->isBeingDefined()) {
1730 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001731 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001732
Balazs Keri3b30d652018-10-19 13:32:20 +00001733 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001734 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001735
Douglas Gregore2e50d332010-12-01 01:36:18 +00001736 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001737
Balazs Keri3b30d652018-10-19 13:32:20 +00001738 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1739 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001740
Douglas Gregore2e50d332010-12-01 01:36:18 +00001741 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001742 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1743 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001744
1745 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1746 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1747 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001748 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001749 ToData.Aggregate = FromData.Aggregate;
1750 ToData.PlainOldData = FromData.PlainOldData;
1751 ToData.Empty = FromData.Empty;
1752 ToData.Polymorphic = FromData.Polymorphic;
1753 ToData.Abstract = FromData.Abstract;
1754 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001755 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1756 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1757 ToData.HasBasesWithNonStaticDataMembers =
1758 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001759 ToData.HasPrivateFields = FromData.HasPrivateFields;
1760 ToData.HasProtectedFields = FromData.HasProtectedFields;
1761 ToData.HasPublicFields = FromData.HasPublicFields;
1762 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001763 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001764 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001765 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001766 ToData.HasUninitializedReferenceMember
1767 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001768 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001769 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1770 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001771 ToData.NeedOverloadResolutionForCopyConstructor
1772 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001773 ToData.NeedOverloadResolutionForMoveConstructor
1774 = FromData.NeedOverloadResolutionForMoveConstructor;
1775 ToData.NeedOverloadResolutionForMoveAssignment
1776 = FromData.NeedOverloadResolutionForMoveAssignment;
1777 ToData.NeedOverloadResolutionForDestructor
1778 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001779 ToData.DefaultedCopyConstructorIsDeleted
1780 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001781 ToData.DefaultedMoveConstructorIsDeleted
1782 = FromData.DefaultedMoveConstructorIsDeleted;
1783 ToData.DefaultedMoveAssignmentIsDeleted
1784 = FromData.DefaultedMoveAssignmentIsDeleted;
1785 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001786 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1787 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001788 ToData.HasConstexprNonCopyMoveConstructor
1789 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001790 ToData.HasDefaultedDefaultConstructor
1791 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001792 ToData.DefaultedDefaultConstructorIsConstexpr
1793 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001794 ToData.HasConstexprDefaultConstructor
1795 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001796 ToData.HasNonLiteralTypeFieldsOrBases
1797 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001798 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001799 ToData.UserProvidedDefaultConstructor
1800 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001801 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001802 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1803 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1804 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1805 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001806 ToData.ImplicitCopyAssignmentHasConstParam
1807 = FromData.ImplicitCopyAssignmentHasConstParam;
1808 ToData.HasDeclaredCopyConstructorWithConstParam
1809 = FromData.HasDeclaredCopyConstructorWithConstParam;
1810 ToData.HasDeclaredCopyAssignmentWithConstParam
1811 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001812
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001813 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001814 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001815 ExpectedType TyOrErr = import(Base1.getType());
1816 if (!TyOrErr)
1817 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001818
1819 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001820 if (Base1.isPackExpansion()) {
1821 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1822 EllipsisLoc = *LocOrErr;
1823 else
1824 return LocOrErr.takeError();
1825 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001826
1827 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001828 if (Error Err =
1829 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1830 return Err;
1831
1832 auto RangeOrErr = import(Base1.getSourceRange());
1833 if (!RangeOrErr)
1834 return RangeOrErr.takeError();
1835
1836 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1837 if (!TSIOrErr)
1838 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001839
Douglas Gregore2e50d332010-12-01 01:36:18 +00001840 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001841 new (Importer.getToContext()) CXXBaseSpecifier(
1842 *RangeOrErr,
1843 Base1.isVirtual(),
1844 Base1.isBaseOfClass(),
1845 Base1.getAccessSpecifierAsWritten(),
1846 *TSIOrErr,
1847 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001848 }
1849 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001850 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001851 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001852
Douglas Gregor2e15c842012-02-01 21:00:38 +00001853 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001854 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1855 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001856
Douglas Gregore2e50d332010-12-01 01:36:18 +00001857 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001858 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001859}
1860
Balazs Keri3b30d652018-10-19 13:32:20 +00001861Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001862 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001863 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001864
Gabor Martonac3a5d62018-09-17 12:04:52 +00001865 Expr *FromInit = From->getInit();
1866 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001867 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001868
Balazs Keri3b30d652018-10-19 13:32:20 +00001869 ExpectedExpr ToInitOrErr = import(FromInit);
1870 if (!ToInitOrErr)
1871 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001872
Balazs Keri3b30d652018-10-19 13:32:20 +00001873 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001874 if (From->isInitKnownICE()) {
1875 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1876 Eval->CheckedICE = true;
1877 Eval->IsICE = From->isInitICE();
1878 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001879
1880 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001881 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001882}
1883
Balazs Keri3b30d652018-10-19 13:32:20 +00001884Error ASTNodeImporter::ImportDefinition(
1885 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001886 if (To->getDefinition() || To->isBeingDefined()) {
1887 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001888 return ImportDeclContext(From, /*ForceImport=*/true);
1889 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001890 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001891
Douglas Gregord451ea92011-07-29 23:31:30 +00001892 To->startDefinition();
1893
Balazs Keri3b30d652018-10-19 13:32:20 +00001894 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1895 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001896
Balazs Keri3b30d652018-10-19 13:32:20 +00001897 ExpectedType ToTypeOrErr =
1898 import(Importer.getFromContext().getTypeDeclType(From));
1899 if (!ToTypeOrErr)
1900 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001901
Balazs Keri3b30d652018-10-19 13:32:20 +00001902 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1903 if (!ToPromotionTypeOrErr)
1904 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001905
1906 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001907 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1908 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001909
Douglas Gregord451ea92011-07-29 23:31:30 +00001910 // FIXME: we might need to merge the number of positive or negative bits
1911 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001912 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001913 From->getNumPositiveBits(),
1914 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001915 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001916}
1917
Balazs Keri3b30d652018-10-19 13:32:20 +00001918// FIXME: Remove this, use `import` instead.
1919Expected<TemplateParameterList *> ASTNodeImporter::ImportTemplateParameterList(
1920 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001921 SmallVector<NamedDecl *, 4> ToParams(Params->size());
Balazs Keri3b30d652018-10-19 13:32:20 +00001922 if (Error Err = ImportContainerChecked(*Params, ToParams))
1923 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001924
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001925 Expr *ToRequiresClause;
1926 if (Expr *const R = Params->getRequiresClause()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001927 if (Error Err = importInto(ToRequiresClause, R))
1928 return std::move(Err);
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001929 } else {
1930 ToRequiresClause = nullptr;
1931 }
1932
Balazs Keri3b30d652018-10-19 13:32:20 +00001933 auto ToTemplateLocOrErr = import(Params->getTemplateLoc());
1934 if (!ToTemplateLocOrErr)
1935 return ToTemplateLocOrErr.takeError();
1936 auto ToLAngleLocOrErr = import(Params->getLAngleLoc());
1937 if (!ToLAngleLocOrErr)
1938 return ToLAngleLocOrErr.takeError();
1939 auto ToRAngleLocOrErr = import(Params->getRAngleLoc());
1940 if (!ToRAngleLocOrErr)
1941 return ToRAngleLocOrErr.takeError();
1942
1943 return TemplateParameterList::Create(
1944 Importer.getToContext(),
1945 *ToTemplateLocOrErr,
1946 *ToLAngleLocOrErr,
1947 ToParams,
1948 *ToRAngleLocOrErr,
1949 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001950}
1951
Balazs Keri3b30d652018-10-19 13:32:20 +00001952Error ASTNodeImporter::ImportTemplateArguments(
1953 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1954 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001955 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001956 if (auto ToOrErr = import(FromArgs[I]))
1957 ToArgs.push_back(*ToOrErr);
1958 else
1959 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001960 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001961
Balazs Keri3b30d652018-10-19 13:32:20 +00001962 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001963}
1964
Balazs Keri3b30d652018-10-19 13:32:20 +00001965// FIXME: Do not forget to remove this and use only 'import'.
1966Expected<TemplateArgument>
1967ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1968 return import(From);
1969}
1970
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001971template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001972Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001973 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1974 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001975 if (auto ToLocOrErr = import(FromLoc))
1976 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001977 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001978 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001979 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001980 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001981}
1982
Gabor Marton26f72a92018-07-12 09:42:05 +00001983static StructuralEquivalenceKind
1984getStructuralEquivalenceKind(const ASTImporter &Importer) {
1985 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1986 : StructuralEquivalenceKind::Default;
1987}
1988
Gabor Marton950fb572018-07-17 12:39:27 +00001989bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1990 StructuralEquivalenceContext Ctx(
1991 Importer.getFromContext(), Importer.getToContext(),
1992 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1993 false, Complain);
1994 return Ctx.IsEquivalent(From, To);
1995}
1996
1997bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001998 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001999 // Eliminate a potential failure point where we attempt to re-import
2000 // something we're trying to import while completing ToRecord.
2001 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2002 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002003 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002004 if (ToOriginRecord)
2005 ToRecord = ToOriginRecord;
2006 }
2007
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002008 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002009 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002010 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002011 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002012 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002013 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002014}
2015
Larisse Voufo39a1e502013-08-06 01:03:05 +00002016bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2017 bool Complain) {
2018 StructuralEquivalenceContext Ctx(
2019 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002020 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2021 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002022 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002023}
2024
Douglas Gregor98c10182010-02-12 22:17:39 +00002025bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002026 StructuralEquivalenceContext Ctx(
2027 Importer.getFromContext(), Importer.getToContext(),
2028 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002029 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002030}
2031
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002032bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2033 FunctionTemplateDecl *To) {
2034 StructuralEquivalenceContext Ctx(
2035 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002036 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2037 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002038 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002039}
2040
Balazs Keric7797c42018-07-11 09:37:24 +00002041bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2042 StructuralEquivalenceContext Ctx(
2043 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002044 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2045 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002046 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002047}
2048
Douglas Gregor91155082012-11-14 22:29:20 +00002049bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002050 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002051 const llvm::APSInt &FromVal = FromEC->getInitVal();
2052 const llvm::APSInt &ToVal = ToEC->getInitVal();
2053
2054 return FromVal.isSigned() == ToVal.isSigned() &&
2055 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2056 FromVal == ToVal;
2057}
2058
2059bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002060 ClassTemplateDecl *To) {
2061 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2062 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002063 Importer.getNonEquivalentDecls(),
2064 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002065 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002066}
2067
Larisse Voufo39a1e502013-08-06 01:03:05 +00002068bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2069 VarTemplateDecl *To) {
2070 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2071 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002072 Importer.getNonEquivalentDecls(),
2073 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002074 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002075}
2076
Balazs Keri3b30d652018-10-19 13:32:20 +00002077ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002078 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002079 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002080 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002081}
2082
Balazs Keri3b30d652018-10-19 13:32:20 +00002083ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2084 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2085 << D->getDeclKindName();
2086 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2087}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002088
Balazs Keri3b30d652018-10-19 13:32:20 +00002089ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2090 // Import the context of this declaration.
2091 DeclContext *DC, *LexicalDC;
2092 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2093 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002094
2095 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002096 ExpectedSLoc LocOrErr = import(D->getLocation());
2097 if (!LocOrErr)
2098 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002099
Gabor Marton26f72a92018-07-12 09:42:05 +00002100 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002101 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002102 return ToD;
2103
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002104 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002105 LexicalDC->addDeclInternal(ToD);
2106 return ToD;
2107}
2108
Balazs Keri3b30d652018-10-19 13:32:20 +00002109ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002110 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002111 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002112
Gabor Marton26f72a92018-07-12 09:42:05 +00002113 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002114
Sean Callanan65198272011-11-17 23:20:56 +00002115 return ToD;
2116}
2117
Balazs Keri3b30d652018-10-19 13:32:20 +00002118ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2119 ExpectedSLoc LocOrErr = import(D->getLocation());
2120 if (!LocOrErr)
2121 return LocOrErr.takeError();
2122 auto ColonLocOrErr = import(D->getColonLoc());
2123 if (!ColonLocOrErr)
2124 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002125
2126 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002127 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2128 if (!DCOrErr)
2129 return DCOrErr.takeError();
2130 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002131
Gabor Marton26f72a92018-07-12 09:42:05 +00002132 AccessSpecDecl *ToD;
2133 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002134 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002135 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002136
2137 // Lexical DeclContext and Semantic DeclContext
2138 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002139 ToD->setLexicalDeclContext(DC);
2140 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002141
Gabor Marton26f72a92018-07-12 09:42:05 +00002142 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002143}
2144
Balazs Keri3b30d652018-10-19 13:32:20 +00002145ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2146 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2147 if (!DCOrErr)
2148 return DCOrErr.takeError();
2149 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002150 DeclContext *LexicalDC = DC;
2151
Balazs Keri3b30d652018-10-19 13:32:20 +00002152 SourceLocation ToLocation, ToRParenLoc;
2153 Expr *ToAssertExpr;
2154 StringLiteral *ToMessage;
2155 if (auto Imp = importSeq(
2156 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2157 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2158 else
2159 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002160
Gabor Marton26f72a92018-07-12 09:42:05 +00002161 StaticAssertDecl *ToD;
2162 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002163 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2164 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002165 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002166
2167 ToD->setLexicalDeclContext(LexicalDC);
2168 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002169 return ToD;
2170}
2171
Balazs Keri3b30d652018-10-19 13:32:20 +00002172ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002173 // Import the major distinguishing characteristics of this namespace.
2174 DeclContext *DC, *LexicalDC;
2175 DeclarationName Name;
2176 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002177 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002178 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2179 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002180 if (ToD)
2181 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002182
2183 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002184 if (!Name) {
2185 // This is an anonymous namespace. Adopt an existing anonymous
2186 // namespace if we can.
2187 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002188 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002189 MergeWithNamespace = TU->getAnonymousNamespace();
2190 else
2191 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2192 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002193 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002194 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002195 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002196 for (auto *FoundDecl : FoundDecls) {
2197 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002198 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002199
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002200 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002201 MergeWithNamespace = FoundNS;
2202 ConflictingDecls.clear();
2203 break;
2204 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002205
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002206 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002207 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002208
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002209 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002210 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002211 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002212 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002213 if (!Name)
2214 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002215 }
2216 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002217
Balazs Keri3b30d652018-10-19 13:32:20 +00002218 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2219 if (!BeginLocOrErr)
2220 return BeginLocOrErr.takeError();
2221
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002222 // Create the "to" namespace, if needed.
2223 NamespaceDecl *ToNamespace = MergeWithNamespace;
2224 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002225 if (GetImportedOrCreateDecl(
2226 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002227 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002228 /*PrevDecl=*/nullptr))
2229 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002230 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002231 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002232
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002233 // If this is an anonymous namespace, register it as the anonymous
2234 // namespace within its context.
2235 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002236 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002237 TU->setAnonymousNamespace(ToNamespace);
2238 else
2239 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2240 }
2241 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002242 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002243
Balazs Keri3b30d652018-10-19 13:32:20 +00002244 if (Error Err = ImportDeclContext(D))
2245 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002246
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002247 return ToNamespace;
2248}
2249
Balazs Keri3b30d652018-10-19 13:32:20 +00002250ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002251 // Import the major distinguishing characteristics of this namespace.
2252 DeclContext *DC, *LexicalDC;
2253 DeclarationName Name;
2254 SourceLocation Loc;
2255 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002256 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2257 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002258 if (LookupD)
2259 return LookupD;
2260
2261 // NOTE: No conflict resolution is done for namespace aliases now.
2262
Balazs Keri3b30d652018-10-19 13:32:20 +00002263 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2264 NestedNameSpecifierLoc ToQualifierLoc;
2265 NamespaceDecl *ToNamespace;
2266 if (auto Imp = importSeq(
2267 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2268 D->getTargetNameLoc(), D->getNamespace()))
2269 std::tie(
2270 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2271 ToNamespace) = *Imp;
2272 else
2273 return Imp.takeError();
2274 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002275
Gabor Marton26f72a92018-07-12 09:42:05 +00002276 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002277 if (GetImportedOrCreateDecl(
2278 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2279 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002280 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002281
2282 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002283 LexicalDC->addDeclInternal(ToD);
2284
2285 return ToD;
2286}
2287
Balazs Keri3b30d652018-10-19 13:32:20 +00002288ExpectedDecl
2289ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002290 // Import the major distinguishing characteristics of this typedef.
2291 DeclContext *DC, *LexicalDC;
2292 DeclarationName Name;
2293 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002294 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002295 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2296 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002297 if (ToD)
2298 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002299
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002300 // If this typedef is not in block scope, determine whether we've
2301 // seen a typedef with the same name (that we can merge with) or any
2302 // other entity by that name (which name lookup could conflict with).
2303 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002304 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002305 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002306 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002307 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002308 for (auto *FoundDecl : FoundDecls) {
2309 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002310 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002311 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002312 if (Importer.IsStructurallyEquivalent(
2313 D->getUnderlyingType(), FoundTypedef->getUnderlyingType()))
2314 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002315 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002316
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002317 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002318 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002319
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002320 if (!ConflictingDecls.empty()) {
2321 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002322 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002323 ConflictingDecls.size());
2324 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002325 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002326 }
2327 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002328
Balazs Keri3b30d652018-10-19 13:32:20 +00002329 QualType ToUnderlyingType;
2330 TypeSourceInfo *ToTypeSourceInfo;
2331 SourceLocation ToBeginLoc;
2332 if (auto Imp = importSeq(
2333 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2334 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2335 else
2336 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002337
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002338 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002339 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002340 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002341 if (IsAlias) {
2342 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002343 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2344 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002345 return ToTypedef;
2346 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002347 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2348 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002349 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002350
Douglas Gregordd483172010-02-22 17:42:47 +00002351 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002352 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002353
2354 // Templated declarations should not appear in DeclContext.
2355 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2356 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2357 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002358
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002359 return ToTypedef;
2360}
2361
Balazs Keri3b30d652018-10-19 13:32:20 +00002362ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002363 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2364}
2365
Balazs Keri3b30d652018-10-19 13:32:20 +00002366ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002367 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2368}
2369
Balazs Keri3b30d652018-10-19 13:32:20 +00002370ExpectedDecl
2371ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002372 // Import the major distinguishing characteristics of this typedef.
2373 DeclContext *DC, *LexicalDC;
2374 DeclarationName Name;
2375 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002376 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002377 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2378 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002379 if (FoundD)
2380 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002381
2382 // If this typedef is not in block scope, determine whether we've
2383 // seen a typedef with the same name (that we can merge with) or any
2384 // other entity by that name (which name lookup could conflict with).
2385 if (!DC->isFunctionOrMethod()) {
2386 SmallVector<NamedDecl *, 4> ConflictingDecls;
2387 unsigned IDNS = Decl::IDNS_Ordinary;
2388 SmallVector<NamedDecl *, 2> FoundDecls;
2389 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002390 for (auto *FoundDecl : FoundDecls) {
2391 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002392 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002393 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002394 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002395 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002396 }
2397
2398 if (!ConflictingDecls.empty()) {
2399 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2400 ConflictingDecls.data(),
2401 ConflictingDecls.size());
2402 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002403 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002404 }
2405 }
2406
Balazs Keri3b30d652018-10-19 13:32:20 +00002407 TemplateParameterList *ToTemplateParameters;
2408 TypeAliasDecl *ToTemplatedDecl;
2409 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2410 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2411 else
2412 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002413
Gabor Marton26f72a92018-07-12 09:42:05 +00002414 TypeAliasTemplateDecl *ToAlias;
2415 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002416 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002417 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002418
Balazs Keri3b30d652018-10-19 13:32:20 +00002419 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002420
Gabor Horvath7a91c082017-11-14 11:30:38 +00002421 ToAlias->setAccess(D->getAccess());
2422 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002423 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002424 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002425}
2426
Balazs Keri3b30d652018-10-19 13:32:20 +00002427ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002428 // Import the major distinguishing characteristics of this label.
2429 DeclContext *DC, *LexicalDC;
2430 DeclarationName Name;
2431 SourceLocation Loc;
2432 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002433 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2434 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002435 if (ToD)
2436 return ToD;
2437
2438 assert(LexicalDC->isFunctionOrMethod());
2439
Gabor Marton26f72a92018-07-12 09:42:05 +00002440 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002441 if (D->isGnuLocal()) {
2442 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2443 if (!BeginLocOrErr)
2444 return BeginLocOrErr.takeError();
2445 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2446 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2447 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002448
Balazs Keri3b30d652018-10-19 13:32:20 +00002449 } else {
2450 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2451 Name.getAsIdentifierInfo()))
2452 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002453
Balazs Keri3b30d652018-10-19 13:32:20 +00002454 }
2455
2456 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2457 if (!ToStmtOrErr)
2458 return ToStmtOrErr.takeError();
2459
2460 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002461 ToLabel->setLexicalDeclContext(LexicalDC);
2462 LexicalDC->addDeclInternal(ToLabel);
2463 return ToLabel;
2464}
2465
Balazs Keri3b30d652018-10-19 13:32:20 +00002466ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002467 // Import the major distinguishing characteristics of this enum.
2468 DeclContext *DC, *LexicalDC;
2469 DeclarationName Name;
2470 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002471 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002472 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2473 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002474 if (ToD)
2475 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002476
Douglas Gregor98c10182010-02-12 22:17:39 +00002477 // Figure out what enum name we're looking for.
2478 unsigned IDNS = Decl::IDNS_Tag;
2479 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002480 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002481 if (Error Err = importInto(
2482 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2483 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002484 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002485 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002486 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002487
Douglas Gregor98c10182010-02-12 22:17:39 +00002488 // We may already have an enum of the same name; try to find and match it.
2489 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002490 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002491 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002492 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002493 for (auto *FoundDecl : FoundDecls) {
2494 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002495 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002496
Balazs Keri3b30d652018-10-19 13:32:20 +00002497 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002498 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002499 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002500 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002501
Balazs Keri3b30d652018-10-19 13:32:20 +00002502 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002503 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002504 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002505 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002506
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002507 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002508 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002509
Douglas Gregor98c10182010-02-12 22:17:39 +00002510 if (!ConflictingDecls.empty()) {
2511 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002512 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002513 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002514 if (!Name)
2515 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002516 }
2517 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002518
Balazs Keri3b30d652018-10-19 13:32:20 +00002519 SourceLocation ToBeginLoc;
2520 NestedNameSpecifierLoc ToQualifierLoc;
2521 QualType ToIntegerType;
2522 if (auto Imp = importSeq(
2523 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2524 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2525 else
2526 return Imp.takeError();
2527
Douglas Gregor98c10182010-02-12 22:17:39 +00002528 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002529 EnumDecl *D2;
2530 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002531 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002532 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2533 D->isScopedUsingClassTag(), D->isFixed()))
2534 return D2;
2535
Balazs Keri3b30d652018-10-19 13:32:20 +00002536 D2->setQualifierInfo(ToQualifierLoc);
2537 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002538 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002539 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002540 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002541
Douglas Gregor98c10182010-02-12 22:17:39 +00002542 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002543 if (D->isCompleteDefinition())
2544 if (Error Err = ImportDefinition(D, D2))
2545 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002546
Douglas Gregor3996e242010-02-15 22:01:00 +00002547 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002548}
2549
Balazs Keri3b30d652018-10-19 13:32:20 +00002550ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002551 bool IsFriendTemplate = false;
2552 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2553 IsFriendTemplate =
2554 DCXX->getDescribedClassTemplate() &&
2555 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2556 Decl::FOK_None;
2557 }
2558
Douglas Gregor5c73e912010-02-11 00:48:18 +00002559 // If this record has a definition in the translation unit we're coming from,
2560 // but this particular declaration is not that definition, import the
2561 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002562 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002563 if (Definition && Definition != D &&
Balazs Keri0c23dc52018-08-13 13:08:37 +00002564 // Friend template declaration must be imported on its own.
2565 !IsFriendTemplate &&
Gabor Martona3af5672018-05-23 14:24:02 +00002566 // In contrast to a normal CXXRecordDecl, the implicit
2567 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2568 // The definition of the implicit CXXRecordDecl in this case is the
2569 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2570 // condition in order to be able to import the implict Decl.
2571 !D->isImplicit()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002572 ExpectedDecl ImportedDefOrErr = import(Definition);
2573 if (!ImportedDefOrErr)
2574 return ImportedDefOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002575
Balazs Keri3b30d652018-10-19 13:32:20 +00002576 return Importer.MapImported(D, *ImportedDefOrErr);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002577 }
Gabor Martona3af5672018-05-23 14:24:02 +00002578
Douglas Gregor5c73e912010-02-11 00:48:18 +00002579 // Import the major distinguishing characteristics of this record.
2580 DeclContext *DC, *LexicalDC;
2581 DeclarationName Name;
2582 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002583 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002584 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2585 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002586 if (ToD)
2587 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002588
Douglas Gregor5c73e912010-02-11 00:48:18 +00002589 // Figure out what structure name we're looking for.
2590 unsigned IDNS = Decl::IDNS_Tag;
2591 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002592 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002593 if (Error Err = importInto(
2594 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2595 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002596 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002597 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002598 IDNS |= Decl::IDNS_Ordinary;
2599
2600 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002601 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002602 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002603 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002604 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002605 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002606 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002607
2608 if (!FoundDecls.empty()) {
2609 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2610 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2611 D->getASTContext().getExternalSource()->CompleteType(D);
2612 }
2613
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002614 for (auto *FoundDecl : FoundDecls) {
2615 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002616 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002617
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002618 Decl *Found = FoundDecl;
2619 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2620 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002621 Found = Tag->getDecl();
2622 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002623
2624 if (D->getDescribedTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002625 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Martona0df7a92018-05-30 09:19:26 +00002626 Found = Template->getTemplatedDecl();
Balazs Keri3b30d652018-10-19 13:32:20 +00002627 } else {
2628 ConflictingDecls.push_back(FoundDecl);
Gabor Martona0df7a92018-05-30 09:19:26 +00002629 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002630 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002631 }
2632
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002633 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002634 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002635 if (!IsStructuralMatch(D, FoundRecord, false))
2636 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002637 } else {
2638 if (!IsStructuralMatch(D, FoundRecord)) {
2639 ConflictingDecls.push_back(FoundDecl);
2640 continue;
2641 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002642 }
2643
Sean Callanan9092d472017-05-13 00:46:33 +00002644 PrevDecl = FoundRecord;
2645
Douglas Gregor25791052010-02-12 00:09:27 +00002646 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002647 if ((SearchName && !D->isCompleteDefinition() && !IsFriendTemplate)
Douglas Gregordd6006f2012-07-17 21:16:27 +00002648 || (D->isCompleteDefinition() &&
2649 D->isAnonymousStructOrUnion()
Balazs Keri3b30d652018-10-19 13:32:20 +00002650 == FoundDef->isAnonymousStructOrUnion())) {
Douglas Gregor25791052010-02-12 00:09:27 +00002651 // The record types structurally match, or the "from" translation
2652 // unit only had a forward declaration anyway; call it the same
2653 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002654 // FIXME: Structural equivalence check should check for same
2655 // user-defined methods.
2656 Importer.MapImported(D, FoundDef);
2657 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2658 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2659 assert(FoundCXX && "Record type mismatch");
2660
2661 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2662 // FoundDef may not have every implicit method that D has
2663 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002664 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2665 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002666 }
2667 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002668 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002669 if (IsFriendTemplate)
2670 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002671 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002672 // We have a forward declaration of this type, so adopt that forward
2673 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002674
Sean Callananc94711c2014-03-04 18:11:50 +00002675 // If one or both can be completed from external storage then try one
2676 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002677
Sean Callananc94711c2014-03-04 18:11:50 +00002678 if (FoundRecord->hasExternalLexicalStorage() &&
2679 !FoundRecord->isCompleteDefinition())
2680 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2681 if (D->hasExternalLexicalStorage())
2682 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002683
Sean Callananc94711c2014-03-04 18:11:50 +00002684 if (FoundRecord->isCompleteDefinition() &&
2685 D->isCompleteDefinition() &&
Balazs Keri3b30d652018-10-19 13:32:20 +00002686 !IsStructuralMatch(D, FoundRecord)) {
2687 ConflictingDecls.push_back(FoundDecl);
Sean Callananc94711c2014-03-04 18:11:50 +00002688 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002689 }
Balazs Keri0c23dc52018-08-13 13:08:37 +00002690
Douglas Gregor25791052010-02-12 00:09:27 +00002691 AdoptDecl = FoundRecord;
2692 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002693 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002694
2695 continue;
2696 } else if (isa<ValueDecl>(Found))
2697 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002698
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002699 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002700 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002701
Douglas Gregordd6006f2012-07-17 21:16:27 +00002702 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002703 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002704 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002705 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002706 if (!Name)
2707 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002708 }
2709 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002710
Balazs Keri3b30d652018-10-19 13:32:20 +00002711 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2712 if (!BeginLocOrErr)
2713 return BeginLocOrErr.takeError();
2714
Douglas Gregor5c73e912010-02-11 00:48:18 +00002715 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002716 RecordDecl *D2 = AdoptDecl;
2717 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002718 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002719 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002720 if (DCXX->isLambda()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002721 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2722 if (!TInfoOrErr)
2723 return TInfoOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00002724 if (GetImportedOrCreateSpecialDecl(
2725 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002726 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002727 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2728 return D2CXX;
Balazs Keri3b30d652018-10-19 13:32:20 +00002729 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2730 if (!CDeclOrErr)
2731 return CDeclOrErr.takeError();
2732 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002733 } else if (DCXX->isInjectedClassName()) {
2734 // We have to be careful to do a similar dance to the one in
2735 // Sema::ActOnStartCXXMemberDeclarations
2736 CXXRecordDecl *const PrevDecl = nullptr;
2737 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002738 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002739 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002740 Name.getAsIdentifierInfo(), PrevDecl,
2741 DelayTypeCreation))
2742 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002743 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002744 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002745 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002746 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002747 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002748 Name.getAsIdentifierInfo(),
2749 cast_or_null<CXXRecordDecl>(PrevDecl)))
2750 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002751 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002752
Douglas Gregor3996e242010-02-15 22:01:00 +00002753 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002754 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002755 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002756 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002757 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002758
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002759 if (ClassTemplateDecl *FromDescribed =
2760 DCXX->getDescribedClassTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002761 ClassTemplateDecl *ToDescribed;
2762 if (Error Err = importInto(ToDescribed, FromDescribed))
2763 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002764 D2CXX->setDescribedClassTemplate(ToDescribed);
Balazs Keri0c23dc52018-08-13 13:08:37 +00002765 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
Gabor Marton5915777e2018-06-26 13:44:24 +00002766 // In a record describing a template the type should be an
2767 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2768 // previously set type to the correct value here (ToDescribed is not
2769 // available at record create).
2770 // FIXME: The previous type is cleared but not removed from
2771 // ASTContext's internal storage.
2772 CXXRecordDecl *Injected = nullptr;
2773 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2774 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2775 if (Record && Record->isInjectedClassName()) {
2776 Injected = Record;
2777 break;
2778 }
2779 }
2780 D2CXX->setTypeForDecl(nullptr);
2781 Importer.getToContext().getInjectedClassNameType(D2CXX,
2782 ToDescribed->getInjectedClassNameSpecialization());
2783 if (Injected) {
2784 Injected->setTypeForDecl(nullptr);
2785 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2786 }
2787 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002788 } else if (MemberSpecializationInfo *MemberInfo =
2789 DCXX->getMemberSpecializationInfo()) {
2790 TemplateSpecializationKind SK =
2791 MemberInfo->getTemplateSpecializationKind();
2792 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002793
2794 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2795 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2796 else
2797 return ToInstOrErr.takeError();
2798
2799 if (ExpectedSLoc POIOrErr =
2800 import(MemberInfo->getPointOfInstantiation()))
2801 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2802 *POIOrErr);
2803 else
2804 return POIOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002805 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002806
Douglas Gregor25791052010-02-12 00:09:27 +00002807 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002808 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002809 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002810 Name.getAsIdentifierInfo(), PrevDecl))
2811 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002812 D2->setLexicalDeclContext(LexicalDC);
2813 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002814 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002815
Balazs Keri3b30d652018-10-19 13:32:20 +00002816 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2817 D2->setQualifierInfo(*QualifierLocOrErr);
2818 else
2819 return QualifierLocOrErr.takeError();
2820
Douglas Gregordd6006f2012-07-17 21:16:27 +00002821 if (D->isAnonymousStructOrUnion())
2822 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002823 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002824
2825 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002826
Balazs Keri3b30d652018-10-19 13:32:20 +00002827 if (D->isCompleteDefinition())
2828 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2829 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002830
Douglas Gregor3996e242010-02-15 22:01:00 +00002831 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002832}
2833
Balazs Keri3b30d652018-10-19 13:32:20 +00002834ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002835 // Import the major distinguishing characteristics of this enumerator.
2836 DeclContext *DC, *LexicalDC;
2837 DeclarationName Name;
2838 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002839 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002840 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2841 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002842 if (ToD)
2843 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002844
Fangrui Song6907ce22018-07-30 19:24:48 +00002845 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002846 // in the same context.
2847 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002848 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002849 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002850 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002851 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002852 for (auto *FoundDecl : FoundDecls) {
2853 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002854 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002855
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002856 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002857 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002858 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002859 }
2860
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002861 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002862 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002863
Douglas Gregor98c10182010-02-12 22:17:39 +00002864 if (!ConflictingDecls.empty()) {
2865 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002866 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002867 ConflictingDecls.size());
2868 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002869 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002870 }
2871 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002872
Balazs Keri3b30d652018-10-19 13:32:20 +00002873 ExpectedType TypeOrErr = import(D->getType());
2874 if (!TypeOrErr)
2875 return TypeOrErr.takeError();
2876
2877 ExpectedExpr InitOrErr = import(D->getInitExpr());
2878 if (!InitOrErr)
2879 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002880
Gabor Marton26f72a92018-07-12 09:42:05 +00002881 EnumConstantDecl *ToEnumerator;
2882 if (GetImportedOrCreateDecl(
2883 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002884 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002885 return ToEnumerator;
2886
Douglas Gregordd483172010-02-22 17:42:47 +00002887 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002888 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002889 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002890 return ToEnumerator;
2891}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002892
Balazs Keri3b30d652018-10-19 13:32:20 +00002893Error ASTNodeImporter::ImportTemplateInformation(
2894 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002895 switch (FromFD->getTemplatedKind()) {
2896 case FunctionDecl::TK_NonTemplate:
2897 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002898 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002899
2900 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002901 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002902
2903 if (Expected<FunctionDecl *> InstFDOrErr =
2904 import(FromFD->getInstantiatedFromMemberFunction()))
2905 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2906 else
2907 return InstFDOrErr.takeError();
2908
2909 if (ExpectedSLoc POIOrErr = import(
2910 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2911 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2912 else
2913 return POIOrErr.takeError();
2914
2915 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002916 }
2917
2918 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002919 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002920 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002921 if (!FunctionAndArgsOrErr)
2922 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002923
2924 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002925 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002926
Gabor Marton5254e642018-06-27 13:32:50 +00002927 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002928 TemplateArgumentListInfo ToTAInfo;
2929 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002930 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002931 if (Error Err = ImportTemplateArgumentListInfo(
2932 *FromTAArgsAsWritten, ToTAInfo))
2933 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002934
Balazs Keri3b30d652018-10-19 13:32:20 +00002935 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2936 if (!POIOrErr)
2937 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002938
Gabor Marton5254e642018-06-27 13:32:50 +00002939 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002940 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002941 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2942 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2943 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002944 }
2945
2946 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2947 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2948 UnresolvedSet<8> TemplDecls;
2949 unsigned NumTemplates = FromInfo->getNumTemplates();
2950 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002951 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2952 import(FromInfo->getTemplate(I)))
2953 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002954 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002955 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002956 }
2957
2958 // Import TemplateArgumentListInfo.
2959 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002960 if (Error Err = ImportTemplateArgumentListInfo(
2961 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2962 llvm::makeArrayRef(
2963 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2964 ToTAInfo))
2965 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002966
2967 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2968 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002969 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002970 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002971 }
Sam McCallfdc32072018-01-26 12:06:44 +00002972 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002973}
2974
Balazs Keri3b30d652018-10-19 13:32:20 +00002975Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002976ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002977 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002978 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002979 if (!FunctionAndArgsOrErr)
2980 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002981
Balazs Keri3b30d652018-10-19 13:32:20 +00002982 FunctionTemplateDecl *Template;
2983 TemplateArgsTy ToTemplArgs;
2984 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002985 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002986 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002987 return FoundSpec;
2988}
2989
Balazs Keri3b30d652018-10-19 13:32:20 +00002990ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002991
Balazs Keri3b30d652018-10-19 13:32:20 +00002992 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002993 auto RedeclIt = Redecls.begin();
2994 // Import the first part of the decl chain. I.e. import all previous
2995 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002996 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2997 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2998 if (!ToRedeclOrErr)
2999 return ToRedeclOrErr.takeError();
3000 }
Gabor Marton5254e642018-06-27 13:32:50 +00003001 assert(*RedeclIt == D);
3002
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003003 // Import the major distinguishing characteristics of this function.
3004 DeclContext *DC, *LexicalDC;
3005 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003006 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003007 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003008 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3009 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003010 if (ToD)
3011 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003012
Gabor Marton5254e642018-06-27 13:32:50 +00003013 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003014 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003015
Gabor Marton5254e642018-06-27 13:32:50 +00003016 // If this is a function template specialization, then try to find the same
3017 // existing specialization in the "to" context. The localUncachedLookup
3018 // below will not find any specialization, but would find the primary
3019 // template; thus, we have to skip normal lookup in case of specializations.
3020 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3021 if (D->getTemplatedKind() ==
3022 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003023 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3024 if (!FoundFunctionOrErr)
3025 return FoundFunctionOrErr.takeError();
3026 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
3027 if (D->doesThisDeclarationHaveABody() && FoundFunction->hasBody())
3028 return Importer.MapImported(D, FoundFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003029 FoundByLookup = FoundFunction;
3030 }
3031 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003032 // Try to find a function in our own ("to") context with the same name, same
3033 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003034 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003035 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003036 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003037 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003038 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003039 for (auto *FoundDecl : FoundDecls) {
3040 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003041 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003042
Balazs Keria35798d2018-07-17 09:52:41 +00003043 // If template was found, look at the templated function.
3044 if (FromFT) {
3045 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
3046 FoundDecl = Template->getTemplatedDecl();
3047 else
3048 continue;
3049 }
3050
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003051 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003052 if (FoundFunction->hasExternalFormalLinkage() &&
3053 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00003054 if (IsStructuralMatch(D, FoundFunction)) {
3055 const FunctionDecl *Definition = nullptr;
3056 if (D->doesThisDeclarationHaveABody() &&
3057 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003058 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00003059 D, const_cast<FunctionDecl *>(Definition));
3060 }
3061 FoundByLookup = FoundFunction;
3062 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003063 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003064
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003065 // FIXME: Check for overloading more carefully, e.g., by boosting
3066 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003067
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003068 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003069 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003070 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003071
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003072 // Complain about inconsistent function types.
3073 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003074 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003075 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003076 diag::note_odr_value_here)
3077 << FoundFunction->getType();
3078 }
3079 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003080
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003081 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003082 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003083
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003084 if (!ConflictingDecls.empty()) {
3085 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003086 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003087 ConflictingDecls.size());
3088 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003089 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003090 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003091 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003092
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003093 DeclarationNameInfo NameInfo(Name, Loc);
3094 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003095 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3096 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003097
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003098 QualType FromTy = D->getType();
3099 bool usedDifferentExceptionSpec = false;
3100
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003101 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003102 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3103 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3104 // FunctionDecl that we are importing the FunctionProtoType for.
3105 // To avoid an infinite recursion when importing, create the FunctionDecl
3106 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003107 if (FromEPI.ExceptionSpec.SourceDecl ||
3108 FromEPI.ExceptionSpec.SourceTemplate ||
3109 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003110 FunctionProtoType::ExtProtoInfo DefaultEPI;
3111 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003112 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003113 usedDifferentExceptionSpec = true;
3114 }
3115 }
3116
Balazs Keri3b30d652018-10-19 13:32:20 +00003117 QualType T;
3118 TypeSourceInfo *TInfo;
3119 SourceLocation ToInnerLocStart, ToEndLoc;
3120 NestedNameSpecifierLoc ToQualifierLoc;
3121 if (auto Imp = importSeq(
3122 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3123 D->getQualifierLoc(), D->getEndLoc()))
3124 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3125 else
3126 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003127
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003128 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003129 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003130 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003131 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3132 Parameters.push_back(*ToPOrErr);
3133 else
3134 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003135 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003136
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003137 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003138 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003139 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003140 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003141 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3142 ToInnerLocStart, NameInfo, T, TInfo,
3143 FromConstructor->isExplicit(),
3144 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003145 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00003146 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003147 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3148 // Import first, then allocate memory and copy if there was no error.
3149 if (Error Err = ImportContainerChecked(
3150 FromConstructor->inits(), CtorInitializers))
3151 return std::move(Err);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003152 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00003153 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3154 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003155 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00003156 ToCtor->setCtorInitializers(Memory);
3157 ToCtor->setNumCtorInitializers(NumInitializers);
3158 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003159 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003160 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003161 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3162 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3163 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003164 return ToFunction;
3165 } else if (CXXConversionDecl *FromConversion =
3166 dyn_cast<CXXConversionDecl>(D)) {
3167 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3168 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003169 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003170 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3171 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003172 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003173 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3174 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003175 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003176 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3177 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003178 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003179 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003180 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003181 D->getStorageClass(), D->isInlineSpecified(),
3182 D->hasWrittenPrototype(), D->isConstexpr()))
3183 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003184 }
John McCall3e11ebe2010-03-15 10:12:16 +00003185
Balazs Keri3b30d652018-10-19 13:32:20 +00003186 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003187 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003188 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003189 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3190 ToFunction->setTrivial(D->isTrivial());
3191 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003192 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003193
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003194 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003195 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003196 Param->setOwningFunction(ToFunction);
3197 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003198 }
David Blaikie9c70e042011-09-21 18:16:56 +00003199 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003200
Gabor Marton5254e642018-06-27 13:32:50 +00003201 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003202 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00003203 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003204 ToFunction->setPreviousDecl(Recent);
3205 }
3206
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003207 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3208 // params it refers to.
3209 if (TInfo) {
3210 if (auto ProtoLoc =
3211 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3212 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3213 ProtoLoc.setParam(I, Parameters[I]);
3214 }
3215 }
3216
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003217 if (usedDifferentExceptionSpec) {
3218 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003219 if (ExpectedType TyOrErr = import(D->getType()))
3220 ToFunction->setType(*TyOrErr);
3221 else
3222 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003223 }
3224
Balazs Keria35798d2018-07-17 09:52:41 +00003225 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003226 if (FromFT) {
3227 auto ToFTOrErr = import(FromFT);
3228 if (!ToFTOrErr)
3229 return ToFTOrErr.takeError();
3230 }
Balazs Keria35798d2018-07-17 09:52:41 +00003231
Gabor Marton5254e642018-06-27 13:32:50 +00003232 if (D->doesThisDeclarationHaveABody()) {
3233 if (Stmt *FromBody = D->getBody()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003234 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3235 ToFunction->setBody(*ToBodyOrErr);
3236 else
3237 return ToBodyOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00003238 }
3239 }
3240
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003241 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003242
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003243 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003244 if (Error Err = ImportTemplateInformation(D, ToFunction))
3245 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003246
Gabor Marton5254e642018-06-27 13:32:50 +00003247 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3248
3249 // TODO Can we generalize this approach to other AST nodes as well?
3250 if (D->getDeclContext()->containsDeclAndLoad(D))
3251 DC->addDeclInternal(ToFunction);
3252 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003253 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003254
Gabor Marton5254e642018-06-27 13:32:50 +00003255 // Friend declaration's lexical context is the befriending class, but the
3256 // semantic context is the enclosing scope of the befriending class.
3257 // We want the friend functions to be found in the semantic context by lookup.
3258 // FIXME should we handle this generically in VisitFriendDecl?
3259 // In Other cases when LexicalDC != DC we don't want it to be added,
3260 // e.g out-of-class definitions like void B::f() {} .
3261 if (LexicalDC != DC && IsFriend) {
3262 DC->makeDeclVisibleInContext(ToFunction);
3263 }
3264
Gabor Marton7a0841e2018-10-29 10:18:28 +00003265 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3266 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3267
Gabor Marton5254e642018-06-27 13:32:50 +00003268 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003269 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3270 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3271 if (!ToRedeclOrErr)
3272 return ToRedeclOrErr.takeError();
3273 }
Gabor Marton5254e642018-06-27 13:32:50 +00003274
Douglas Gregor43f54792010-02-17 02:12:47 +00003275 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003276}
3277
Balazs Keri3b30d652018-10-19 13:32:20 +00003278ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003279 return VisitFunctionDecl(D);
3280}
3281
Balazs Keri3b30d652018-10-19 13:32:20 +00003282ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003283 return VisitCXXMethodDecl(D);
3284}
3285
Balazs Keri3b30d652018-10-19 13:32:20 +00003286ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003287 return VisitCXXMethodDecl(D);
3288}
3289
Balazs Keri3b30d652018-10-19 13:32:20 +00003290ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003291 return VisitCXXMethodDecl(D);
3292}
3293
Balazs Keri3b30d652018-10-19 13:32:20 +00003294ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003295 // Import the major distinguishing characteristics of a variable.
3296 DeclContext *DC, *LexicalDC;
3297 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003298 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003299 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003300 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3301 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003302 if (ToD)
3303 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003304
Fangrui Song6907ce22018-07-30 19:24:48 +00003305 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003306 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003307 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003308 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003309 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003310 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003311 if (!Name &&
3312 ASTImporter::getFieldIndex(D) !=
3313 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003314 continue;
3315
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003316 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003317 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003318 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003319 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3320 // initializer of a FieldDecl might not had been instantiated in the
3321 // "To" context. However, the "From" context might instantiated that,
3322 // thus we have to merge that.
3323 if (Expr *FromInitializer = D->getInClassInitializer()) {
3324 // We don't have yet the initializer set.
3325 if (FoundField->hasInClassInitializer() &&
3326 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003327 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3328 FoundField->setInClassInitializer(*ToInitializerOrErr);
3329 else {
3330 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003331 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003332 // FIXME: warning message?
3333 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003334 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003335 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003336 }
3337 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003338 return FoundField;
3339 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003340
Balazs Keri3b30d652018-10-19 13:32:20 +00003341 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003342 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3343 << Name << D->getType() << FoundField->getType();
3344 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3345 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003346
3347 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003348 }
3349 }
3350
Balazs Keri3b30d652018-10-19 13:32:20 +00003351 QualType ToType;
3352 TypeSourceInfo *ToTInfo;
3353 Expr *ToBitWidth;
3354 SourceLocation ToInnerLocStart;
3355 Expr *ToInitializer;
3356 if (auto Imp = importSeq(
3357 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3358 D->getInnerLocStart(), D->getInClassInitializer()))
3359 std::tie(
3360 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3361 else
3362 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003363
Gabor Marton26f72a92018-07-12 09:42:05 +00003364 FieldDecl *ToField;
3365 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003366 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3367 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3368 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003369 return ToField;
3370
Douglas Gregordd483172010-02-22 17:42:47 +00003371 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003372 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003373 if (ToInitializer)
3374 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003375 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003376 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003377 return ToField;
3378}
3379
Balazs Keri3b30d652018-10-19 13:32:20 +00003380ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003381 // Import the major distinguishing characteristics of a variable.
3382 DeclContext *DC, *LexicalDC;
3383 DeclarationName Name;
3384 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003385 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003386 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3387 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003388 if (ToD)
3389 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003390
Fangrui Song6907ce22018-07-30 19:24:48 +00003391 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003392 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003393 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003394 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003395 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003396 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003397 if (!Name &&
3398 ASTImporter::getFieldIndex(D) !=
3399 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003400 continue;
3401
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003402 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003403 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003404 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003405 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003406 return FoundField;
3407 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003408
3409 // If there are more anonymous fields to check, continue.
3410 if (!Name && I < N-1)
3411 continue;
3412
Balazs Keri3b30d652018-10-19 13:32:20 +00003413 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003414 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3415 << Name << D->getType() << FoundField->getType();
3416 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3417 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003418
3419 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003420 }
3421 }
3422
Francois Pichet783dd6e2010-11-21 06:08:52 +00003423 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003424 auto TypeOrErr = import(D->getType());
3425 if (!TypeOrErr)
3426 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003427
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003428 auto **NamedChain =
3429 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003430
3431 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003432 for (auto *PI : D->chain())
3433 if (Expected<NamedDecl *> ToD = import(PI))
3434 NamedChain[i++] = *ToD;
3435 else
3436 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003437
Gabor Marton26f72a92018-07-12 09:42:05 +00003438 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3439 IndirectFieldDecl *ToIndirectField;
3440 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003441 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003442 // FIXME here we leak `NamedChain` which is allocated before
3443 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003444
Balazs Keri3b30d652018-10-19 13:32:20 +00003445 for (const auto *Attr : D->attrs())
3446 ToIndirectField->addAttr(Importer.Import(Attr));
Aaron Ballman260995b2014-10-15 16:58:18 +00003447
Francois Pichet783dd6e2010-11-21 06:08:52 +00003448 ToIndirectField->setAccess(D->getAccess());
3449 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003450 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003451 return ToIndirectField;
3452}
3453
Balazs Keri3b30d652018-10-19 13:32:20 +00003454ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003455 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003456 DeclContext *DC, *LexicalDC;
3457 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3458 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003459
3460 // Determine whether we've already imported this decl.
3461 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3462 auto *RD = cast<CXXRecordDecl>(DC);
3463 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003464
3465 while (ImportedFriend) {
3466 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003467 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3468 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003469 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003470
3471 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3472 if (Importer.IsStructurallyEquivalent(
3473 D->getFriendType()->getType(),
3474 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003475 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003476 }
3477 ImportedFriend = ImportedFriend->getNextFriend();
3478 }
3479
3480 // Not found. Create it.
3481 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003482 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003483 NamedDecl *ToFriendD;
3484 if (Error Err = importInto(ToFriendD, FriendD))
3485 return std::move(Err);
3486
3487 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003488 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3489 ToFriendD->setObjectOfFriendDecl(false);
3490
3491 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003492 } else { // The friend is a type, not a decl.
3493 if (auto TSIOrErr = import(D->getFriendType()))
3494 ToFU = *TSIOrErr;
3495 else
3496 return TSIOrErr.takeError();
3497 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003498
3499 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003500 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003501 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003502 if (auto ListOrErr = ImportTemplateParameterList(FromTPLists[I]))
3503 ToTPLists[I] = *ListOrErr;
3504 else
3505 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003506 }
3507
Balazs Keri3b30d652018-10-19 13:32:20 +00003508 auto LocationOrErr = import(D->getLocation());
3509 if (!LocationOrErr)
3510 return LocationOrErr.takeError();
3511 auto FriendLocOrErr = import(D->getFriendLoc());
3512 if (!FriendLocOrErr)
3513 return FriendLocOrErr.takeError();
3514
Gabor Marton26f72a92018-07-12 09:42:05 +00003515 FriendDecl *FrD;
3516 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003517 *LocationOrErr, ToFU,
3518 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003519 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003520
3521 FrD->setAccess(D->getAccess());
3522 FrD->setLexicalDeclContext(LexicalDC);
3523 LexicalDC->addDeclInternal(FrD);
3524 return FrD;
3525}
3526
Balazs Keri3b30d652018-10-19 13:32:20 +00003527ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003528 // Import the major distinguishing characteristics of an ivar.
3529 DeclContext *DC, *LexicalDC;
3530 DeclarationName Name;
3531 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003532 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003533 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3534 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003535 if (ToD)
3536 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003537
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003538 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003539 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003540 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003541 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003542 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003543 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003544 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003545 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003546 return FoundIvar;
3547 }
3548
3549 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3550 << Name << D->getType() << FoundIvar->getType();
3551 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3552 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003553
3554 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003555 }
3556 }
3557
Balazs Keri3b30d652018-10-19 13:32:20 +00003558 QualType ToType;
3559 TypeSourceInfo *ToTypeSourceInfo;
3560 Expr *ToBitWidth;
3561 SourceLocation ToInnerLocStart;
3562 if (auto Imp = importSeq(
3563 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3564 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3565 else
3566 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003567
Gabor Marton26f72a92018-07-12 09:42:05 +00003568 ObjCIvarDecl *ToIvar;
3569 if (GetImportedOrCreateDecl(
3570 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003571 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3572 ToType, ToTypeSourceInfo,
3573 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003574 return ToIvar;
3575
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003576 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003577 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003578 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003579}
3580
Balazs Keri3b30d652018-10-19 13:32:20 +00003581ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003582
3583 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3584 auto RedeclIt = Redecls.begin();
3585 // Import the first part of the decl chain. I.e. import all previous
3586 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003587 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3588 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3589 if (!RedeclOrErr)
3590 return RedeclOrErr.takeError();
3591 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003592 assert(*RedeclIt == D);
3593
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003594 // Import the major distinguishing characteristics of a variable.
3595 DeclContext *DC, *LexicalDC;
3596 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003597 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003598 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003599 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3600 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003601 if (ToD)
3602 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003603
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003604 // Try to find a variable in our own ("to") context with the same name and
3605 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003606 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003607 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003608 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003609 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003610 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003611 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003612 for (auto *FoundDecl : FoundDecls) {
3613 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003614 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003615
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003616 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003617 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003618 if (FoundVar->hasExternalFormalLinkage() &&
3619 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003620 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003621 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003622
3623 // The VarDecl in the "From" context has a definition, but in the
3624 // "To" context we already have a definition.
3625 VarDecl *FoundDef = FoundVar->getDefinition();
3626 if (D->isThisDeclarationADefinition() && FoundDef)
3627 // FIXME Check for ODR error if the two definitions have
3628 // different initializers?
3629 return Importer.MapImported(D, FoundDef);
3630
3631 // The VarDecl in the "From" context has an initializer, but in the
3632 // "To" context we already have an initializer.
3633 const VarDecl *FoundDInit = nullptr;
3634 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3635 // FIXME Diagnose ODR error if the two initializers are different?
3636 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3637
3638 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003639 break;
3640 }
3641
Douglas Gregor56521c52010-02-12 17:23:39 +00003642 const ArrayType *FoundArray
3643 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3644 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003645 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003646 if (FoundArray && TArray) {
3647 if (isa<IncompleteArrayType>(FoundArray) &&
3648 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003649 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003650 if (auto TyOrErr = import(D->getType()))
3651 FoundVar->setType(*TyOrErr);
3652 else
3653 return TyOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003654
Gabor Martonac3a5d62018-09-17 12:04:52 +00003655 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003656 break;
3657 } else if (isa<IncompleteArrayType>(TArray) &&
3658 isa<ConstantArrayType>(FoundArray)) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003659 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003660 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003661 }
3662 }
3663
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003664 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003665 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003666 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3667 << FoundVar->getType();
3668 }
3669 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003670
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003671 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003672 }
3673
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003674 if (!ConflictingDecls.empty()) {
3675 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003676 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003677 ConflictingDecls.size());
3678 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003679 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003680 }
3681 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003682
Balazs Keri3b30d652018-10-19 13:32:20 +00003683 QualType ToType;
3684 TypeSourceInfo *ToTypeSourceInfo;
3685 SourceLocation ToInnerLocStart;
3686 NestedNameSpecifierLoc ToQualifierLoc;
3687 if (auto Imp = importSeq(
3688 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3689 D->getQualifierLoc()))
3690 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3691 else
3692 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003693
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003694 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003695 VarDecl *ToVar;
3696 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003697 ToInnerLocStart, Loc,
3698 Name.getAsIdentifierInfo(),
3699 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003700 D->getStorageClass()))
3701 return ToVar;
3702
Balazs Keri3b30d652018-10-19 13:32:20 +00003703 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003704 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003705 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003706
Gabor Martonac3a5d62018-09-17 12:04:52 +00003707 if (FoundByLookup) {
3708 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3709 ToVar->setPreviousDecl(Recent);
3710 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003711
Balazs Keri3b30d652018-10-19 13:32:20 +00003712 if (Error Err = ImportInitializer(D, ToVar))
3713 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003714
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003715 if (D->isConstexpr())
3716 ToVar->setConstexpr(true);
3717
Gabor Martonac3a5d62018-09-17 12:04:52 +00003718 if (D->getDeclContext()->containsDeclAndLoad(D))
3719 DC->addDeclInternal(ToVar);
3720 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3721 LexicalDC->addDeclInternal(ToVar);
3722
3723 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003724 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3725 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3726 if (!RedeclOrErr)
3727 return RedeclOrErr.takeError();
3728 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003729
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003730 return ToVar;
3731}
3732
Balazs Keri3b30d652018-10-19 13:32:20 +00003733ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003734 // Parameters are created in the translation unit's context, then moved
3735 // into the function declaration's context afterward.
3736 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003737
Balazs Keri3b30d652018-10-19 13:32:20 +00003738 DeclarationName ToDeclName;
3739 SourceLocation ToLocation;
3740 QualType ToType;
3741 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3742 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3743 else
3744 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003745
Douglas Gregor8b228d72010-02-17 21:22:52 +00003746 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003747 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003748 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3749 ToLocation, ToDeclName.getAsIdentifierInfo(),
3750 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003751 return ToParm;
3752 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003753}
3754
Balazs Keri3b30d652018-10-19 13:32:20 +00003755ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003756 // Parameters are created in the translation unit's context, then moved
3757 // into the function declaration's context afterward.
3758 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003759
Balazs Keri3b30d652018-10-19 13:32:20 +00003760 DeclarationName ToDeclName;
3761 SourceLocation ToLocation, ToInnerLocStart;
3762 QualType ToType;
3763 TypeSourceInfo *ToTypeSourceInfo;
3764 if (auto Imp = importSeq(
3765 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3766 D->getTypeSourceInfo()))
3767 std::tie(
3768 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3769 ToTypeSourceInfo) = *Imp;
3770 else
3771 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003772
Gabor Marton26f72a92018-07-12 09:42:05 +00003773 ParmVarDecl *ToParm;
3774 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003775 ToInnerLocStart, ToLocation,
3776 ToDeclName.getAsIdentifierInfo(), ToType,
3777 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003778 /*DefaultArg*/ nullptr))
3779 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003780
3781 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003782 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003783 ToParm->setKNRPromoted(D->isKNRPromoted());
3784
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003785 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003786 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3787 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3788 else
3789 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003790 } else if (D->hasUnparsedDefaultArg()) {
3791 ToParm->setUnparsedDefaultArg();
3792 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003793 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3794 ToParm->setDefaultArg(*ToDefArgOrErr);
3795 else
3796 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003797 }
Sean Callanan59721b32015-04-28 18:41:46 +00003798
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003799 if (D->isObjCMethodParameter()) {
3800 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3801 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3802 } else {
3803 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3804 D->getFunctionScopeIndex());
3805 }
3806
Gabor Marton26f72a92018-07-12 09:42:05 +00003807 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003808}
3809
Balazs Keri3b30d652018-10-19 13:32:20 +00003810ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003811 // Import the major distinguishing characteristics of a method.
3812 DeclContext *DC, *LexicalDC;
3813 DeclarationName Name;
3814 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003815 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003816 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3817 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003818 if (ToD)
3819 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003820
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003821 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003822 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003823 for (auto *FoundDecl : FoundDecls) {
3824 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003825 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3826 continue;
3827
3828 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003829 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3830 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003831 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003832 << D->isInstanceMethod() << Name << D->getReturnType()
3833 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003834 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003835 diag::note_odr_objc_method_here)
3836 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003837
3838 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003839 }
3840
3841 // Check the number of parameters.
3842 if (D->param_size() != FoundMethod->param_size()) {
3843 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3844 << D->isInstanceMethod() << Name
3845 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003846 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003847 diag::note_odr_objc_method_here)
3848 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003849
3850 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003851 }
3852
3853 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003854 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003855 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3856 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003857 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003858 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003859 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003860 diag::err_odr_objc_method_param_type_inconsistent)
3861 << D->isInstanceMethod() << Name
3862 << (*P)->getType() << (*FoundP)->getType();
3863 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3864 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003865
3866 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003867 }
3868 }
3869
3870 // Check variadic/non-variadic.
3871 // Check the number of parameters.
3872 if (D->isVariadic() != FoundMethod->isVariadic()) {
3873 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3874 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003875 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003876 diag::note_odr_objc_method_here)
3877 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003878
3879 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003880 }
3881
3882 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003883 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003884 }
3885 }
3886
Balazs Keri3b30d652018-10-19 13:32:20 +00003887 SourceLocation ToEndLoc;
3888 QualType ToReturnType;
3889 TypeSourceInfo *ToReturnTypeSourceInfo;
3890 if (auto Imp = importSeq(
3891 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3892 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3893 else
3894 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003895
Gabor Marton26f72a92018-07-12 09:42:05 +00003896 ObjCMethodDecl *ToMethod;
3897 if (GetImportedOrCreateDecl(
3898 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003899 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3900 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003901 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3902 D->getImplementationControl(), D->hasRelatedResultType()))
3903 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003904
3905 // FIXME: When we decide to merge method definitions, we'll need to
3906 // deal with implicit parameters.
3907
3908 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003909 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003910 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003911 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3912 ToParams.push_back(*ToPOrErr);
3913 else
3914 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003915 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003916
Douglas Gregor43f54792010-02-17 02:12:47 +00003917 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003918 for (auto *ToParam : ToParams) {
3919 ToParam->setOwningFunction(ToMethod);
3920 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003921 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003922
Balazs Keri3b30d652018-10-19 13:32:20 +00003923 SmallVector<SourceLocation, 12> FromSelLocs;
3924 D->getSelectorLocs(FromSelLocs);
3925 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3926 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3927 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003928
Balazs Keri3b30d652018-10-19 13:32:20 +00003929 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003930
3931 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003932 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003933 return ToMethod;
3934}
3935
Balazs Keri3b30d652018-10-19 13:32:20 +00003936ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003937 // Import the major distinguishing characteristics of a category.
3938 DeclContext *DC, *LexicalDC;
3939 DeclarationName Name;
3940 SourceLocation Loc;
3941 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003942 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3943 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003944 if (ToD)
3945 return ToD;
3946
Balazs Keri3b30d652018-10-19 13:32:20 +00003947 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3948 TypeSourceInfo *ToTypeSourceInfo;
3949 if (auto Imp = importSeq(
3950 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3951 D->getTypeSourceInfo()))
3952 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3953 else
3954 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003955
Gabor Marton26f72a92018-07-12 09:42:05 +00003956 ObjCTypeParamDecl *Result;
3957 if (GetImportedOrCreateDecl(
3958 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003959 ToVarianceLoc, D->getIndex(),
3960 ToLocation, Name.getAsIdentifierInfo(),
3961 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003962 return Result;
3963
Douglas Gregor85f3f952015-07-07 03:57:15 +00003964 Result->setLexicalDeclContext(LexicalDC);
3965 return Result;
3966}
3967
Balazs Keri3b30d652018-10-19 13:32:20 +00003968ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003969 // Import the major distinguishing characteristics of a category.
3970 DeclContext *DC, *LexicalDC;
3971 DeclarationName Name;
3972 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003973 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003974 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3975 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003976 if (ToD)
3977 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003978
Balazs Keri3b30d652018-10-19 13:32:20 +00003979 ObjCInterfaceDecl *ToInterface;
3980 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3981 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003982
Douglas Gregor84c51c32010-02-18 01:47:50 +00003983 // Determine if we've already encountered this category.
3984 ObjCCategoryDecl *MergeWithCategory
3985 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3986 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3987 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003988 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3989 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3990 if (auto Imp = importSeq(
3991 D->getAtStartLoc(), D->getCategoryNameLoc(),
3992 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3993 std::tie(
3994 ToAtStartLoc, ToCategoryNameLoc,
3995 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3996 else
3997 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003998
3999 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004000 ToAtStartLoc, Loc,
4001 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004002 Name.getAsIdentifierInfo(), ToInterface,
4003 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004004 ToIvarLBraceLoc,
4005 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004006 return ToCategory;
4007
Douglas Gregor84c51c32010-02-18 01:47:50 +00004008 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004009 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004010 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004011 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004012 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4013 ToCategory->setTypeParamList(*PListOrErr);
4014 else
4015 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004016
Douglas Gregor84c51c32010-02-18 01:47:50 +00004017 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004018 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4019 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004020 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4021 = D->protocol_loc_begin();
4022 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4023 FromProtoEnd = D->protocol_end();
4024 FromProto != FromProtoEnd;
4025 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004026 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4027 Protocols.push_back(*ToProtoOrErr);
4028 else
4029 return ToProtoOrErr.takeError();
4030
4031 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4032 ProtocolLocs.push_back(*ToProtoLocOrErr);
4033 else
4034 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004035 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004036
Douglas Gregor84c51c32010-02-18 01:47:50 +00004037 // FIXME: If we're merging, make sure that the protocol list is the same.
4038 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4039 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004040
Douglas Gregor84c51c32010-02-18 01:47:50 +00004041 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004042 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004043 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004044
Douglas Gregor84c51c32010-02-18 01:47:50 +00004045 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004046 if (Error Err = ImportDeclContext(D))
4047 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004048
Douglas Gregor84c51c32010-02-18 01:47:50 +00004049 // If we have an implementation, import it as well.
4050 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004051 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4052 import(D->getImplementation()))
4053 ToCategory->setImplementation(*ToImplOrErr);
4054 else
4055 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004056 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004057
Douglas Gregor84c51c32010-02-18 01:47:50 +00004058 return ToCategory;
4059}
4060
Balazs Keri3b30d652018-10-19 13:32:20 +00004061Error ASTNodeImporter::ImportDefinition(
4062 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004063 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004064 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004065 if (Error Err = ImportDeclContext(From))
4066 return Err;
4067 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004068 }
4069
4070 // Start the protocol definition
4071 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004072
Douglas Gregor2aa53772012-01-24 17:42:07 +00004073 // Import protocols
4074 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4075 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004076 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4077 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004078 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4079 FromProtoEnd = From->protocol_end();
4080 FromProto != FromProtoEnd;
4081 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004082 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4083 Protocols.push_back(*ToProtoOrErr);
4084 else
4085 return ToProtoOrErr.takeError();
4086
4087 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4088 ProtocolLocs.push_back(*ToProtoLocOrErr);
4089 else
4090 return ToProtoLocOrErr.takeError();
4091
Douglas Gregor2aa53772012-01-24 17:42:07 +00004092 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004093
Douglas Gregor2aa53772012-01-24 17:42:07 +00004094 // FIXME: If we're merging, make sure that the protocol list is the same.
4095 To->setProtocolList(Protocols.data(), Protocols.size(),
4096 ProtocolLocs.data(), Importer.getToContext());
4097
Douglas Gregor2e15c842012-02-01 21:00:38 +00004098 if (shouldForceImportDeclContext(Kind)) {
4099 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004100 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4101 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004102 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004103 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004104}
4105
Balazs Keri3b30d652018-10-19 13:32:20 +00004106ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004107 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004108 // from, but this particular declaration is not that definition, import the
4109 // definition and map to that.
4110 ObjCProtocolDecl *Definition = D->getDefinition();
4111 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004112 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4113 return Importer.MapImported(D, *ImportedDefOrErr);
4114 else
4115 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004116 }
4117
Douglas Gregor84c51c32010-02-18 01:47:50 +00004118 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004119 DeclContext *DC, *LexicalDC;
4120 DeclarationName Name;
4121 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004122 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004123 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4124 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004125 if (ToD)
4126 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004127
Craig Topper36250ad2014-05-12 05:36:57 +00004128 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004129 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004130 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004131 for (auto *FoundDecl : FoundDecls) {
4132 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004133 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004134
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004135 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004136 break;
4137 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004138
Douglas Gregor98d156a2010-02-17 16:12:00 +00004139 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004140 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004141 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4142 if (!ToAtBeginLocOrErr)
4143 return ToAtBeginLocOrErr.takeError();
4144
Gabor Marton26f72a92018-07-12 09:42:05 +00004145 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4146 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004147 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004148 /*PrevDecl=*/nullptr))
4149 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004150 ToProto->setLexicalDeclContext(LexicalDC);
4151 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004152 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004153
4154 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004155
Balazs Keri3b30d652018-10-19 13:32:20 +00004156 if (D->isThisDeclarationADefinition())
4157 if (Error Err = ImportDefinition(D, ToProto))
4158 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004159
Douglas Gregor98d156a2010-02-17 16:12:00 +00004160 return ToProto;
4161}
4162
Balazs Keri3b30d652018-10-19 13:32:20 +00004163ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4164 DeclContext *DC, *LexicalDC;
4165 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4166 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004167
Balazs Keri3b30d652018-10-19 13:32:20 +00004168 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4169 if (!ExternLocOrErr)
4170 return ExternLocOrErr.takeError();
4171
4172 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4173 if (!LangLocOrErr)
4174 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004175
4176 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004177
4178 LinkageSpecDecl *ToLinkageSpec;
4179 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004180 *ExternLocOrErr, *LangLocOrErr,
4181 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004182 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004183
4184 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004185 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4186 if (!RBraceLocOrErr)
4187 return RBraceLocOrErr.takeError();
4188 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004189 }
4190
4191 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4192 LexicalDC->addDeclInternal(ToLinkageSpec);
4193
Sean Callanan0aae0412014-12-10 00:00:37 +00004194 return ToLinkageSpec;
4195}
4196
Balazs Keri3b30d652018-10-19 13:32:20 +00004197ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004198 DeclContext *DC, *LexicalDC;
4199 DeclarationName Name;
4200 SourceLocation Loc;
4201 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004202 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4203 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004204 if (ToD)
4205 return ToD;
4206
Balazs Keri3b30d652018-10-19 13:32:20 +00004207 SourceLocation ToLoc, ToUsingLoc;
4208 NestedNameSpecifierLoc ToQualifierLoc;
4209 if (auto Imp = importSeq(
4210 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4211 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4212 else
4213 return Imp.takeError();
4214
4215 DeclarationNameInfo NameInfo(Name, ToLoc);
4216 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4217 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004218
Gabor Marton26f72a92018-07-12 09:42:05 +00004219 UsingDecl *ToUsing;
4220 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004221 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004222 D->hasTypename()))
4223 return ToUsing;
4224
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004225 ToUsing->setLexicalDeclContext(LexicalDC);
4226 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004227
4228 if (NamedDecl *FromPattern =
4229 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004230 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4231 Importer.getToContext().setInstantiatedFromUsingDecl(
4232 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004233 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004234 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004235 }
4236
Balazs Keri3b30d652018-10-19 13:32:20 +00004237 for (UsingShadowDecl *FromShadow : D->shadows()) {
4238 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4239 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004240 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004241 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004242 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004243 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004244 }
4245 return ToUsing;
4246}
4247
Balazs Keri3b30d652018-10-19 13:32:20 +00004248ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004249 DeclContext *DC, *LexicalDC;
4250 DeclarationName Name;
4251 SourceLocation Loc;
4252 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004253 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4254 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004255 if (ToD)
4256 return ToD;
4257
Balazs Keri3b30d652018-10-19 13:32:20 +00004258 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4259 if (!ToUsingOrErr)
4260 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004261
Balazs Keri3b30d652018-10-19 13:32:20 +00004262 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4263 if (!ToTargetOrErr)
4264 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004265
Gabor Marton26f72a92018-07-12 09:42:05 +00004266 UsingShadowDecl *ToShadow;
4267 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004268 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004269 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004270
4271 ToShadow->setLexicalDeclContext(LexicalDC);
4272 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004273
4274 if (UsingShadowDecl *FromPattern =
4275 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004276 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4277 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4278 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004279 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004280 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004281 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004282 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004283 }
4284
4285 LexicalDC->addDeclInternal(ToShadow);
4286
4287 return ToShadow;
4288}
4289
Balazs Keri3b30d652018-10-19 13:32:20 +00004290ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004291 DeclContext *DC, *LexicalDC;
4292 DeclarationName Name;
4293 SourceLocation Loc;
4294 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004295 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4296 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004297 if (ToD)
4298 return ToD;
4299
Balazs Keri3b30d652018-10-19 13:32:20 +00004300 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4301 if (!ToComAncestorOrErr)
4302 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004303
Balazs Keri3b30d652018-10-19 13:32:20 +00004304 NamespaceDecl *ToNominatedNamespace;
4305 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4306 NestedNameSpecifierLoc ToQualifierLoc;
4307 if (auto Imp = importSeq(
4308 D->getNominatedNamespace(), D->getUsingLoc(),
4309 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4310 D->getIdentLocation()))
4311 std::tie(
4312 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4313 ToQualifierLoc, ToIdentLocation) = *Imp;
4314 else
4315 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004316
Gabor Marton26f72a92018-07-12 09:42:05 +00004317 UsingDirectiveDecl *ToUsingDir;
4318 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004319 ToUsingLoc,
4320 ToNamespaceKeyLocation,
4321 ToQualifierLoc,
4322 ToIdentLocation,
4323 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004324 return ToUsingDir;
4325
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004326 ToUsingDir->setLexicalDeclContext(LexicalDC);
4327 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004328
4329 return ToUsingDir;
4330}
4331
Balazs Keri3b30d652018-10-19 13:32:20 +00004332ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004333 UnresolvedUsingValueDecl *D) {
4334 DeclContext *DC, *LexicalDC;
4335 DeclarationName Name;
4336 SourceLocation Loc;
4337 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004338 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4339 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004340 if (ToD)
4341 return ToD;
4342
Balazs Keri3b30d652018-10-19 13:32:20 +00004343 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4344 NestedNameSpecifierLoc ToQualifierLoc;
4345 if (auto Imp = importSeq(
4346 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4347 D->getEllipsisLoc()))
4348 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4349 else
4350 return Imp.takeError();
4351
4352 DeclarationNameInfo NameInfo(Name, ToLoc);
4353 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4354 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004355
Gabor Marton26f72a92018-07-12 09:42:05 +00004356 UnresolvedUsingValueDecl *ToUsingValue;
4357 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004358 ToUsingLoc, ToQualifierLoc, NameInfo,
4359 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004360 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004361
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004362 ToUsingValue->setAccess(D->getAccess());
4363 ToUsingValue->setLexicalDeclContext(LexicalDC);
4364 LexicalDC->addDeclInternal(ToUsingValue);
4365
4366 return ToUsingValue;
4367}
4368
Balazs Keri3b30d652018-10-19 13:32:20 +00004369ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004370 UnresolvedUsingTypenameDecl *D) {
4371 DeclContext *DC, *LexicalDC;
4372 DeclarationName Name;
4373 SourceLocation Loc;
4374 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004375 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4376 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004377 if (ToD)
4378 return ToD;
4379
Balazs Keri3b30d652018-10-19 13:32:20 +00004380 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4381 NestedNameSpecifierLoc ToQualifierLoc;
4382 if (auto Imp = importSeq(
4383 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4384 D->getEllipsisLoc()))
4385 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4386 else
4387 return Imp.takeError();
4388
Gabor Marton26f72a92018-07-12 09:42:05 +00004389 UnresolvedUsingTypenameDecl *ToUsing;
4390 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004391 ToUsingLoc, ToTypenameLoc,
4392 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004393 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004394
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004395 ToUsing->setAccess(D->getAccess());
4396 ToUsing->setLexicalDeclContext(LexicalDC);
4397 LexicalDC->addDeclInternal(ToUsing);
4398
4399 return ToUsing;
4400}
4401
Balazs Keri3b30d652018-10-19 13:32:20 +00004402
4403Error ASTNodeImporter::ImportDefinition(
4404 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004405 if (To->getDefinition()) {
4406 // Check consistency of superclass.
4407 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4408 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004409 if (auto FromSuperOrErr = import(FromSuper))
4410 FromSuper = *FromSuperOrErr;
4411 else
4412 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004413 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004414
4415 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004416 if ((bool)FromSuper != (bool)ToSuper ||
4417 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004418 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004419 diag::err_odr_objc_superclass_inconsistent)
4420 << To->getDeclName();
4421 if (ToSuper)
4422 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4423 << To->getSuperClass()->getDeclName();
4424 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004425 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004426 diag::note_odr_objc_missing_superclass);
4427 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004428 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004429 diag::note_odr_objc_superclass)
4430 << From->getSuperClass()->getDeclName();
4431 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004432 Importer.FromDiag(From->getLocation(),
4433 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004434 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004435
Douglas Gregor2e15c842012-02-01 21:00:38 +00004436 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004437 if (Error Err = ImportDeclContext(From))
4438 return Err;
4439 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004440 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004441
Douglas Gregor2aa53772012-01-24 17:42:07 +00004442 // Start the definition.
4443 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004444
Douglas Gregor2aa53772012-01-24 17:42:07 +00004445 // If this class has a superclass, import it.
4446 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004447 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4448 To->setSuperClass(*SuperTInfoOrErr);
4449 else
4450 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004451 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004452
Douglas Gregor2aa53772012-01-24 17:42:07 +00004453 // Import protocols
4454 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4455 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004456 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4457 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004458
Douglas Gregor2aa53772012-01-24 17:42:07 +00004459 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4460 FromProtoEnd = From->protocol_end();
4461 FromProto != FromProtoEnd;
4462 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004463 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4464 Protocols.push_back(*ToProtoOrErr);
4465 else
4466 return ToProtoOrErr.takeError();
4467
4468 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4469 ProtocolLocs.push_back(*ToProtoLocOrErr);
4470 else
4471 return ToProtoLocOrErr.takeError();
4472
Douglas Gregor2aa53772012-01-24 17:42:07 +00004473 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004474
Douglas Gregor2aa53772012-01-24 17:42:07 +00004475 // FIXME: If we're merging, make sure that the protocol list is the same.
4476 To->setProtocolList(Protocols.data(), Protocols.size(),
4477 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004478
Douglas Gregor2aa53772012-01-24 17:42:07 +00004479 // Import categories. When the categories themselves are imported, they'll
4480 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004481 for (auto *Cat : From->known_categories()) {
4482 auto ToCatOrErr = import(Cat);
4483 if (!ToCatOrErr)
4484 return ToCatOrErr.takeError();
4485 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004486
Douglas Gregor2aa53772012-01-24 17:42:07 +00004487 // If we have an @implementation, import it as well.
4488 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004489 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4490 import(From->getImplementation()))
4491 To->setImplementation(*ToImplOrErr);
4492 else
4493 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004494 }
4495
Douglas Gregor2e15c842012-02-01 21:00:38 +00004496 if (shouldForceImportDeclContext(Kind)) {
4497 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004498 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4499 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004500 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004501 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004502}
4503
Balazs Keri3b30d652018-10-19 13:32:20 +00004504Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004505ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4506 if (!list)
4507 return nullptr;
4508
4509 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004510 for (auto *fromTypeParam : *list) {
4511 if (auto toTypeParamOrErr = import(fromTypeParam))
4512 toTypeParams.push_back(*toTypeParamOrErr);
4513 else
4514 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004515 }
4516
Balazs Keri3b30d652018-10-19 13:32:20 +00004517 auto LAngleLocOrErr = import(list->getLAngleLoc());
4518 if (!LAngleLocOrErr)
4519 return LAngleLocOrErr.takeError();
4520
4521 auto RAngleLocOrErr = import(list->getRAngleLoc());
4522 if (!RAngleLocOrErr)
4523 return RAngleLocOrErr.takeError();
4524
Douglas Gregor85f3f952015-07-07 03:57:15 +00004525 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004526 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004527 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004528 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004529}
4530
Balazs Keri3b30d652018-10-19 13:32:20 +00004531ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004532 // If this class has a definition in the translation unit we're coming from,
4533 // but this particular declaration is not that definition, import the
4534 // definition and map to that.
4535 ObjCInterfaceDecl *Definition = D->getDefinition();
4536 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004537 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4538 return Importer.MapImported(D, *ImportedDefOrErr);
4539 else
4540 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004541 }
4542
Douglas Gregor45635322010-02-16 01:20:57 +00004543 // Import the major distinguishing characteristics of an @interface.
4544 DeclContext *DC, *LexicalDC;
4545 DeclarationName Name;
4546 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004547 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004548 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4549 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004550 if (ToD)
4551 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004552
Douglas Gregor2aa53772012-01-24 17:42:07 +00004553 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004554 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004555 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004556 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004557 for (auto *FoundDecl : FoundDecls) {
4558 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004559 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004560
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004561 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004562 break;
4563 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004564
Douglas Gregor2aa53772012-01-24 17:42:07 +00004565 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004566 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004567 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004568 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4569 if (!AtBeginLocOrErr)
4570 return AtBeginLocOrErr.takeError();
4571
Gabor Marton26f72a92018-07-12 09:42:05 +00004572 if (GetImportedOrCreateDecl(
4573 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004574 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004575 /*TypeParamList=*/nullptr,
4576 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4577 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004578 ToIface->setLexicalDeclContext(LexicalDC);
4579 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004580 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004581 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004582 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004583 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004584 if (auto ToPListOrErr =
4585 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4586 ToIface->setTypeParamList(*ToPListOrErr);
4587 else
4588 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004589
Balazs Keri3b30d652018-10-19 13:32:20 +00004590 if (D->isThisDeclarationADefinition())
4591 if (Error Err = ImportDefinition(D, ToIface))
4592 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004593
Douglas Gregor98d156a2010-02-17 16:12:00 +00004594 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004595}
4596
Balazs Keri3b30d652018-10-19 13:32:20 +00004597ExpectedDecl
4598ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4599 ObjCCategoryDecl *Category;
4600 if (Error Err = importInto(Category, D->getCategoryDecl()))
4601 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004602
Douglas Gregor4da9d682010-12-07 15:32:12 +00004603 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4604 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004605 DeclContext *DC, *LexicalDC;
4606 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4607 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004608
Balazs Keri3b30d652018-10-19 13:32:20 +00004609 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4610 if (auto Imp = importSeq(
4611 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4612 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4613 else
4614 return Imp.takeError();
4615
Gabor Marton26f72a92018-07-12 09:42:05 +00004616 if (GetImportedOrCreateDecl(
4617 ToImpl, D, Importer.getToContext(), DC,
4618 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004619 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004620 return ToImpl;
4621
Balazs Keri3b30d652018-10-19 13:32:20 +00004622 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004623 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004624 Category->setImplementation(ToImpl);
4625 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004626
Gabor Marton26f72a92018-07-12 09:42:05 +00004627 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004628 if (Error Err = ImportDeclContext(D))
4629 return std::move(Err);
4630
Douglas Gregor4da9d682010-12-07 15:32:12 +00004631 return ToImpl;
4632}
4633
Balazs Keri3b30d652018-10-19 13:32:20 +00004634ExpectedDecl
4635ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004636 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004637 ObjCInterfaceDecl *Iface;
4638 if (Error Err = importInto(Iface, D->getClassInterface()))
4639 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004640
4641 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004642 ObjCInterfaceDecl *Super;
4643 if (Error Err = importInto(Super, D->getSuperClass()))
4644 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004645
4646 ObjCImplementationDecl *Impl = Iface->getImplementation();
4647 if (!Impl) {
4648 // We haven't imported an implementation yet. Create a new @implementation
4649 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004650 DeclContext *DC, *LexicalDC;
4651 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4652 return std::move(Err);
4653
4654 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4655 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4656 if (auto Imp = importSeq(
4657 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4658 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4659 std::tie(
4660 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4661 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4662 else
4663 return Imp.takeError();
4664
Gabor Marton26f72a92018-07-12 09:42:05 +00004665 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004666 DC, Iface, Super,
4667 ToLocation,
4668 ToAtStartLoc,
4669 ToSuperClassLoc,
4670 ToIvarLBraceLoc,
4671 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004672 return Impl;
4673
Balazs Keri3b30d652018-10-19 13:32:20 +00004674 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004675
Douglas Gregorda8025c2010-12-07 01:26:03 +00004676 // Associate the implementation with the class it implements.
4677 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004678 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004679 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004680 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004681
4682 // Verify that the existing @implementation has the same superclass.
4683 if ((Super && !Impl->getSuperClass()) ||
4684 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004685 (Super && Impl->getSuperClass() &&
4686 !declaresSameEntity(Super->getCanonicalDecl(),
4687 Impl->getSuperClass()))) {
4688 Importer.ToDiag(Impl->getLocation(),
4689 diag::err_odr_objc_superclass_inconsistent)
4690 << Iface->getDeclName();
4691 // FIXME: It would be nice to have the location of the superclass
4692 // below.
4693 if (Impl->getSuperClass())
4694 Importer.ToDiag(Impl->getLocation(),
4695 diag::note_odr_objc_superclass)
4696 << Impl->getSuperClass()->getDeclName();
4697 else
4698 Importer.ToDiag(Impl->getLocation(),
4699 diag::note_odr_objc_missing_superclass);
4700 if (D->getSuperClass())
4701 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004702 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004703 << D->getSuperClass()->getDeclName();
4704 else
4705 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004706 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004707
4708 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004709 }
4710 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004711
Douglas Gregorda8025c2010-12-07 01:26:03 +00004712 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004713 if (Error Err = ImportDeclContext(D))
4714 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004715
4716 return Impl;
4717}
4718
Balazs Keri3b30d652018-10-19 13:32:20 +00004719ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004720 // Import the major distinguishing characteristics of an @property.
4721 DeclContext *DC, *LexicalDC;
4722 DeclarationName Name;
4723 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004724 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004725 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4726 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004727 if (ToD)
4728 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004729
4730 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004731 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004732 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004733 for (auto *FoundDecl : FoundDecls) {
4734 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004735 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004736 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004737 FoundProp->getType())) {
4738 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4739 << Name << D->getType() << FoundProp->getType();
4740 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4741 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004742
4743 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004744 }
4745
4746 // FIXME: Check property attributes, getters, setters, etc.?
4747
4748 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004749 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004750 return FoundProp;
4751 }
4752 }
4753
Balazs Keri3b30d652018-10-19 13:32:20 +00004754 QualType ToType;
4755 TypeSourceInfo *ToTypeSourceInfo;
4756 SourceLocation ToAtLoc, ToLParenLoc;
4757 if (auto Imp = importSeq(
4758 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4759 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4760 else
4761 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004762
4763 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004764 ObjCPropertyDecl *ToProperty;
4765 if (GetImportedOrCreateDecl(
4766 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004767 Name.getAsIdentifierInfo(), ToAtLoc,
4768 ToLParenLoc, ToType,
4769 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004770 return ToProperty;
4771
Balazs Keri3b30d652018-10-19 13:32:20 +00004772 Selector ToGetterName, ToSetterName;
4773 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4774 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4775 ObjCIvarDecl *ToPropertyIvarDecl;
4776 if (auto Imp = importSeq(
4777 D->getGetterName(), D->getSetterName(),
4778 D->getGetterNameLoc(), D->getSetterNameLoc(),
4779 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4780 D->getPropertyIvarDecl()))
4781 std::tie(
4782 ToGetterName, ToSetterName,
4783 ToGetterNameLoc, ToSetterNameLoc,
4784 ToGetterMethodDecl, ToSetterMethodDecl,
4785 ToPropertyIvarDecl) = *Imp;
4786 else
4787 return Imp.takeError();
4788
Douglas Gregora11c4582010-02-17 18:02:10 +00004789 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004790 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004791
4792 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004793 ToProperty->setPropertyAttributesAsWritten(
4794 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004795 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4796 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4797 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4798 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4799 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004800 return ToProperty;
4801}
4802
Balazs Keri3b30d652018-10-19 13:32:20 +00004803ExpectedDecl
4804ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4805 ObjCPropertyDecl *Property;
4806 if (Error Err = importInto(Property, D->getPropertyDecl()))
4807 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004808
Balazs Keri3b30d652018-10-19 13:32:20 +00004809 DeclContext *DC, *LexicalDC;
4810 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4811 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004812
Balazs Keri3b30d652018-10-19 13:32:20 +00004813 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004814
4815 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004816 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004817 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4818 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004819
4820 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004821 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4822 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004823 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004824 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4825 if (auto Imp = importSeq(
4826 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4827 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4828 else
4829 return Imp.takeError();
4830
Gabor Marton26f72a92018-07-12 09:42:05 +00004831 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004832 ToBeginLoc,
4833 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004834 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004835 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004836 return ToImpl;
4837
Douglas Gregor14a49e22010-12-07 18:32:03 +00004838 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004839 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004840 } else {
4841 // Check that we have the same kind of property implementation (@synthesize
4842 // vs. @dynamic).
4843 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004844 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004845 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004846 << Property->getDeclName()
4847 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004848 == ObjCPropertyImplDecl::Dynamic);
4849 Importer.FromDiag(D->getLocation(),
4850 diag::note_odr_objc_property_impl_kind)
4851 << D->getPropertyDecl()->getDeclName()
4852 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004853
4854 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004855 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004856
4857 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004858 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4859 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004860 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004861 diag::err_odr_objc_synthesize_ivar_inconsistent)
4862 << Property->getDeclName()
4863 << ToImpl->getPropertyIvarDecl()->getDeclName()
4864 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004865 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004866 diag::note_odr_objc_synthesize_ivar_here)
4867 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004868
4869 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004870 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004871
Douglas Gregor14a49e22010-12-07 18:32:03 +00004872 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004873 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004874 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004875
Douglas Gregor14a49e22010-12-07 18:32:03 +00004876 return ToImpl;
4877}
4878
Balazs Keri3b30d652018-10-19 13:32:20 +00004879ExpectedDecl
4880ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004881 // For template arguments, we adopt the translation unit as our declaration
4882 // context. This context will be fixed when the actual template declaration
4883 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004884
Douglas Gregora082a492010-11-30 19:14:50 +00004885 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004886
4887 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4888 if (!BeginLocOrErr)
4889 return BeginLocOrErr.takeError();
4890
4891 ExpectedSLoc LocationOrErr = import(D->getLocation());
4892 if (!LocationOrErr)
4893 return LocationOrErr.takeError();
4894
Gabor Marton26f72a92018-07-12 09:42:05 +00004895 TemplateTypeParmDecl *ToD = nullptr;
4896 (void)GetImportedOrCreateDecl(
4897 ToD, D, Importer.getToContext(),
4898 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004899 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004900 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4901 D->wasDeclaredWithTypename(), D->isParameterPack());
4902 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004903}
4904
Balazs Keri3b30d652018-10-19 13:32:20 +00004905ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004906ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004907 DeclarationName ToDeclName;
4908 SourceLocation ToLocation, ToInnerLocStart;
4909 QualType ToType;
4910 TypeSourceInfo *ToTypeSourceInfo;
4911 if (auto Imp = importSeq(
4912 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4913 D->getInnerLocStart()))
4914 std::tie(
4915 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4916 ToInnerLocStart) = *Imp;
4917 else
4918 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004919
Douglas Gregora082a492010-11-30 19:14:50 +00004920 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004921
4922 NonTypeTemplateParmDecl *ToD = nullptr;
4923 (void)GetImportedOrCreateDecl(
4924 ToD, D, Importer.getToContext(),
4925 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004926 ToInnerLocStart, ToLocation, D->getDepth(),
4927 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4928 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004929 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004930}
4931
Balazs Keri3b30d652018-10-19 13:32:20 +00004932ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004933ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4934 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004935 auto NameOrErr = import(D->getDeclName());
4936 if (!NameOrErr)
4937 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004938
Douglas Gregora082a492010-11-30 19:14:50 +00004939 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004940 ExpectedSLoc LocationOrErr = import(D->getLocation());
4941 if (!LocationOrErr)
4942 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004943
Douglas Gregora082a492010-11-30 19:14:50 +00004944 // Import template parameters.
Balazs Keri3b30d652018-10-19 13:32:20 +00004945 auto TemplateParamsOrErr = ImportTemplateParameterList(
4946 D->getTemplateParameters());
4947 if (!TemplateParamsOrErr)
4948 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004949
Douglas Gregora082a492010-11-30 19:14:50 +00004950 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004951
4952 TemplateTemplateParmDecl *ToD = nullptr;
4953 (void)GetImportedOrCreateDecl(
4954 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004955 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4956 D->getDepth(), D->getPosition(), D->isParameterPack(),
4957 (*NameOrErr).getAsIdentifierInfo(),
4958 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004959 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004960}
4961
Gabor Marton9581c332018-05-23 13:53:36 +00004962// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4963// it has any definition in the redecl chain.
4964static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4965 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4966 if (!ToTemplatedDef)
4967 return nullptr;
4968 ClassTemplateDecl *TemplateWithDef =
4969 ToTemplatedDef->getDescribedClassTemplate();
4970 return TemplateWithDef;
4971}
4972
Balazs Keri3b30d652018-10-19 13:32:20 +00004973ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004974 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4975
Douglas Gregora082a492010-11-30 19:14:50 +00004976 // If this record has a definition in the translation unit we're coming from,
4977 // but this particular declaration is not that definition, import the
4978 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004979 auto *Definition =
4980 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Balazs Keri0c23dc52018-08-13 13:08:37 +00004981 if (Definition && Definition != D->getTemplatedDecl() && !IsFriend) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004982 if (ExpectedDecl ImportedDefOrErr = import(
4983 Definition->getDescribedClassTemplate()))
4984 return Importer.MapImported(D, *ImportedDefOrErr);
4985 else
4986 return ImportedDefOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00004987 }
Gabor Marton9581c332018-05-23 13:53:36 +00004988
Douglas Gregora082a492010-11-30 19:14:50 +00004989 // Import the major distinguishing characteristics of this class template.
4990 DeclContext *DC, *LexicalDC;
4991 DeclarationName Name;
4992 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004993 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004994 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4995 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004996 if (ToD)
4997 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004998
Douglas Gregora082a492010-11-30 19:14:50 +00004999 // We may already have a template of the same name; try to find and match it.
5000 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005001 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005002 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005003 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005004 for (auto *FoundDecl : FoundDecls) {
5005 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00005006 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005007
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005008 Decl *Found = FoundDecl;
5009 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00005010
5011 // The class to be imported is a definition.
5012 if (D->isThisDeclarationADefinition()) {
5013 // Lookup will find the fwd decl only if that is more recent than the
5014 // definition. So, try to get the definition if that is available in
5015 // the redecl chain.
5016 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005017 if (TemplateWithDef)
5018 FoundTemplate = TemplateWithDef;
5019 else
Gabor Marton9581c332018-05-23 13:53:36 +00005020 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005021 }
5022
Douglas Gregora082a492010-11-30 19:14:50 +00005023 if (IsStructuralMatch(D, FoundTemplate)) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005024 if (!IsFriend) {
5025 Importer.MapImported(D->getTemplatedDecl(),
5026 FoundTemplate->getTemplatedDecl());
5027 return Importer.MapImported(D, FoundTemplate);
5028 }
Aleksei Sidorin761c2242018-05-15 11:09:07 +00005029
Balazs Keri0c23dc52018-08-13 13:08:37 +00005030 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005031 }
Douglas Gregora082a492010-11-30 19:14:50 +00005032 }
Gabor Marton9581c332018-05-23 13:53:36 +00005033
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005034 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005035 }
Gabor Marton9581c332018-05-23 13:53:36 +00005036
Douglas Gregora082a492010-11-30 19:14:50 +00005037 if (!ConflictingDecls.empty()) {
5038 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005039 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005040 ConflictingDecls.size());
5041 }
Gabor Marton9581c332018-05-23 13:53:36 +00005042
Douglas Gregora082a492010-11-30 19:14:50 +00005043 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005044 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005045 }
5046
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005047 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5048
Douglas Gregora082a492010-11-30 19:14:50 +00005049 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005050 CXXRecordDecl *ToTemplated;
5051 if (Error Err = importInto(ToTemplated, FromTemplated))
5052 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005053
Douglas Gregora082a492010-11-30 19:14:50 +00005054 // Create the class template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005055 auto TemplateParamsOrErr = ImportTemplateParameterList(
5056 D->getTemplateParameters());
5057 if (!TemplateParamsOrErr)
5058 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005059
Gabor Marton26f72a92018-07-12 09:42:05 +00005060 ClassTemplateDecl *D2;
5061 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005062 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005063 return D2;
5064
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005065 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005066
Balazs Keri0c23dc52018-08-13 13:08:37 +00005067 if (ToTemplated->getPreviousDecl()) {
5068 assert(
5069 ToTemplated->getPreviousDecl()->getDescribedClassTemplate() &&
5070 "Missing described template");
5071 D2->setPreviousDecl(
5072 ToTemplated->getPreviousDecl()->getDescribedClassTemplate());
5073 }
Douglas Gregora082a492010-11-30 19:14:50 +00005074 D2->setAccess(D->getAccess());
5075 D2->setLexicalDeclContext(LexicalDC);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005076 if (!IsFriend)
5077 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005078
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005079 if (FromTemplated->isCompleteDefinition() &&
5080 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005081 // FIXME: Import definition!
5082 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005083
Douglas Gregora082a492010-11-30 19:14:50 +00005084 return D2;
5085}
5086
Balazs Keri3b30d652018-10-19 13:32:20 +00005087ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005088 ClassTemplateSpecializationDecl *D) {
5089 // If this record has a definition in the translation unit we're coming from,
5090 // but this particular declaration is not that definition, import the
5091 // definition and map to that.
5092 TagDecl *Definition = D->getDefinition();
5093 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005094 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5095 return Importer.MapImported(D, *ImportedDefOrErr);
5096 else
5097 return ImportedDefOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005098 }
5099
Balazs Keri3b30d652018-10-19 13:32:20 +00005100 ClassTemplateDecl *ClassTemplate;
5101 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5102 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005103
Douglas Gregore2e50d332010-12-01 01:36:18 +00005104 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005105 DeclContext *DC, *LexicalDC;
5106 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5107 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005108
5109 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005110 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005111 if (Error Err = ImportTemplateArguments(
5112 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5113 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005114
Douglas Gregore2e50d332010-12-01 01:36:18 +00005115 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005116 void *InsertPos = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005117 ClassTemplateSpecializationDecl *D2 = nullptr;
5118 ClassTemplatePartialSpecializationDecl *PartialSpec =
5119 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5120 if (PartialSpec)
5121 D2 = ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
5122 else
5123 D2 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
5124 ClassTemplateSpecializationDecl * const PrevDecl = D2;
5125 RecordDecl *FoundDef = D2 ? D2->getDefinition() : nullptr;
5126 if (FoundDef) {
5127 if (!D->isCompleteDefinition()) {
5128 // The "From" translation unit only had a forward declaration; call it
5129 // the same declaration.
5130 // TODO Handle the redecl chain properly!
5131 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005132 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005133
5134 if (IsStructuralMatch(D, FoundDef)) {
5135
5136 Importer.MapImported(D, FoundDef);
5137
5138 // Import those those default field initializers which have been
5139 // instantiated in the "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005140 for (auto *FromField : D->fields()) {
5141 auto ToOrErr = import(FromField);
5142 if (!ToOrErr)
5143 // FIXME: return the error?
5144 consumeError(ToOrErr.takeError());
5145 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005146
5147 // Import those methods which have been instantiated in the
5148 // "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005149 for (CXXMethodDecl *FromM : D->methods()) {
5150 auto ToOrErr = import(FromM);
5151 if (!ToOrErr)
5152 // FIXME: return the error?
5153 consumeError(ToOrErr.takeError());
5154 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005155
5156 // TODO Import instantiated default arguments.
5157 // TODO Import instantiated exception specifications.
5158 //
5159 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint what
5160 // else could be fused during an AST merge.
5161
5162 return FoundDef;
5163 }
5164 } else { // We either couldn't find any previous specialization in the "To"
5165 // context, or we found one but without definition. Let's create a
5166 // new specialization and register that at the class template.
Balazs Keri3b30d652018-10-19 13:32:20 +00005167
5168 // Import the location of this declaration.
5169 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5170 if (!BeginLocOrErr)
5171 return BeginLocOrErr.takeError();
5172 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5173 if (!IdLocOrErr)
5174 return IdLocOrErr.takeError();
5175
Gabor Marton42e15de2018-08-22 11:52:14 +00005176 if (PartialSpec) {
5177 // Import TemplateArgumentListInfo.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005178 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005179 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Balazs Keri3b30d652018-10-19 13:32:20 +00005180 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5181 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005182
Balazs Keri3b30d652018-10-19 13:32:20 +00005183 QualType CanonInjType;
5184 if (Error Err = importInto(
5185 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5186 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005187 CanonInjType = CanonInjType.getCanonicalType();
5188
Balazs Keri3b30d652018-10-19 13:32:20 +00005189 auto ToTPListOrErr = ImportTemplateParameterList(
5190 PartialSpec->getTemplateParameters());
5191 if (!ToTPListOrErr)
5192 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005193
Gabor Marton26f72a92018-07-12 09:42:05 +00005194 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00005195 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5196 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
Gabor Marton26f72a92018-07-12 09:42:05 +00005197 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
Gabor Marton42e15de2018-08-22 11:52:14 +00005198 ToTAInfo, CanonInjType,
5199 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
Gabor Marton26f72a92018-07-12 09:42:05 +00005200 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005201
Gabor Marton42e15de2018-08-22 11:52:14 +00005202 // Update InsertPos, because preceding import calls may have invalidated
5203 // it by adding new specializations.
5204 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5205 // Add this partial specialization to the class template.
5206 ClassTemplate->AddPartialSpecialization(
5207 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
5208
5209 } else { // Not a partial specialization.
Gabor Marton26f72a92018-07-12 09:42:05 +00005210 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00005211 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5212 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5213 PrevDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00005214 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005215
5216 // Update InsertPos, because preceding import calls may have invalidated
5217 // it by adding new specializations.
5218 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5219 // Add this specialization to the class template.
5220 ClassTemplate->AddSpecialization(D2, InsertPos);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005221 }
5222
Douglas Gregore2e50d332010-12-01 01:36:18 +00005223 D2->setSpecializationKind(D->getSpecializationKind());
5224
Douglas Gregore2e50d332010-12-01 01:36:18 +00005225 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005226 if (auto LocOrErr = import(D->getQualifierLoc()))
5227 D2->setQualifierInfo(*LocOrErr);
5228 else
5229 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005230
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005231 if (auto *TSI = D->getTypeAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005232 if (auto TInfoOrErr = import(TSI))
5233 D2->setTypeAsWritten(*TInfoOrErr);
5234 else
5235 return TInfoOrErr.takeError();
5236
5237 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5238 D2->setTemplateKeywordLoc(*LocOrErr);
5239 else
5240 return LocOrErr.takeError();
5241
5242 if (auto LocOrErr = import(D->getExternLoc()))
5243 D2->setExternLoc(*LocOrErr);
5244 else
5245 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005246 }
5247
Balazs Keri3b30d652018-10-19 13:32:20 +00005248 if (D->getPointOfInstantiation().isValid()) {
5249 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5250 D2->setPointOfInstantiation(*POIOrErr);
5251 else
5252 return POIOrErr.takeError();
5253 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005254
5255 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5256
Gabor Martonb14056b2018-05-25 11:21:24 +00005257 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00005258 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00005259
5260 // Add to the DC only if it was an explicit specialization/instantiation.
5261 if (D2->isExplicitInstantiationOrSpecialization()) {
5262 LexicalDC->addDeclInternal(D2);
5263 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005264 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005265 if (D->isCompleteDefinition())
5266 if (Error Err = ImportDefinition(D, D2))
5267 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005268
Douglas Gregore2e50d332010-12-01 01:36:18 +00005269 return D2;
5270}
5271
Balazs Keri3b30d652018-10-19 13:32:20 +00005272ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005273 // If this variable has a definition in the translation unit we're coming
5274 // from,
5275 // but this particular declaration is not that definition, import the
5276 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005277 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005278 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5279 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005280 if (ExpectedDecl ImportedDefOrErr = import(
5281 Definition->getDescribedVarTemplate()))
5282 return Importer.MapImported(D, *ImportedDefOrErr);
5283 else
5284 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005285 }
5286
5287 // Import the major distinguishing characteristics of this variable template.
5288 DeclContext *DC, *LexicalDC;
5289 DeclarationName Name;
5290 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005291 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005292 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5293 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005294 if (ToD)
5295 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005296
5297 // We may already have a template of the same name; try to find and match it.
5298 assert(!DC->isFunctionOrMethod() &&
5299 "Variable templates cannot be declared at function scope");
5300 SmallVector<NamedDecl *, 4> ConflictingDecls;
5301 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005302 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005303 for (auto *FoundDecl : FoundDecls) {
5304 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005305 continue;
5306
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005307 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005308 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005309 if (IsStructuralMatch(D, FoundTemplate)) {
5310 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005311 Importer.MapImported(D->getTemplatedDecl(),
5312 FoundTemplate->getTemplatedDecl());
5313 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005314 }
5315 }
5316
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005317 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005318 }
5319
5320 if (!ConflictingDecls.empty()) {
5321 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5322 ConflictingDecls.data(),
5323 ConflictingDecls.size());
5324 }
5325
5326 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005327 // FIXME: Is it possible to get other error than name conflict?
5328 // (Put this `if` into the previous `if`?)
5329 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005330
5331 VarDecl *DTemplated = D->getTemplatedDecl();
5332
5333 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005334 // FIXME: Value not used?
5335 ExpectedType TypeOrErr = import(DTemplated->getType());
5336 if (!TypeOrErr)
5337 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005338
5339 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005340 VarDecl *ToTemplated;
5341 if (Error Err = importInto(ToTemplated, DTemplated))
5342 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005343
5344 // Create the variable template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005345 auto TemplateParamsOrErr = ImportTemplateParameterList(
5346 D->getTemplateParameters());
5347 if (!TemplateParamsOrErr)
5348 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005349
Gabor Marton26f72a92018-07-12 09:42:05 +00005350 VarTemplateDecl *ToVarTD;
5351 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005352 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005353 return ToVarTD;
5354
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005355 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005356
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005357 ToVarTD->setAccess(D->getAccess());
5358 ToVarTD->setLexicalDeclContext(LexicalDC);
5359 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005360
Larisse Voufo39a1e502013-08-06 01:03:05 +00005361 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005362 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005363 // FIXME: Import definition!
5364 }
5365
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005366 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005367}
5368
Balazs Keri3b30d652018-10-19 13:32:20 +00005369ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005370 VarTemplateSpecializationDecl *D) {
5371 // If this record has a definition in the translation unit we're coming from,
5372 // but this particular declaration is not that definition, import the
5373 // definition and map to that.
5374 VarDecl *Definition = D->getDefinition();
5375 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005376 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5377 return Importer.MapImported(D, *ImportedDefOrErr);
5378 else
5379 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005380 }
5381
Balazs Keri3b30d652018-10-19 13:32:20 +00005382 VarTemplateDecl *VarTemplate;
5383 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5384 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005385
5386 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005387 DeclContext *DC, *LexicalDC;
5388 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5389 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005390
5391 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005392 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5393 if (!BeginLocOrErr)
5394 return BeginLocOrErr.takeError();
5395
5396 auto IdLocOrErr = import(D->getLocation());
5397 if (!IdLocOrErr)
5398 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005399
5400 // Import template arguments.
5401 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005402 if (Error Err = ImportTemplateArguments(
5403 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5404 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005405
5406 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005407 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005408 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005409 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005410 if (D2) {
5411 // We already have a variable template specialization with these template
5412 // arguments.
5413
5414 // FIXME: Check for specialization vs. instantiation errors.
5415
5416 if (VarDecl *FoundDef = D2->getDefinition()) {
5417 if (!D->isThisDeclarationADefinition() ||
5418 IsStructuralMatch(D, FoundDef)) {
5419 // The record types structurally match, or the "from" translation
5420 // unit only had a forward declaration anyway; call it the same
5421 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005422 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005423 }
5424 }
5425 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005426 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005427 QualType T;
5428 if (Error Err = importInto(T, D->getType()))
5429 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005430
Balazs Keri3b30d652018-10-19 13:32:20 +00005431 auto TInfoOrErr = import(D->getTypeSourceInfo());
5432 if (!TInfoOrErr)
5433 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005434
5435 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005436 if (Error Err = ImportTemplateArgumentListInfo(
5437 D->getTemplateArgsInfo(), ToTAInfo))
5438 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005439
5440 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005441 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005442 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5443 // Import TemplateArgumentListInfo
5444 TemplateArgumentListInfo ArgInfos;
5445 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5446 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005447 if (Error Err = ImportTemplateArgumentListInfo(
5448 *FromTAArgsAsWritten, ArgInfos))
5449 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005450
Balazs Keri3b30d652018-10-19 13:32:20 +00005451 auto ToTPListOrErr = ImportTemplateParameterList(
5452 FromPartial->getTemplateParameters());
5453 if (!ToTPListOrErr)
5454 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005455
Gabor Marton26f72a92018-07-12 09:42:05 +00005456 PartVarSpecDecl *ToPartial;
5457 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005458 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5459 VarTemplate, T, *TInfoOrErr,
5460 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005461 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005462
Balazs Keri3b30d652018-10-19 13:32:20 +00005463 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5464 FromPartial->getInstantiatedFromMember()))
5465 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5466 else
5467 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005468
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005469 if (FromPartial->isMemberSpecialization())
5470 ToPartial->setMemberSpecialization();
5471
5472 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005473
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005474 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005475 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5476 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5477 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005478 D->getStorageClass(), TemplateArgs))
5479 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005480 }
5481
Balazs Keri3b30d652018-10-19 13:32:20 +00005482 if (D->getPointOfInstantiation().isValid()) {
5483 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5484 D2->setPointOfInstantiation(*POIOrErr);
5485 else
5486 return POIOrErr.takeError();
5487 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005488
Larisse Voufo39a1e502013-08-06 01:03:05 +00005489 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005490 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005491
5492 // Add this specialization to the class template.
5493 VarTemplate->AddSpecialization(D2, InsertPos);
5494
5495 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005496 if (auto LocOrErr = import(D->getQualifierLoc()))
5497 D2->setQualifierInfo(*LocOrErr);
5498 else
5499 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005500
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005501 if (D->isConstexpr())
5502 D2->setConstexpr(true);
5503
Larisse Voufo39a1e502013-08-06 01:03:05 +00005504 // Add the specialization to this context.
5505 D2->setLexicalDeclContext(LexicalDC);
5506 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005507
5508 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005509 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005510
Balazs Keri3b30d652018-10-19 13:32:20 +00005511 if (Error Err = ImportInitializer(D, D2))
5512 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005513
5514 return D2;
5515}
5516
Balazs Keri3b30d652018-10-19 13:32:20 +00005517ExpectedDecl
5518ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005519 DeclContext *DC, *LexicalDC;
5520 DeclarationName Name;
5521 SourceLocation Loc;
5522 NamedDecl *ToD;
5523
Balazs Keri3b30d652018-10-19 13:32:20 +00005524 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5525 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005526
5527 if (ToD)
5528 return ToD;
5529
5530 // Try to find a function in our own ("to") context with the same name, same
5531 // type, and in the same context as the function we're importing.
5532 if (!LexicalDC->isFunctionOrMethod()) {
5533 unsigned IDNS = Decl::IDNS_Ordinary;
5534 SmallVector<NamedDecl *, 2> FoundDecls;
5535 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005536 for (auto *FoundDecl : FoundDecls) {
5537 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005538 continue;
5539
Balazs Keri3b30d652018-10-19 13:32:20 +00005540 if (auto *FoundFunction =
5541 dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005542 if (FoundFunction->hasExternalFormalLinkage() &&
5543 D->hasExternalFormalLinkage()) {
5544 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00005545 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005546 // FIXME: Actually try to merge the body and other attributes.
5547 return FoundFunction;
5548 }
5549 }
5550 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005551 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005552 }
5553 }
5554
Balazs Keri3b30d652018-10-19 13:32:20 +00005555 auto ParamsOrErr = ImportTemplateParameterList(
5556 D->getTemplateParameters());
5557 if (!ParamsOrErr)
5558 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005559
Balazs Keri3b30d652018-10-19 13:32:20 +00005560 FunctionDecl *TemplatedFD;
5561 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5562 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005563
Gabor Marton26f72a92018-07-12 09:42:05 +00005564 FunctionTemplateDecl *ToFunc;
5565 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005566 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005567 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005568
5569 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
5570 ToFunc->setAccess(D->getAccess());
5571 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005572
5573 LexicalDC->addDeclInternal(ToFunc);
5574 return ToFunc;
5575}
5576
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005577//----------------------------------------------------------------------------
5578// Import Statements
5579//----------------------------------------------------------------------------
5580
Balazs Keri3b30d652018-10-19 13:32:20 +00005581ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005582 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5583 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005584 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005585}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005586
Balazs Keri3b30d652018-10-19 13:32:20 +00005587
5588ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005589 SmallVector<IdentifierInfo *, 4> Names;
5590 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5591 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005592 // ToII is nullptr when no symbolic name is given for output operand
5593 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005594 Names.push_back(ToII);
5595 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005596
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005597 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5598 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005599 // ToII is nullptr when no symbolic name is given for input operand
5600 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005601 Names.push_back(ToII);
5602 }
5603
5604 SmallVector<StringLiteral *, 4> Clobbers;
5605 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005606 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5607 Clobbers.push_back(*ClobberOrErr);
5608 else
5609 return ClobberOrErr.takeError();
5610
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005611 }
5612
5613 SmallVector<StringLiteral *, 4> Constraints;
5614 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005615 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5616 Constraints.push_back(*OutputOrErr);
5617 else
5618 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005619 }
5620
5621 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005622 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5623 Constraints.push_back(*InputOrErr);
5624 else
5625 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005626 }
5627
5628 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005629 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5630 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005631
Balazs Keri3b30d652018-10-19 13:32:20 +00005632 if (Error Err = ImportArrayChecked(
5633 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5634 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005635
Balazs Keri3b30d652018-10-19 13:32:20 +00005636 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5637 if (!AsmLocOrErr)
5638 return AsmLocOrErr.takeError();
5639 auto AsmStrOrErr = import(S->getAsmString());
5640 if (!AsmStrOrErr)
5641 return AsmStrOrErr.takeError();
5642 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5643 if (!RParenLocOrErr)
5644 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005645
5646 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005647 Importer.getToContext(),
5648 *AsmLocOrErr,
5649 S->isSimple(),
5650 S->isVolatile(),
5651 S->getNumOutputs(),
5652 S->getNumInputs(),
5653 Names.data(),
5654 Constraints.data(),
5655 Exprs.data(),
5656 *AsmStrOrErr,
5657 S->getNumClobbers(),
5658 Clobbers.data(),
5659 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005660}
5661
Balazs Keri3b30d652018-10-19 13:32:20 +00005662ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5663 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5664 if (!Imp)
5665 return Imp.takeError();
5666
5667 DeclGroupRef ToDG;
5668 SourceLocation ToBeginLoc, ToEndLoc;
5669 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5670
5671 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005672}
5673
Balazs Keri3b30d652018-10-19 13:32:20 +00005674ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5675 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5676 if (!ToSemiLocOrErr)
5677 return ToSemiLocOrErr.takeError();
5678 return new (Importer.getToContext()) NullStmt(
5679 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005680}
5681
Balazs Keri3b30d652018-10-19 13:32:20 +00005682ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005683 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005684
Balazs Keri3b30d652018-10-19 13:32:20 +00005685 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5686 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005687
Balazs Keri3b30d652018-10-19 13:32:20 +00005688 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5689 if (!ToLBracLocOrErr)
5690 return ToLBracLocOrErr.takeError();
5691
5692 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5693 if (!ToRBracLocOrErr)
5694 return ToRBracLocOrErr.takeError();
5695
5696 return CompoundStmt::Create(
5697 Importer.getToContext(), ToStmts,
5698 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005699}
5700
Balazs Keri3b30d652018-10-19 13:32:20 +00005701ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5702 auto Imp = importSeq(
5703 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5704 S->getEllipsisLoc(), S->getColonLoc());
5705 if (!Imp)
5706 return Imp.takeError();
5707
5708 Expr *ToLHS, *ToRHS;
5709 Stmt *ToSubStmt;
5710 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5711 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5712 *Imp;
5713
Bruno Ricci5b30571752018-10-28 12:30:53 +00005714 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5715 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005716 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005717
Gabor Horvath480892b2017-10-18 09:25:18 +00005718 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005719}
5720
Balazs Keri3b30d652018-10-19 13:32:20 +00005721ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5722 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5723 if (!Imp)
5724 return Imp.takeError();
5725
5726 SourceLocation ToDefaultLoc, ToColonLoc;
5727 Stmt *ToSubStmt;
5728 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5729
5730 return new (Importer.getToContext()) DefaultStmt(
5731 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005732}
5733
Balazs Keri3b30d652018-10-19 13:32:20 +00005734ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5735 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5736 if (!Imp)
5737 return Imp.takeError();
5738
5739 SourceLocation ToIdentLoc;
5740 LabelDecl *ToLabelDecl;
5741 Stmt *ToSubStmt;
5742 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5743
5744 return new (Importer.getToContext()) LabelStmt(
5745 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005746}
5747
Balazs Keri3b30d652018-10-19 13:32:20 +00005748ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5749 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5750 if (!ToAttrLocOrErr)
5751 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005752 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5753 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005754 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5755 return std::move(Err);
5756 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5757 if (!ToSubStmtOrErr)
5758 return ToSubStmtOrErr.takeError();
5759
5760 return AttributedStmt::Create(
5761 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005762}
5763
Balazs Keri3b30d652018-10-19 13:32:20 +00005764ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5765 auto Imp = importSeq(
5766 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5767 S->getThen(), S->getElseLoc(), S->getElse());
5768 if (!Imp)
5769 return Imp.takeError();
5770
5771 SourceLocation ToIfLoc, ToElseLoc;
5772 Stmt *ToInit, *ToThen, *ToElse;
5773 VarDecl *ToConditionVariable;
5774 Expr *ToCond;
5775 std::tie(
5776 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5777 *Imp;
5778
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005779 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5780 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5781 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005782}
5783
Balazs Keri3b30d652018-10-19 13:32:20 +00005784ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5785 auto Imp = importSeq(
5786 S->getInit(), S->getConditionVariable(), S->getCond(),
5787 S->getBody(), S->getSwitchLoc());
5788 if (!Imp)
5789 return Imp.takeError();
5790
5791 Stmt *ToInit, *ToBody;
5792 VarDecl *ToConditionVariable;
5793 Expr *ToCond;
5794 SourceLocation ToSwitchLoc;
5795 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5796
Bruno Riccie2806f82018-10-29 16:12:37 +00005797 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5798 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005799 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005800 ToStmt->setSwitchLoc(ToSwitchLoc);
5801
Sean Callanan59721b32015-04-28 18:41:46 +00005802 // Now we have to re-chain the cases.
5803 SwitchCase *LastChainedSwitchCase = nullptr;
5804 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5805 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005806 Expected<SwitchCase *> ToSCOrErr = import(SC);
5807 if (!ToSCOrErr)
5808 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005809 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005810 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005811 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005812 ToStmt->setSwitchCaseList(*ToSCOrErr);
5813 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005814 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005815
Sean Callanan59721b32015-04-28 18:41:46 +00005816 return ToStmt;
5817}
5818
Balazs Keri3b30d652018-10-19 13:32:20 +00005819ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5820 auto Imp = importSeq(
5821 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5822 if (!Imp)
5823 return Imp.takeError();
5824
5825 VarDecl *ToConditionVariable;
5826 Expr *ToCond;
5827 Stmt *ToBody;
5828 SourceLocation ToWhileLoc;
5829 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5830
Bruno Riccibacf7512018-10-30 13:42:41 +00005831 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5832 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005833}
5834
Balazs Keri3b30d652018-10-19 13:32:20 +00005835ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5836 auto Imp = importSeq(
5837 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5838 S->getRParenLoc());
5839 if (!Imp)
5840 return Imp.takeError();
5841
5842 Stmt *ToBody;
5843 Expr *ToCond;
5844 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5845 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5846
5847 return new (Importer.getToContext()) DoStmt(
5848 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005849}
5850
Balazs Keri3b30d652018-10-19 13:32:20 +00005851ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5852 auto Imp = importSeq(
5853 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5854 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5855 if (!Imp)
5856 return Imp.takeError();
5857
5858 Stmt *ToInit;
5859 Expr *ToCond, *ToInc;
5860 VarDecl *ToConditionVariable;
5861 Stmt *ToBody;
5862 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5863 std::tie(
5864 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5865 ToLParenLoc, ToRParenLoc) = *Imp;
5866
5867 return new (Importer.getToContext()) ForStmt(
5868 Importer.getToContext(),
5869 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5870 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005871}
5872
Balazs Keri3b30d652018-10-19 13:32:20 +00005873ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5874 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5875 if (!Imp)
5876 return Imp.takeError();
5877
5878 LabelDecl *ToLabel;
5879 SourceLocation ToGotoLoc, ToLabelLoc;
5880 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5881
5882 return new (Importer.getToContext()) GotoStmt(
5883 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005884}
5885
Balazs Keri3b30d652018-10-19 13:32:20 +00005886ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5887 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5888 if (!Imp)
5889 return Imp.takeError();
5890
5891 SourceLocation ToGotoLoc, ToStarLoc;
5892 Expr *ToTarget;
5893 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5894
5895 return new (Importer.getToContext()) IndirectGotoStmt(
5896 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005897}
5898
Balazs Keri3b30d652018-10-19 13:32:20 +00005899ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5900 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5901 if (!ToContinueLocOrErr)
5902 return ToContinueLocOrErr.takeError();
5903 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005904}
5905
Balazs Keri3b30d652018-10-19 13:32:20 +00005906ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5907 auto ToBreakLocOrErr = import(S->getBreakLoc());
5908 if (!ToBreakLocOrErr)
5909 return ToBreakLocOrErr.takeError();
5910 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005911}
5912
Balazs Keri3b30d652018-10-19 13:32:20 +00005913ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5914 auto Imp = importSeq(
5915 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5916 if (!Imp)
5917 return Imp.takeError();
5918
5919 SourceLocation ToReturnLoc;
5920 Expr *ToRetValue;
5921 const VarDecl *ToNRVOCandidate;
5922 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5923
Bruno Ricci023b1d12018-10-30 14:40:49 +00005924 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5925 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005926}
5927
Balazs Keri3b30d652018-10-19 13:32:20 +00005928ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5929 auto Imp = importSeq(
5930 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5931 if (!Imp)
5932 return Imp.takeError();
5933
5934 SourceLocation ToCatchLoc;
5935 VarDecl *ToExceptionDecl;
5936 Stmt *ToHandlerBlock;
5937 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5938
5939 return new (Importer.getToContext()) CXXCatchStmt (
5940 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005941}
5942
Balazs Keri3b30d652018-10-19 13:32:20 +00005943ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5944 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5945 if (!ToTryLocOrErr)
5946 return ToTryLocOrErr.takeError();
5947
5948 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5949 if (!ToTryBlockOrErr)
5950 return ToTryBlockOrErr.takeError();
5951
Sean Callanan59721b32015-04-28 18:41:46 +00005952 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5953 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5954 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005955 if (auto ToHandlerOrErr = import(FromHandler))
5956 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005957 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005958 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005959 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005960
5961 return CXXTryStmt::Create(
5962 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005963}
5964
Balazs Keri3b30d652018-10-19 13:32:20 +00005965ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5966 auto Imp1 = importSeq(
5967 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5968 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5969 if (!Imp1)
5970 return Imp1.takeError();
5971 auto Imp2 = importSeq(
5972 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5973 if (!Imp2)
5974 return Imp2.takeError();
5975
5976 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5977 Expr *ToCond, *ToInc;
5978 Stmt *ToInit, *ToBody;
5979 std::tie(
5980 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5981 ToBody) = *Imp1;
5982 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5983 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5984
5985 return new (Importer.getToContext()) CXXForRangeStmt(
5986 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5987 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005988}
5989
Balazs Keri3b30d652018-10-19 13:32:20 +00005990ExpectedStmt
5991ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5992 auto Imp = importSeq(
5993 S->getElement(), S->getCollection(), S->getBody(),
5994 S->getForLoc(), S->getRParenLoc());
5995 if (!Imp)
5996 return Imp.takeError();
5997
5998 Stmt *ToElement, *ToBody;
5999 Expr *ToCollection;
6000 SourceLocation ToForLoc, ToRParenLoc;
6001 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6002
6003 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6004 ToCollection,
6005 ToBody,
6006 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006007 ToRParenLoc);
6008}
6009
Balazs Keri3b30d652018-10-19 13:32:20 +00006010ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6011 auto Imp = importSeq(
6012 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6013 S->getCatchBody());
6014 if (!Imp)
6015 return Imp.takeError();
6016
6017 SourceLocation ToAtCatchLoc, ToRParenLoc;
6018 VarDecl *ToCatchParamDecl;
6019 Stmt *ToCatchBody;
6020 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6021
6022 return new (Importer.getToContext()) ObjCAtCatchStmt (
6023 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006024}
6025
Balazs Keri3b30d652018-10-19 13:32:20 +00006026ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6027 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6028 if (!ToAtFinallyLocOrErr)
6029 return ToAtFinallyLocOrErr.takeError();
6030 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6031 if (!ToAtFinallyStmtOrErr)
6032 return ToAtFinallyStmtOrErr.takeError();
6033 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6034 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006035}
6036
Balazs Keri3b30d652018-10-19 13:32:20 +00006037ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6038 auto Imp = importSeq(
6039 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6040 if (!Imp)
6041 return Imp.takeError();
6042
6043 SourceLocation ToAtTryLoc;
6044 Stmt *ToTryBody, *ToFinallyStmt;
6045 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6046
Sean Callanan59721b32015-04-28 18:41:46 +00006047 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6048 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6049 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006050 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6051 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006052 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006053 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006054 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006055
Sean Callanan59721b32015-04-28 18:41:46 +00006056 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006057 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006058 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006059 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006060}
6061
Balazs Keri3b30d652018-10-19 13:32:20 +00006062ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006063 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006064 auto Imp = importSeq(
6065 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6066 if (!Imp)
6067 return Imp.takeError();
6068
6069 SourceLocation ToAtSynchronizedLoc;
6070 Expr *ToSynchExpr;
6071 Stmt *ToSynchBody;
6072 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6073
Sean Callanan59721b32015-04-28 18:41:46 +00006074 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6075 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6076}
6077
Balazs Keri3b30d652018-10-19 13:32:20 +00006078ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6079 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6080 if (!ToThrowLocOrErr)
6081 return ToThrowLocOrErr.takeError();
6082 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6083 if (!ToThrowExprOrErr)
6084 return ToThrowExprOrErr.takeError();
6085 return new (Importer.getToContext()) ObjCAtThrowStmt(
6086 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006087}
6088
Balazs Keri3b30d652018-10-19 13:32:20 +00006089ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6090 ObjCAutoreleasePoolStmt *S) {
6091 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6092 if (!ToAtLocOrErr)
6093 return ToAtLocOrErr.takeError();
6094 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6095 if (!ToSubStmtOrErr)
6096 return ToSubStmtOrErr.takeError();
6097 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6098 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006099}
6100
6101//----------------------------------------------------------------------------
6102// Import Expressions
6103//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006104ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006105 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6106 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006107 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006108}
6109
Balazs Keri3b30d652018-10-19 13:32:20 +00006110ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6111 auto Imp = importSeq(
6112 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6113 E->getRParenLoc(), E->getType());
6114 if (!Imp)
6115 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006116
Balazs Keri3b30d652018-10-19 13:32:20 +00006117 SourceLocation ToBuiltinLoc, ToRParenLoc;
6118 Expr *ToSubExpr;
6119 TypeSourceInfo *ToWrittenTypeInfo;
6120 QualType ToType;
6121 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6122 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006123
6124 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006125 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6126 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006127}
6128
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006129
Balazs Keri3b30d652018-10-19 13:32:20 +00006130ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6131 ExpectedType TypeOrErr = import(E->getType());
6132 if (!TypeOrErr)
6133 return TypeOrErr.takeError();
6134
6135 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6136 if (!BeginLocOrErr)
6137 return BeginLocOrErr.takeError();
6138
6139 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006140}
6141
Balazs Keri3b30d652018-10-19 13:32:20 +00006142ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6143 auto Imp = importSeq(
6144 E->getBeginLoc(), E->getType(), E->getFunctionName());
6145 if (!Imp)
6146 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006147
Balazs Keri3b30d652018-10-19 13:32:20 +00006148 SourceLocation ToBeginLoc;
6149 QualType ToType;
6150 StringLiteral *ToFunctionName;
6151 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006152
Bruno Ricci17ff0262018-10-27 19:21:19 +00006153 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6154 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006155}
6156
Balazs Keri3b30d652018-10-19 13:32:20 +00006157ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6158 auto Imp = importSeq(
6159 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6160 E->getLocation(), E->getType());
6161 if (!Imp)
6162 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006163
Balazs Keri3b30d652018-10-19 13:32:20 +00006164 NestedNameSpecifierLoc ToQualifierLoc;
6165 SourceLocation ToTemplateKeywordLoc, ToLocation;
6166 ValueDecl *ToDecl;
6167 QualType ToType;
6168 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6169 *Imp;
6170
6171 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006172 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006173 auto FoundDOrErr = import(E->getFoundDecl());
6174 if (!FoundDOrErr)
6175 return FoundDOrErr.takeError();
6176 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006177 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006178
Aleksei Sidorina693b372016-09-28 10:16:56 +00006179 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006180 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006181 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006182 if (Error Err =
6183 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6184 return std::move(Err);
6185 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006186 }
6187
Balazs Keri3b30d652018-10-19 13:32:20 +00006188 auto *ToE = DeclRefExpr::Create(
6189 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6190 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6191 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006192 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006193 ToE->setHadMultipleCandidates(true);
6194 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006195}
6196
Balazs Keri3b30d652018-10-19 13:32:20 +00006197ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6198 ExpectedType TypeOrErr = import(E->getType());
6199 if (!TypeOrErr)
6200 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006201
Balazs Keri3b30d652018-10-19 13:32:20 +00006202 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006203}
6204
Balazs Keri3b30d652018-10-19 13:32:20 +00006205ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6206 ExpectedExpr ToInitOrErr = import(E->getInit());
6207 if (!ToInitOrErr)
6208 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006209
Balazs Keri3b30d652018-10-19 13:32:20 +00006210 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6211 if (!ToEqualOrColonLocOrErr)
6212 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006213
Balazs Keri3b30d652018-10-19 13:32:20 +00006214 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006215 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006216 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6217 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6218 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006219 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006220 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006221 }
6222
Balazs Keri3b30d652018-10-19 13:32:20 +00006223 SmallVector<Designator, 4> ToDesignators(E->size());
6224 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6225 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006226
6227 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006228 Importer.getToContext(), ToDesignators,
6229 ToIndexExprs, *ToEqualOrColonLocOrErr,
6230 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006231}
6232
Balazs Keri3b30d652018-10-19 13:32:20 +00006233ExpectedStmt
6234ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6235 ExpectedType ToTypeOrErr = import(E->getType());
6236 if (!ToTypeOrErr)
6237 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006238
Balazs Keri3b30d652018-10-19 13:32:20 +00006239 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6240 if (!ToLocationOrErr)
6241 return ToLocationOrErr.takeError();
6242
6243 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6244 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006245}
6246
Balazs Keri3b30d652018-10-19 13:32:20 +00006247ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6248 ExpectedType ToTypeOrErr = import(E->getType());
6249 if (!ToTypeOrErr)
6250 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006251
Balazs Keri3b30d652018-10-19 13:32:20 +00006252 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6253 if (!ToLocationOrErr)
6254 return ToLocationOrErr.takeError();
6255
6256 return IntegerLiteral::Create(
6257 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006258}
6259
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006260
Balazs Keri3b30d652018-10-19 13:32:20 +00006261ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6262 ExpectedType ToTypeOrErr = import(E->getType());
6263 if (!ToTypeOrErr)
6264 return ToTypeOrErr.takeError();
6265
6266 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6267 if (!ToLocationOrErr)
6268 return ToLocationOrErr.takeError();
6269
6270 return FloatingLiteral::Create(
6271 Importer.getToContext(), E->getValue(), E->isExact(),
6272 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006273}
6274
Balazs Keri3b30d652018-10-19 13:32:20 +00006275ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6276 auto ToTypeOrErr = import(E->getType());
6277 if (!ToTypeOrErr)
6278 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006279
Balazs Keri3b30d652018-10-19 13:32:20 +00006280 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6281 if (!ToSubExprOrErr)
6282 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006283
Balazs Keri3b30d652018-10-19 13:32:20 +00006284 return new (Importer.getToContext()) ImaginaryLiteral(
6285 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006286}
6287
Balazs Keri3b30d652018-10-19 13:32:20 +00006288ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6289 ExpectedType ToTypeOrErr = import(E->getType());
6290 if (!ToTypeOrErr)
6291 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006292
Balazs Keri3b30d652018-10-19 13:32:20 +00006293 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6294 if (!ToLocationOrErr)
6295 return ToLocationOrErr.takeError();
6296
6297 return new (Importer.getToContext()) CharacterLiteral(
6298 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006299}
6300
Balazs Keri3b30d652018-10-19 13:32:20 +00006301ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6302 ExpectedType ToTypeOrErr = import(E->getType());
6303 if (!ToTypeOrErr)
6304 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006305
Balazs Keri3b30d652018-10-19 13:32:20 +00006306 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6307 if (Error Err = ImportArrayChecked(
6308 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6309 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006310
Balazs Keri3b30d652018-10-19 13:32:20 +00006311 return StringLiteral::Create(
6312 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6313 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006314}
6315
Balazs Keri3b30d652018-10-19 13:32:20 +00006316ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6317 auto Imp = importSeq(
6318 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6319 E->getInitializer());
6320 if (!Imp)
6321 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006322
Balazs Keri3b30d652018-10-19 13:32:20 +00006323 SourceLocation ToLParenLoc;
6324 TypeSourceInfo *ToTypeSourceInfo;
6325 QualType ToType;
6326 Expr *ToInitializer;
6327 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006328
6329 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006330 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6331 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006332}
6333
Balazs Keri3b30d652018-10-19 13:32:20 +00006334ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6335 auto Imp = importSeq(
6336 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6337 if (!Imp)
6338 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006339
Balazs Keri3b30d652018-10-19 13:32:20 +00006340 SourceLocation ToBuiltinLoc, ToRParenLoc;
6341 QualType ToType;
6342 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6343
6344 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6345 if (Error Err = ImportArrayChecked(
6346 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6347 ToExprs.begin()))
6348 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006349
6350 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006351 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006352}
6353
Balazs Keri3b30d652018-10-19 13:32:20 +00006354ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6355 auto Imp = importSeq(
6356 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6357 if (!Imp)
6358 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006359
Balazs Keri3b30d652018-10-19 13:32:20 +00006360 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6361 LabelDecl *ToLabel;
6362 QualType ToType;
6363 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006364
6365 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006366 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006367}
6368
Balazs Keri3b30d652018-10-19 13:32:20 +00006369ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6370 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6371 if (!Imp)
6372 return Imp.takeError();
6373
6374 SourceLocation ToLParen, ToRParen;
6375 Expr *ToSubExpr;
6376 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006377
Fangrui Song6907ce22018-07-30 19:24:48 +00006378 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006379 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006380}
6381
Balazs Keri3b30d652018-10-19 13:32:20 +00006382ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6383 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6384 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6385 return std::move(Err);
6386
6387 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6388 if (!ToLParenLocOrErr)
6389 return ToLParenLocOrErr.takeError();
6390
6391 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6392 if (!ToRParenLocOrErr)
6393 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006394
6395 return new (Importer.getToContext()) ParenListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006396 Importer.getToContext(), *ToLParenLocOrErr, ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006397}
6398
Balazs Keri3b30d652018-10-19 13:32:20 +00006399ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6400 auto Imp = importSeq(
6401 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6402 if (!Imp)
6403 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006404
Balazs Keri3b30d652018-10-19 13:32:20 +00006405 CompoundStmt *ToSubStmt;
6406 QualType ToType;
6407 SourceLocation ToLParenLoc, ToRParenLoc;
6408 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006409
Balazs Keri3b30d652018-10-19 13:32:20 +00006410 return new (Importer.getToContext()) StmtExpr(
6411 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006412}
6413
Balazs Keri3b30d652018-10-19 13:32:20 +00006414ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6415 auto Imp = importSeq(
6416 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6417 if (!Imp)
6418 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006419
Balazs Keri3b30d652018-10-19 13:32:20 +00006420 Expr *ToSubExpr;
6421 QualType ToType;
6422 SourceLocation ToOperatorLoc;
6423 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006424
Aaron Ballmana5038552018-01-09 13:07:03 +00006425 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006426 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6427 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006428}
6429
Balazs Keri3b30d652018-10-19 13:32:20 +00006430ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006431ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006432 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6433 if (!Imp)
6434 return Imp.takeError();
6435
6436 QualType ToType;
6437 SourceLocation ToOperatorLoc, ToRParenLoc;
6438 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006439
Douglas Gregord8552cd2010-02-19 01:24:23 +00006440 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006441 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6442 import(E->getArgumentTypeInfo());
6443 if (!ToArgumentTypeInfoOrErr)
6444 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006445
Balazs Keri3b30d652018-10-19 13:32:20 +00006446 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6447 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6448 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006449 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006450
Balazs Keri3b30d652018-10-19 13:32:20 +00006451 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6452 if (!ToArgumentExprOrErr)
6453 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006454
Balazs Keri3b30d652018-10-19 13:32:20 +00006455 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6456 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006457}
6458
Balazs Keri3b30d652018-10-19 13:32:20 +00006459ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6460 auto Imp = importSeq(
6461 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6462 if (!Imp)
6463 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006464
Balazs Keri3b30d652018-10-19 13:32:20 +00006465 Expr *ToLHS, *ToRHS;
6466 QualType ToType;
6467 SourceLocation ToOperatorLoc;
6468 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006469
Balazs Keri3b30d652018-10-19 13:32:20 +00006470 return new (Importer.getToContext()) BinaryOperator(
6471 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6472 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006473}
6474
Balazs Keri3b30d652018-10-19 13:32:20 +00006475ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6476 auto Imp = importSeq(
6477 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6478 E->getRHS(), E->getType());
6479 if (!Imp)
6480 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006481
Balazs Keri3b30d652018-10-19 13:32:20 +00006482 Expr *ToCond, *ToLHS, *ToRHS;
6483 SourceLocation ToQuestionLoc, ToColonLoc;
6484 QualType ToType;
6485 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006486
6487 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006488 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6489 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006490}
6491
Balazs Keri3b30d652018-10-19 13:32:20 +00006492ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006493 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006494 auto Imp = importSeq(
6495 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6496 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6497 if (!Imp)
6498 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006499
Balazs Keri3b30d652018-10-19 13:32:20 +00006500 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6501 OpaqueValueExpr *ToOpaqueValue;
6502 SourceLocation ToQuestionLoc, ToColonLoc;
6503 QualType ToType;
6504 std::tie(
6505 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6506 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006507
6508 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006509 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6510 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6511 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006512}
6513
Balazs Keri3b30d652018-10-19 13:32:20 +00006514ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6515 auto Imp = importSeq(
6516 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6517 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6518 if (!Imp)
6519 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006520
Balazs Keri3b30d652018-10-19 13:32:20 +00006521 SourceLocation ToBeginLoc, ToEndLoc;
6522 TypeSourceInfo *ToQueriedTypeSourceInfo;
6523 Expr *ToDimensionExpression;
6524 QualType ToType;
6525 std::tie(
6526 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6527 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006528
6529 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006530 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6531 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006532}
6533
Balazs Keri3b30d652018-10-19 13:32:20 +00006534ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6535 auto Imp = importSeq(
6536 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6537 if (!Imp)
6538 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006539
Balazs Keri3b30d652018-10-19 13:32:20 +00006540 SourceLocation ToBeginLoc, ToEndLoc;
6541 Expr *ToQueriedExpression;
6542 QualType ToType;
6543 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006544
6545 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006546 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6547 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006548}
6549
Balazs Keri3b30d652018-10-19 13:32:20 +00006550ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6551 auto Imp = importSeq(
6552 E->getLocation(), E->getType(), E->getSourceExpr());
6553 if (!Imp)
6554 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006555
Balazs Keri3b30d652018-10-19 13:32:20 +00006556 SourceLocation ToLocation;
6557 QualType ToType;
6558 Expr *ToSourceExpr;
6559 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006560
6561 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006562 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006563}
6564
Balazs Keri3b30d652018-10-19 13:32:20 +00006565ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6566 auto Imp = importSeq(
6567 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6568 if (!Imp)
6569 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006570
Balazs Keri3b30d652018-10-19 13:32:20 +00006571 Expr *ToLHS, *ToRHS;
6572 SourceLocation ToRBracketLoc;
6573 QualType ToType;
6574 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006575
6576 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006577 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6578 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006579}
6580
Balazs Keri3b30d652018-10-19 13:32:20 +00006581ExpectedStmt
6582ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6583 auto Imp = importSeq(
6584 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6585 E->getComputationResultType(), E->getOperatorLoc());
6586 if (!Imp)
6587 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006588
Balazs Keri3b30d652018-10-19 13:32:20 +00006589 Expr *ToLHS, *ToRHS;
6590 QualType ToType, ToComputationLHSType, ToComputationResultType;
6591 SourceLocation ToOperatorLoc;
6592 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6593 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006594
Balazs Keri3b30d652018-10-19 13:32:20 +00006595 return new (Importer.getToContext()) CompoundAssignOperator(
6596 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6597 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6598 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006599}
6600
Balazs Keri3b30d652018-10-19 13:32:20 +00006601Expected<CXXCastPath>
6602ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6603 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006604 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006605 if (auto SpecOrErr = import(*I))
6606 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006607 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006608 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006609 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006610 return Path;
John McCallcf142162010-08-07 06:22:56 +00006611}
6612
Balazs Keri3b30d652018-10-19 13:32:20 +00006613ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6614 ExpectedType ToTypeOrErr = import(E->getType());
6615 if (!ToTypeOrErr)
6616 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006617
Balazs Keri3b30d652018-10-19 13:32:20 +00006618 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6619 if (!ToSubExprOrErr)
6620 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006621
Balazs Keri3b30d652018-10-19 13:32:20 +00006622 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6623 if (!ToBasePathOrErr)
6624 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006625
Balazs Keri3b30d652018-10-19 13:32:20 +00006626 return ImplicitCastExpr::Create(
6627 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6628 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006629}
6630
Balazs Keri3b30d652018-10-19 13:32:20 +00006631ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6632 auto Imp1 = importSeq(
6633 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6634 if (!Imp1)
6635 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006636
Balazs Keri3b30d652018-10-19 13:32:20 +00006637 QualType ToType;
6638 Expr *ToSubExpr;
6639 TypeSourceInfo *ToTypeInfoAsWritten;
6640 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006641
Balazs Keri3b30d652018-10-19 13:32:20 +00006642 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6643 if (!ToBasePathOrErr)
6644 return ToBasePathOrErr.takeError();
6645 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006646
Aleksei Sidorina693b372016-09-28 10:16:56 +00006647 switch (E->getStmtClass()) {
6648 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006649 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006650 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6651 if (!ToLParenLocOrErr)
6652 return ToLParenLocOrErr.takeError();
6653 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6654 if (!ToRParenLocOrErr)
6655 return ToRParenLocOrErr.takeError();
6656 return CStyleCastExpr::Create(
6657 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6658 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6659 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006660 }
6661
6662 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006663 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006664 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6665 if (!ToLParenLocOrErr)
6666 return ToLParenLocOrErr.takeError();
6667 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6668 if (!ToRParenLocOrErr)
6669 return ToRParenLocOrErr.takeError();
6670 return CXXFunctionalCastExpr::Create(
6671 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6672 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6673 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006674 }
6675
6676 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006677 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6678 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6679 if (!ToLParenLocOrErr)
6680 return ToLParenLocOrErr.takeError();
6681 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6682 if (!ToBridgeKeywordLocOrErr)
6683 return ToBridgeKeywordLocOrErr.takeError();
6684 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6685 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6686 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006687 }
6688 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006689 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006690 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006691 }
6692}
6693
Balazs Keri3b30d652018-10-19 13:32:20 +00006694ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6695 SmallVector<OffsetOfNode, 4> ToNodes;
6696 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6697 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006698
Balazs Keri3b30d652018-10-19 13:32:20 +00006699 SourceLocation ToBeginLoc, ToEndLoc;
6700 if (FromNode.getKind() != OffsetOfNode::Base) {
6701 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6702 if (!Imp)
6703 return Imp.takeError();
6704 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6705 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006706
Balazs Keri3b30d652018-10-19 13:32:20 +00006707 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006708 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006709 ToNodes.push_back(
6710 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006711 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006712 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006713 auto ToBSOrErr = import(FromNode.getBase());
6714 if (!ToBSOrErr)
6715 return ToBSOrErr.takeError();
6716 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006717 break;
6718 }
6719 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006720 auto ToFieldOrErr = import(FromNode.getField());
6721 if (!ToFieldOrErr)
6722 return ToFieldOrErr.takeError();
6723 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006724 break;
6725 }
6726 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006727 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6728 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729 break;
6730 }
6731 }
6732 }
6733
Balazs Keri3b30d652018-10-19 13:32:20 +00006734 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6735 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6736 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6737 if (!ToIndexExprOrErr)
6738 return ToIndexExprOrErr.takeError();
6739 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006740 }
6741
Balazs Keri3b30d652018-10-19 13:32:20 +00006742 auto Imp = importSeq(
6743 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6744 E->getRParenLoc());
6745 if (!Imp)
6746 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006747
Balazs Keri3b30d652018-10-19 13:32:20 +00006748 QualType ToType;
6749 TypeSourceInfo *ToTypeSourceInfo;
6750 SourceLocation ToOperatorLoc, ToRParenLoc;
6751 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6752
6753 return OffsetOfExpr::Create(
6754 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6755 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006756}
6757
Balazs Keri3b30d652018-10-19 13:32:20 +00006758ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6759 auto Imp = importSeq(
6760 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6761 if (!Imp)
6762 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006763
Balazs Keri3b30d652018-10-19 13:32:20 +00006764 QualType ToType;
6765 Expr *ToOperand;
6766 SourceLocation ToBeginLoc, ToEndLoc;
6767 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006768
Balazs Keri3b30d652018-10-19 13:32:20 +00006769 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006770 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006771 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006772 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006773 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006774
Balazs Keri3b30d652018-10-19 13:32:20 +00006775 return new (Importer.getToContext()) CXXNoexceptExpr(
6776 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006777}
6778
Balazs Keri3b30d652018-10-19 13:32:20 +00006779ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6780 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6781 if (!Imp)
6782 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006783
Balazs Keri3b30d652018-10-19 13:32:20 +00006784 Expr *ToSubExpr;
6785 QualType ToType;
6786 SourceLocation ToThrowLoc;
6787 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006788
6789 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006790 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006791}
6792
Balazs Keri3b30d652018-10-19 13:32:20 +00006793ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6794 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6795 if (!ToUsedLocOrErr)
6796 return ToUsedLocOrErr.takeError();
6797
6798 auto ToParamOrErr = import(E->getParam());
6799 if (!ToParamOrErr)
6800 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006801
6802 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006803 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006804}
6805
Balazs Keri3b30d652018-10-19 13:32:20 +00006806ExpectedStmt
6807ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6808 auto Imp = importSeq(
6809 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6810 if (!Imp)
6811 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006812
Balazs Keri3b30d652018-10-19 13:32:20 +00006813 QualType ToType;
6814 TypeSourceInfo *ToTypeSourceInfo;
6815 SourceLocation ToRParenLoc;
6816 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006817
6818 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006819 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006820}
6821
Balazs Keri3b30d652018-10-19 13:32:20 +00006822ExpectedStmt
6823ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6824 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6825 if (!ToSubExprOrErr)
6826 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006827
Balazs Keri3b30d652018-10-19 13:32:20 +00006828 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6829 if (!ToDtorOrErr)
6830 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006831
6832 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006833 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6834 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006835}
6836
Balazs Keri3b30d652018-10-19 13:32:20 +00006837ExpectedStmt
6838ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6839 auto Imp = importSeq(
6840 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6841 E->getParenOrBraceRange());
6842 if (!Imp)
6843 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006844
Balazs Keri3b30d652018-10-19 13:32:20 +00006845 CXXConstructorDecl *ToConstructor;
6846 QualType ToType;
6847 TypeSourceInfo *ToTypeSourceInfo;
6848 SourceRange ToParenOrBraceRange;
6849 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006850
Balazs Keri3b30d652018-10-19 13:32:20 +00006851 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6852 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6853 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006854
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006855 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006856 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6857 ToParenOrBraceRange, E->hadMultipleCandidates(),
6858 E->isListInitialization(), E->isStdInitListInitialization(),
6859 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006860}
6861
Balazs Keri3b30d652018-10-19 13:32:20 +00006862ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006863ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006864 auto Imp = importSeq(
6865 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6866 if (!Imp)
6867 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006868
Balazs Keri3b30d652018-10-19 13:32:20 +00006869 QualType ToType;
6870 Expr *ToTemporaryExpr;
6871 const ValueDecl *ToExtendingDecl;
6872 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006873
6874 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006875 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006876
6877 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006878 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006879 return ToMTE;
6880}
6881
Balazs Keri3b30d652018-10-19 13:32:20 +00006882ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6883 auto Imp = importSeq(
6884 E->getType(), E->getPattern(), E->getEllipsisLoc());
6885 if (!Imp)
6886 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006887
Balazs Keri3b30d652018-10-19 13:32:20 +00006888 QualType ToType;
6889 Expr *ToPattern;
6890 SourceLocation ToEllipsisLoc;
6891 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006892
6893 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006894 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006895}
6896
Balazs Keri3b30d652018-10-19 13:32:20 +00006897ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6898 auto Imp = importSeq(
6899 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6900 if (!Imp)
6901 return Imp.takeError();
6902
6903 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6904 NamedDecl *ToPack;
6905 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006906
6907 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006908 if (!E->isValueDependent())
6909 Length = E->getPackLength();
6910
Balazs Keri3b30d652018-10-19 13:32:20 +00006911 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006912 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006913 if (Error Err = ImportTemplateArguments(
6914 E->getPartialArguments().data(),
6915 E->getPartialArguments().size(),
6916 ToPartialArguments))
6917 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006918 }
6919
6920 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006921 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6922 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006923}
6924
Aleksei Sidorina693b372016-09-28 10:16:56 +00006925
Balazs Keri3b30d652018-10-19 13:32:20 +00006926ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6927 auto Imp = importSeq(
6928 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6929 E->getArraySize(), E->getInitializer(), E->getType(),
6930 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6931 E->getDirectInitRange());
6932 if (!Imp)
6933 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006934
Balazs Keri3b30d652018-10-19 13:32:20 +00006935 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6936 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6937 Expr *ToArraySize, *ToInitializer;
6938 QualType ToType;
6939 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6940 std::tie(
6941 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6942 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006943
Balazs Keri3b30d652018-10-19 13:32:20 +00006944 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6945 if (Error Err =
6946 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6947 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006948
6949 return new (Importer.getToContext()) CXXNewExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006950 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6951 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6952 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6953 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6954 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006955}
6956
Balazs Keri3b30d652018-10-19 13:32:20 +00006957ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6958 auto Imp = importSeq(
6959 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6960 if (!Imp)
6961 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006962
Balazs Keri3b30d652018-10-19 13:32:20 +00006963 QualType ToType;
6964 FunctionDecl *ToOperatorDelete;
6965 Expr *ToArgument;
6966 SourceLocation ToBeginLoc;
6967 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006968
6969 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006970 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6971 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6972 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006973}
6974
Balazs Keri3b30d652018-10-19 13:32:20 +00006975ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6976 auto Imp = importSeq(
6977 E->getType(), E->getLocation(), E->getConstructor(),
6978 E->getParenOrBraceRange());
6979 if (!Imp)
6980 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006981
Balazs Keri3b30d652018-10-19 13:32:20 +00006982 QualType ToType;
6983 SourceLocation ToLocation;
6984 CXXConstructorDecl *ToConstructor;
6985 SourceRange ToParenOrBraceRange;
6986 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006987
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006988 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006989 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6990 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006991
Balazs Keri3b30d652018-10-19 13:32:20 +00006992 return CXXConstructExpr::Create(
6993 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6994 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6995 E->isListInitialization(), E->isStdInitListInitialization(),
6996 E->requiresZeroInitialization(), E->getConstructionKind(),
6997 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006998}
6999
Balazs Keri3b30d652018-10-19 13:32:20 +00007000ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7001 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7002 if (!ToSubExprOrErr)
7003 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007004
Balazs Keri3b30d652018-10-19 13:32:20 +00007005 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7006 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7007 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007008
Balazs Keri3b30d652018-10-19 13:32:20 +00007009 return ExprWithCleanups::Create(
7010 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7011 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007012}
7013
Balazs Keri3b30d652018-10-19 13:32:20 +00007014ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7015 auto Imp = importSeq(
7016 E->getCallee(), E->getType(), E->getRParenLoc());
7017 if (!Imp)
7018 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007019
Balazs Keri3b30d652018-10-19 13:32:20 +00007020 Expr *ToCallee;
7021 QualType ToType;
7022 SourceLocation ToRParenLoc;
7023 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007024
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007025 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007026 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7027 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007028
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007029 return new (Importer.getToContext()) CXXMemberCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007030 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7031 ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007032}
7033
Balazs Keri3b30d652018-10-19 13:32:20 +00007034ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7035 ExpectedType ToTypeOrErr = import(E->getType());
7036 if (!ToTypeOrErr)
7037 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007038
Balazs Keri3b30d652018-10-19 13:32:20 +00007039 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7040 if (!ToLocationOrErr)
7041 return ToLocationOrErr.takeError();
7042
7043 return new (Importer.getToContext()) CXXThisExpr(
7044 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007045}
7046
Balazs Keri3b30d652018-10-19 13:32:20 +00007047ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7048 ExpectedType ToTypeOrErr = import(E->getType());
7049 if (!ToTypeOrErr)
7050 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007051
Balazs Keri3b30d652018-10-19 13:32:20 +00007052 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7053 if (!ToLocationOrErr)
7054 return ToLocationOrErr.takeError();
7055
7056 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7057 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007058}
7059
Balazs Keri3b30d652018-10-19 13:32:20 +00007060ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7061 auto Imp1 = importSeq(
7062 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7063 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7064 if (!Imp1)
7065 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007066
Balazs Keri3b30d652018-10-19 13:32:20 +00007067 Expr *ToBase;
7068 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7069 NestedNameSpecifierLoc ToQualifierLoc;
7070 ValueDecl *ToMemberDecl;
7071 QualType ToType;
7072 std::tie(
7073 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7074 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007075
Balazs Keri3b30d652018-10-19 13:32:20 +00007076 auto Imp2 = importSeq(
7077 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7078 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7079 if (!Imp2)
7080 return Imp2.takeError();
7081 NamedDecl *ToDecl;
7082 DeclarationName ToName;
7083 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7084 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007085
7086 DeclAccessPair ToFoundDecl =
7087 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007088
Balazs Keri3b30d652018-10-19 13:32:20 +00007089 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007090
7091 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007092 // FIXME: handle template arguments
7093 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Sean Callanan59721b32015-04-28 18:41:46 +00007094 }
7095
Balazs Keri3b30d652018-10-19 13:32:20 +00007096 return MemberExpr::Create(
7097 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7098 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
7099 ToMemberNameInfo, nullptr, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007100}
7101
Balazs Keri3b30d652018-10-19 13:32:20 +00007102ExpectedStmt
7103ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7104 auto Imp = importSeq(
7105 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7106 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7107 if (!Imp)
7108 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007109
Balazs Keri3b30d652018-10-19 13:32:20 +00007110 Expr *ToBase;
7111 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7112 NestedNameSpecifierLoc ToQualifierLoc;
7113 TypeSourceInfo *ToScopeTypeInfo;
7114 std::tie(
7115 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7116 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007117
7118 PseudoDestructorTypeStorage Storage;
7119 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7120 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007121 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7122 if (!ToDestroyedTypeLocOrErr)
7123 return ToDestroyedTypeLocOrErr.takeError();
7124 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007125 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007126 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7127 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7128 else
7129 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007130 }
7131
7132 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007133 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7134 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007135}
7136
Balazs Keri3b30d652018-10-19 13:32:20 +00007137ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007138 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007139 auto Imp = importSeq(
7140 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7141 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7142 if (!Imp)
7143 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007144
Balazs Keri3b30d652018-10-19 13:32:20 +00007145 QualType ToType;
7146 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7147 NestedNameSpecifierLoc ToQualifierLoc;
7148 NamedDecl *ToFirstQualifierFoundInScope;
7149 std::tie(
7150 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7151 ToFirstQualifierFoundInScope) = *Imp;
7152
7153 Expr *ToBase = nullptr;
7154 if (!E->isImplicitAccess()) {
7155 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7156 ToBase = *ToBaseOrErr;
7157 else
7158 return ToBaseOrErr.takeError();
7159 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007160
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007161 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007162 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007163 if (Error Err = ImportTemplateArgumentListInfo(
7164 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7165 ToTAInfo))
7166 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007167 ResInfo = &ToTAInfo;
7168 }
7169
Balazs Keri3b30d652018-10-19 13:32:20 +00007170 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7171 if (!ToMemberNameInfoOrErr)
7172 return ToMemberNameInfoOrErr.takeError();
7173 DeclarationNameInfo ToMemberNameInfo(
7174 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007175 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007176 if (Error Err = ImportDeclarationNameLoc(
7177 E->getMemberNameInfo(), ToMemberNameInfo))
7178 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007179
7180 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007181 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7182 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7183 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007184}
7185
Balazs Keri3b30d652018-10-19 13:32:20 +00007186ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007187ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007188 auto Imp = importSeq(
7189 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7190 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7191 if (!Imp)
7192 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007193
Balazs Keri3b30d652018-10-19 13:32:20 +00007194 NestedNameSpecifierLoc ToQualifierLoc;
7195 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7196 DeclarationName ToDeclName;
7197 std::tie(
7198 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7199 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007200
Balazs Keri3b30d652018-10-19 13:32:20 +00007201 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7202 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7203 return std::move(Err);
7204
7205 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007206 TemplateArgumentListInfo *ResInfo = nullptr;
7207 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007208 if (Error Err =
7209 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7210 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007211 ResInfo = &ToTAInfo;
7212 }
7213
7214 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007215 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7216 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007217}
7218
Balazs Keri3b30d652018-10-19 13:32:20 +00007219ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7220 CXXUnresolvedConstructExpr *E) {
7221 auto Imp = importSeq(
7222 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7223 if (!Imp)
7224 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007225
Balazs Keri3b30d652018-10-19 13:32:20 +00007226 SourceLocation ToLParenLoc, ToRParenLoc;
7227 TypeSourceInfo *ToTypeSourceInfo;
7228 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7229
7230 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7231 if (Error Err =
7232 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7233 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007234
7235 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007236 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7237 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007238}
7239
Balazs Keri3b30d652018-10-19 13:32:20 +00007240ExpectedStmt
7241ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7242 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7243 if (!ToNamingClassOrErr)
7244 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007245
Balazs Keri3b30d652018-10-19 13:32:20 +00007246 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7247 if (!ToQualifierLocOrErr)
7248 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007249
Balazs Keri3b30d652018-10-19 13:32:20 +00007250 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7251 if (!ToNameInfoOrErr)
7252 return ToNameInfoOrErr.takeError();
7253 DeclarationNameInfo ToNameInfo(
7254 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007255 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007256 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7257 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007258
7259 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007260 for (auto *D : E->decls())
7261 if (auto ToDOrErr = import(D))
7262 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007263 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007264 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007265
Balazs Keri3b30d652018-10-19 13:32:20 +00007266 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7267 TemplateArgumentListInfo ToTAInfo;
7268 if (Error Err = ImportTemplateArgumentListInfo(
7269 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7270 ToTAInfo))
7271 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007272
Balazs Keri3b30d652018-10-19 13:32:20 +00007273 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7274 if (!ToTemplateKeywordLocOrErr)
7275 return ToTemplateKeywordLocOrErr.takeError();
7276
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007277 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007278 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7279 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7280 ToDecls.begin(), ToDecls.end());
7281 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007282
7283 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007284 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7285 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7286 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007287}
7288
Balazs Keri3b30d652018-10-19 13:32:20 +00007289ExpectedStmt
7290ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7291 auto Imp1 = importSeq(
7292 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7293 E->getTemplateKeywordLoc());
7294 if (!Imp1)
7295 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007296
Balazs Keri3b30d652018-10-19 13:32:20 +00007297 QualType ToType;
7298 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7299 NestedNameSpecifierLoc ToQualifierLoc;
7300 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7301
7302 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7303 if (!Imp2)
7304 return Imp2.takeError();
7305 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7306 // Import additional name location/type info.
7307 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7308 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007309
7310 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007311 for (Decl *D : E->decls())
7312 if (auto ToDOrErr = import(D))
7313 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007314 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007315 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007316
7317 TemplateArgumentListInfo ToTAInfo;
7318 TemplateArgumentListInfo *ResInfo = nullptr;
7319 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007320 if (Error Err =
7321 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7322 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007323 ResInfo = &ToTAInfo;
7324 }
7325
Balazs Keri3b30d652018-10-19 13:32:20 +00007326 Expr *ToBase = nullptr;
7327 if (!E->isImplicitAccess()) {
7328 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7329 ToBase = *ToBaseOrErr;
7330 else
7331 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007332 }
7333
7334 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007335 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7336 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7337 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007338}
7339
Balazs Keri3b30d652018-10-19 13:32:20 +00007340ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7341 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7342 if (!Imp)
7343 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007344
Balazs Keri3b30d652018-10-19 13:32:20 +00007345 Expr *ToCallee;
7346 QualType ToType;
7347 SourceLocation ToRParenLoc;
7348 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007349
7350 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007351 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7352 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7353 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007354
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007355 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
7356 return new (Importer.getToContext()) CXXOperatorCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007357 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
7358 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007359 }
7360
Balazs Keri3b30d652018-10-19 13:32:20 +00007361 return new (Importer.getToContext()) CallExpr(
7362 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7363 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007364}
7365
Balazs Keri3b30d652018-10-19 13:32:20 +00007366ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7367 CXXRecordDecl *FromClass = E->getLambdaClass();
7368 auto ToClassOrErr = import(FromClass);
7369 if (!ToClassOrErr)
7370 return ToClassOrErr.takeError();
7371 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007372
7373 // NOTE: lambda classes are created with BeingDefined flag set up.
7374 // It means that ImportDefinition doesn't work for them and we should fill it
7375 // manually.
7376 if (ToClass->isBeingDefined()) {
7377 for (auto FromField : FromClass->fields()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007378 auto ToFieldOrErr = import(FromField);
7379 if (!ToFieldOrErr)
7380 return ToFieldOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007381 }
7382 }
7383
Balazs Keri3b30d652018-10-19 13:32:20 +00007384 auto ToCallOpOrErr = import(E->getCallOperator());
7385 if (!ToCallOpOrErr)
7386 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007387
7388 ToClass->completeDefinition();
7389
Balazs Keri3b30d652018-10-19 13:32:20 +00007390 SmallVector<LambdaCapture, 8> ToCaptures;
7391 ToCaptures.reserve(E->capture_size());
7392 for (const auto &FromCapture : E->captures()) {
7393 if (auto ToCaptureOrErr = import(FromCapture))
7394 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007395 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007396 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007397 }
7398
Balazs Keri3b30d652018-10-19 13:32:20 +00007399 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7400 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7401 return std::move(Err);
7402
7403 auto Imp = importSeq(
7404 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7405 if (!Imp)
7406 return Imp.takeError();
7407
7408 SourceRange ToIntroducerRange;
7409 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7410 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007411
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007412 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007413 Importer.getToContext(), ToClass, ToIntroducerRange,
7414 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7415 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7416 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007417}
7418
Sean Callanan8bca9962016-03-28 21:43:01 +00007419
Balazs Keri3b30d652018-10-19 13:32:20 +00007420ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7421 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7422 if (!Imp)
7423 return Imp.takeError();
7424
7425 SourceLocation ToLBraceLoc, ToRBraceLoc;
7426 QualType ToType;
7427 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7428
7429 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7430 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7431 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007432
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007433 ASTContext &ToCtx = Importer.getToContext();
7434 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007435 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7436 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007437
Balazs Keri3b30d652018-10-19 13:32:20 +00007438 if (E->hasArrayFiller()) {
7439 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7440 To->setArrayFiller(*ToFillerOrErr);
7441 else
7442 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007443 }
7444
Balazs Keri3b30d652018-10-19 13:32:20 +00007445 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7446 if (auto ToFDOrErr = import(FromFD))
7447 To->setInitializedFieldInUnion(*ToFDOrErr);
7448 else
7449 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007450 }
7451
Balazs Keri3b30d652018-10-19 13:32:20 +00007452 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7453 if (auto ToSyntFormOrErr = import(SyntForm))
7454 To->setSyntacticForm(*ToSyntFormOrErr);
7455 else
7456 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007457 }
7458
Gabor Martona20ce602018-09-03 13:10:53 +00007459 // Copy InitListExprBitfields, which are not handled in the ctor of
7460 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007461 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007462
7463 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007464}
7465
Balazs Keri3b30d652018-10-19 13:32:20 +00007466ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007467 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007468 ExpectedType ToTypeOrErr = import(E->getType());
7469 if (!ToTypeOrErr)
7470 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007471
Balazs Keri3b30d652018-10-19 13:32:20 +00007472 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7473 if (!ToSubExprOrErr)
7474 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007475
Balazs Keri3b30d652018-10-19 13:32:20 +00007476 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7477 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007478}
7479
Balazs Keri3b30d652018-10-19 13:32:20 +00007480ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007481 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007482 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7483 if (!Imp)
7484 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007485
Balazs Keri3b30d652018-10-19 13:32:20 +00007486 SourceLocation ToLocation;
7487 QualType ToType;
7488 CXXConstructorDecl *ToConstructor;
7489 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007490
7491 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007492 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7493 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007494}
7495
Balazs Keri3b30d652018-10-19 13:32:20 +00007496ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7497 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7498 if (!Imp)
7499 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007500
Balazs Keri3b30d652018-10-19 13:32:20 +00007501 QualType ToType;
7502 Expr *ToCommonExpr, *ToSubExpr;
7503 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007504
Balazs Keri3b30d652018-10-19 13:32:20 +00007505 return new (Importer.getToContext()) ArrayInitLoopExpr(
7506 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007507}
7508
Balazs Keri3b30d652018-10-19 13:32:20 +00007509ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7510 ExpectedType ToTypeOrErr = import(E->getType());
7511 if (!ToTypeOrErr)
7512 return ToTypeOrErr.takeError();
7513 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007514}
7515
Balazs Keri3b30d652018-10-19 13:32:20 +00007516ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7517 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7518 if (!ToBeginLocOrErr)
7519 return ToBeginLocOrErr.takeError();
7520
7521 auto ToFieldOrErr = import(E->getField());
7522 if (!ToFieldOrErr)
7523 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007524
7525 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007526 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007527}
7528
Balazs Keri3b30d652018-10-19 13:32:20 +00007529ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7530 auto Imp = importSeq(
7531 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7532 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7533 if (!Imp)
7534 return Imp.takeError();
7535
7536 QualType ToType;
7537 Expr *ToSubExpr;
7538 TypeSourceInfo *ToTypeInfoAsWritten;
7539 SourceLocation ToOperatorLoc, ToRParenLoc;
7540 SourceRange ToAngleBrackets;
7541 std::tie(
7542 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7543 ToAngleBrackets) = *Imp;
7544
Sean Callanandd2c1742016-05-16 20:48:03 +00007545 ExprValueKind VK = E->getValueKind();
7546 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007547 auto ToBasePathOrErr = ImportCastPath(E);
7548 if (!ToBasePathOrErr)
7549 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007550
Sean Callanandd2c1742016-05-16 20:48:03 +00007551 if (isa<CXXStaticCastExpr>(E)) {
7552 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007553 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7554 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007555 } else if (isa<CXXDynamicCastExpr>(E)) {
7556 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007557 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7558 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007559 } else if (isa<CXXReinterpretCastExpr>(E)) {
7560 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007561 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7562 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007563 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007564 return CXXConstCastExpr::Create(
7565 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7566 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007567 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007568 llvm_unreachable("Unknown cast type");
7569 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007570 }
7571}
7572
Balazs Keri3b30d652018-10-19 13:32:20 +00007573ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007574 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007575 auto Imp = importSeq(
7576 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7577 if (!Imp)
7578 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007579
Balazs Keri3b30d652018-10-19 13:32:20 +00007580 QualType ToType;
7581 SourceLocation ToExprLoc;
7582 NonTypeTemplateParmDecl *ToParameter;
7583 Expr *ToReplacement;
7584 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007585
7586 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007587 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007588}
7589
Balazs Keri3b30d652018-10-19 13:32:20 +00007590ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7591 auto Imp = importSeq(
7592 E->getType(), E->getBeginLoc(), E->getEndLoc());
7593 if (!Imp)
7594 return Imp.takeError();
7595
7596 QualType ToType;
7597 SourceLocation ToBeginLoc, ToEndLoc;
7598 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007599
7600 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007601 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7602 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007603
7604 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7605 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007606 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007607
7608 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007609 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7610 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007611}
7612
Balazs Keri3b30d652018-10-19 13:32:20 +00007613ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7614 ExpectedType ToTypeOrErr = import(E->getType());
7615 if (!ToTypeOrErr)
7616 return ToTypeOrErr.takeError();
7617
7618 auto ToSourceRangeOrErr = import(E->getSourceRange());
7619 if (!ToSourceRangeOrErr)
7620 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007621
7622 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007623 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7624 return new (Importer.getToContext()) CXXTypeidExpr(
7625 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7626 else
7627 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007628 }
7629
Balazs Keri3b30d652018-10-19 13:32:20 +00007630 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7631 if (!ToExprOperandOrErr)
7632 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007633
Balazs Keri3b30d652018-10-19 13:32:20 +00007634 return new (Importer.getToContext()) CXXTypeidExpr(
7635 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007636}
7637
Lang Hames19e07e12017-06-20 21:06:00 +00007638void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7639 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007640 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7641 if (auto ImportedOrErr = import(FromOverriddenMethod))
7642 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7643 (*ImportedOrErr)->getCanonicalDecl()));
7644 else
7645 consumeError(ImportedOrErr.takeError());
7646 }
Lang Hames19e07e12017-06-20 21:06:00 +00007647}
7648
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007649ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007650 ASTContext &FromContext, FileManager &FromFileManager,
7651 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007652 : ToContext(ToContext), FromContext(FromContext),
7653 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7654 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007655 ImportedDecls[FromContext.getTranslationUnitDecl()]
7656 = ToContext.getTranslationUnitDecl();
7657}
7658
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007659ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007660
7661QualType ASTImporter::Import(QualType FromT) {
7662 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007663 return {};
John McCall424cec92011-01-19 06:33:43 +00007664
Balazs Keri3b30d652018-10-19 13:32:20 +00007665 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007666
7667 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007668 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007669 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007670 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007671 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007672
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007673 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007674 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007675 ExpectedType ToTOrErr = Importer.Visit(FromTy);
7676 if (!ToTOrErr) {
7677 llvm::consumeError(ToTOrErr.takeError());
7678 return {};
7679 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007680
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007681 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007682 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007683
Balazs Keri3b30d652018-10-19 13:32:20 +00007684 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007685}
7686
Douglas Gregor62d311f2010-02-09 19:21:46 +00007687TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007688 if (!FromTSI)
7689 return FromTSI;
7690
7691 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007692 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007693 QualType T = Import(FromTSI->getType());
7694 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007695 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007696
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007697 return ToContext.getTrivialTypeSourceInfo(
7698 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007699}
7700
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007701Attr *ASTImporter::Import(const Attr *FromAttr) {
7702 Attr *ToAttr = FromAttr->clone(ToContext);
7703 ToAttr->setRange(Import(FromAttr->getRange()));
7704 return ToAttr;
7705}
7706
Sean Callanan59721b32015-04-28 18:41:46 +00007707Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7708 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7709 if (Pos != ImportedDecls.end()) {
7710 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007711 // FIXME: move this call to ImportDeclParts().
Balazs Keri3b30d652018-10-19 13:32:20 +00007712 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD))
7713 llvm::consumeError(std::move(Err));
Sean Callanan59721b32015-04-28 18:41:46 +00007714 return ToD;
7715 } else {
7716 return nullptr;
7717 }
7718}
7719
Douglas Gregor62d311f2010-02-09 19:21:46 +00007720Decl *ASTImporter::Import(Decl *FromD) {
7721 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007722 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007723
Douglas Gregord451ea92011-07-29 23:31:30 +00007724 ASTNodeImporter Importer(*this);
7725
Gabor Marton26f72a92018-07-12 09:42:05 +00007726 // Check whether we've already imported this declaration.
7727 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7728 if (ToD) {
7729 // If FromD has some updated flags after last import, apply it
7730 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007731 return ToD;
7732 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007733
7734 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007735 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
7736 if (!ToDOrErr) {
7737 llvm::consumeError(ToDOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007738 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007739 }
7740 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007741
Gabor Marton26f72a92018-07-12 09:42:05 +00007742 // Notify subclasses.
7743 Imported(FromD, ToD);
7744
Gabor Martonac3a5d62018-09-17 12:04:52 +00007745 updateFlags(FromD, ToD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007746 return ToD;
7747}
7748
Balazs Keri3b30d652018-10-19 13:32:20 +00007749Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007750 if (!FromDC)
7751 return FromDC;
7752
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007753 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007754 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007755 return nullptr;
7756
Fangrui Song6907ce22018-07-30 19:24:48 +00007757 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007758 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007759 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7760 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007761 if (ToRecord->isCompleteDefinition()) {
7762 // Do nothing.
7763 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007764 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7765 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7766 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007767 } else {
7768 CompleteDecl(ToRecord);
7769 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007770 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7771 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007772 if (ToEnum->isCompleteDefinition()) {
7773 // Do nothing.
7774 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007775 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7776 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7777 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007778 } else {
7779 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007780 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007781 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7782 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007783 if (ToClass->getDefinition()) {
7784 // Do nothing.
7785 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007786 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7787 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7788 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007789 } else {
7790 CompleteDecl(ToClass);
7791 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007792 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7793 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007794 if (ToProto->getDefinition()) {
7795 // Do nothing.
7796 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007797 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7798 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7799 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007800 } else {
7801 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007802 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007803 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007804
Douglas Gregor95d82832012-01-24 18:36:04 +00007805 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007806}
7807
7808Expr *ASTImporter::Import(Expr *FromE) {
7809 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007810 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007811
7812 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7813}
7814
7815Stmt *ASTImporter::Import(Stmt *FromS) {
7816 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007817 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007818
Fangrui Song6907ce22018-07-30 19:24:48 +00007819 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007820 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7821 if (Pos != ImportedStmts.end())
7822 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007823
Balazs Keri3b30d652018-10-19 13:32:20 +00007824 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007825 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007826 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
7827 if (!ToSOrErr) {
7828 llvm::consumeError(ToSOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007829 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007830 }
Craig Topper36250ad2014-05-12 05:36:57 +00007831
Balazs Keri3b30d652018-10-19 13:32:20 +00007832 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007833 auto *FromE = cast<Expr>(FromS);
7834 // Copy ExprBitfields, which may not be handled in Expr subclasses
7835 // constructors.
7836 ToE->setValueKind(FromE->getValueKind());
7837 ToE->setObjectKind(FromE->getObjectKind());
7838 ToE->setTypeDependent(FromE->isTypeDependent());
7839 ToE->setValueDependent(FromE->isValueDependent());
7840 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7841 ToE->setContainsUnexpandedParameterPack(
7842 FromE->containsUnexpandedParameterPack());
7843 }
7844
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007845 // Record the imported declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007846 ImportedStmts[FromS] = *ToSOrErr;
7847 return *ToSOrErr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007848}
7849
7850NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7851 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007852 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007853
Douglas Gregor90ebf252011-04-27 16:48:40 +00007854 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7855
7856 switch (FromNNS->getKind()) {
7857 case NestedNameSpecifier::Identifier:
7858 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7859 return NestedNameSpecifier::Create(ToContext, prefix, II);
7860 }
Craig Topper36250ad2014-05-12 05:36:57 +00007861 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007862
7863 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007864 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007865 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007866 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7867 }
Craig Topper36250ad2014-05-12 05:36:57 +00007868 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007869
7870 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007871 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007872 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007873 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7874 }
Craig Topper36250ad2014-05-12 05:36:57 +00007875 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007876
7877 case NestedNameSpecifier::Global:
7878 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7879
Nikola Smiljanic67860242014-09-26 00:28:20 +00007880 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007881 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007882 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007883 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7884 }
7885 return nullptr;
7886
Douglas Gregor90ebf252011-04-27 16:48:40 +00007887 case NestedNameSpecifier::TypeSpec:
7888 case NestedNameSpecifier::TypeSpecWithTemplate: {
7889 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7890 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007891 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007892 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007893 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007894 bTemplate, T.getTypePtr());
7895 }
7896 }
Craig Topper36250ad2014-05-12 05:36:57 +00007897 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007898 }
7899
7900 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007901}
7902
Douglas Gregor14454802011-02-25 02:25:35 +00007903NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007904 // Copied from NestedNameSpecifier mostly.
7905 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7906 NestedNameSpecifierLoc NNS = FromNNS;
7907
7908 // Push each of the nested-name-specifiers's onto a stack for
7909 // serialization in reverse order.
7910 while (NNS) {
7911 NestedNames.push_back(NNS);
7912 NNS = NNS.getPrefix();
7913 }
7914
7915 NestedNameSpecifierLocBuilder Builder;
7916
7917 while (!NestedNames.empty()) {
7918 NNS = NestedNames.pop_back_val();
7919 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7920 if (!Spec)
7921 return NestedNameSpecifierLoc();
7922
7923 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7924 switch (Kind) {
7925 case NestedNameSpecifier::Identifier:
7926 Builder.Extend(getToContext(),
7927 Spec->getAsIdentifier(),
7928 Import(NNS.getLocalBeginLoc()),
7929 Import(NNS.getLocalEndLoc()));
7930 break;
7931
7932 case NestedNameSpecifier::Namespace:
7933 Builder.Extend(getToContext(),
7934 Spec->getAsNamespace(),
7935 Import(NNS.getLocalBeginLoc()),
7936 Import(NNS.getLocalEndLoc()));
7937 break;
7938
7939 case NestedNameSpecifier::NamespaceAlias:
7940 Builder.Extend(getToContext(),
7941 Spec->getAsNamespaceAlias(),
7942 Import(NNS.getLocalBeginLoc()),
7943 Import(NNS.getLocalEndLoc()));
7944 break;
7945
7946 case NestedNameSpecifier::TypeSpec:
7947 case NestedNameSpecifier::TypeSpecWithTemplate: {
7948 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7949 QualType(Spec->getAsType(), 0));
7950 Builder.Extend(getToContext(),
7951 Import(NNS.getLocalBeginLoc()),
7952 TSI->getTypeLoc(),
7953 Import(NNS.getLocalEndLoc()));
7954 break;
7955 }
7956
7957 case NestedNameSpecifier::Global:
7958 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7959 break;
7960
7961 case NestedNameSpecifier::Super: {
7962 SourceRange ToRange = Import(NNS.getSourceRange());
7963 Builder.MakeSuper(getToContext(),
7964 Spec->getAsRecordDecl(),
7965 ToRange.getBegin(),
7966 ToRange.getEnd());
7967 }
7968 }
7969 }
7970
7971 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007972}
7973
Douglas Gregore2e50d332010-12-01 01:36:18 +00007974TemplateName ASTImporter::Import(TemplateName From) {
7975 switch (From.getKind()) {
7976 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007977 if (auto *ToTemplate =
7978 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007979 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007980
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007981 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007982
Douglas Gregore2e50d332010-12-01 01:36:18 +00007983 case TemplateName::OverloadedTemplate: {
7984 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7985 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007986 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007987 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007988 ToTemplates.addDecl(To);
7989 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007990 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007991 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007992 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00007993 ToTemplates.end());
7994 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007995
Douglas Gregore2e50d332010-12-01 01:36:18 +00007996 case TemplateName::QualifiedTemplate: {
7997 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7998 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7999 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008000 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008001
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008002 if (auto *ToTemplate =
8003 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00008004 return ToContext.getQualifiedTemplateName(Qualifier,
8005 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008006 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008007
8008 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008009 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008010
Douglas Gregore2e50d332010-12-01 01:36:18 +00008011 case TemplateName::DependentTemplate: {
8012 DependentTemplateName *DTN = From.getAsDependentTemplateName();
8013 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
8014 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008015 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008016
Douglas Gregore2e50d332010-12-01 01:36:18 +00008017 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008018 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008019 Import(DTN->getIdentifier()));
8020 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008021
Douglas Gregore2e50d332010-12-01 01:36:18 +00008022 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
8023 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008024
8025 case TemplateName::SubstTemplateTemplateParm: {
8026 SubstTemplateTemplateParmStorage *subst
8027 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008028 auto *param =
8029 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00008030 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008031 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00008032
8033 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008034 if (replacement.isNull())
8035 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008036
John McCalld9dfe3a2011-06-30 08:33:18 +00008037 return ToContext.getSubstTemplateTemplateParm(param, replacement);
8038 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008039
Douglas Gregor5590be02011-01-15 06:45:20 +00008040 case TemplateName::SubstTemplateTemplateParmPack: {
8041 SubstTemplateTemplateParmPackStorage *SubstPack
8042 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008043 auto *Param =
8044 cast_or_null<TemplateTemplateParmDecl>(
8045 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00008046 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008047 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008048
Douglas Gregor5590be02011-01-15 06:45:20 +00008049 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008050 Expected<TemplateArgument> ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00008051 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
Balazs Keri3b30d652018-10-19 13:32:20 +00008052 if (!ArgPack) {
8053 llvm::consumeError(ArgPack.takeError());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008054 return {};
Balazs Keri3b30d652018-10-19 13:32:20 +00008055 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008056
Balazs Keri3b30d652018-10-19 13:32:20 +00008057 return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00008058 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008059 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008060
Douglas Gregore2e50d332010-12-01 01:36:18 +00008061 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008062}
8063
Douglas Gregor62d311f2010-02-09 19:21:46 +00008064SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
8065 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008066 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008067
Douglas Gregor811663e2010-02-10 00:15:17 +00008068 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008069
Douglas Gregor811663e2010-02-10 00:15:17 +00008070 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00008071 FileID ToFileID = Import(Decomposed.first);
8072 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008073 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008074 SourceManager &ToSM = ToContext.getSourceManager();
8075 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00008076}
8077
8078SourceRange ASTImporter::Import(SourceRange FromRange) {
8079 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
8080}
8081
Douglas Gregor811663e2010-02-10 00:15:17 +00008082FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008083 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008084 if (Pos != ImportedFileIDs.end())
8085 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008086
Douglas Gregor811663e2010-02-10 00:15:17 +00008087 SourceManager &FromSM = FromContext.getSourceManager();
8088 SourceManager &ToSM = ToContext.getSourceManager();
8089 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008090
8091 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008092 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008093 if (FromSLoc.isExpansion()) {
8094 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
8095 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
8096 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
8097 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8098 SourceLocation MLoc;
8099 if (FromEx.isMacroArgExpansion()) {
8100 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
8101 } else {
8102 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
8103 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
8104 FromEx.isExpansionTokenRange());
8105 }
8106 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008107 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008108 // Include location of this file.
8109 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
8110
8111 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
8112 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8113 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8114 // disk again
8115 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8116 // than mmap the files several times.
8117 const FileEntry *Entry =
8118 ToFileManager.getFile(Cache->OrigEntry->getName());
8119 if (!Entry)
8120 return {};
8121 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
8122 FromSLoc.getFile().getFileCharacteristic());
8123 } else {
8124 // FIXME: We want to re-use the existing MemoryBuffer!
8125 const llvm::MemoryBuffer *FromBuf =
8126 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
8127 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8128 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8129 FromBuf->getBufferIdentifier());
8130 ToID = ToSM.createFileID(std::move(ToBuf),
8131 FromSLoc.getFile().getFileCharacteristic());
8132 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008133 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008134
Sebastian Redl99219f12010-09-30 01:03:06 +00008135 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008136 return ToID;
8137}
8138
Sean Callanandd2c1742016-05-16 20:48:03 +00008139CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8140 Expr *ToExpr = Import(From->getInit());
8141 if (!ToExpr && From->getInit())
8142 return nullptr;
8143
8144 if (From->isBaseInitializer()) {
8145 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8146 if (!ToTInfo && From->getTypeSourceInfo())
8147 return nullptr;
8148
8149 return new (ToContext) CXXCtorInitializer(
8150 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
8151 ToExpr, Import(From->getRParenLoc()),
8152 From->isPackExpansion() ? Import(From->getEllipsisLoc())
8153 : SourceLocation());
8154 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008155 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008156 if (!ToField && From->getMember())
8157 return nullptr;
8158
8159 return new (ToContext) CXXCtorInitializer(
8160 ToContext, ToField, Import(From->getMemberLocation()),
8161 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8162 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008163 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00008164 Import(From->getIndirectMember()));
8165 if (!ToIField && From->getIndirectMember())
8166 return nullptr;
8167
8168 return new (ToContext) CXXCtorInitializer(
8169 ToContext, ToIField, Import(From->getMemberLocation()),
8170 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8171 } else if (From->isDelegatingInitializer()) {
8172 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8173 if (!ToTInfo && From->getTypeSourceInfo())
8174 return nullptr;
8175
8176 return new (ToContext)
8177 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
8178 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008179 } else {
8180 return nullptr;
8181 }
8182}
8183
Aleksei Sidorina693b372016-09-28 10:16:56 +00008184CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
8185 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8186 if (Pos != ImportedCXXBaseSpecifiers.end())
8187 return Pos->second;
8188
8189 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
8190 Import(BaseSpec->getSourceRange()),
8191 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8192 BaseSpec->getAccessSpecifierAsWritten(),
8193 Import(BaseSpec->getTypeSourceInfo()),
8194 Import(BaseSpec->getEllipsisLoc()));
8195 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8196 return Imported;
8197}
8198
Balazs Keri3b30d652018-10-19 13:32:20 +00008199Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008200 Decl *To = Import(From);
8201 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008202 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008203
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008204 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008205 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008206
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008207 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00008208 if (!ToRecord->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008209 return Importer.ImportDefinition(
8210 cast<RecordDecl>(FromDC), ToRecord,
8211 ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008212 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00008213 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008214
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008215 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00008216 if (!ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008217 return Importer.ImportDefinition(
8218 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008219 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008220 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008221
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008222 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008223 if (!ToIFace->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008224 return Importer.ImportDefinition(
8225 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8226 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008227 }
8228 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008229
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008230 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008231 if (!ToProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008232 return Importer.ImportDefinition(
8233 cast<ObjCProtocolDecl>(FromDC), ToProto,
8234 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008235 }
8236 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008237
Balazs Keri3b30d652018-10-19 13:32:20 +00008238 return Importer.ImportDeclContext(FromDC, true);
Douglas Gregor0a791672011-01-18 03:11:38 +00008239 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008240
8241 return Error::success();
8242}
8243
8244void ASTImporter::ImportDefinition(Decl *From) {
8245 Error Err = ImportDefinition_New(From);
8246 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008247}
8248
Douglas Gregor96e578d2010-02-05 17:54:41 +00008249DeclarationName ASTImporter::Import(DeclarationName FromName) {
8250 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008251 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008252
8253 switch (FromName.getNameKind()) {
8254 case DeclarationName::Identifier:
8255 return Import(FromName.getAsIdentifierInfo());
8256
8257 case DeclarationName::ObjCZeroArgSelector:
8258 case DeclarationName::ObjCOneArgSelector:
8259 case DeclarationName::ObjCMultiArgSelector:
8260 return Import(FromName.getObjCSelector());
8261
8262 case DeclarationName::CXXConstructorName: {
8263 QualType T = Import(FromName.getCXXNameType());
8264 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008265 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008266
8267 return ToContext.DeclarationNames.getCXXConstructorName(
8268 ToContext.getCanonicalType(T));
8269 }
8270
8271 case DeclarationName::CXXDestructorName: {
8272 QualType T = Import(FromName.getCXXNameType());
8273 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008274 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008275
8276 return ToContext.DeclarationNames.getCXXDestructorName(
8277 ToContext.getCanonicalType(T));
8278 }
8279
Richard Smith35845152017-02-07 01:37:30 +00008280 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008281 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00008282 Import(FromName.getCXXDeductionGuideTemplate()));
8283 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008284 return {};
Richard Smith35845152017-02-07 01:37:30 +00008285 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
8286 }
8287
Douglas Gregor96e578d2010-02-05 17:54:41 +00008288 case DeclarationName::CXXConversionFunctionName: {
8289 QualType T = Import(FromName.getCXXNameType());
8290 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008291 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008292
8293 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8294 ToContext.getCanonicalType(T));
8295 }
8296
8297 case DeclarationName::CXXOperatorName:
8298 return ToContext.DeclarationNames.getCXXOperatorName(
8299 FromName.getCXXOverloadedOperator());
8300
8301 case DeclarationName::CXXLiteralOperatorName:
8302 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
8303 Import(FromName.getCXXLiteralIdentifier()));
8304
8305 case DeclarationName::CXXUsingDirective:
8306 // FIXME: STATICS!
8307 return DeclarationName::getUsingDirectiveName();
8308 }
8309
David Blaikiee4d798f2012-01-20 21:50:17 +00008310 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008311}
8312
Douglas Gregore2e50d332010-12-01 01:36:18 +00008313IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008314 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008315 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008316
Sean Callananf94ef1d2016-05-14 06:11:19 +00008317 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8318
8319 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8320 ToId->setBuiltinID(FromId->getBuiltinID());
8321
8322 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008323}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008324
Douglas Gregor43f54792010-02-17 02:12:47 +00008325Selector ASTImporter::Import(Selector FromSel) {
8326 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008327 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00008328
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008329 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008330 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8331 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8332 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8333 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8334}
8335
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008336DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8337 DeclContext *DC,
8338 unsigned IDNS,
8339 NamedDecl **Decls,
8340 unsigned NumDecls) {
8341 return Name;
8342}
8343
8344DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008345 if (LastDiagFromFrom)
8346 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8347 FromContext.getDiagnostics());
8348 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008349 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008350}
8351
8352DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008353 if (!LastDiagFromFrom)
8354 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8355 ToContext.getDiagnostics());
8356 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008357 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008358}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008359
Douglas Gregor2e15c842012-02-01 21:00:38 +00008360void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008361 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008362 if (!ID->getDefinition())
8363 ID->startDefinition();
8364 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008365 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008366 if (!PD->getDefinition())
8367 PD->startDefinition();
8368 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008369 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008370 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8371 TD->startDefinition();
8372 TD->setCompleteDefinition(true);
8373 }
8374 }
8375 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008376 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008377 }
8378}
8379
Gabor Marton26f72a92018-07-12 09:42:05 +00008380Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8381 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8382 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8383 "Try to import an already imported Decl");
8384 if (Pos != ImportedDecls.end())
8385 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008386 ImportedDecls[From] = To;
8387 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008388}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008389
Douglas Gregordd6006f2012-07-17 21:16:27 +00008390bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8391 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00008392 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00008393 = ImportedTypes.find(From.getTypePtr());
8394 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8395 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008396
Douglas Gregordd6006f2012-07-17 21:16:27 +00008397 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008398 getStructuralEquivalenceKind(*this), false,
8399 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008400 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008401}