blob: 31607dcf446bab700a0b92a1254591d12e628031 [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000014
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Balazs Keri3b30d652018-10-19 13:32:20 +000074 using llvm::make_error;
75 using llvm::Error;
76 using llvm::Expected;
77 using ExpectedType = llvm::Expected<QualType>;
78 using ExpectedStmt = llvm::Expected<Stmt *>;
79 using ExpectedExpr = llvm::Expected<Expr *>;
80 using ExpectedDecl = llvm::Expected<Decl *>;
81 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000082
Balazs Keri3b30d652018-10-19 13:32:20 +000083 std::string ImportError::toString() const {
84 // FIXME: Improve error texts.
85 switch (Error) {
86 case NameConflict:
87 return "NameConflict";
88 case UnsupportedConstruct:
89 return "UnsupportedConstruct";
90 case Unknown:
91 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000092 }
Balazs Keri2a13d662018-10-19 15:16:51 +000093 llvm_unreachable("Invalid error code.");
94 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
125 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000126 }
127
Gabor Marton26f72a92018-07-12 09:42:05 +0000128 void updateFlags(const Decl *From, Decl *To) {
129 // Check if some flags or attrs are new in 'From' and copy into 'To'.
130 // FIXME: Other flags or attrs?
131 if (From->isUsed(false) && !To->isUsed(false))
132 To->setIsUsed();
133 }
134
Balazs Keri3b30d652018-10-19 13:32:20 +0000135 Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
136 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
137 "Try to get field index for non-field.");
138
139 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
140 if (!Owner)
141 return None;
142
143 unsigned Index = 0;
144 for (const auto *D : Owner->decls()) {
145 if (D == F)
146 return Index;
147
148 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
149 ++Index;
150 }
151
152 llvm_unreachable("Field was not found in its parent context.");
153
154 return None;
155 }
156
157 // FIXME: Temporary until every import returns Expected.
158 template <>
159 LLVM_NODISCARD Error
160 ASTImporter::importInto(SourceLocation &To, const SourceLocation &From) {
161 To = Import(From);
162 if (From.isValid() && To.isInvalid())
163 return llvm::make_error<ImportError>();
164 return Error::success();
165 }
166 // FIXME: Temporary until every import returns Expected.
167 template <>
168 LLVM_NODISCARD Error
169 ASTImporter::importInto(QualType &To, const QualType &From) {
170 To = Import(From);
171 if (!From.isNull() && To.isNull())
172 return llvm::make_error<ImportError>();
173 return Error::success();
174 }
175
176 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
177 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
178 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000180
Balazs Keri3b30d652018-10-19 13:32:20 +0000181 // Use this instead of Importer.importInto .
182 template <typename ImportT>
183 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
184 return Importer.importInto(To, From);
185 }
186
187 // Use this to import pointers of specific type.
188 template <typename ImportT>
189 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
190 auto ToI = Importer.Import(From);
191 if (!ToI && From)
192 return make_error<ImportError>();
193 To = cast_or_null<ImportT>(ToI);
194 return Error::success();
195 // FIXME: This should be the final code.
196 //auto ToOrErr = Importer.Import(From);
197 //if (ToOrErr) {
198 // To = cast_or_null<ImportT>(*ToOrErr);
199 //}
200 //return ToOrErr.takeError();
201 }
202
203 // Call the import function of ASTImporter for a baseclass of type `T` and
204 // cast the return value to `T`.
205 template <typename T>
206 Expected<T *> import(T *From) {
207 auto *To = Importer.Import(From);
208 if (!To && From)
209 return make_error<ImportError>();
210 return cast_or_null<T>(To);
211 // FIXME: This should be the final code.
212 //auto ToOrErr = Importer.Import(From);
213 //if (!ToOrErr)
214 // return ToOrErr.takeError();
215 //return cast_or_null<T>(*ToOrErr);
216 }
217
218 template <typename T>
219 Expected<T *> import(const T *From) {
220 return import(const_cast<T *>(From));
221 }
222
223 // Call the import function of ASTImporter for type `T`.
224 template <typename T>
225 Expected<T> import(const T &From) {
226 T To = Importer.Import(From);
227 T DefaultT;
228 if (To == DefaultT && !(From == DefaultT))
229 return make_error<ImportError>();
230 return To;
231 // FIXME: This should be the final code.
232 //return Importer.Import(From);
233 }
234
235 template <class T>
236 Expected<std::tuple<T>>
237 importSeq(const T &From) {
238 Expected<T> ToOrErr = import(From);
239 if (!ToOrErr)
240 return ToOrErr.takeError();
241 return std::make_tuple<T>(std::move(*ToOrErr));
242 }
243
244 // Import multiple objects with a single function call.
245 // This should work for every type for which a variant of `import` exists.
246 // The arguments are processed from left to right and import is stopped on
247 // first error.
248 template <class THead, class... TTail>
249 Expected<std::tuple<THead, TTail...>>
250 importSeq(const THead &FromHead, const TTail &...FromTail) {
251 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
252 if (!ToHeadOrErr)
253 return ToHeadOrErr.takeError();
254 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
255 if (!ToTailOrErr)
256 return ToTailOrErr.takeError();
257 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
258 }
259
260// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 template <typename ToDeclT> struct CallOverloadedCreateFun {
262 template <typename... Args>
263 auto operator()(Args &&... args)
264 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
265 return ToDeclT::Create(std::forward<Args>(args)...);
266 }
267 };
268
269 // Always use these functions to create a Decl during import. There are
270 // certain tasks which must be done after the Decl was created, e.g. we
271 // must immediately register that as an imported Decl. The parameter `ToD`
272 // will be set to the newly created Decl or if had been imported before
273 // then to the already imported Decl. Returns a bool value set to true if
274 // the `FromD` had been imported before.
275 template <typename ToDeclT, typename FromDeclT, typename... Args>
276 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
277 Args &&... args) {
278 // There may be several overloads of ToDeclT::Create. We must make sure
279 // to call the one which would be chosen by the arguments, thus we use a
280 // wrapper for the overload set.
281 CallOverloadedCreateFun<ToDeclT> OC;
282 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
283 std::forward<Args>(args)...);
284 }
285 // Use this overload if a special Type is needed to be created. E.g if we
286 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
287 // then:
288 // TypedefNameDecl *ToTypedef;
289 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
290 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
291 typename... Args>
292 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
293 Args &&... args) {
294 CallOverloadedCreateFun<NewDeclT> OC;
295 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
296 std::forward<Args>(args)...);
297 }
298 // Use this version if a special create function must be
299 // used, e.g. CXXRecordDecl::CreateLambda .
300 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
301 typename... Args>
302 LLVM_NODISCARD bool
303 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
304 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000305 // FIXME: This code is needed later.
306 //if (Importer.getImportDeclErrorIfAny(FromD)) {
307 // ToD = nullptr;
308 // return true; // Already imported but with error.
309 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000310 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
311 if (ToD)
312 return true; // Already imported.
313 ToD = CreateFun(std::forward<Args>(args)...);
314 InitializeImportedDecl(FromD, ToD);
315 return false; // A new Decl is created.
316 }
317
318 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
319 Importer.MapImported(FromD, ToD);
320 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
321 if (FromD->hasAttrs())
322 for (const Attr *FromAttr : FromD->getAttrs())
323 ToD->addAttr(Importer.Import(FromAttr));
324 if (FromD->isUsed())
325 ToD->setIsUsed();
326 if (FromD->isImplicit())
327 ToD->setImplicit();
328 }
329
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000331 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000332
Balazs Keri3b30d652018-10-19 13:32:20 +0000333 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
334 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
335 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336
337 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000338 ExpectedType VisitType(const Type *T);
339 ExpectedType VisitAtomicType(const AtomicType *T);
340 ExpectedType VisitBuiltinType(const BuiltinType *T);
341 ExpectedType VisitDecayedType(const DecayedType *T);
342 ExpectedType VisitComplexType(const ComplexType *T);
343 ExpectedType VisitPointerType(const PointerType *T);
344 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
345 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
346 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
347 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
348 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
349 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
350 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
351 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000352 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000353 ExpectedType VisitVectorType(const VectorType *T);
354 ExpectedType VisitExtVectorType(const ExtVectorType *T);
355 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
356 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
357 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
358 ExpectedType VisitParenType(const ParenType *T);
359 ExpectedType VisitTypedefType(const TypedefType *T);
360 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000361 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000362 ExpectedType VisitTypeOfType(const TypeOfType *T);
363 ExpectedType VisitDecltypeType(const DecltypeType *T);
364 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
365 ExpectedType VisitAutoType(const AutoType *T);
366 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000368 ExpectedType VisitRecordType(const RecordType *T);
369 ExpectedType VisitEnumType(const EnumType *T);
370 ExpectedType VisitAttributedType(const AttributedType *T);
371 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
372 ExpectedType VisitSubstTemplateTypeParmType(
373 const SubstTemplateTypeParmType *T);
374 ExpectedType VisitTemplateSpecializationType(
375 const TemplateSpecializationType *T);
376 ExpectedType VisitElaboratedType(const ElaboratedType *T);
377 ExpectedType VisitDependentNameType(const DependentNameType *T);
378 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
379 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000380 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
382 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
383 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000384
385 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000386 Error ImportDeclParts(
387 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
388 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
389 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
390 Error ImportDeclarationNameLoc(
391 const DeclarationNameInfo &From, DeclarationNameInfo &To);
392 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
393 Error ImportDeclContext(
394 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
395 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000396
Balazs Keri3b30d652018-10-19 13:32:20 +0000397 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000398
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000399 using Designator = DesignatedInitExpr::Designator;
400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000401 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000402 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000403 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000404 /// nothing (if minimal import is set) or might be everything (if minimal
405 /// import is not set).
406 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000407 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000408 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000410 /// DeclContext.
411 IDK_Basic
412 };
413
Douglas Gregor2e15c842012-02-01 21:00:38 +0000414 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
415 return IDK == IDK_Everything ||
416 (IDK == IDK_Default && !Importer.isMinimalImport());
417 }
418
Balazs Keri3b30d652018-10-19 13:32:20 +0000419 Error ImportInitializer(VarDecl *From, VarDecl *To);
420 Error ImportDefinition(
421 RecordDecl *From, RecordDecl *To,
422 ImportDefinitionKind Kind = IDK_Default);
423 Error ImportDefinition(
424 EnumDecl *From, EnumDecl *To,
425 ImportDefinitionKind Kind = IDK_Default);
426 Error ImportDefinition(
427 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
428 ImportDefinitionKind Kind = IDK_Default);
429 Error ImportDefinition(
430 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
431 ImportDefinitionKind Kind = IDK_Default);
432 Expected<TemplateParameterList *> ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000433 TemplateParameterList *Params);
Balazs Keri3b30d652018-10-19 13:32:20 +0000434 Error ImportTemplateArguments(
435 const TemplateArgument *FromArgs, unsigned NumFromArgs,
436 SmallVectorImpl<TemplateArgument> &ToArgs);
437 Expected<TemplateArgument>
438 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000439
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000440 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000441 Error ImportTemplateArgumentListInfo(
442 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000443
444 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000445 Error ImportTemplateArgumentListInfo(
446 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
447 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000448
Gabor Marton5254e642018-06-27 13:32:50 +0000449 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000450 using FunctionTemplateAndArgsTy =
451 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
452 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000453 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
454 FunctionDecl *FromFD);
455
Balazs Keri3b30d652018-10-19 13:32:20 +0000456 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000457
Gabor Marton950fb572018-07-17 12:39:27 +0000458 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000459 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
460 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000461 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
462 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000463 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000464 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000465 bool IsStructuralMatch(FunctionTemplateDecl *From,
466 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000467 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000468 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000469 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000470 ExpectedDecl VisitDecl(Decl *D);
471 ExpectedDecl VisitImportDecl(ImportDecl *D);
472 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
473 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
474 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
475 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
476 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
477 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
479 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
480 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
481 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
482 ExpectedDecl VisitLabelDecl(LabelDecl *D);
483 ExpectedDecl VisitEnumDecl(EnumDecl *D);
484 ExpectedDecl VisitRecordDecl(RecordDecl *D);
485 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
486 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
487 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
488 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
489 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
490 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
491 ExpectedDecl VisitFieldDecl(FieldDecl *D);
492 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
493 ExpectedDecl VisitFriendDecl(FriendDecl *D);
494 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
495 ExpectedDecl VisitVarDecl(VarDecl *D);
496 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
497 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
498 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
499 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
500 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
501 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
502 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
503 ExpectedDecl VisitUsingDecl(UsingDecl *D);
504 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
505 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
506 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
507 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000508
Balazs Keri3b30d652018-10-19 13:32:20 +0000509 Expected<ObjCTypeParamList *>
510 ImportObjCTypeParamList(ObjCTypeParamList *list);
511
512 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
513 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
514 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
515 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
516 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
517 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
518 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
519 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
520 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
521 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000522 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000523 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
524 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
525 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000526
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000527 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000528 ExpectedStmt VisitStmt(Stmt *S);
529 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
530 ExpectedStmt VisitDeclStmt(DeclStmt *S);
531 ExpectedStmt VisitNullStmt(NullStmt *S);
532 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
533 ExpectedStmt VisitCaseStmt(CaseStmt *S);
534 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
535 ExpectedStmt VisitLabelStmt(LabelStmt *S);
536 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
537 ExpectedStmt VisitIfStmt(IfStmt *S);
538 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
539 ExpectedStmt VisitWhileStmt(WhileStmt *S);
540 ExpectedStmt VisitDoStmt(DoStmt *S);
541 ExpectedStmt VisitForStmt(ForStmt *S);
542 ExpectedStmt VisitGotoStmt(GotoStmt *S);
543 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
544 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
545 ExpectedStmt VisitBreakStmt(BreakStmt *S);
546 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000547 // FIXME: MSAsmStmt
548 // FIXME: SEHExceptStmt
549 // FIXME: SEHFinallyStmt
550 // FIXME: SEHTryStmt
551 // FIXME: SEHLeaveStmt
552 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000553 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
554 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
555 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000556 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000557 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
558 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
559 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
560 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
561 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
562 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
563 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000564
565 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000566 ExpectedStmt VisitExpr(Expr *E);
567 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
568 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
569 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
570 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
571 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
572 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
573 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
574 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
575 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
576 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
577 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
578 ExpectedStmt VisitStringLiteral(StringLiteral *E);
579 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
580 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
581 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000582 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000583 ExpectedStmt VisitParenExpr(ParenExpr *E);
584 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
585 ExpectedStmt VisitStmtExpr(StmtExpr *E);
586 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
587 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
588 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
589 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
590 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
591 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
592 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
593 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
594 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
595 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
596 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
597 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
598 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
599 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
600 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
601 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
602 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
603 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
604 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
605 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
606 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
607 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
608 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
609 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
610 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
611 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
612 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
613 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
614 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
615 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
616 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
617 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
618 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
619 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
620 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
621 ExpectedStmt VisitMemberExpr(MemberExpr *E);
622 ExpectedStmt VisitCallExpr(CallExpr *E);
623 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
624 ExpectedStmt VisitInitListExpr(InitListExpr *E);
625 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
626 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
627 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
628 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
629 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
630 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
631 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
632 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
633 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000634
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000635 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000636 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000637 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000638 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
639 Expected<ItemT> ToOrErr = import(*Ibegin);
640 if (!ToOrErr)
641 return ToOrErr.takeError();
642 *Obegin = *ToOrErr;
643 }
644 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000645 }
646
Balazs Keri3b30d652018-10-19 13:32:20 +0000647 // Import every item from a container structure into an output container.
648 // If error occurs, stops at first error and returns the error.
649 // The output container should have space for all needed elements (it is not
650 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000651 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000652 Error ImportContainerChecked(
653 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
654 return ImportArrayChecked(
655 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000656 }
657
658 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000659 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000660 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
661 }
Lang Hames19e07e12017-06-20 21:06:00 +0000662
Lang Hames19e07e12017-06-20 21:06:00 +0000663 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000664
Balazs Keri3b30d652018-10-19 13:32:20 +0000665 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
666 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000668
Balazs Keri3b30d652018-10-19 13:32:20 +0000669// FIXME: Temporary until every import returns Expected.
670template <>
671Expected<TemplateName> ASTNodeImporter::import(const TemplateName &From) {
672 TemplateName To = Importer.Import(From);
673 if (To.isNull() && !From.isNull())
674 return make_error<ImportError>();
675 return To;
676}
677
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000678template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000679Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000680 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
681 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000682 auto ToLAngleLocOrErr = import(FromLAngleLoc);
683 if (!ToLAngleLocOrErr)
684 return ToLAngleLocOrErr.takeError();
685 auto ToRAngleLocOrErr = import(FromRAngleLoc);
686 if (!ToRAngleLocOrErr)
687 return ToRAngleLocOrErr.takeError();
688
689 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
690 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
691 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000692 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000693 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000694}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000695
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000696template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000697Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000698 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
699 return ImportTemplateArgumentListInfo(
700 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
701}
702
703template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000704Error ASTNodeImporter::ImportTemplateArgumentListInfo<
705 ASTTemplateArgumentListInfo>(
706 const ASTTemplateArgumentListInfo &From,
707 TemplateArgumentListInfo &Result) {
708 return ImportTemplateArgumentListInfo(
709 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000710}
711
Balazs Keri3b30d652018-10-19 13:32:20 +0000712Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000713ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
714 FunctionDecl *FromFD) {
715 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000716 FunctionDecl::TK_FunctionTemplateSpecialization);
717
718 FunctionTemplateAndArgsTy Result;
719
Gabor Marton5254e642018-06-27 13:32:50 +0000720 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000721 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
722 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000723
724 // Import template arguments.
725 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000726 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
727 std::get<1>(Result)))
728 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000729
Balazs Keri3b30d652018-10-19 13:32:20 +0000730 return Result;
731}
732
733template <>
734Expected<TemplateParameterList *>
735ASTNodeImporter::import(TemplateParameterList *From) {
736 SmallVector<NamedDecl *, 4> To(From->size());
737 if (Error Err = ImportContainerChecked(*From, To))
738 return std::move(Err);
739
740 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
741 if (!ToRequiresClause)
742 return ToRequiresClause.takeError();
743
744 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
745 if (!ToTemplateLocOrErr)
746 return ToTemplateLocOrErr.takeError();
747 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
748 if (!ToLAngleLocOrErr)
749 return ToLAngleLocOrErr.takeError();
750 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
751 if (!ToRAngleLocOrErr)
752 return ToRAngleLocOrErr.takeError();
753
754 return TemplateParameterList::Create(
755 Importer.getToContext(),
756 *ToTemplateLocOrErr,
757 *ToLAngleLocOrErr,
758 To,
759 *ToRAngleLocOrErr,
760 *ToRequiresClause);
761}
762
763template <>
764Expected<TemplateArgument>
765ASTNodeImporter::import(const TemplateArgument &From) {
766 switch (From.getKind()) {
767 case TemplateArgument::Null:
768 return TemplateArgument();
769
770 case TemplateArgument::Type: {
771 ExpectedType ToTypeOrErr = import(From.getAsType());
772 if (!ToTypeOrErr)
773 return ToTypeOrErr.takeError();
774 return TemplateArgument(*ToTypeOrErr);
775 }
776
777 case TemplateArgument::Integral: {
778 ExpectedType ToTypeOrErr = import(From.getIntegralType());
779 if (!ToTypeOrErr)
780 return ToTypeOrErr.takeError();
781 return TemplateArgument(From, *ToTypeOrErr);
782 }
783
784 case TemplateArgument::Declaration: {
785 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
786 if (!ToOrErr)
787 return ToOrErr.takeError();
788 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
789 if (!ToTypeOrErr)
790 return ToTypeOrErr.takeError();
791 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
792 }
793
794 case TemplateArgument::NullPtr: {
795 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
796 if (!ToTypeOrErr)
797 return ToTypeOrErr.takeError();
798 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
799 }
800
801 case TemplateArgument::Template: {
802 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
803 if (!ToTemplateOrErr)
804 return ToTemplateOrErr.takeError();
805
806 return TemplateArgument(*ToTemplateOrErr);
807 }
808
809 case TemplateArgument::TemplateExpansion: {
810 Expected<TemplateName> ToTemplateOrErr =
811 import(From.getAsTemplateOrTemplatePattern());
812 if (!ToTemplateOrErr)
813 return ToTemplateOrErr.takeError();
814
815 return TemplateArgument(
816 *ToTemplateOrErr, From.getNumTemplateExpansions());
817 }
818
819 case TemplateArgument::Expression:
820 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
821 return TemplateArgument(*ToExpr);
822 else
823 return ToExpr.takeError();
824
825 case TemplateArgument::Pack: {
826 SmallVector<TemplateArgument, 2> ToPack;
827 ToPack.reserve(From.pack_size());
828 if (Error Err = ImportTemplateArguments(
829 From.pack_begin(), From.pack_size(), ToPack))
830 return std::move(Err);
831
832 return TemplateArgument(
833 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
834 }
835 }
836
837 llvm_unreachable("Invalid template argument kind");
838}
839
840template <>
841Expected<TemplateArgumentLoc>
842ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
843 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
844 if (!ArgOrErr)
845 return ArgOrErr.takeError();
846 TemplateArgument Arg = *ArgOrErr;
847
848 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
849
850 TemplateArgumentLocInfo ToInfo;
851 if (Arg.getKind() == TemplateArgument::Expression) {
852 ExpectedExpr E = import(FromInfo.getAsExpr());
853 if (!E)
854 return E.takeError();
855 ToInfo = TemplateArgumentLocInfo(*E);
856 } else if (Arg.getKind() == TemplateArgument::Type) {
857 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
858 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
859 else
860 return TSIOrErr.takeError();
861 } else {
862 auto ToTemplateQualifierLocOrErr =
863 import(FromInfo.getTemplateQualifierLoc());
864 if (!ToTemplateQualifierLocOrErr)
865 return ToTemplateQualifierLocOrErr.takeError();
866 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
867 if (!ToTemplateNameLocOrErr)
868 return ToTemplateNameLocOrErr.takeError();
869 auto ToTemplateEllipsisLocOrErr =
870 import(FromInfo.getTemplateEllipsisLoc());
871 if (!ToTemplateEllipsisLocOrErr)
872 return ToTemplateEllipsisLocOrErr.takeError();
873
874 ToInfo = TemplateArgumentLocInfo(
875 *ToTemplateQualifierLocOrErr,
876 *ToTemplateNameLocOrErr,
877 *ToTemplateEllipsisLocOrErr);
878 }
879
880 return TemplateArgumentLoc(Arg, ToInfo);
881}
882
883template <>
884Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
885 if (DG.isNull())
886 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
887 size_t NumDecls = DG.end() - DG.begin();
888 SmallVector<Decl *, 1> ToDecls;
889 ToDecls.reserve(NumDecls);
890 for (Decl *FromD : DG) {
891 if (auto ToDOrErr = import(FromD))
892 ToDecls.push_back(*ToDOrErr);
893 else
894 return ToDOrErr.takeError();
895 }
896 return DeclGroupRef::Create(Importer.getToContext(),
897 ToDecls.begin(),
898 NumDecls);
899}
900
901template <>
902Expected<ASTNodeImporter::Designator>
903ASTNodeImporter::import(const Designator &D) {
904 if (D.isFieldDesignator()) {
905 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
906
907 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
908 if (!ToDotLocOrErr)
909 return ToDotLocOrErr.takeError();
910
911 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
912 if (!ToFieldLocOrErr)
913 return ToFieldLocOrErr.takeError();
914
915 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
916 }
917
918 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
919 if (!ToLBracketLocOrErr)
920 return ToLBracketLocOrErr.takeError();
921
922 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
923 if (!ToRBracketLocOrErr)
924 return ToRBracketLocOrErr.takeError();
925
926 if (D.isArrayDesignator())
927 return Designator(D.getFirstExprIndex(),
928 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
929
930 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
931 if (!ToEllipsisLocOrErr)
932 return ToEllipsisLocOrErr.takeError();
933
934 assert(D.isArrayRangeDesignator());
935 return Designator(
936 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
937 *ToRBracketLocOrErr);
938}
939
940template <>
941Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
942 VarDecl *Var = nullptr;
943 if (From.capturesVariable()) {
944 if (auto VarOrErr = import(From.getCapturedVar()))
945 Var = *VarOrErr;
946 else
947 return VarOrErr.takeError();
948 }
949
950 auto LocationOrErr = import(From.getLocation());
951 if (!LocationOrErr)
952 return LocationOrErr.takeError();
953
954 SourceLocation EllipsisLoc;
955 if (From.isPackExpansion())
956 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
957 return std::move(Err);
958
959 return LambdaCapture(
960 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
961 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000962}
963
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000964} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000965
Douglas Gregor3996e242010-02-15 22:01:00 +0000966//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000967// Import Types
968//----------------------------------------------------------------------------
969
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000970using namespace clang;
971
Balazs Keri3b30d652018-10-19 13:32:20 +0000972ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000973 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
974 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000975 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000976}
977
Balazs Keri3b30d652018-10-19 13:32:20 +0000978ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
979 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
980 if (!UnderlyingTypeOrErr)
981 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000982
Balazs Keri3b30d652018-10-19 13:32:20 +0000983 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000984}
985
Balazs Keri3b30d652018-10-19 13:32:20 +0000986ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000988#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
989 case BuiltinType::Id: \
990 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000991#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000992#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
993 case BuiltinType::Id: \
994 return Importer.getToContext().Id##Ty;
995#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000996#define SHARED_SINGLETON_TYPE(Expansion)
997#define BUILTIN_TYPE(Id, SingletonId) \
998 case BuiltinType::Id: return Importer.getToContext().SingletonId;
999#include "clang/AST/BuiltinTypes.def"
1000
1001 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1002 // context supports C++.
1003
1004 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1005 // context supports ObjC.
1006
Douglas Gregor96e578d2010-02-05 17:54:41 +00001007 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +00001008 // The context we're importing from has an unsigned 'char'. If we're
1009 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001010 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001011 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001012 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001013
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014 return Importer.getToContext().CharTy;
1015
Douglas Gregor96e578d2010-02-05 17:54:41 +00001016 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +00001017 // The context we're importing from has an unsigned 'char'. If we're
1018 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001019 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001020 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001021 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001022
Douglas Gregor96e578d2010-02-05 17:54:41 +00001023 return Importer.getToContext().CharTy;
1024
Chris Lattnerad3467e2010-12-25 23:25:43 +00001025 case BuiltinType::WChar_S:
1026 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001027 // FIXME: If not in C++, shall we translate to the C equivalent of
1028 // wchar_t?
1029 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001030 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001031
1032 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001033}
1034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1036 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1037 if (!ToOriginalTypeOrErr)
1038 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001039
Balazs Keri3b30d652018-10-19 13:32:20 +00001040 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001041}
1042
Balazs Keri3b30d652018-10-19 13:32:20 +00001043ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1044 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1045 if (!ToElementTypeOrErr)
1046 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001047
Balazs Keri3b30d652018-10-19 13:32:20 +00001048 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001049}
1050
Balazs Keri3b30d652018-10-19 13:32:20 +00001051ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1052 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1053 if (!ToPointeeTypeOrErr)
1054 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001055
Balazs Keri3b30d652018-10-19 13:32:20 +00001056 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001057}
1058
Balazs Keri3b30d652018-10-19 13:32:20 +00001059ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001060 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001061 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1062 if (!ToPointeeTypeOrErr)
1063 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001064
Balazs Keri3b30d652018-10-19 13:32:20 +00001065 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001066}
1067
Balazs Keri3b30d652018-10-19 13:32:20 +00001068ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001069ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001070 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001071 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1072 if (!ToPointeeTypeOrErr)
1073 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Balazs Keri3b30d652018-10-19 13:32:20 +00001075 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001076}
1077
Balazs Keri3b30d652018-10-19 13:32:20 +00001078ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001079ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001080 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001081 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1082 if (!ToPointeeTypeOrErr)
1083 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001084
Balazs Keri3b30d652018-10-19 13:32:20 +00001085 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001086}
1087
Balazs Keri3b30d652018-10-19 13:32:20 +00001088ExpectedType
1089ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001090 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001091 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1092 if (!ToPointeeTypeOrErr)
1093 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001094
Balazs Keri3b30d652018-10-19 13:32:20 +00001095 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1096 if (!ClassTypeOrErr)
1097 return ClassTypeOrErr.takeError();
1098
1099 return Importer.getToContext().getMemberPointerType(
1100 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001101}
1102
Balazs Keri3b30d652018-10-19 13:32:20 +00001103ExpectedType
1104ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1105 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1106 if (!ToElementTypeOrErr)
1107 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001108
Balazs Keri3b30d652018-10-19 13:32:20 +00001109 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001110 T->getSize(),
1111 T->getSizeModifier(),
1112 T->getIndexTypeCVRQualifiers());
1113}
1114
Balazs Keri3b30d652018-10-19 13:32:20 +00001115ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001116ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001117 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1118 if (!ToElementTypeOrErr)
1119 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001120
Balazs Keri3b30d652018-10-19 13:32:20 +00001121 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001122 T->getSizeModifier(),
1123 T->getIndexTypeCVRQualifiers());
1124}
1125
Balazs Keri3b30d652018-10-19 13:32:20 +00001126ExpectedType
1127ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1128 QualType ToElementType;
1129 Expr *ToSizeExpr;
1130 SourceRange ToBracketsRange;
1131 if (auto Imp = importSeq(
1132 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1133 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1134 else
1135 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001136
Balazs Keri3b30d652018-10-19 13:32:20 +00001137 return Importer.getToContext().getVariableArrayType(
1138 ToElementType, ToSizeExpr, T->getSizeModifier(),
1139 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001140}
1141
Balazs Keri3b30d652018-10-19 13:32:20 +00001142ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001143 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001144 QualType ToElementType;
1145 Expr *ToSizeExpr;
1146 SourceRange ToBracketsRange;
1147 if (auto Imp = importSeq(
1148 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1149 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1150 else
1151 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001152 // SizeExpr may be null if size is not specified directly.
1153 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001154
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001155 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001156 ToElementType, ToSizeExpr, T->getSizeModifier(),
1157 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001158}
1159
Balazs Keri3b30d652018-10-19 13:32:20 +00001160ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1161 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1162 if (!ToElementTypeOrErr)
1163 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001164
Balazs Keri3b30d652018-10-19 13:32:20 +00001165 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001166 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001167 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001168}
1169
Balazs Keri3b30d652018-10-19 13:32:20 +00001170ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1171 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1172 if (!ToElementTypeOrErr)
1173 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001174
Balazs Keri3b30d652018-10-19 13:32:20 +00001175 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001176 T->getNumElements());
1177}
1178
Balazs Keri3b30d652018-10-19 13:32:20 +00001179ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001180ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001181 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001182 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001183 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1184 if (!ToReturnTypeOrErr)
1185 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001186
Balazs Keri3b30d652018-10-19 13:32:20 +00001187 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001188 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001189}
1190
Balazs Keri3b30d652018-10-19 13:32:20 +00001191ExpectedType
1192ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1193 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1194 if (!ToReturnTypeOrErr)
1195 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001196
Douglas Gregor96e578d2010-02-05 17:54:41 +00001197 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001198 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001199 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001200 ExpectedType TyOrErr = import(A);
1201 if (!TyOrErr)
1202 return TyOrErr.takeError();
1203 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001204 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001205
Douglas Gregor96e578d2010-02-05 17:54:41 +00001206 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001207 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001208 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001209 ExpectedType TyOrErr = import(E);
1210 if (!TyOrErr)
1211 return TyOrErr.takeError();
1212 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001213 }
John McCalldb40c7f2010-12-14 08:05:40 +00001214
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001215 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1216 FunctionProtoType::ExtProtoInfo ToEPI;
1217
Balazs Keri3b30d652018-10-19 13:32:20 +00001218 auto Imp = importSeq(
1219 FromEPI.ExceptionSpec.NoexceptExpr,
1220 FromEPI.ExceptionSpec.SourceDecl,
1221 FromEPI.ExceptionSpec.SourceTemplate);
1222 if (!Imp)
1223 return Imp.takeError();
1224
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001225 ToEPI.ExtInfo = FromEPI.ExtInfo;
1226 ToEPI.Variadic = FromEPI.Variadic;
1227 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1228 ToEPI.TypeQuals = FromEPI.TypeQuals;
1229 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001230 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1231 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 std::tie(
1233 ToEPI.ExceptionSpec.NoexceptExpr,
1234 ToEPI.ExceptionSpec.SourceDecl,
1235 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001236
Balazs Keri3b30d652018-10-19 13:32:20 +00001237 return Importer.getToContext().getFunctionType(
1238 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001239}
1240
Balazs Keri3b30d652018-10-19 13:32:20 +00001241ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001242 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001243 UnresolvedUsingTypenameDecl *ToD;
1244 Decl *ToPrevD;
1245 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1246 std::tie(ToD, ToPrevD) = *Imp;
1247 else
1248 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001249
Balazs Keri3b30d652018-10-19 13:32:20 +00001250 return Importer.getToContext().getTypeDeclType(
1251 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001252}
1253
Balazs Keri3b30d652018-10-19 13:32:20 +00001254ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1255 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1256 if (!ToInnerTypeOrErr)
1257 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001258
Balazs Keri3b30d652018-10-19 13:32:20 +00001259 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001260}
1261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1263 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1264 if (!ToDeclOrErr)
1265 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001266
Balazs Keri3b30d652018-10-19 13:32:20 +00001267 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001268}
1269
Balazs Keri3b30d652018-10-19 13:32:20 +00001270ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1271 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1272 if (!ToExprOrErr)
1273 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001274
Balazs Keri3b30d652018-10-19 13:32:20 +00001275 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001276}
1277
Balazs Keri3b30d652018-10-19 13:32:20 +00001278ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1279 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1280 if (!ToUnderlyingTypeOrErr)
1281 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001282
Balazs Keri3b30d652018-10-19 13:32:20 +00001283 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001284}
1285
Balazs Keri3b30d652018-10-19 13:32:20 +00001286ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001287 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001288 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1289 if (!ToExprOrErr)
1290 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001291
Balazs Keri3b30d652018-10-19 13:32:20 +00001292 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1293 if (!ToUnderlyingTypeOrErr)
1294 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001295
Balazs Keri3b30d652018-10-19 13:32:20 +00001296 return Importer.getToContext().getDecltypeType(
1297 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001298}
1299
Balazs Keri3b30d652018-10-19 13:32:20 +00001300ExpectedType
1301ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1302 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1303 if (!ToBaseTypeOrErr)
1304 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001305
Balazs Keri3b30d652018-10-19 13:32:20 +00001306 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1307 if (!ToUnderlyingTypeOrErr)
1308 return ToUnderlyingTypeOrErr.takeError();
1309
1310 return Importer.getToContext().getUnaryTransformType(
1311 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001312}
1313
Balazs Keri3b30d652018-10-19 13:32:20 +00001314ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001315 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001316 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1317 if (!ToDeducedTypeOrErr)
1318 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001319
Balazs Keri3b30d652018-10-19 13:32:20 +00001320 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1321 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001322 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001323}
1324
Balazs Keri3b30d652018-10-19 13:32:20 +00001325ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001326 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001327 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1328 if (!ToDeclOrErr)
1329 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001330
Balazs Keri3b30d652018-10-19 13:32:20 +00001331 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1332 if (!ToInjTypeOrErr)
1333 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001334
1335 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1336 // See comments in InjectedClassNameType definition for details
1337 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1338 enum {
1339 TypeAlignmentInBits = 4,
1340 TypeAlignment = 1 << TypeAlignmentInBits
1341 };
1342
1343 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001344 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001345}
1346
Balazs Keri3b30d652018-10-19 13:32:20 +00001347ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1348 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1349 if (!ToDeclOrErr)
1350 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001351
Balazs Keri3b30d652018-10-19 13:32:20 +00001352 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001353}
1354
Balazs Keri3b30d652018-10-19 13:32:20 +00001355ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1356 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1357 if (!ToDeclOrErr)
1358 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001359
Balazs Keri3b30d652018-10-19 13:32:20 +00001360 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001361}
1362
Balazs Keri3b30d652018-10-19 13:32:20 +00001363ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1364 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1365 if (!ToModifiedTypeOrErr)
1366 return ToModifiedTypeOrErr.takeError();
1367 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1368 if (!ToEquivalentTypeOrErr)
1369 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001370
1371 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001372 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001373}
1374
Balazs Keri3b30d652018-10-19 13:32:20 +00001375ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001376 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001377 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1378 if (!ToDeclOrErr)
1379 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001380
1381 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001382 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001383}
1384
Balazs Keri3b30d652018-10-19 13:32:20 +00001385ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001386 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001387 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1388 if (!ReplacedOrErr)
1389 return ReplacedOrErr.takeError();
1390 const TemplateTypeParmType *Replaced =
1391 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001392
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1394 if (!ToReplacementTypeOrErr)
1395 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001396
1397 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001399}
1400
Balazs Keri3b30d652018-10-19 13:32:20 +00001401ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001402 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001403 auto ToTemplateOrErr = import(T->getTemplateName());
1404 if (!ToTemplateOrErr)
1405 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001407 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001408 if (Error Err = ImportTemplateArguments(
1409 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1410 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001411
Douglas Gregore2e50d332010-12-01 01:36:18 +00001412 QualType ToCanonType;
1413 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001414 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001415 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001416 if (ExpectedType TyOrErr = import(FromCanonType))
1417 ToCanonType = *TyOrErr;
1418 else
1419 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001420 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001421 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001422 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001423 ToCanonType);
1424}
1425
Balazs Keri3b30d652018-10-19 13:32:20 +00001426ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001427 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 auto ToQualifierOrErr = import(T->getQualifier());
1429 if (!ToQualifierOrErr)
1430 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001431
Balazs Keri3b30d652018-10-19 13:32:20 +00001432 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1433 if (!ToNamedTypeOrErr)
1434 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001435
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1437 if (!ToOwnedTagDeclOrErr)
1438 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001439
Abramo Bagnara6150c882010-05-11 21:36:43 +00001440 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001441 *ToQualifierOrErr,
1442 *ToNamedTypeOrErr,
1443 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001444}
1445
Balazs Keri3b30d652018-10-19 13:32:20 +00001446ExpectedType
1447ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1448 ExpectedType ToPatternOrErr = import(T->getPattern());
1449 if (!ToPatternOrErr)
1450 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001451
Balazs Keri3b30d652018-10-19 13:32:20 +00001452 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001453 T->getNumExpansions());
1454}
1455
Balazs Keri3b30d652018-10-19 13:32:20 +00001456ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001457 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001458 auto ToQualifierOrErr = import(T->getQualifier());
1459 if (!ToQualifierOrErr)
1460 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001461
Balazs Keri3b30d652018-10-19 13:32:20 +00001462 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001463
1464 SmallVector<TemplateArgument, 2> ToPack;
1465 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001466 if (Error Err = ImportTemplateArguments(
1467 T->getArgs(), T->getNumArgs(), ToPack))
1468 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001469
1470 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001471 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001472}
1473
Balazs Keri3b30d652018-10-19 13:32:20 +00001474ExpectedType
1475ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1476 auto ToQualifierOrErr = import(T->getQualifier());
1477 if (!ToQualifierOrErr)
1478 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001479
1480 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001481
Balazs Keri3b30d652018-10-19 13:32:20 +00001482 QualType Canon;
1483 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1484 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1485 Canon = (*TyOrErr).getCanonicalType();
1486 else
1487 return TyOrErr.takeError();
1488 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001489
Balazs Keri3b30d652018-10-19 13:32:20 +00001490 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1491 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001492 Name, Canon);
1493}
1494
Balazs Keri3b30d652018-10-19 13:32:20 +00001495ExpectedType
1496ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1497 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1498 if (!ToDeclOrErr)
1499 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500
Balazs Keri3b30d652018-10-19 13:32:20 +00001501 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001502}
1503
Balazs Keri3b30d652018-10-19 13:32:20 +00001504ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1505 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1506 if (!ToBaseTypeOrErr)
1507 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001508
Douglas Gregore9d95f12015-07-07 03:57:35 +00001509 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001510 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001511 if (ExpectedType TyOrErr = import(TypeArg))
1512 TypeArgs.push_back(*TyOrErr);
1513 else
1514 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001515 }
1516
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001517 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001518 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001519 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1520 Protocols.push_back(*ProtocolOrErr);
1521 else
1522 return ProtocolOrErr.takeError();
1523
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524 }
1525
Balazs Keri3b30d652018-10-19 13:32:20 +00001526 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001527 Protocols,
1528 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001529}
1530
Balazs Keri3b30d652018-10-19 13:32:20 +00001531ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001532ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001533 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1534 if (!ToPointeeTypeOrErr)
1535 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001536
Balazs Keri3b30d652018-10-19 13:32:20 +00001537 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001538}
1539
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001540//----------------------------------------------------------------------------
1541// Import Declarations
1542//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001543Error ASTNodeImporter::ImportDeclParts(
1544 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1545 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001546 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1547 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1548 DeclContext *OrigDC = D->getDeclContext();
1549 FunctionDecl *FunDecl;
1550 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1551 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001552 auto getLeafPointeeType = [](const Type *T) {
1553 while (T->isPointerType() || T->isArrayType()) {
1554 T = T->getPointeeOrArrayElementType();
1555 }
1556 return T;
1557 };
1558 for (const ParmVarDecl *P : FunDecl->parameters()) {
1559 const Type *LeafT =
1560 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1561 auto *RT = dyn_cast<RecordType>(LeafT);
1562 if (RT && RT->getDecl() == D) {
1563 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1564 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001565 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001566 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001567 }
1568 }
1569
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001570 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001571 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1572 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001573
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001574 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001575 if (Error Err = importInto(Name, D->getDeclName()))
1576 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001577
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001578 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001579 if (Error Err = importInto(Loc, D->getLocation()))
1580 return Err;
1581
Sean Callanan59721b32015-04-28 18:41:46 +00001582 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Balazs Keri3b30d652018-10-19 13:32:20 +00001583
1584 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001585}
1586
Balazs Keri3b30d652018-10-19 13:32:20 +00001587Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001588 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001589 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001590
Balazs Keri3b30d652018-10-19 13:32:20 +00001591 if (!ToD)
1592 if (Error Err = importInto(ToD, FromD))
1593 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001594
Balazs Keri3b30d652018-10-19 13:32:20 +00001595 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1596 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1597 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1598 !ToRecord->getDefinition()) {
1599 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1600 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001601 }
1602 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001603 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001604 }
1605
Balazs Keri3b30d652018-10-19 13:32:20 +00001606 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1607 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001608 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001609 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1610 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001611 }
1612 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001613 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001614 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001615
1616 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001617}
1618
Balazs Keri3b30d652018-10-19 13:32:20 +00001619Error
1620ASTNodeImporter::ImportDeclarationNameLoc(
1621 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001622 // NOTE: To.Name and To.Loc are already imported.
1623 // We only have to import To.LocInfo.
1624 switch (To.getName().getNameKind()) {
1625 case DeclarationName::Identifier:
1626 case DeclarationName::ObjCZeroArgSelector:
1627 case DeclarationName::ObjCOneArgSelector:
1628 case DeclarationName::ObjCMultiArgSelector:
1629 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001630 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001631 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001632
1633 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001634 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1635 To.setCXXOperatorNameRange(*ToRangeOrErr);
1636 else
1637 return ToRangeOrErr.takeError();
1638 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001639 }
1640 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001641 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1642 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1643 else
1644 return LocOrErr.takeError();
1645 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001646 }
1647 case DeclarationName::CXXConstructorName:
1648 case DeclarationName::CXXDestructorName:
1649 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001650 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1651 To.setNamedTypeInfo(*ToTInfoOrErr);
1652 else
1653 return ToTInfoOrErr.takeError();
1654 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001655 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001656 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001657 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001658}
1659
Balazs Keri3b30d652018-10-19 13:32:20 +00001660Error
1661ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001662 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001663 auto ToDCOrErr = Importer.ImportContext(FromDC);
1664 return ToDCOrErr.takeError();
1665 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001666 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001667 for (auto *From : FromDC->decls()) {
1668 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001669 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001670 // Ignore the error, continue with next Decl.
1671 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001672 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001673 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001674
Balazs Keri3b30d652018-10-19 13:32:20 +00001675 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001676}
1677
Balazs Keri3b30d652018-10-19 13:32:20 +00001678Error ASTNodeImporter::ImportDeclContext(
1679 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1680 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1681 if (!ToDCOrErr)
1682 return ToDCOrErr.takeError();
1683 ToDC = *ToDCOrErr;
1684
1685 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1686 auto ToLexicalDCOrErr = Importer.ImportContext(
1687 FromD->getLexicalDeclContext());
1688 if (!ToLexicalDCOrErr)
1689 return ToLexicalDCOrErr.takeError();
1690 ToLexicalDC = *ToLexicalDCOrErr;
1691 } else
1692 ToLexicalDC = ToDC;
1693
1694 return Error::success();
1695}
1696
1697Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001698 const CXXRecordDecl *From, CXXRecordDecl *To) {
1699 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1700 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001701
Balazs Keri1d20cc22018-07-16 12:16:39 +00001702 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001703 if (FromM->isImplicit()) {
1704 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1705 if (!ToMOrErr)
1706 return ToMOrErr.takeError();
1707 }
1708
1709 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001710}
1711
Balazs Keri3b30d652018-10-19 13:32:20 +00001712static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1713 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001714 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001715 Decl *ToTypedef = Importer.Import(FromTypedef);
1716 if (!ToTypedef)
1717 return make_error<ImportError>();
1718 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToTypedef));
1719 // FIXME: This should be the final code.
1720 //if (Expected<Decl *> ToTypedefOrErr = Importer.Import(FromTypedef))
1721 // To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1722 //else
1723 // return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001724 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001725 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001726}
1727
Balazs Keri3b30d652018-10-19 13:32:20 +00001728Error ASTNodeImporter::ImportDefinition(
1729 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001730 if (To->getDefinition() || To->isBeingDefined()) {
1731 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001732 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001733
Balazs Keri3b30d652018-10-19 13:32:20 +00001734 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001735 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001736
Douglas Gregore2e50d332010-12-01 01:36:18 +00001737 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001738
Balazs Keri3b30d652018-10-19 13:32:20 +00001739 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1740 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001741
Douglas Gregore2e50d332010-12-01 01:36:18 +00001742 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001743 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1744 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001745
1746 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1747 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1748 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001749 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001750 ToData.Aggregate = FromData.Aggregate;
1751 ToData.PlainOldData = FromData.PlainOldData;
1752 ToData.Empty = FromData.Empty;
1753 ToData.Polymorphic = FromData.Polymorphic;
1754 ToData.Abstract = FromData.Abstract;
1755 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001756 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1757 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1758 ToData.HasBasesWithNonStaticDataMembers =
1759 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001760 ToData.HasPrivateFields = FromData.HasPrivateFields;
1761 ToData.HasProtectedFields = FromData.HasProtectedFields;
1762 ToData.HasPublicFields = FromData.HasPublicFields;
1763 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001764 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001765 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001766 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001767 ToData.HasUninitializedReferenceMember
1768 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001769 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001770 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1771 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001772 ToData.NeedOverloadResolutionForCopyConstructor
1773 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001774 ToData.NeedOverloadResolutionForMoveConstructor
1775 = FromData.NeedOverloadResolutionForMoveConstructor;
1776 ToData.NeedOverloadResolutionForMoveAssignment
1777 = FromData.NeedOverloadResolutionForMoveAssignment;
1778 ToData.NeedOverloadResolutionForDestructor
1779 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001780 ToData.DefaultedCopyConstructorIsDeleted
1781 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001782 ToData.DefaultedMoveConstructorIsDeleted
1783 = FromData.DefaultedMoveConstructorIsDeleted;
1784 ToData.DefaultedMoveAssignmentIsDeleted
1785 = FromData.DefaultedMoveAssignmentIsDeleted;
1786 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001787 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1788 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001789 ToData.HasConstexprNonCopyMoveConstructor
1790 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001791 ToData.HasDefaultedDefaultConstructor
1792 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001793 ToData.DefaultedDefaultConstructorIsConstexpr
1794 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001795 ToData.HasConstexprDefaultConstructor
1796 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001797 ToData.HasNonLiteralTypeFieldsOrBases
1798 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001799 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001800 ToData.UserProvidedDefaultConstructor
1801 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001802 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001803 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1804 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1805 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1806 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001807 ToData.ImplicitCopyAssignmentHasConstParam
1808 = FromData.ImplicitCopyAssignmentHasConstParam;
1809 ToData.HasDeclaredCopyConstructorWithConstParam
1810 = FromData.HasDeclaredCopyConstructorWithConstParam;
1811 ToData.HasDeclaredCopyAssignmentWithConstParam
1812 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001813
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001814 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001815 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001816 ExpectedType TyOrErr = import(Base1.getType());
1817 if (!TyOrErr)
1818 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001819
1820 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001821 if (Base1.isPackExpansion()) {
1822 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1823 EllipsisLoc = *LocOrErr;
1824 else
1825 return LocOrErr.takeError();
1826 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001827
1828 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001829 if (Error Err =
1830 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1831 return Err;
1832
1833 auto RangeOrErr = import(Base1.getSourceRange());
1834 if (!RangeOrErr)
1835 return RangeOrErr.takeError();
1836
1837 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1838 if (!TSIOrErr)
1839 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001840
Douglas Gregore2e50d332010-12-01 01:36:18 +00001841 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001842 new (Importer.getToContext()) CXXBaseSpecifier(
1843 *RangeOrErr,
1844 Base1.isVirtual(),
1845 Base1.isBaseOfClass(),
1846 Base1.getAccessSpecifierAsWritten(),
1847 *TSIOrErr,
1848 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001849 }
1850 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001851 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001852 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001853
Douglas Gregor2e15c842012-02-01 21:00:38 +00001854 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001855 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1856 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001857
Douglas Gregore2e50d332010-12-01 01:36:18 +00001858 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001859 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001860}
1861
Balazs Keri3b30d652018-10-19 13:32:20 +00001862Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001863 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001864 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001865
Gabor Martonac3a5d62018-09-17 12:04:52 +00001866 Expr *FromInit = From->getInit();
1867 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001868 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001869
Balazs Keri3b30d652018-10-19 13:32:20 +00001870 ExpectedExpr ToInitOrErr = import(FromInit);
1871 if (!ToInitOrErr)
1872 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001873
Balazs Keri3b30d652018-10-19 13:32:20 +00001874 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001875 if (From->isInitKnownICE()) {
1876 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1877 Eval->CheckedICE = true;
1878 Eval->IsICE = From->isInitICE();
1879 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001880
1881 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001882 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001883}
1884
Balazs Keri3b30d652018-10-19 13:32:20 +00001885Error ASTNodeImporter::ImportDefinition(
1886 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001887 if (To->getDefinition() || To->isBeingDefined()) {
1888 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001889 return ImportDeclContext(From, /*ForceImport=*/true);
1890 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001891 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001892
Douglas Gregord451ea92011-07-29 23:31:30 +00001893 To->startDefinition();
1894
Balazs Keri3b30d652018-10-19 13:32:20 +00001895 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1896 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001897
Balazs Keri3b30d652018-10-19 13:32:20 +00001898 ExpectedType ToTypeOrErr =
1899 import(Importer.getFromContext().getTypeDeclType(From));
1900 if (!ToTypeOrErr)
1901 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001902
Balazs Keri3b30d652018-10-19 13:32:20 +00001903 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1904 if (!ToPromotionTypeOrErr)
1905 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001906
1907 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001908 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1909 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001910
Douglas Gregord451ea92011-07-29 23:31:30 +00001911 // FIXME: we might need to merge the number of positive or negative bits
1912 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001913 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001914 From->getNumPositiveBits(),
1915 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001916 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001917}
1918
Balazs Keri3b30d652018-10-19 13:32:20 +00001919// FIXME: Remove this, use `import` instead.
1920Expected<TemplateParameterList *> ASTNodeImporter::ImportTemplateParameterList(
1921 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001922 SmallVector<NamedDecl *, 4> ToParams(Params->size());
Balazs Keri3b30d652018-10-19 13:32:20 +00001923 if (Error Err = ImportContainerChecked(*Params, ToParams))
1924 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001925
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001926 Expr *ToRequiresClause;
1927 if (Expr *const R = Params->getRequiresClause()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001928 if (Error Err = importInto(ToRequiresClause, R))
1929 return std::move(Err);
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001930 } else {
1931 ToRequiresClause = nullptr;
1932 }
1933
Balazs Keri3b30d652018-10-19 13:32:20 +00001934 auto ToTemplateLocOrErr = import(Params->getTemplateLoc());
1935 if (!ToTemplateLocOrErr)
1936 return ToTemplateLocOrErr.takeError();
1937 auto ToLAngleLocOrErr = import(Params->getLAngleLoc());
1938 if (!ToLAngleLocOrErr)
1939 return ToLAngleLocOrErr.takeError();
1940 auto ToRAngleLocOrErr = import(Params->getRAngleLoc());
1941 if (!ToRAngleLocOrErr)
1942 return ToRAngleLocOrErr.takeError();
1943
1944 return TemplateParameterList::Create(
1945 Importer.getToContext(),
1946 *ToTemplateLocOrErr,
1947 *ToLAngleLocOrErr,
1948 ToParams,
1949 *ToRAngleLocOrErr,
1950 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001951}
1952
Balazs Keri3b30d652018-10-19 13:32:20 +00001953Error ASTNodeImporter::ImportTemplateArguments(
1954 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1955 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001956 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001957 if (auto ToOrErr = import(FromArgs[I]))
1958 ToArgs.push_back(*ToOrErr);
1959 else
1960 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001961 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001962
Balazs Keri3b30d652018-10-19 13:32:20 +00001963 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001964}
1965
Balazs Keri3b30d652018-10-19 13:32:20 +00001966// FIXME: Do not forget to remove this and use only 'import'.
1967Expected<TemplateArgument>
1968ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1969 return import(From);
1970}
1971
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001972template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001973Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001974 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1975 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001976 if (auto ToLocOrErr = import(FromLoc))
1977 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001978 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001979 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001980 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001981 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001982}
1983
Gabor Marton26f72a92018-07-12 09:42:05 +00001984static StructuralEquivalenceKind
1985getStructuralEquivalenceKind(const ASTImporter &Importer) {
1986 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1987 : StructuralEquivalenceKind::Default;
1988}
1989
Gabor Marton950fb572018-07-17 12:39:27 +00001990bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1991 StructuralEquivalenceContext Ctx(
1992 Importer.getFromContext(), Importer.getToContext(),
1993 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1994 false, Complain);
1995 return Ctx.IsEquivalent(From, To);
1996}
1997
1998bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001999 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002000 // Eliminate a potential failure point where we attempt to re-import
2001 // something we're trying to import while completing ToRecord.
2002 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2003 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002004 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002005 if (ToOriginRecord)
2006 ToRecord = ToOriginRecord;
2007 }
2008
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002009 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002010 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002011 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002012 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002013 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002014 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002015}
2016
Larisse Voufo39a1e502013-08-06 01:03:05 +00002017bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2018 bool Complain) {
2019 StructuralEquivalenceContext Ctx(
2020 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002021 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2022 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002023 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002024}
2025
Douglas Gregor98c10182010-02-12 22:17:39 +00002026bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002027 StructuralEquivalenceContext Ctx(
2028 Importer.getFromContext(), Importer.getToContext(),
2029 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002030 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002031}
2032
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002033bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2034 FunctionTemplateDecl *To) {
2035 StructuralEquivalenceContext Ctx(
2036 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002037 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2038 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002039 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002040}
2041
Balazs Keric7797c42018-07-11 09:37:24 +00002042bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2043 StructuralEquivalenceContext Ctx(
2044 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002045 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2046 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002047 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002048}
2049
Douglas Gregor91155082012-11-14 22:29:20 +00002050bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002051 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002052 const llvm::APSInt &FromVal = FromEC->getInitVal();
2053 const llvm::APSInt &ToVal = ToEC->getInitVal();
2054
2055 return FromVal.isSigned() == ToVal.isSigned() &&
2056 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2057 FromVal == ToVal;
2058}
2059
2060bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002061 ClassTemplateDecl *To) {
2062 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2063 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002064 Importer.getNonEquivalentDecls(),
2065 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002066 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002067}
2068
Larisse Voufo39a1e502013-08-06 01:03:05 +00002069bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2070 VarTemplateDecl *To) {
2071 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2072 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002073 Importer.getNonEquivalentDecls(),
2074 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002075 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002076}
2077
Balazs Keri3b30d652018-10-19 13:32:20 +00002078ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002079 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002080 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002081 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002082}
2083
Balazs Keri3b30d652018-10-19 13:32:20 +00002084ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2085 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2086 << D->getDeclKindName();
2087 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2088}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002089
Balazs Keri3b30d652018-10-19 13:32:20 +00002090ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2091 // Import the context of this declaration.
2092 DeclContext *DC, *LexicalDC;
2093 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2094 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002095
2096 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002097 ExpectedSLoc LocOrErr = import(D->getLocation());
2098 if (!LocOrErr)
2099 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002100
Gabor Marton26f72a92018-07-12 09:42:05 +00002101 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002102 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002103 return ToD;
2104
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002105 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002106 LexicalDC->addDeclInternal(ToD);
2107 return ToD;
2108}
2109
Balazs Keri3b30d652018-10-19 13:32:20 +00002110ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002111 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002112 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002113
Gabor Marton26f72a92018-07-12 09:42:05 +00002114 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002115
Sean Callanan65198272011-11-17 23:20:56 +00002116 return ToD;
2117}
2118
Balazs Keri3b30d652018-10-19 13:32:20 +00002119ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2120 ExpectedSLoc LocOrErr = import(D->getLocation());
2121 if (!LocOrErr)
2122 return LocOrErr.takeError();
2123 auto ColonLocOrErr = import(D->getColonLoc());
2124 if (!ColonLocOrErr)
2125 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002126
2127 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002128 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2129 if (!DCOrErr)
2130 return DCOrErr.takeError();
2131 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002132
Gabor Marton26f72a92018-07-12 09:42:05 +00002133 AccessSpecDecl *ToD;
2134 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002135 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002136 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002137
2138 // Lexical DeclContext and Semantic DeclContext
2139 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002140 ToD->setLexicalDeclContext(DC);
2141 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002142
Gabor Marton26f72a92018-07-12 09:42:05 +00002143 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002144}
2145
Balazs Keri3b30d652018-10-19 13:32:20 +00002146ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2147 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2148 if (!DCOrErr)
2149 return DCOrErr.takeError();
2150 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002151 DeclContext *LexicalDC = DC;
2152
Balazs Keri3b30d652018-10-19 13:32:20 +00002153 SourceLocation ToLocation, ToRParenLoc;
2154 Expr *ToAssertExpr;
2155 StringLiteral *ToMessage;
2156 if (auto Imp = importSeq(
2157 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2158 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2159 else
2160 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002161
Gabor Marton26f72a92018-07-12 09:42:05 +00002162 StaticAssertDecl *ToD;
2163 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002164 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2165 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002166 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002167
2168 ToD->setLexicalDeclContext(LexicalDC);
2169 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002170 return ToD;
2171}
2172
Balazs Keri3b30d652018-10-19 13:32:20 +00002173ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002174 // Import the major distinguishing characteristics of this namespace.
2175 DeclContext *DC, *LexicalDC;
2176 DeclarationName Name;
2177 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002178 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002179 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2180 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002181 if (ToD)
2182 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002183
2184 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002185 if (!Name) {
2186 // This is an anonymous namespace. Adopt an existing anonymous
2187 // namespace if we can.
2188 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002189 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002190 MergeWithNamespace = TU->getAnonymousNamespace();
2191 else
2192 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2193 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002194 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002195 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002196 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002197 for (auto *FoundDecl : FoundDecls) {
2198 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002199 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002200
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002201 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002202 MergeWithNamespace = FoundNS;
2203 ConflictingDecls.clear();
2204 break;
2205 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002206
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002207 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002208 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002209
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002210 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002211 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002212 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002213 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002214 if (!Name)
2215 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002216 }
2217 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002218
Balazs Keri3b30d652018-10-19 13:32:20 +00002219 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2220 if (!BeginLocOrErr)
2221 return BeginLocOrErr.takeError();
2222
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002223 // Create the "to" namespace, if needed.
2224 NamespaceDecl *ToNamespace = MergeWithNamespace;
2225 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002226 if (GetImportedOrCreateDecl(
2227 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002228 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002229 /*PrevDecl=*/nullptr))
2230 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002231 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002232 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002233
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002234 // If this is an anonymous namespace, register it as the anonymous
2235 // namespace within its context.
2236 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002237 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002238 TU->setAnonymousNamespace(ToNamespace);
2239 else
2240 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2241 }
2242 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002243 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002244
Balazs Keri3b30d652018-10-19 13:32:20 +00002245 if (Error Err = ImportDeclContext(D))
2246 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002247
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002248 return ToNamespace;
2249}
2250
Balazs Keri3b30d652018-10-19 13:32:20 +00002251ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002252 // Import the major distinguishing characteristics of this namespace.
2253 DeclContext *DC, *LexicalDC;
2254 DeclarationName Name;
2255 SourceLocation Loc;
2256 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002257 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2258 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002259 if (LookupD)
2260 return LookupD;
2261
2262 // NOTE: No conflict resolution is done for namespace aliases now.
2263
Balazs Keri3b30d652018-10-19 13:32:20 +00002264 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2265 NestedNameSpecifierLoc ToQualifierLoc;
2266 NamespaceDecl *ToNamespace;
2267 if (auto Imp = importSeq(
2268 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2269 D->getTargetNameLoc(), D->getNamespace()))
2270 std::tie(
2271 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2272 ToNamespace) = *Imp;
2273 else
2274 return Imp.takeError();
2275 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002276
Gabor Marton26f72a92018-07-12 09:42:05 +00002277 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002278 if (GetImportedOrCreateDecl(
2279 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2280 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002281 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002282
2283 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002284 LexicalDC->addDeclInternal(ToD);
2285
2286 return ToD;
2287}
2288
Balazs Keri3b30d652018-10-19 13:32:20 +00002289ExpectedDecl
2290ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002291 // Import the major distinguishing characteristics of this typedef.
2292 DeclContext *DC, *LexicalDC;
2293 DeclarationName Name;
2294 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002295 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002296 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2297 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002298 if (ToD)
2299 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002300
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002301 // If this typedef is not in block scope, determine whether we've
2302 // seen a typedef with the same name (that we can merge with) or any
2303 // other entity by that name (which name lookup could conflict with).
2304 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002305 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002306 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002307 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002308 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002309 for (auto *FoundDecl : FoundDecls) {
2310 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002311 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002312 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002313 if (Importer.IsStructurallyEquivalent(
2314 D->getUnderlyingType(), FoundTypedef->getUnderlyingType()))
2315 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002316 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002317
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002318 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002319 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002320
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002321 if (!ConflictingDecls.empty()) {
2322 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002323 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002324 ConflictingDecls.size());
2325 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002326 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002327 }
2328 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002329
Balazs Keri3b30d652018-10-19 13:32:20 +00002330 QualType ToUnderlyingType;
2331 TypeSourceInfo *ToTypeSourceInfo;
2332 SourceLocation ToBeginLoc;
2333 if (auto Imp = importSeq(
2334 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2335 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2336 else
2337 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002338
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002339 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002340 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002341 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002342 if (IsAlias) {
2343 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002344 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2345 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002346 return ToTypedef;
2347 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002348 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2349 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002350 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002351
Douglas Gregordd483172010-02-22 17:42:47 +00002352 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002353 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002354
2355 // Templated declarations should not appear in DeclContext.
2356 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2357 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2358 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002359
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002360 return ToTypedef;
2361}
2362
Balazs Keri3b30d652018-10-19 13:32:20 +00002363ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002364 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2365}
2366
Balazs Keri3b30d652018-10-19 13:32:20 +00002367ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002368 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2369}
2370
Balazs Keri3b30d652018-10-19 13:32:20 +00002371ExpectedDecl
2372ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002373 // Import the major distinguishing characteristics of this typedef.
2374 DeclContext *DC, *LexicalDC;
2375 DeclarationName Name;
2376 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002377 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002378 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2379 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002380 if (FoundD)
2381 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002382
2383 // If this typedef is not in block scope, determine whether we've
2384 // seen a typedef with the same name (that we can merge with) or any
2385 // other entity by that name (which name lookup could conflict with).
2386 if (!DC->isFunctionOrMethod()) {
2387 SmallVector<NamedDecl *, 4> ConflictingDecls;
2388 unsigned IDNS = Decl::IDNS_Ordinary;
2389 SmallVector<NamedDecl *, 2> FoundDecls;
2390 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002391 for (auto *FoundDecl : FoundDecls) {
2392 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002393 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002394 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002395 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002396 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002397 }
2398
2399 if (!ConflictingDecls.empty()) {
2400 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2401 ConflictingDecls.data(),
2402 ConflictingDecls.size());
2403 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002404 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002405 }
2406 }
2407
Balazs Keri3b30d652018-10-19 13:32:20 +00002408 TemplateParameterList *ToTemplateParameters;
2409 TypeAliasDecl *ToTemplatedDecl;
2410 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2411 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2412 else
2413 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002414
Gabor Marton26f72a92018-07-12 09:42:05 +00002415 TypeAliasTemplateDecl *ToAlias;
2416 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002417 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002418 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002419
Balazs Keri3b30d652018-10-19 13:32:20 +00002420 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002421
Gabor Horvath7a91c082017-11-14 11:30:38 +00002422 ToAlias->setAccess(D->getAccess());
2423 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002424 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002425 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002426}
2427
Balazs Keri3b30d652018-10-19 13:32:20 +00002428ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002429 // Import the major distinguishing characteristics of this label.
2430 DeclContext *DC, *LexicalDC;
2431 DeclarationName Name;
2432 SourceLocation Loc;
2433 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002434 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2435 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002436 if (ToD)
2437 return ToD;
2438
2439 assert(LexicalDC->isFunctionOrMethod());
2440
Gabor Marton26f72a92018-07-12 09:42:05 +00002441 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002442 if (D->isGnuLocal()) {
2443 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2444 if (!BeginLocOrErr)
2445 return BeginLocOrErr.takeError();
2446 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2447 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2448 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002449
Balazs Keri3b30d652018-10-19 13:32:20 +00002450 } else {
2451 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2452 Name.getAsIdentifierInfo()))
2453 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002454
Balazs Keri3b30d652018-10-19 13:32:20 +00002455 }
2456
2457 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2458 if (!ToStmtOrErr)
2459 return ToStmtOrErr.takeError();
2460
2461 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002462 ToLabel->setLexicalDeclContext(LexicalDC);
2463 LexicalDC->addDeclInternal(ToLabel);
2464 return ToLabel;
2465}
2466
Balazs Keri3b30d652018-10-19 13:32:20 +00002467ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002468 // Import the major distinguishing characteristics of this enum.
2469 DeclContext *DC, *LexicalDC;
2470 DeclarationName Name;
2471 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002472 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002473 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2474 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002475 if (ToD)
2476 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002477
Douglas Gregor98c10182010-02-12 22:17:39 +00002478 // Figure out what enum name we're looking for.
2479 unsigned IDNS = Decl::IDNS_Tag;
2480 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002481 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002482 if (Error Err = importInto(
2483 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2484 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002485 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002486 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002487 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002488
Douglas Gregor98c10182010-02-12 22:17:39 +00002489 // We may already have an enum of the same name; try to find and match it.
2490 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002491 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002492 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002493 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002494 for (auto *FoundDecl : FoundDecls) {
2495 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002496 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002497
Balazs Keri3b30d652018-10-19 13:32:20 +00002498 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002499 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002500 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002501 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002502
Balazs Keri3b30d652018-10-19 13:32:20 +00002503 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002504 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002505 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002506 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002507
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002508 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002509 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002510
Douglas Gregor98c10182010-02-12 22:17:39 +00002511 if (!ConflictingDecls.empty()) {
2512 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002513 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002514 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002515 if (!Name)
2516 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002517 }
2518 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002519
Balazs Keri3b30d652018-10-19 13:32:20 +00002520 SourceLocation ToBeginLoc;
2521 NestedNameSpecifierLoc ToQualifierLoc;
2522 QualType ToIntegerType;
2523 if (auto Imp = importSeq(
2524 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2525 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2526 else
2527 return Imp.takeError();
2528
Douglas Gregor98c10182010-02-12 22:17:39 +00002529 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002530 EnumDecl *D2;
2531 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002532 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002533 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2534 D->isScopedUsingClassTag(), D->isFixed()))
2535 return D2;
2536
Balazs Keri3b30d652018-10-19 13:32:20 +00002537 D2->setQualifierInfo(ToQualifierLoc);
2538 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002539 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002540 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002541 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002542
Douglas Gregor98c10182010-02-12 22:17:39 +00002543 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002544 if (D->isCompleteDefinition())
2545 if (Error Err = ImportDefinition(D, D2))
2546 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002547
Douglas Gregor3996e242010-02-15 22:01:00 +00002548 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002549}
2550
Balazs Keri3b30d652018-10-19 13:32:20 +00002551ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002552 bool IsFriendTemplate = false;
2553 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2554 IsFriendTemplate =
2555 DCXX->getDescribedClassTemplate() &&
2556 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2557 Decl::FOK_None;
2558 }
2559
Douglas Gregor5c73e912010-02-11 00:48:18 +00002560 // If this record has a definition in the translation unit we're coming from,
2561 // but this particular declaration is not that definition, import the
2562 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002563 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002564 if (Definition && Definition != D &&
Balazs Keri0c23dc52018-08-13 13:08:37 +00002565 // Friend template declaration must be imported on its own.
2566 !IsFriendTemplate &&
Gabor Martona3af5672018-05-23 14:24:02 +00002567 // In contrast to a normal CXXRecordDecl, the implicit
2568 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2569 // The definition of the implicit CXXRecordDecl in this case is the
2570 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2571 // condition in order to be able to import the implict Decl.
2572 !D->isImplicit()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002573 ExpectedDecl ImportedDefOrErr = import(Definition);
2574 if (!ImportedDefOrErr)
2575 return ImportedDefOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002576
Balazs Keri3b30d652018-10-19 13:32:20 +00002577 return Importer.MapImported(D, *ImportedDefOrErr);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002578 }
Gabor Martona3af5672018-05-23 14:24:02 +00002579
Douglas Gregor5c73e912010-02-11 00:48:18 +00002580 // Import the major distinguishing characteristics of this record.
2581 DeclContext *DC, *LexicalDC;
2582 DeclarationName Name;
2583 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002584 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002585 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2586 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002587 if (ToD)
2588 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002589
Douglas Gregor5c73e912010-02-11 00:48:18 +00002590 // Figure out what structure name we're looking for.
2591 unsigned IDNS = Decl::IDNS_Tag;
2592 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002593 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002594 if (Error Err = importInto(
2595 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2596 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002597 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002598 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002599 IDNS |= Decl::IDNS_Ordinary;
2600
2601 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002602 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002603 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002604 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002605 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002606 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002607 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002608
2609 if (!FoundDecls.empty()) {
2610 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2611 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2612 D->getASTContext().getExternalSource()->CompleteType(D);
2613 }
2614
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002615 for (auto *FoundDecl : FoundDecls) {
2616 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002617 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002618
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002619 Decl *Found = FoundDecl;
2620 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2621 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002622 Found = Tag->getDecl();
2623 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002624
2625 if (D->getDescribedTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002626 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Martona0df7a92018-05-30 09:19:26 +00002627 Found = Template->getTemplatedDecl();
Balazs Keri3b30d652018-10-19 13:32:20 +00002628 } else {
2629 ConflictingDecls.push_back(FoundDecl);
Gabor Martona0df7a92018-05-30 09:19:26 +00002630 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002631 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002632 }
2633
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002634 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002635 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002636 if (!IsStructuralMatch(D, FoundRecord, false))
2637 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002638 } else {
2639 if (!IsStructuralMatch(D, FoundRecord)) {
2640 ConflictingDecls.push_back(FoundDecl);
2641 continue;
2642 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002643 }
2644
Sean Callanan9092d472017-05-13 00:46:33 +00002645 PrevDecl = FoundRecord;
2646
Douglas Gregor25791052010-02-12 00:09:27 +00002647 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002648 if ((SearchName && !D->isCompleteDefinition() && !IsFriendTemplate)
Douglas Gregordd6006f2012-07-17 21:16:27 +00002649 || (D->isCompleteDefinition() &&
2650 D->isAnonymousStructOrUnion()
Balazs Keri3b30d652018-10-19 13:32:20 +00002651 == FoundDef->isAnonymousStructOrUnion())) {
Douglas Gregor25791052010-02-12 00:09:27 +00002652 // The record types structurally match, or the "from" translation
2653 // unit only had a forward declaration anyway; call it the same
2654 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002655 // FIXME: Structural equivalence check should check for same
2656 // user-defined methods.
2657 Importer.MapImported(D, FoundDef);
2658 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2659 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2660 assert(FoundCXX && "Record type mismatch");
2661
2662 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2663 // FoundDef may not have every implicit method that D has
2664 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002665 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2666 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002667 }
2668 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002669 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002670 if (IsFriendTemplate)
2671 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002672 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002673 // We have a forward declaration of this type, so adopt that forward
2674 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002675
Sean Callananc94711c2014-03-04 18:11:50 +00002676 // If one or both can be completed from external storage then try one
2677 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002678
Sean Callananc94711c2014-03-04 18:11:50 +00002679 if (FoundRecord->hasExternalLexicalStorage() &&
2680 !FoundRecord->isCompleteDefinition())
2681 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2682 if (D->hasExternalLexicalStorage())
2683 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002684
Sean Callananc94711c2014-03-04 18:11:50 +00002685 if (FoundRecord->isCompleteDefinition() &&
2686 D->isCompleteDefinition() &&
Balazs Keri3b30d652018-10-19 13:32:20 +00002687 !IsStructuralMatch(D, FoundRecord)) {
2688 ConflictingDecls.push_back(FoundDecl);
Sean Callananc94711c2014-03-04 18:11:50 +00002689 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002690 }
Balazs Keri0c23dc52018-08-13 13:08:37 +00002691
Douglas Gregor25791052010-02-12 00:09:27 +00002692 AdoptDecl = FoundRecord;
2693 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002694 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002695
2696 continue;
2697 } else if (isa<ValueDecl>(Found))
2698 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002699
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002700 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002701 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002702
Douglas Gregordd6006f2012-07-17 21:16:27 +00002703 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002704 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002705 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002706 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002707 if (!Name)
2708 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002709 }
2710 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002711
Balazs Keri3b30d652018-10-19 13:32:20 +00002712 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2713 if (!BeginLocOrErr)
2714 return BeginLocOrErr.takeError();
2715
Douglas Gregor5c73e912010-02-11 00:48:18 +00002716 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002717 RecordDecl *D2 = AdoptDecl;
2718 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002719 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002720 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002721 if (DCXX->isLambda()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002722 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2723 if (!TInfoOrErr)
2724 return TInfoOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00002725 if (GetImportedOrCreateSpecialDecl(
2726 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002727 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002728 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2729 return D2CXX;
Balazs Keri3b30d652018-10-19 13:32:20 +00002730 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2731 if (!CDeclOrErr)
2732 return CDeclOrErr.takeError();
2733 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002734 } else if (DCXX->isInjectedClassName()) {
2735 // We have to be careful to do a similar dance to the one in
2736 // Sema::ActOnStartCXXMemberDeclarations
2737 CXXRecordDecl *const PrevDecl = nullptr;
2738 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002739 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002740 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002741 Name.getAsIdentifierInfo(), PrevDecl,
2742 DelayTypeCreation))
2743 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002744 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002745 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002746 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002747 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002748 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002749 Name.getAsIdentifierInfo(),
2750 cast_or_null<CXXRecordDecl>(PrevDecl)))
2751 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002752 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002753
Douglas Gregor3996e242010-02-15 22:01:00 +00002754 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002755 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002756 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002757 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002758 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002759
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002760 if (ClassTemplateDecl *FromDescribed =
2761 DCXX->getDescribedClassTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002762 ClassTemplateDecl *ToDescribed;
2763 if (Error Err = importInto(ToDescribed, FromDescribed))
2764 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002765 D2CXX->setDescribedClassTemplate(ToDescribed);
Balazs Keri0c23dc52018-08-13 13:08:37 +00002766 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
Gabor Marton5915777e2018-06-26 13:44:24 +00002767 // In a record describing a template the type should be an
2768 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2769 // previously set type to the correct value here (ToDescribed is not
2770 // available at record create).
2771 // FIXME: The previous type is cleared but not removed from
2772 // ASTContext's internal storage.
2773 CXXRecordDecl *Injected = nullptr;
2774 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2775 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2776 if (Record && Record->isInjectedClassName()) {
2777 Injected = Record;
2778 break;
2779 }
2780 }
2781 D2CXX->setTypeForDecl(nullptr);
2782 Importer.getToContext().getInjectedClassNameType(D2CXX,
2783 ToDescribed->getInjectedClassNameSpecialization());
2784 if (Injected) {
2785 Injected->setTypeForDecl(nullptr);
2786 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2787 }
2788 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002789 } else if (MemberSpecializationInfo *MemberInfo =
2790 DCXX->getMemberSpecializationInfo()) {
2791 TemplateSpecializationKind SK =
2792 MemberInfo->getTemplateSpecializationKind();
2793 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002794
2795 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2796 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2797 else
2798 return ToInstOrErr.takeError();
2799
2800 if (ExpectedSLoc POIOrErr =
2801 import(MemberInfo->getPointOfInstantiation()))
2802 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2803 *POIOrErr);
2804 else
2805 return POIOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002806 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002807
Douglas Gregor25791052010-02-12 00:09:27 +00002808 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002809 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002810 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002811 Name.getAsIdentifierInfo(), PrevDecl))
2812 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002813 D2->setLexicalDeclContext(LexicalDC);
2814 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002815 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002816
Balazs Keri3b30d652018-10-19 13:32:20 +00002817 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2818 D2->setQualifierInfo(*QualifierLocOrErr);
2819 else
2820 return QualifierLocOrErr.takeError();
2821
Douglas Gregordd6006f2012-07-17 21:16:27 +00002822 if (D->isAnonymousStructOrUnion())
2823 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002824 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002825
2826 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002827
Balazs Keri3b30d652018-10-19 13:32:20 +00002828 if (D->isCompleteDefinition())
2829 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2830 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002831
Douglas Gregor3996e242010-02-15 22:01:00 +00002832 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002833}
2834
Balazs Keri3b30d652018-10-19 13:32:20 +00002835ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002836 // Import the major distinguishing characteristics of this enumerator.
2837 DeclContext *DC, *LexicalDC;
2838 DeclarationName Name;
2839 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002840 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002841 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2842 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002843 if (ToD)
2844 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002845
Fangrui Song6907ce22018-07-30 19:24:48 +00002846 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002847 // in the same context.
2848 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002849 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002850 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002851 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002852 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002853 for (auto *FoundDecl : FoundDecls) {
2854 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002855 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002856
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002857 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002858 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002859 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002860 }
2861
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002862 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002863 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002864
Douglas Gregor98c10182010-02-12 22:17:39 +00002865 if (!ConflictingDecls.empty()) {
2866 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002867 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002868 ConflictingDecls.size());
2869 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002870 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002871 }
2872 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002873
Balazs Keri3b30d652018-10-19 13:32:20 +00002874 ExpectedType TypeOrErr = import(D->getType());
2875 if (!TypeOrErr)
2876 return TypeOrErr.takeError();
2877
2878 ExpectedExpr InitOrErr = import(D->getInitExpr());
2879 if (!InitOrErr)
2880 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002881
Gabor Marton26f72a92018-07-12 09:42:05 +00002882 EnumConstantDecl *ToEnumerator;
2883 if (GetImportedOrCreateDecl(
2884 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002885 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002886 return ToEnumerator;
2887
Douglas Gregordd483172010-02-22 17:42:47 +00002888 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002889 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002890 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002891 return ToEnumerator;
2892}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002893
Balazs Keri3b30d652018-10-19 13:32:20 +00002894Error ASTNodeImporter::ImportTemplateInformation(
2895 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002896 switch (FromFD->getTemplatedKind()) {
2897 case FunctionDecl::TK_NonTemplate:
2898 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002899 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002900
2901 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002902 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002903
2904 if (Expected<FunctionDecl *> InstFDOrErr =
2905 import(FromFD->getInstantiatedFromMemberFunction()))
2906 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2907 else
2908 return InstFDOrErr.takeError();
2909
2910 if (ExpectedSLoc POIOrErr = import(
2911 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2912 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2913 else
2914 return POIOrErr.takeError();
2915
2916 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002917 }
2918
2919 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002920 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002921 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002922 if (!FunctionAndArgsOrErr)
2923 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002924
2925 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002926 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002927
Gabor Marton5254e642018-06-27 13:32:50 +00002928 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002929 TemplateArgumentListInfo ToTAInfo;
2930 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002931 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002932 if (Error Err = ImportTemplateArgumentListInfo(
2933 *FromTAArgsAsWritten, ToTAInfo))
2934 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002935
Balazs Keri3b30d652018-10-19 13:32:20 +00002936 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2937 if (!POIOrErr)
2938 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002939
Gabor Marton5254e642018-06-27 13:32:50 +00002940 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002941 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002942 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2943 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2944 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002945 }
2946
2947 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2948 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2949 UnresolvedSet<8> TemplDecls;
2950 unsigned NumTemplates = FromInfo->getNumTemplates();
2951 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002952 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2953 import(FromInfo->getTemplate(I)))
2954 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002955 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002956 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002957 }
2958
2959 // Import TemplateArgumentListInfo.
2960 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002961 if (Error Err = ImportTemplateArgumentListInfo(
2962 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2963 llvm::makeArrayRef(
2964 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2965 ToTAInfo))
2966 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002967
2968 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2969 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002970 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002971 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002972 }
Sam McCallfdc32072018-01-26 12:06:44 +00002973 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002974}
2975
Balazs Keri3b30d652018-10-19 13:32:20 +00002976Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002977ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002978 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002979 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002980 if (!FunctionAndArgsOrErr)
2981 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002982
Balazs Keri3b30d652018-10-19 13:32:20 +00002983 FunctionTemplateDecl *Template;
2984 TemplateArgsTy ToTemplArgs;
2985 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002986 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002987 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002988 return FoundSpec;
2989}
2990
Balazs Keri3b30d652018-10-19 13:32:20 +00002991ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002992
Balazs Keri3b30d652018-10-19 13:32:20 +00002993 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002994 auto RedeclIt = Redecls.begin();
2995 // Import the first part of the decl chain. I.e. import all previous
2996 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002997 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2998 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2999 if (!ToRedeclOrErr)
3000 return ToRedeclOrErr.takeError();
3001 }
Gabor Marton5254e642018-06-27 13:32:50 +00003002 assert(*RedeclIt == D);
3003
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003004 // Import the major distinguishing characteristics of this function.
3005 DeclContext *DC, *LexicalDC;
3006 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003007 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003008 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003009 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3010 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003011 if (ToD)
3012 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003013
Gabor Marton5254e642018-06-27 13:32:50 +00003014 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003015 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003016
Gabor Marton5254e642018-06-27 13:32:50 +00003017 // If this is a function template specialization, then try to find the same
3018 // existing specialization in the "to" context. The localUncachedLookup
3019 // below will not find any specialization, but would find the primary
3020 // template; thus, we have to skip normal lookup in case of specializations.
3021 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3022 if (D->getTemplatedKind() ==
3023 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003024 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3025 if (!FoundFunctionOrErr)
3026 return FoundFunctionOrErr.takeError();
3027 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
3028 if (D->doesThisDeclarationHaveABody() && FoundFunction->hasBody())
3029 return Importer.MapImported(D, FoundFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003030 FoundByLookup = FoundFunction;
3031 }
3032 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003033 // Try to find a function in our own ("to") context with the same name, same
3034 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003035 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003036 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003037 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003038 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003039 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003040 for (auto *FoundDecl : FoundDecls) {
3041 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003042 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003043
Balazs Keria35798d2018-07-17 09:52:41 +00003044 // If template was found, look at the templated function.
3045 if (FromFT) {
3046 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
3047 FoundDecl = Template->getTemplatedDecl();
3048 else
3049 continue;
3050 }
3051
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003052 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003053 if (FoundFunction->hasExternalFormalLinkage() &&
3054 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00003055 if (IsStructuralMatch(D, FoundFunction)) {
3056 const FunctionDecl *Definition = nullptr;
3057 if (D->doesThisDeclarationHaveABody() &&
3058 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003059 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00003060 D, const_cast<FunctionDecl *>(Definition));
3061 }
3062 FoundByLookup = FoundFunction;
3063 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003064 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003065
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003066 // FIXME: Check for overloading more carefully, e.g., by boosting
3067 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003068
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003069 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003070 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003071 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003072
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003073 // Complain about inconsistent function types.
3074 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003075 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003076 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003077 diag::note_odr_value_here)
3078 << FoundFunction->getType();
3079 }
3080 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003081
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003082 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003083 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003084
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003085 if (!ConflictingDecls.empty()) {
3086 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003087 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003088 ConflictingDecls.size());
3089 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003090 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003091 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003092 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003093
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003094 DeclarationNameInfo NameInfo(Name, Loc);
3095 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003096 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3097 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003098
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003099 QualType FromTy = D->getType();
3100 bool usedDifferentExceptionSpec = false;
3101
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003102 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003103 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3104 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3105 // FunctionDecl that we are importing the FunctionProtoType for.
3106 // To avoid an infinite recursion when importing, create the FunctionDecl
3107 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003108 if (FromEPI.ExceptionSpec.SourceDecl ||
3109 FromEPI.ExceptionSpec.SourceTemplate ||
3110 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003111 FunctionProtoType::ExtProtoInfo DefaultEPI;
3112 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003113 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003114 usedDifferentExceptionSpec = true;
3115 }
3116 }
3117
Balazs Keri3b30d652018-10-19 13:32:20 +00003118 QualType T;
3119 TypeSourceInfo *TInfo;
3120 SourceLocation ToInnerLocStart, ToEndLoc;
3121 NestedNameSpecifierLoc ToQualifierLoc;
3122 if (auto Imp = importSeq(
3123 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3124 D->getQualifierLoc(), D->getEndLoc()))
3125 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3126 else
3127 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003128
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003129 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003130 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003131 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003132 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3133 Parameters.push_back(*ToPOrErr);
3134 else
3135 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003136 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003137
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003138 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003139 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003140 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003141 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003142 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3143 ToInnerLocStart, NameInfo, T, TInfo,
3144 FromConstructor->isExplicit(),
3145 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003146 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003147 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003148 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003149 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3150 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3151 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003152 return ToFunction;
3153 } else if (CXXConversionDecl *FromConversion =
3154 dyn_cast<CXXConversionDecl>(D)) {
3155 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3156 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003157 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003158 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3159 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003160 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003161 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3162 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003163 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003164 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3165 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003166 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003167 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003168 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003169 D->getStorageClass(), D->isInlineSpecified(),
3170 D->hasWrittenPrototype(), D->isConstexpr()))
3171 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003172 }
John McCall3e11ebe2010-03-15 10:12:16 +00003173
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003174 // Connect the redecl chain.
3175 if (FoundByLookup) {
3176 auto *Recent = const_cast<FunctionDecl *>(
3177 FoundByLookup->getMostRecentDecl());
3178 ToFunction->setPreviousDecl(Recent);
3179 }
3180
3181 // Import Ctor initializers.
3182 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3183 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3184 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3185 // Import first, then allocate memory and copy if there was no error.
3186 if (Error Err = ImportContainerChecked(
3187 FromConstructor->inits(), CtorInitializers))
3188 return std::move(Err);
3189 auto **Memory =
3190 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3191 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3192 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3193 ToCtor->setCtorInitializers(Memory);
3194 ToCtor->setNumCtorInitializers(NumInitializers);
3195 }
3196 }
3197
Balazs Keri3b30d652018-10-19 13:32:20 +00003198 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003199 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003200 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003201 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3202 ToFunction->setTrivial(D->isTrivial());
3203 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003204 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003205
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003206 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003207 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003208 Param->setOwningFunction(ToFunction);
3209 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003210 }
David Blaikie9c70e042011-09-21 18:16:56 +00003211 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003212
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003213 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3214 // params it refers to.
3215 if (TInfo) {
3216 if (auto ProtoLoc =
3217 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3218 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3219 ProtoLoc.setParam(I, Parameters[I]);
3220 }
3221 }
3222
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003223 if (usedDifferentExceptionSpec) {
3224 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003225 if (ExpectedType TyOrErr = import(D->getType()))
3226 ToFunction->setType(*TyOrErr);
3227 else
3228 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003229 }
3230
Balazs Keria35798d2018-07-17 09:52:41 +00003231 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003232 if (FromFT) {
3233 auto ToFTOrErr = import(FromFT);
3234 if (!ToFTOrErr)
3235 return ToFTOrErr.takeError();
3236 }
Balazs Keria35798d2018-07-17 09:52:41 +00003237
Gabor Marton5254e642018-06-27 13:32:50 +00003238 if (D->doesThisDeclarationHaveABody()) {
3239 if (Stmt *FromBody = D->getBody()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003240 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3241 ToFunction->setBody(*ToBodyOrErr);
3242 else
3243 return ToBodyOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00003244 }
3245 }
3246
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003247 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003248
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003249 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003250 if (Error Err = ImportTemplateInformation(D, ToFunction))
3251 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003252
Gabor Marton5254e642018-06-27 13:32:50 +00003253 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3254
3255 // TODO Can we generalize this approach to other AST nodes as well?
3256 if (D->getDeclContext()->containsDeclAndLoad(D))
3257 DC->addDeclInternal(ToFunction);
3258 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003259 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003260
Gabor Marton5254e642018-06-27 13:32:50 +00003261 // Friend declaration's lexical context is the befriending class, but the
3262 // semantic context is the enclosing scope of the befriending class.
3263 // We want the friend functions to be found in the semantic context by lookup.
3264 // FIXME should we handle this generically in VisitFriendDecl?
3265 // In Other cases when LexicalDC != DC we don't want it to be added,
3266 // e.g out-of-class definitions like void B::f() {} .
3267 if (LexicalDC != DC && IsFriend) {
3268 DC->makeDeclVisibleInContext(ToFunction);
3269 }
3270
Gabor Marton7a0841e2018-10-29 10:18:28 +00003271 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3272 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3273
Gabor Marton5254e642018-06-27 13:32:50 +00003274 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003275 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3276 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3277 if (!ToRedeclOrErr)
3278 return ToRedeclOrErr.takeError();
3279 }
Gabor Marton5254e642018-06-27 13:32:50 +00003280
Douglas Gregor43f54792010-02-17 02:12:47 +00003281 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003282}
3283
Balazs Keri3b30d652018-10-19 13:32:20 +00003284ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003285 return VisitFunctionDecl(D);
3286}
3287
Balazs Keri3b30d652018-10-19 13:32:20 +00003288ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003289 return VisitCXXMethodDecl(D);
3290}
3291
Balazs Keri3b30d652018-10-19 13:32:20 +00003292ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003293 return VisitCXXMethodDecl(D);
3294}
3295
Balazs Keri3b30d652018-10-19 13:32:20 +00003296ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003297 return VisitCXXMethodDecl(D);
3298}
3299
Balazs Keri3b30d652018-10-19 13:32:20 +00003300ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003301 // Import the major distinguishing characteristics of a variable.
3302 DeclContext *DC, *LexicalDC;
3303 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003304 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003305 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003306 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3307 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003308 if (ToD)
3309 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003310
Fangrui Song6907ce22018-07-30 19:24:48 +00003311 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003312 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003313 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003314 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003315 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003316 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003317 if (!Name &&
3318 ASTImporter::getFieldIndex(D) !=
3319 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003320 continue;
3321
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003322 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003323 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003324 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003325 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3326 // initializer of a FieldDecl might not had been instantiated in the
3327 // "To" context. However, the "From" context might instantiated that,
3328 // thus we have to merge that.
3329 if (Expr *FromInitializer = D->getInClassInitializer()) {
3330 // We don't have yet the initializer set.
3331 if (FoundField->hasInClassInitializer() &&
3332 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003333 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3334 FoundField->setInClassInitializer(*ToInitializerOrErr);
3335 else {
3336 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003337 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003338 // FIXME: warning message?
3339 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003340 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003341 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003342 }
3343 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003344 return FoundField;
3345 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003346
Balazs Keri3b30d652018-10-19 13:32:20 +00003347 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003348 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3349 << Name << D->getType() << FoundField->getType();
3350 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3351 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003352
3353 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003354 }
3355 }
3356
Balazs Keri3b30d652018-10-19 13:32:20 +00003357 QualType ToType;
3358 TypeSourceInfo *ToTInfo;
3359 Expr *ToBitWidth;
3360 SourceLocation ToInnerLocStart;
3361 Expr *ToInitializer;
3362 if (auto Imp = importSeq(
3363 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3364 D->getInnerLocStart(), D->getInClassInitializer()))
3365 std::tie(
3366 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3367 else
3368 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003369
Gabor Marton26f72a92018-07-12 09:42:05 +00003370 FieldDecl *ToField;
3371 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003372 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3373 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3374 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003375 return ToField;
3376
Douglas Gregordd483172010-02-22 17:42:47 +00003377 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003378 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003379 if (ToInitializer)
3380 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003381 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003382 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003383 return ToField;
3384}
3385
Balazs Keri3b30d652018-10-19 13:32:20 +00003386ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003387 // Import the major distinguishing characteristics of a variable.
3388 DeclContext *DC, *LexicalDC;
3389 DeclarationName Name;
3390 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003391 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003392 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3393 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003394 if (ToD)
3395 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003396
Fangrui Song6907ce22018-07-30 19:24:48 +00003397 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003398 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003399 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003400 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003401 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003402 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003403 if (!Name &&
3404 ASTImporter::getFieldIndex(D) !=
3405 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003406 continue;
3407
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003408 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003409 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003410 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003411 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003412 return FoundField;
3413 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003414
3415 // If there are more anonymous fields to check, continue.
3416 if (!Name && I < N-1)
3417 continue;
3418
Balazs Keri3b30d652018-10-19 13:32:20 +00003419 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003420 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3421 << Name << D->getType() << FoundField->getType();
3422 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3423 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003424
3425 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003426 }
3427 }
3428
Francois Pichet783dd6e2010-11-21 06:08:52 +00003429 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003430 auto TypeOrErr = import(D->getType());
3431 if (!TypeOrErr)
3432 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003433
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003434 auto **NamedChain =
3435 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003436
3437 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003438 for (auto *PI : D->chain())
3439 if (Expected<NamedDecl *> ToD = import(PI))
3440 NamedChain[i++] = *ToD;
3441 else
3442 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003443
Gabor Marton26f72a92018-07-12 09:42:05 +00003444 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3445 IndirectFieldDecl *ToIndirectField;
3446 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003447 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003448 // FIXME here we leak `NamedChain` which is allocated before
3449 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003450
Balazs Keri3b30d652018-10-19 13:32:20 +00003451 for (const auto *Attr : D->attrs())
3452 ToIndirectField->addAttr(Importer.Import(Attr));
Aaron Ballman260995b2014-10-15 16:58:18 +00003453
Francois Pichet783dd6e2010-11-21 06:08:52 +00003454 ToIndirectField->setAccess(D->getAccess());
3455 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003456 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003457 return ToIndirectField;
3458}
3459
Balazs Keri3b30d652018-10-19 13:32:20 +00003460ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003461 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003462 DeclContext *DC, *LexicalDC;
3463 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3464 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003465
3466 // Determine whether we've already imported this decl.
3467 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3468 auto *RD = cast<CXXRecordDecl>(DC);
3469 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003470
3471 while (ImportedFriend) {
3472 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003473 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3474 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003475 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003476
3477 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3478 if (Importer.IsStructurallyEquivalent(
3479 D->getFriendType()->getType(),
3480 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003481 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003482 }
3483 ImportedFriend = ImportedFriend->getNextFriend();
3484 }
3485
3486 // Not found. Create it.
3487 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003488 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003489 NamedDecl *ToFriendD;
3490 if (Error Err = importInto(ToFriendD, FriendD))
3491 return std::move(Err);
3492
3493 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003494 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3495 ToFriendD->setObjectOfFriendDecl(false);
3496
3497 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003498 } else { // The friend is a type, not a decl.
3499 if (auto TSIOrErr = import(D->getFriendType()))
3500 ToFU = *TSIOrErr;
3501 else
3502 return TSIOrErr.takeError();
3503 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003504
3505 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003506 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003507 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003508 if (auto ListOrErr = ImportTemplateParameterList(FromTPLists[I]))
3509 ToTPLists[I] = *ListOrErr;
3510 else
3511 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003512 }
3513
Balazs Keri3b30d652018-10-19 13:32:20 +00003514 auto LocationOrErr = import(D->getLocation());
3515 if (!LocationOrErr)
3516 return LocationOrErr.takeError();
3517 auto FriendLocOrErr = import(D->getFriendLoc());
3518 if (!FriendLocOrErr)
3519 return FriendLocOrErr.takeError();
3520
Gabor Marton26f72a92018-07-12 09:42:05 +00003521 FriendDecl *FrD;
3522 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003523 *LocationOrErr, ToFU,
3524 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003525 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003526
3527 FrD->setAccess(D->getAccess());
3528 FrD->setLexicalDeclContext(LexicalDC);
3529 LexicalDC->addDeclInternal(FrD);
3530 return FrD;
3531}
3532
Balazs Keri3b30d652018-10-19 13:32:20 +00003533ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003534 // Import the major distinguishing characteristics of an ivar.
3535 DeclContext *DC, *LexicalDC;
3536 DeclarationName Name;
3537 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003538 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003539 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3540 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003541 if (ToD)
3542 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003543
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003544 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003545 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003546 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003547 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003548 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003549 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003550 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003551 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003552 return FoundIvar;
3553 }
3554
3555 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3556 << Name << D->getType() << FoundIvar->getType();
3557 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3558 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003559
3560 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003561 }
3562 }
3563
Balazs Keri3b30d652018-10-19 13:32:20 +00003564 QualType ToType;
3565 TypeSourceInfo *ToTypeSourceInfo;
3566 Expr *ToBitWidth;
3567 SourceLocation ToInnerLocStart;
3568 if (auto Imp = importSeq(
3569 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3570 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3571 else
3572 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003573
Gabor Marton26f72a92018-07-12 09:42:05 +00003574 ObjCIvarDecl *ToIvar;
3575 if (GetImportedOrCreateDecl(
3576 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003577 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3578 ToType, ToTypeSourceInfo,
3579 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003580 return ToIvar;
3581
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003582 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003583 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003584 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003585}
3586
Balazs Keri3b30d652018-10-19 13:32:20 +00003587ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003588
3589 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3590 auto RedeclIt = Redecls.begin();
3591 // Import the first part of the decl chain. I.e. import all previous
3592 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003593 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3594 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3595 if (!RedeclOrErr)
3596 return RedeclOrErr.takeError();
3597 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003598 assert(*RedeclIt == D);
3599
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003600 // Import the major distinguishing characteristics of a variable.
3601 DeclContext *DC, *LexicalDC;
3602 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003603 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003604 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003605 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3606 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003607 if (ToD)
3608 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003609
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003610 // Try to find a variable in our own ("to") context with the same name and
3611 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003612 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003613 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003614 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003615 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003616 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003617 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003618 for (auto *FoundDecl : FoundDecls) {
3619 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003620 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003621
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003622 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003623 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003624 if (FoundVar->hasExternalFormalLinkage() &&
3625 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003626 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003627 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003628
3629 // The VarDecl in the "From" context has a definition, but in the
3630 // "To" context we already have a definition.
3631 VarDecl *FoundDef = FoundVar->getDefinition();
3632 if (D->isThisDeclarationADefinition() && FoundDef)
3633 // FIXME Check for ODR error if the two definitions have
3634 // different initializers?
3635 return Importer.MapImported(D, FoundDef);
3636
3637 // The VarDecl in the "From" context has an initializer, but in the
3638 // "To" context we already have an initializer.
3639 const VarDecl *FoundDInit = nullptr;
3640 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3641 // FIXME Diagnose ODR error if the two initializers are different?
3642 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3643
3644 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003645 break;
3646 }
3647
Douglas Gregor56521c52010-02-12 17:23:39 +00003648 const ArrayType *FoundArray
3649 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3650 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003651 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003652 if (FoundArray && TArray) {
3653 if (isa<IncompleteArrayType>(FoundArray) &&
3654 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003655 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003656 if (auto TyOrErr = import(D->getType()))
3657 FoundVar->setType(*TyOrErr);
3658 else
3659 return TyOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003660
Gabor Martonac3a5d62018-09-17 12:04:52 +00003661 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003662 break;
3663 } else if (isa<IncompleteArrayType>(TArray) &&
3664 isa<ConstantArrayType>(FoundArray)) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003665 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003666 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003667 }
3668 }
3669
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003670 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003671 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003672 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3673 << FoundVar->getType();
3674 }
3675 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003676
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003677 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003678 }
3679
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003680 if (!ConflictingDecls.empty()) {
3681 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003682 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003683 ConflictingDecls.size());
3684 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003685 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003686 }
3687 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003688
Balazs Keri3b30d652018-10-19 13:32:20 +00003689 QualType ToType;
3690 TypeSourceInfo *ToTypeSourceInfo;
3691 SourceLocation ToInnerLocStart;
3692 NestedNameSpecifierLoc ToQualifierLoc;
3693 if (auto Imp = importSeq(
3694 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3695 D->getQualifierLoc()))
3696 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3697 else
3698 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003699
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003700 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003701 VarDecl *ToVar;
3702 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003703 ToInnerLocStart, Loc,
3704 Name.getAsIdentifierInfo(),
3705 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003706 D->getStorageClass()))
3707 return ToVar;
3708
Balazs Keri3b30d652018-10-19 13:32:20 +00003709 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003710 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003711 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003712
Gabor Martonac3a5d62018-09-17 12:04:52 +00003713 if (FoundByLookup) {
3714 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3715 ToVar->setPreviousDecl(Recent);
3716 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003717
Balazs Keri3b30d652018-10-19 13:32:20 +00003718 if (Error Err = ImportInitializer(D, ToVar))
3719 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003720
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003721 if (D->isConstexpr())
3722 ToVar->setConstexpr(true);
3723
Gabor Martonac3a5d62018-09-17 12:04:52 +00003724 if (D->getDeclContext()->containsDeclAndLoad(D))
3725 DC->addDeclInternal(ToVar);
3726 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3727 LexicalDC->addDeclInternal(ToVar);
3728
3729 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003730 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3731 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3732 if (!RedeclOrErr)
3733 return RedeclOrErr.takeError();
3734 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003735
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003736 return ToVar;
3737}
3738
Balazs Keri3b30d652018-10-19 13:32:20 +00003739ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003740 // Parameters are created in the translation unit's context, then moved
3741 // into the function declaration's context afterward.
3742 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003743
Balazs Keri3b30d652018-10-19 13:32:20 +00003744 DeclarationName ToDeclName;
3745 SourceLocation ToLocation;
3746 QualType ToType;
3747 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3748 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3749 else
3750 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003751
Douglas Gregor8b228d72010-02-17 21:22:52 +00003752 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003753 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003754 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3755 ToLocation, ToDeclName.getAsIdentifierInfo(),
3756 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003757 return ToParm;
3758 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003759}
3760
Balazs Keri3b30d652018-10-19 13:32:20 +00003761ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003762 // Parameters are created in the translation unit's context, then moved
3763 // into the function declaration's context afterward.
3764 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003765
Balazs Keri3b30d652018-10-19 13:32:20 +00003766 DeclarationName ToDeclName;
3767 SourceLocation ToLocation, ToInnerLocStart;
3768 QualType ToType;
3769 TypeSourceInfo *ToTypeSourceInfo;
3770 if (auto Imp = importSeq(
3771 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3772 D->getTypeSourceInfo()))
3773 std::tie(
3774 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3775 ToTypeSourceInfo) = *Imp;
3776 else
3777 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003778
Gabor Marton26f72a92018-07-12 09:42:05 +00003779 ParmVarDecl *ToParm;
3780 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003781 ToInnerLocStart, ToLocation,
3782 ToDeclName.getAsIdentifierInfo(), ToType,
3783 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003784 /*DefaultArg*/ nullptr))
3785 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003786
3787 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003788 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003789 ToParm->setKNRPromoted(D->isKNRPromoted());
3790
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003791 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003792 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3793 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3794 else
3795 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003796 } else if (D->hasUnparsedDefaultArg()) {
3797 ToParm->setUnparsedDefaultArg();
3798 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003799 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3800 ToParm->setDefaultArg(*ToDefArgOrErr);
3801 else
3802 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003803 }
Sean Callanan59721b32015-04-28 18:41:46 +00003804
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003805 if (D->isObjCMethodParameter()) {
3806 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3807 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3808 } else {
3809 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3810 D->getFunctionScopeIndex());
3811 }
3812
Gabor Marton26f72a92018-07-12 09:42:05 +00003813 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003814}
3815
Balazs Keri3b30d652018-10-19 13:32:20 +00003816ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003817 // Import the major distinguishing characteristics of a method.
3818 DeclContext *DC, *LexicalDC;
3819 DeclarationName Name;
3820 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003821 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003822 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3823 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003824 if (ToD)
3825 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003826
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003827 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003828 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003829 for (auto *FoundDecl : FoundDecls) {
3830 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003831 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3832 continue;
3833
3834 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003835 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3836 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003837 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003838 << D->isInstanceMethod() << Name << D->getReturnType()
3839 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003840 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003841 diag::note_odr_objc_method_here)
3842 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003843
3844 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003845 }
3846
3847 // Check the number of parameters.
3848 if (D->param_size() != FoundMethod->param_size()) {
3849 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3850 << D->isInstanceMethod() << Name
3851 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003852 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003853 diag::note_odr_objc_method_here)
3854 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003855
3856 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003857 }
3858
3859 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003860 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003861 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3862 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003863 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003864 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003865 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003866 diag::err_odr_objc_method_param_type_inconsistent)
3867 << D->isInstanceMethod() << Name
3868 << (*P)->getType() << (*FoundP)->getType();
3869 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3870 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003871
3872 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003873 }
3874 }
3875
3876 // Check variadic/non-variadic.
3877 // Check the number of parameters.
3878 if (D->isVariadic() != FoundMethod->isVariadic()) {
3879 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3880 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003881 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003882 diag::note_odr_objc_method_here)
3883 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003884
3885 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003886 }
3887
3888 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003889 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003890 }
3891 }
3892
Balazs Keri3b30d652018-10-19 13:32:20 +00003893 SourceLocation ToEndLoc;
3894 QualType ToReturnType;
3895 TypeSourceInfo *ToReturnTypeSourceInfo;
3896 if (auto Imp = importSeq(
3897 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3898 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3899 else
3900 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003901
Gabor Marton26f72a92018-07-12 09:42:05 +00003902 ObjCMethodDecl *ToMethod;
3903 if (GetImportedOrCreateDecl(
3904 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003905 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3906 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003907 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3908 D->getImplementationControl(), D->hasRelatedResultType()))
3909 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003910
3911 // FIXME: When we decide to merge method definitions, we'll need to
3912 // deal with implicit parameters.
3913
3914 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003915 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003916 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003917 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3918 ToParams.push_back(*ToPOrErr);
3919 else
3920 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003921 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003922
Douglas Gregor43f54792010-02-17 02:12:47 +00003923 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003924 for (auto *ToParam : ToParams) {
3925 ToParam->setOwningFunction(ToMethod);
3926 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003927 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003928
Balazs Keri3b30d652018-10-19 13:32:20 +00003929 SmallVector<SourceLocation, 12> FromSelLocs;
3930 D->getSelectorLocs(FromSelLocs);
3931 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3932 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3933 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003934
Balazs Keri3b30d652018-10-19 13:32:20 +00003935 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003936
3937 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003938 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003939 return ToMethod;
3940}
3941
Balazs Keri3b30d652018-10-19 13:32:20 +00003942ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003943 // Import the major distinguishing characteristics of a category.
3944 DeclContext *DC, *LexicalDC;
3945 DeclarationName Name;
3946 SourceLocation Loc;
3947 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003948 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3949 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003950 if (ToD)
3951 return ToD;
3952
Balazs Keri3b30d652018-10-19 13:32:20 +00003953 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3954 TypeSourceInfo *ToTypeSourceInfo;
3955 if (auto Imp = importSeq(
3956 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3957 D->getTypeSourceInfo()))
3958 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3959 else
3960 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003961
Gabor Marton26f72a92018-07-12 09:42:05 +00003962 ObjCTypeParamDecl *Result;
3963 if (GetImportedOrCreateDecl(
3964 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003965 ToVarianceLoc, D->getIndex(),
3966 ToLocation, Name.getAsIdentifierInfo(),
3967 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003968 return Result;
3969
Douglas Gregor85f3f952015-07-07 03:57:15 +00003970 Result->setLexicalDeclContext(LexicalDC);
3971 return Result;
3972}
3973
Balazs Keri3b30d652018-10-19 13:32:20 +00003974ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003975 // Import the major distinguishing characteristics of a category.
3976 DeclContext *DC, *LexicalDC;
3977 DeclarationName Name;
3978 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003979 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003980 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3981 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003982 if (ToD)
3983 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003984
Balazs Keri3b30d652018-10-19 13:32:20 +00003985 ObjCInterfaceDecl *ToInterface;
3986 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3987 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003988
Douglas Gregor84c51c32010-02-18 01:47:50 +00003989 // Determine if we've already encountered this category.
3990 ObjCCategoryDecl *MergeWithCategory
3991 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3992 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3993 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003994 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3995 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3996 if (auto Imp = importSeq(
3997 D->getAtStartLoc(), D->getCategoryNameLoc(),
3998 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3999 std::tie(
4000 ToAtStartLoc, ToCategoryNameLoc,
4001 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4002 else
4003 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004004
4005 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004006 ToAtStartLoc, Loc,
4007 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004008 Name.getAsIdentifierInfo(), ToInterface,
4009 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004010 ToIvarLBraceLoc,
4011 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004012 return ToCategory;
4013
Douglas Gregor84c51c32010-02-18 01:47:50 +00004014 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004015 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004016 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004017 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004018 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4019 ToCategory->setTypeParamList(*PListOrErr);
4020 else
4021 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004022
Douglas Gregor84c51c32010-02-18 01:47:50 +00004023 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004024 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4025 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004026 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4027 = D->protocol_loc_begin();
4028 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4029 FromProtoEnd = D->protocol_end();
4030 FromProto != FromProtoEnd;
4031 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004032 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4033 Protocols.push_back(*ToProtoOrErr);
4034 else
4035 return ToProtoOrErr.takeError();
4036
4037 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4038 ProtocolLocs.push_back(*ToProtoLocOrErr);
4039 else
4040 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004041 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004042
Douglas Gregor84c51c32010-02-18 01:47:50 +00004043 // FIXME: If we're merging, make sure that the protocol list is the same.
4044 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4045 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004046
Douglas Gregor84c51c32010-02-18 01:47:50 +00004047 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004048 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004049 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004050
Douglas Gregor84c51c32010-02-18 01:47:50 +00004051 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004052 if (Error Err = ImportDeclContext(D))
4053 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004054
Douglas Gregor84c51c32010-02-18 01:47:50 +00004055 // If we have an implementation, import it as well.
4056 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004057 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4058 import(D->getImplementation()))
4059 ToCategory->setImplementation(*ToImplOrErr);
4060 else
4061 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004062 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004063
Douglas Gregor84c51c32010-02-18 01:47:50 +00004064 return ToCategory;
4065}
4066
Balazs Keri3b30d652018-10-19 13:32:20 +00004067Error ASTNodeImporter::ImportDefinition(
4068 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004070 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004071 if (Error Err = ImportDeclContext(From))
4072 return Err;
4073 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004074 }
4075
4076 // Start the protocol definition
4077 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004078
Douglas Gregor2aa53772012-01-24 17:42:07 +00004079 // Import protocols
4080 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4081 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004082 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4083 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004084 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4085 FromProtoEnd = From->protocol_end();
4086 FromProto != FromProtoEnd;
4087 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004088 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4089 Protocols.push_back(*ToProtoOrErr);
4090 else
4091 return ToProtoOrErr.takeError();
4092
4093 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4094 ProtocolLocs.push_back(*ToProtoLocOrErr);
4095 else
4096 return ToProtoLocOrErr.takeError();
4097
Douglas Gregor2aa53772012-01-24 17:42:07 +00004098 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004099
Douglas Gregor2aa53772012-01-24 17:42:07 +00004100 // FIXME: If we're merging, make sure that the protocol list is the same.
4101 To->setProtocolList(Protocols.data(), Protocols.size(),
4102 ProtocolLocs.data(), Importer.getToContext());
4103
Douglas Gregor2e15c842012-02-01 21:00:38 +00004104 if (shouldForceImportDeclContext(Kind)) {
4105 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004106 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4107 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004108 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004109 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004110}
4111
Balazs Keri3b30d652018-10-19 13:32:20 +00004112ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004113 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004114 // from, but this particular declaration is not that definition, import the
4115 // definition and map to that.
4116 ObjCProtocolDecl *Definition = D->getDefinition();
4117 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004118 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4119 return Importer.MapImported(D, *ImportedDefOrErr);
4120 else
4121 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004122 }
4123
Douglas Gregor84c51c32010-02-18 01:47:50 +00004124 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004125 DeclContext *DC, *LexicalDC;
4126 DeclarationName Name;
4127 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004128 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004129 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4130 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004131 if (ToD)
4132 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004133
Craig Topper36250ad2014-05-12 05:36:57 +00004134 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004135 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004136 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004137 for (auto *FoundDecl : FoundDecls) {
4138 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004139 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004140
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004141 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004142 break;
4143 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004144
Douglas Gregor98d156a2010-02-17 16:12:00 +00004145 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004146 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004147 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4148 if (!ToAtBeginLocOrErr)
4149 return ToAtBeginLocOrErr.takeError();
4150
Gabor Marton26f72a92018-07-12 09:42:05 +00004151 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4152 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004153 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004154 /*PrevDecl=*/nullptr))
4155 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004156 ToProto->setLexicalDeclContext(LexicalDC);
4157 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004158 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004159
4160 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004161
Balazs Keri3b30d652018-10-19 13:32:20 +00004162 if (D->isThisDeclarationADefinition())
4163 if (Error Err = ImportDefinition(D, ToProto))
4164 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004165
Douglas Gregor98d156a2010-02-17 16:12:00 +00004166 return ToProto;
4167}
4168
Balazs Keri3b30d652018-10-19 13:32:20 +00004169ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4170 DeclContext *DC, *LexicalDC;
4171 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4172 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004173
Balazs Keri3b30d652018-10-19 13:32:20 +00004174 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4175 if (!ExternLocOrErr)
4176 return ExternLocOrErr.takeError();
4177
4178 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4179 if (!LangLocOrErr)
4180 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004181
4182 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004183
4184 LinkageSpecDecl *ToLinkageSpec;
4185 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004186 *ExternLocOrErr, *LangLocOrErr,
4187 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004188 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004189
4190 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004191 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4192 if (!RBraceLocOrErr)
4193 return RBraceLocOrErr.takeError();
4194 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004195 }
4196
4197 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4198 LexicalDC->addDeclInternal(ToLinkageSpec);
4199
Sean Callanan0aae0412014-12-10 00:00:37 +00004200 return ToLinkageSpec;
4201}
4202
Balazs Keri3b30d652018-10-19 13:32:20 +00004203ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004204 DeclContext *DC, *LexicalDC;
4205 DeclarationName Name;
4206 SourceLocation Loc;
4207 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004208 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4209 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004210 if (ToD)
4211 return ToD;
4212
Balazs Keri3b30d652018-10-19 13:32:20 +00004213 SourceLocation ToLoc, ToUsingLoc;
4214 NestedNameSpecifierLoc ToQualifierLoc;
4215 if (auto Imp = importSeq(
4216 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4217 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4218 else
4219 return Imp.takeError();
4220
4221 DeclarationNameInfo NameInfo(Name, ToLoc);
4222 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4223 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004224
Gabor Marton26f72a92018-07-12 09:42:05 +00004225 UsingDecl *ToUsing;
4226 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004227 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004228 D->hasTypename()))
4229 return ToUsing;
4230
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004231 ToUsing->setLexicalDeclContext(LexicalDC);
4232 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004233
4234 if (NamedDecl *FromPattern =
4235 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004236 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4237 Importer.getToContext().setInstantiatedFromUsingDecl(
4238 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004239 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004240 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004241 }
4242
Balazs Keri3b30d652018-10-19 13:32:20 +00004243 for (UsingShadowDecl *FromShadow : D->shadows()) {
4244 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4245 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004246 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004247 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004248 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004249 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004250 }
4251 return ToUsing;
4252}
4253
Balazs Keri3b30d652018-10-19 13:32:20 +00004254ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004255 DeclContext *DC, *LexicalDC;
4256 DeclarationName Name;
4257 SourceLocation Loc;
4258 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004259 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4260 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004261 if (ToD)
4262 return ToD;
4263
Balazs Keri3b30d652018-10-19 13:32:20 +00004264 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4265 if (!ToUsingOrErr)
4266 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004267
Balazs Keri3b30d652018-10-19 13:32:20 +00004268 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4269 if (!ToTargetOrErr)
4270 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004271
Gabor Marton26f72a92018-07-12 09:42:05 +00004272 UsingShadowDecl *ToShadow;
4273 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004274 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004275 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004276
4277 ToShadow->setLexicalDeclContext(LexicalDC);
4278 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004279
4280 if (UsingShadowDecl *FromPattern =
4281 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004282 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4283 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4284 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004285 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004286 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004287 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004288 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004289 }
4290
4291 LexicalDC->addDeclInternal(ToShadow);
4292
4293 return ToShadow;
4294}
4295
Balazs Keri3b30d652018-10-19 13:32:20 +00004296ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004297 DeclContext *DC, *LexicalDC;
4298 DeclarationName Name;
4299 SourceLocation Loc;
4300 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004301 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4302 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004303 if (ToD)
4304 return ToD;
4305
Balazs Keri3b30d652018-10-19 13:32:20 +00004306 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4307 if (!ToComAncestorOrErr)
4308 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004309
Balazs Keri3b30d652018-10-19 13:32:20 +00004310 NamespaceDecl *ToNominatedNamespace;
4311 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4312 NestedNameSpecifierLoc ToQualifierLoc;
4313 if (auto Imp = importSeq(
4314 D->getNominatedNamespace(), D->getUsingLoc(),
4315 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4316 D->getIdentLocation()))
4317 std::tie(
4318 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4319 ToQualifierLoc, ToIdentLocation) = *Imp;
4320 else
4321 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004322
Gabor Marton26f72a92018-07-12 09:42:05 +00004323 UsingDirectiveDecl *ToUsingDir;
4324 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004325 ToUsingLoc,
4326 ToNamespaceKeyLocation,
4327 ToQualifierLoc,
4328 ToIdentLocation,
4329 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004330 return ToUsingDir;
4331
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004332 ToUsingDir->setLexicalDeclContext(LexicalDC);
4333 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004334
4335 return ToUsingDir;
4336}
4337
Balazs Keri3b30d652018-10-19 13:32:20 +00004338ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004339 UnresolvedUsingValueDecl *D) {
4340 DeclContext *DC, *LexicalDC;
4341 DeclarationName Name;
4342 SourceLocation Loc;
4343 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004344 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4345 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004346 if (ToD)
4347 return ToD;
4348
Balazs Keri3b30d652018-10-19 13:32:20 +00004349 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4350 NestedNameSpecifierLoc ToQualifierLoc;
4351 if (auto Imp = importSeq(
4352 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4353 D->getEllipsisLoc()))
4354 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4355 else
4356 return Imp.takeError();
4357
4358 DeclarationNameInfo NameInfo(Name, ToLoc);
4359 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4360 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004361
Gabor Marton26f72a92018-07-12 09:42:05 +00004362 UnresolvedUsingValueDecl *ToUsingValue;
4363 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004364 ToUsingLoc, ToQualifierLoc, NameInfo,
4365 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004366 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004367
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004368 ToUsingValue->setAccess(D->getAccess());
4369 ToUsingValue->setLexicalDeclContext(LexicalDC);
4370 LexicalDC->addDeclInternal(ToUsingValue);
4371
4372 return ToUsingValue;
4373}
4374
Balazs Keri3b30d652018-10-19 13:32:20 +00004375ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004376 UnresolvedUsingTypenameDecl *D) {
4377 DeclContext *DC, *LexicalDC;
4378 DeclarationName Name;
4379 SourceLocation Loc;
4380 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004381 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4382 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004383 if (ToD)
4384 return ToD;
4385
Balazs Keri3b30d652018-10-19 13:32:20 +00004386 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4387 NestedNameSpecifierLoc ToQualifierLoc;
4388 if (auto Imp = importSeq(
4389 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4390 D->getEllipsisLoc()))
4391 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4392 else
4393 return Imp.takeError();
4394
Gabor Marton26f72a92018-07-12 09:42:05 +00004395 UnresolvedUsingTypenameDecl *ToUsing;
4396 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004397 ToUsingLoc, ToTypenameLoc,
4398 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004399 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004400
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004401 ToUsing->setAccess(D->getAccess());
4402 ToUsing->setLexicalDeclContext(LexicalDC);
4403 LexicalDC->addDeclInternal(ToUsing);
4404
4405 return ToUsing;
4406}
4407
Balazs Keri3b30d652018-10-19 13:32:20 +00004408
4409Error ASTNodeImporter::ImportDefinition(
4410 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004411 if (To->getDefinition()) {
4412 // Check consistency of superclass.
4413 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4414 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004415 if (auto FromSuperOrErr = import(FromSuper))
4416 FromSuper = *FromSuperOrErr;
4417 else
4418 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004419 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004420
4421 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004422 if ((bool)FromSuper != (bool)ToSuper ||
4423 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004424 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004425 diag::err_odr_objc_superclass_inconsistent)
4426 << To->getDeclName();
4427 if (ToSuper)
4428 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4429 << To->getSuperClass()->getDeclName();
4430 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004431 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004432 diag::note_odr_objc_missing_superclass);
4433 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004434 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004435 diag::note_odr_objc_superclass)
4436 << From->getSuperClass()->getDeclName();
4437 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004438 Importer.FromDiag(From->getLocation(),
4439 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004440 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004441
Douglas Gregor2e15c842012-02-01 21:00:38 +00004442 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004443 if (Error Err = ImportDeclContext(From))
4444 return Err;
4445 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004446 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004447
Douglas Gregor2aa53772012-01-24 17:42:07 +00004448 // Start the definition.
4449 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004450
Douglas Gregor2aa53772012-01-24 17:42:07 +00004451 // If this class has a superclass, import it.
4452 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004453 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4454 To->setSuperClass(*SuperTInfoOrErr);
4455 else
4456 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004457 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004458
Douglas Gregor2aa53772012-01-24 17:42:07 +00004459 // Import protocols
4460 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4461 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004462 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4463 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004464
Douglas Gregor2aa53772012-01-24 17:42:07 +00004465 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4466 FromProtoEnd = From->protocol_end();
4467 FromProto != FromProtoEnd;
4468 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004469 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4470 Protocols.push_back(*ToProtoOrErr);
4471 else
4472 return ToProtoOrErr.takeError();
4473
4474 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4475 ProtocolLocs.push_back(*ToProtoLocOrErr);
4476 else
4477 return ToProtoLocOrErr.takeError();
4478
Douglas Gregor2aa53772012-01-24 17:42:07 +00004479 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004480
Douglas Gregor2aa53772012-01-24 17:42:07 +00004481 // FIXME: If we're merging, make sure that the protocol list is the same.
4482 To->setProtocolList(Protocols.data(), Protocols.size(),
4483 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004484
Douglas Gregor2aa53772012-01-24 17:42:07 +00004485 // Import categories. When the categories themselves are imported, they'll
4486 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004487 for (auto *Cat : From->known_categories()) {
4488 auto ToCatOrErr = import(Cat);
4489 if (!ToCatOrErr)
4490 return ToCatOrErr.takeError();
4491 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004492
Douglas Gregor2aa53772012-01-24 17:42:07 +00004493 // If we have an @implementation, import it as well.
4494 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004495 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4496 import(From->getImplementation()))
4497 To->setImplementation(*ToImplOrErr);
4498 else
4499 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004500 }
4501
Douglas Gregor2e15c842012-02-01 21:00:38 +00004502 if (shouldForceImportDeclContext(Kind)) {
4503 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004504 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4505 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004506 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004507 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004508}
4509
Balazs Keri3b30d652018-10-19 13:32:20 +00004510Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004511ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4512 if (!list)
4513 return nullptr;
4514
4515 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004516 for (auto *fromTypeParam : *list) {
4517 if (auto toTypeParamOrErr = import(fromTypeParam))
4518 toTypeParams.push_back(*toTypeParamOrErr);
4519 else
4520 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004521 }
4522
Balazs Keri3b30d652018-10-19 13:32:20 +00004523 auto LAngleLocOrErr = import(list->getLAngleLoc());
4524 if (!LAngleLocOrErr)
4525 return LAngleLocOrErr.takeError();
4526
4527 auto RAngleLocOrErr = import(list->getRAngleLoc());
4528 if (!RAngleLocOrErr)
4529 return RAngleLocOrErr.takeError();
4530
Douglas Gregor85f3f952015-07-07 03:57:15 +00004531 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004532 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004533 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004534 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004535}
4536
Balazs Keri3b30d652018-10-19 13:32:20 +00004537ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004538 // If this class has a definition in the translation unit we're coming from,
4539 // but this particular declaration is not that definition, import the
4540 // definition and map to that.
4541 ObjCInterfaceDecl *Definition = D->getDefinition();
4542 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004543 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4544 return Importer.MapImported(D, *ImportedDefOrErr);
4545 else
4546 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004547 }
4548
Douglas Gregor45635322010-02-16 01:20:57 +00004549 // Import the major distinguishing characteristics of an @interface.
4550 DeclContext *DC, *LexicalDC;
4551 DeclarationName Name;
4552 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004553 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004554 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4555 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004556 if (ToD)
4557 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004558
Douglas Gregor2aa53772012-01-24 17:42:07 +00004559 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004560 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004561 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004562 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004563 for (auto *FoundDecl : FoundDecls) {
4564 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004565 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004566
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004567 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004568 break;
4569 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004570
Douglas Gregor2aa53772012-01-24 17:42:07 +00004571 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004572 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004573 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004574 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4575 if (!AtBeginLocOrErr)
4576 return AtBeginLocOrErr.takeError();
4577
Gabor Marton26f72a92018-07-12 09:42:05 +00004578 if (GetImportedOrCreateDecl(
4579 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004580 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004581 /*TypeParamList=*/nullptr,
4582 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4583 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004584 ToIface->setLexicalDeclContext(LexicalDC);
4585 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004586 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004587 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004588 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004589 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004590 if (auto ToPListOrErr =
4591 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4592 ToIface->setTypeParamList(*ToPListOrErr);
4593 else
4594 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004595
Balazs Keri3b30d652018-10-19 13:32:20 +00004596 if (D->isThisDeclarationADefinition())
4597 if (Error Err = ImportDefinition(D, ToIface))
4598 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004599
Douglas Gregor98d156a2010-02-17 16:12:00 +00004600 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004601}
4602
Balazs Keri3b30d652018-10-19 13:32:20 +00004603ExpectedDecl
4604ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4605 ObjCCategoryDecl *Category;
4606 if (Error Err = importInto(Category, D->getCategoryDecl()))
4607 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004608
Douglas Gregor4da9d682010-12-07 15:32:12 +00004609 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4610 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004611 DeclContext *DC, *LexicalDC;
4612 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4613 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004614
Balazs Keri3b30d652018-10-19 13:32:20 +00004615 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4616 if (auto Imp = importSeq(
4617 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4618 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4619 else
4620 return Imp.takeError();
4621
Gabor Marton26f72a92018-07-12 09:42:05 +00004622 if (GetImportedOrCreateDecl(
4623 ToImpl, D, Importer.getToContext(), DC,
4624 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004625 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004626 return ToImpl;
4627
Balazs Keri3b30d652018-10-19 13:32:20 +00004628 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004629 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004630 Category->setImplementation(ToImpl);
4631 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004632
Gabor Marton26f72a92018-07-12 09:42:05 +00004633 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004634 if (Error Err = ImportDeclContext(D))
4635 return std::move(Err);
4636
Douglas Gregor4da9d682010-12-07 15:32:12 +00004637 return ToImpl;
4638}
4639
Balazs Keri3b30d652018-10-19 13:32:20 +00004640ExpectedDecl
4641ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004642 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004643 ObjCInterfaceDecl *Iface;
4644 if (Error Err = importInto(Iface, D->getClassInterface()))
4645 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004646
4647 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004648 ObjCInterfaceDecl *Super;
4649 if (Error Err = importInto(Super, D->getSuperClass()))
4650 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004651
4652 ObjCImplementationDecl *Impl = Iface->getImplementation();
4653 if (!Impl) {
4654 // We haven't imported an implementation yet. Create a new @implementation
4655 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004656 DeclContext *DC, *LexicalDC;
4657 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4658 return std::move(Err);
4659
4660 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4661 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4662 if (auto Imp = importSeq(
4663 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4664 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4665 std::tie(
4666 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4667 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4668 else
4669 return Imp.takeError();
4670
Gabor Marton26f72a92018-07-12 09:42:05 +00004671 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004672 DC, Iface, Super,
4673 ToLocation,
4674 ToAtStartLoc,
4675 ToSuperClassLoc,
4676 ToIvarLBraceLoc,
4677 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004678 return Impl;
4679
Balazs Keri3b30d652018-10-19 13:32:20 +00004680 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004681
Douglas Gregorda8025c2010-12-07 01:26:03 +00004682 // Associate the implementation with the class it implements.
4683 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004684 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004685 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004686 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004687
4688 // Verify that the existing @implementation has the same superclass.
4689 if ((Super && !Impl->getSuperClass()) ||
4690 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004691 (Super && Impl->getSuperClass() &&
4692 !declaresSameEntity(Super->getCanonicalDecl(),
4693 Impl->getSuperClass()))) {
4694 Importer.ToDiag(Impl->getLocation(),
4695 diag::err_odr_objc_superclass_inconsistent)
4696 << Iface->getDeclName();
4697 // FIXME: It would be nice to have the location of the superclass
4698 // below.
4699 if (Impl->getSuperClass())
4700 Importer.ToDiag(Impl->getLocation(),
4701 diag::note_odr_objc_superclass)
4702 << Impl->getSuperClass()->getDeclName();
4703 else
4704 Importer.ToDiag(Impl->getLocation(),
4705 diag::note_odr_objc_missing_superclass);
4706 if (D->getSuperClass())
4707 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004708 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004709 << D->getSuperClass()->getDeclName();
4710 else
4711 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004712 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004713
4714 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004715 }
4716 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004717
Douglas Gregorda8025c2010-12-07 01:26:03 +00004718 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004719 if (Error Err = ImportDeclContext(D))
4720 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004721
4722 return Impl;
4723}
4724
Balazs Keri3b30d652018-10-19 13:32:20 +00004725ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004726 // Import the major distinguishing characteristics of an @property.
4727 DeclContext *DC, *LexicalDC;
4728 DeclarationName Name;
4729 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004730 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004731 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4732 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004733 if (ToD)
4734 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004735
4736 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004737 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004738 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004739 for (auto *FoundDecl : FoundDecls) {
4740 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004741 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004742 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004743 FoundProp->getType())) {
4744 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4745 << Name << D->getType() << FoundProp->getType();
4746 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4747 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004748
4749 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004750 }
4751
4752 // FIXME: Check property attributes, getters, setters, etc.?
4753
4754 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004755 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004756 return FoundProp;
4757 }
4758 }
4759
Balazs Keri3b30d652018-10-19 13:32:20 +00004760 QualType ToType;
4761 TypeSourceInfo *ToTypeSourceInfo;
4762 SourceLocation ToAtLoc, ToLParenLoc;
4763 if (auto Imp = importSeq(
4764 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4765 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4766 else
4767 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004768
4769 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004770 ObjCPropertyDecl *ToProperty;
4771 if (GetImportedOrCreateDecl(
4772 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004773 Name.getAsIdentifierInfo(), ToAtLoc,
4774 ToLParenLoc, ToType,
4775 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004776 return ToProperty;
4777
Balazs Keri3b30d652018-10-19 13:32:20 +00004778 Selector ToGetterName, ToSetterName;
4779 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4780 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4781 ObjCIvarDecl *ToPropertyIvarDecl;
4782 if (auto Imp = importSeq(
4783 D->getGetterName(), D->getSetterName(),
4784 D->getGetterNameLoc(), D->getSetterNameLoc(),
4785 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4786 D->getPropertyIvarDecl()))
4787 std::tie(
4788 ToGetterName, ToSetterName,
4789 ToGetterNameLoc, ToSetterNameLoc,
4790 ToGetterMethodDecl, ToSetterMethodDecl,
4791 ToPropertyIvarDecl) = *Imp;
4792 else
4793 return Imp.takeError();
4794
Douglas Gregora11c4582010-02-17 18:02:10 +00004795 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004796 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004797
4798 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004799 ToProperty->setPropertyAttributesAsWritten(
4800 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004801 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4802 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4803 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4804 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4805 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004806 return ToProperty;
4807}
4808
Balazs Keri3b30d652018-10-19 13:32:20 +00004809ExpectedDecl
4810ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4811 ObjCPropertyDecl *Property;
4812 if (Error Err = importInto(Property, D->getPropertyDecl()))
4813 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004814
Balazs Keri3b30d652018-10-19 13:32:20 +00004815 DeclContext *DC, *LexicalDC;
4816 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4817 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004818
Balazs Keri3b30d652018-10-19 13:32:20 +00004819 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004820
4821 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004822 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004823 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4824 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004825
4826 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004827 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4828 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004829 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004830 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4831 if (auto Imp = importSeq(
4832 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4833 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4834 else
4835 return Imp.takeError();
4836
Gabor Marton26f72a92018-07-12 09:42:05 +00004837 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004838 ToBeginLoc,
4839 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004840 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004841 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004842 return ToImpl;
4843
Douglas Gregor14a49e22010-12-07 18:32:03 +00004844 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004845 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004846 } else {
4847 // Check that we have the same kind of property implementation (@synthesize
4848 // vs. @dynamic).
4849 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004850 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004851 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004852 << Property->getDeclName()
4853 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004854 == ObjCPropertyImplDecl::Dynamic);
4855 Importer.FromDiag(D->getLocation(),
4856 diag::note_odr_objc_property_impl_kind)
4857 << D->getPropertyDecl()->getDeclName()
4858 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004859
4860 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004861 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004862
4863 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004864 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4865 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004866 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004867 diag::err_odr_objc_synthesize_ivar_inconsistent)
4868 << Property->getDeclName()
4869 << ToImpl->getPropertyIvarDecl()->getDeclName()
4870 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004871 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004872 diag::note_odr_objc_synthesize_ivar_here)
4873 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004874
4875 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004876 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004877
Douglas Gregor14a49e22010-12-07 18:32:03 +00004878 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004879 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004880 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004881
Douglas Gregor14a49e22010-12-07 18:32:03 +00004882 return ToImpl;
4883}
4884
Balazs Keri3b30d652018-10-19 13:32:20 +00004885ExpectedDecl
4886ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004887 // For template arguments, we adopt the translation unit as our declaration
4888 // context. This context will be fixed when the actual template declaration
4889 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004890
Douglas Gregora082a492010-11-30 19:14:50 +00004891 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004892
4893 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4894 if (!BeginLocOrErr)
4895 return BeginLocOrErr.takeError();
4896
4897 ExpectedSLoc LocationOrErr = import(D->getLocation());
4898 if (!LocationOrErr)
4899 return LocationOrErr.takeError();
4900
Gabor Marton26f72a92018-07-12 09:42:05 +00004901 TemplateTypeParmDecl *ToD = nullptr;
4902 (void)GetImportedOrCreateDecl(
4903 ToD, D, Importer.getToContext(),
4904 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004905 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004906 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4907 D->wasDeclaredWithTypename(), D->isParameterPack());
4908 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004909}
4910
Balazs Keri3b30d652018-10-19 13:32:20 +00004911ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004912ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004913 DeclarationName ToDeclName;
4914 SourceLocation ToLocation, ToInnerLocStart;
4915 QualType ToType;
4916 TypeSourceInfo *ToTypeSourceInfo;
4917 if (auto Imp = importSeq(
4918 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4919 D->getInnerLocStart()))
4920 std::tie(
4921 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4922 ToInnerLocStart) = *Imp;
4923 else
4924 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004925
Douglas Gregora082a492010-11-30 19:14:50 +00004926 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004927
4928 NonTypeTemplateParmDecl *ToD = nullptr;
4929 (void)GetImportedOrCreateDecl(
4930 ToD, D, Importer.getToContext(),
4931 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004932 ToInnerLocStart, ToLocation, D->getDepth(),
4933 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4934 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004935 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004936}
4937
Balazs Keri3b30d652018-10-19 13:32:20 +00004938ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004939ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4940 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004941 auto NameOrErr = import(D->getDeclName());
4942 if (!NameOrErr)
4943 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004944
Douglas Gregora082a492010-11-30 19:14:50 +00004945 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004946 ExpectedSLoc LocationOrErr = import(D->getLocation());
4947 if (!LocationOrErr)
4948 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004949
Douglas Gregora082a492010-11-30 19:14:50 +00004950 // Import template parameters.
Balazs Keri3b30d652018-10-19 13:32:20 +00004951 auto TemplateParamsOrErr = ImportTemplateParameterList(
4952 D->getTemplateParameters());
4953 if (!TemplateParamsOrErr)
4954 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004955
Douglas Gregora082a492010-11-30 19:14:50 +00004956 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004957
4958 TemplateTemplateParmDecl *ToD = nullptr;
4959 (void)GetImportedOrCreateDecl(
4960 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004961 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4962 D->getDepth(), D->getPosition(), D->isParameterPack(),
4963 (*NameOrErr).getAsIdentifierInfo(),
4964 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004965 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004966}
4967
Gabor Marton9581c332018-05-23 13:53:36 +00004968// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4969// it has any definition in the redecl chain.
4970static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4971 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4972 if (!ToTemplatedDef)
4973 return nullptr;
4974 ClassTemplateDecl *TemplateWithDef =
4975 ToTemplatedDef->getDescribedClassTemplate();
4976 return TemplateWithDef;
4977}
4978
Balazs Keri3b30d652018-10-19 13:32:20 +00004979ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004980 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4981
Douglas Gregora082a492010-11-30 19:14:50 +00004982 // If this record has a definition in the translation unit we're coming from,
4983 // but this particular declaration is not that definition, import the
4984 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004985 auto *Definition =
4986 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Balazs Keri0c23dc52018-08-13 13:08:37 +00004987 if (Definition && Definition != D->getTemplatedDecl() && !IsFriend) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004988 if (ExpectedDecl ImportedDefOrErr = import(
4989 Definition->getDescribedClassTemplate()))
4990 return Importer.MapImported(D, *ImportedDefOrErr);
4991 else
4992 return ImportedDefOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00004993 }
Gabor Marton9581c332018-05-23 13:53:36 +00004994
Douglas Gregora082a492010-11-30 19:14:50 +00004995 // Import the major distinguishing characteristics of this class template.
4996 DeclContext *DC, *LexicalDC;
4997 DeclarationName Name;
4998 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004999 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005000 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5001 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005002 if (ToD)
5003 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00005004
Douglas Gregora082a492010-11-30 19:14:50 +00005005 // We may already have a template of the same name; try to find and match it.
5006 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005007 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005008 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005009 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005010 for (auto *FoundDecl : FoundDecls) {
5011 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00005012 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005013
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005014 Decl *Found = FoundDecl;
5015 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00005016
5017 // The class to be imported is a definition.
5018 if (D->isThisDeclarationADefinition()) {
5019 // Lookup will find the fwd decl only if that is more recent than the
5020 // definition. So, try to get the definition if that is available in
5021 // the redecl chain.
5022 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005023 if (TemplateWithDef)
5024 FoundTemplate = TemplateWithDef;
5025 else
Gabor Marton9581c332018-05-23 13:53:36 +00005026 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005027 }
5028
Douglas Gregora082a492010-11-30 19:14:50 +00005029 if (IsStructuralMatch(D, FoundTemplate)) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005030 if (!IsFriend) {
5031 Importer.MapImported(D->getTemplatedDecl(),
5032 FoundTemplate->getTemplatedDecl());
5033 return Importer.MapImported(D, FoundTemplate);
5034 }
Aleksei Sidorin761c2242018-05-15 11:09:07 +00005035
Balazs Keri0c23dc52018-08-13 13:08:37 +00005036 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005037 }
Douglas Gregora082a492010-11-30 19:14:50 +00005038 }
Gabor Marton9581c332018-05-23 13:53:36 +00005039
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005040 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005041 }
Gabor Marton9581c332018-05-23 13:53:36 +00005042
Douglas Gregora082a492010-11-30 19:14:50 +00005043 if (!ConflictingDecls.empty()) {
5044 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005045 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005046 ConflictingDecls.size());
5047 }
Gabor Marton9581c332018-05-23 13:53:36 +00005048
Douglas Gregora082a492010-11-30 19:14:50 +00005049 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005050 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005051 }
5052
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005053 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5054
Douglas Gregora082a492010-11-30 19:14:50 +00005055 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005056 CXXRecordDecl *ToTemplated;
5057 if (Error Err = importInto(ToTemplated, FromTemplated))
5058 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005059
Douglas Gregora082a492010-11-30 19:14:50 +00005060 // Create the class template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005061 auto TemplateParamsOrErr = ImportTemplateParameterList(
5062 D->getTemplateParameters());
5063 if (!TemplateParamsOrErr)
5064 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005065
Gabor Marton26f72a92018-07-12 09:42:05 +00005066 ClassTemplateDecl *D2;
5067 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005068 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005069 return D2;
5070
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005071 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005072
Balazs Keri0c23dc52018-08-13 13:08:37 +00005073 if (ToTemplated->getPreviousDecl()) {
5074 assert(
5075 ToTemplated->getPreviousDecl()->getDescribedClassTemplate() &&
5076 "Missing described template");
5077 D2->setPreviousDecl(
5078 ToTemplated->getPreviousDecl()->getDescribedClassTemplate());
5079 }
Douglas Gregora082a492010-11-30 19:14:50 +00005080 D2->setAccess(D->getAccess());
5081 D2->setLexicalDeclContext(LexicalDC);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005082 if (!IsFriend)
5083 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005084
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005085 if (FromTemplated->isCompleteDefinition() &&
5086 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005087 // FIXME: Import definition!
5088 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005089
Douglas Gregora082a492010-11-30 19:14:50 +00005090 return D2;
5091}
5092
Balazs Keri3b30d652018-10-19 13:32:20 +00005093ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005094 ClassTemplateSpecializationDecl *D) {
5095 // If this record has a definition in the translation unit we're coming from,
5096 // but this particular declaration is not that definition, import the
5097 // definition and map to that.
5098 TagDecl *Definition = D->getDefinition();
5099 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005100 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5101 return Importer.MapImported(D, *ImportedDefOrErr);
5102 else
5103 return ImportedDefOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005104 }
5105
Balazs Keri3b30d652018-10-19 13:32:20 +00005106 ClassTemplateDecl *ClassTemplate;
5107 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5108 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005109
Douglas Gregore2e50d332010-12-01 01:36:18 +00005110 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005111 DeclContext *DC, *LexicalDC;
5112 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5113 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005114
5115 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005116 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005117 if (Error Err = ImportTemplateArguments(
5118 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5119 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005120
Douglas Gregore2e50d332010-12-01 01:36:18 +00005121 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005122 void *InsertPos = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005123 ClassTemplateSpecializationDecl *D2 = nullptr;
5124 ClassTemplatePartialSpecializationDecl *PartialSpec =
5125 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5126 if (PartialSpec)
5127 D2 = ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
5128 else
5129 D2 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
5130 ClassTemplateSpecializationDecl * const PrevDecl = D2;
5131 RecordDecl *FoundDef = D2 ? D2->getDefinition() : nullptr;
5132 if (FoundDef) {
5133 if (!D->isCompleteDefinition()) {
5134 // The "From" translation unit only had a forward declaration; call it
5135 // the same declaration.
5136 // TODO Handle the redecl chain properly!
5137 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005138 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005139
5140 if (IsStructuralMatch(D, FoundDef)) {
5141
5142 Importer.MapImported(D, FoundDef);
5143
5144 // Import those those default field initializers which have been
5145 // instantiated in the "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005146 for (auto *FromField : D->fields()) {
5147 auto ToOrErr = import(FromField);
5148 if (!ToOrErr)
5149 // FIXME: return the error?
5150 consumeError(ToOrErr.takeError());
5151 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005152
5153 // Import those methods which have been instantiated in the
5154 // "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005155 for (CXXMethodDecl *FromM : D->methods()) {
5156 auto ToOrErr = import(FromM);
5157 if (!ToOrErr)
5158 // FIXME: return the error?
5159 consumeError(ToOrErr.takeError());
5160 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005161
5162 // TODO Import instantiated default arguments.
5163 // TODO Import instantiated exception specifications.
5164 //
5165 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint what
5166 // else could be fused during an AST merge.
5167
5168 return FoundDef;
5169 }
5170 } else { // We either couldn't find any previous specialization in the "To"
5171 // context, or we found one but without definition. Let's create a
5172 // new specialization and register that at the class template.
Balazs Keri3b30d652018-10-19 13:32:20 +00005173
5174 // Import the location of this declaration.
5175 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5176 if (!BeginLocOrErr)
5177 return BeginLocOrErr.takeError();
5178 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5179 if (!IdLocOrErr)
5180 return IdLocOrErr.takeError();
5181
Gabor Marton42e15de2018-08-22 11:52:14 +00005182 if (PartialSpec) {
5183 // Import TemplateArgumentListInfo.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005184 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005185 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Balazs Keri3b30d652018-10-19 13:32:20 +00005186 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5187 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005188
Balazs Keri3b30d652018-10-19 13:32:20 +00005189 QualType CanonInjType;
5190 if (Error Err = importInto(
5191 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5192 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005193 CanonInjType = CanonInjType.getCanonicalType();
5194
Balazs Keri3b30d652018-10-19 13:32:20 +00005195 auto ToTPListOrErr = ImportTemplateParameterList(
5196 PartialSpec->getTemplateParameters());
5197 if (!ToTPListOrErr)
5198 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005199
Gabor Marton26f72a92018-07-12 09:42:05 +00005200 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00005201 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5202 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
Gabor Marton26f72a92018-07-12 09:42:05 +00005203 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
Gabor Marton42e15de2018-08-22 11:52:14 +00005204 ToTAInfo, CanonInjType,
5205 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
Gabor Marton26f72a92018-07-12 09:42:05 +00005206 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005207
Gabor Marton42e15de2018-08-22 11:52:14 +00005208 // Update InsertPos, because preceding import calls may have invalidated
5209 // it by adding new specializations.
5210 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5211 // Add this partial specialization to the class template.
5212 ClassTemplate->AddPartialSpecialization(
5213 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
5214
5215 } else { // Not a partial specialization.
Gabor Marton26f72a92018-07-12 09:42:05 +00005216 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00005217 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5218 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5219 PrevDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00005220 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005221
5222 // Update InsertPos, because preceding import calls may have invalidated
5223 // it by adding new specializations.
5224 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5225 // Add this specialization to the class template.
5226 ClassTemplate->AddSpecialization(D2, InsertPos);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005227 }
5228
Douglas Gregore2e50d332010-12-01 01:36:18 +00005229 D2->setSpecializationKind(D->getSpecializationKind());
5230
Douglas Gregore2e50d332010-12-01 01:36:18 +00005231 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005232 if (auto LocOrErr = import(D->getQualifierLoc()))
5233 D2->setQualifierInfo(*LocOrErr);
5234 else
5235 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005236
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005237 if (auto *TSI = D->getTypeAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005238 if (auto TInfoOrErr = import(TSI))
5239 D2->setTypeAsWritten(*TInfoOrErr);
5240 else
5241 return TInfoOrErr.takeError();
5242
5243 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5244 D2->setTemplateKeywordLoc(*LocOrErr);
5245 else
5246 return LocOrErr.takeError();
5247
5248 if (auto LocOrErr = import(D->getExternLoc()))
5249 D2->setExternLoc(*LocOrErr);
5250 else
5251 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005252 }
5253
Balazs Keri3b30d652018-10-19 13:32:20 +00005254 if (D->getPointOfInstantiation().isValid()) {
5255 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5256 D2->setPointOfInstantiation(*POIOrErr);
5257 else
5258 return POIOrErr.takeError();
5259 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005260
5261 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5262
Gabor Martonb14056b2018-05-25 11:21:24 +00005263 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00005264 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00005265
5266 // Add to the DC only if it was an explicit specialization/instantiation.
5267 if (D2->isExplicitInstantiationOrSpecialization()) {
5268 LexicalDC->addDeclInternal(D2);
5269 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005270 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005271 if (D->isCompleteDefinition())
5272 if (Error Err = ImportDefinition(D, D2))
5273 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005274
Douglas Gregore2e50d332010-12-01 01:36:18 +00005275 return D2;
5276}
5277
Balazs Keri3b30d652018-10-19 13:32:20 +00005278ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005279 // If this variable has a definition in the translation unit we're coming
5280 // from,
5281 // but this particular declaration is not that definition, import the
5282 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005283 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005284 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5285 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005286 if (ExpectedDecl ImportedDefOrErr = import(
5287 Definition->getDescribedVarTemplate()))
5288 return Importer.MapImported(D, *ImportedDefOrErr);
5289 else
5290 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005291 }
5292
5293 // Import the major distinguishing characteristics of this variable template.
5294 DeclContext *DC, *LexicalDC;
5295 DeclarationName Name;
5296 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005297 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005298 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5299 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005300 if (ToD)
5301 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005302
5303 // We may already have a template of the same name; try to find and match it.
5304 assert(!DC->isFunctionOrMethod() &&
5305 "Variable templates cannot be declared at function scope");
5306 SmallVector<NamedDecl *, 4> ConflictingDecls;
5307 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005308 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005309 for (auto *FoundDecl : FoundDecls) {
5310 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005311 continue;
5312
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005313 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005314 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005315 if (IsStructuralMatch(D, FoundTemplate)) {
5316 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005317 Importer.MapImported(D->getTemplatedDecl(),
5318 FoundTemplate->getTemplatedDecl());
5319 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005320 }
5321 }
5322
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005323 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005324 }
5325
5326 if (!ConflictingDecls.empty()) {
5327 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5328 ConflictingDecls.data(),
5329 ConflictingDecls.size());
5330 }
5331
5332 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005333 // FIXME: Is it possible to get other error than name conflict?
5334 // (Put this `if` into the previous `if`?)
5335 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005336
5337 VarDecl *DTemplated = D->getTemplatedDecl();
5338
5339 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005340 // FIXME: Value not used?
5341 ExpectedType TypeOrErr = import(DTemplated->getType());
5342 if (!TypeOrErr)
5343 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005344
5345 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005346 VarDecl *ToTemplated;
5347 if (Error Err = importInto(ToTemplated, DTemplated))
5348 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005349
5350 // Create the variable template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005351 auto TemplateParamsOrErr = ImportTemplateParameterList(
5352 D->getTemplateParameters());
5353 if (!TemplateParamsOrErr)
5354 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005355
Gabor Marton26f72a92018-07-12 09:42:05 +00005356 VarTemplateDecl *ToVarTD;
5357 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005358 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005359 return ToVarTD;
5360
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005361 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005362
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005363 ToVarTD->setAccess(D->getAccess());
5364 ToVarTD->setLexicalDeclContext(LexicalDC);
5365 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005366
Larisse Voufo39a1e502013-08-06 01:03:05 +00005367 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005368 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005369 // FIXME: Import definition!
5370 }
5371
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005372 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005373}
5374
Balazs Keri3b30d652018-10-19 13:32:20 +00005375ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005376 VarTemplateSpecializationDecl *D) {
5377 // If this record has a definition in the translation unit we're coming from,
5378 // but this particular declaration is not that definition, import the
5379 // definition and map to that.
5380 VarDecl *Definition = D->getDefinition();
5381 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005382 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5383 return Importer.MapImported(D, *ImportedDefOrErr);
5384 else
5385 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005386 }
5387
Balazs Keri3b30d652018-10-19 13:32:20 +00005388 VarTemplateDecl *VarTemplate;
5389 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5390 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005391
5392 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005393 DeclContext *DC, *LexicalDC;
5394 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5395 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005396
5397 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005398 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5399 if (!BeginLocOrErr)
5400 return BeginLocOrErr.takeError();
5401
5402 auto IdLocOrErr = import(D->getLocation());
5403 if (!IdLocOrErr)
5404 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005405
5406 // Import template arguments.
5407 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005408 if (Error Err = ImportTemplateArguments(
5409 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5410 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005411
5412 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005413 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005414 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005415 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005416 if (D2) {
5417 // We already have a variable template specialization with these template
5418 // arguments.
5419
5420 // FIXME: Check for specialization vs. instantiation errors.
5421
5422 if (VarDecl *FoundDef = D2->getDefinition()) {
5423 if (!D->isThisDeclarationADefinition() ||
5424 IsStructuralMatch(D, FoundDef)) {
5425 // The record types structurally match, or the "from" translation
5426 // unit only had a forward declaration anyway; call it the same
5427 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005428 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005429 }
5430 }
5431 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005432 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005433 QualType T;
5434 if (Error Err = importInto(T, D->getType()))
5435 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005436
Balazs Keri3b30d652018-10-19 13:32:20 +00005437 auto TInfoOrErr = import(D->getTypeSourceInfo());
5438 if (!TInfoOrErr)
5439 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005440
5441 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005442 if (Error Err = ImportTemplateArgumentListInfo(
5443 D->getTemplateArgsInfo(), ToTAInfo))
5444 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005445
5446 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005447 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005448 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5449 // Import TemplateArgumentListInfo
5450 TemplateArgumentListInfo ArgInfos;
5451 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5452 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005453 if (Error Err = ImportTemplateArgumentListInfo(
5454 *FromTAArgsAsWritten, ArgInfos))
5455 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005456
Balazs Keri3b30d652018-10-19 13:32:20 +00005457 auto ToTPListOrErr = ImportTemplateParameterList(
5458 FromPartial->getTemplateParameters());
5459 if (!ToTPListOrErr)
5460 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005461
Gabor Marton26f72a92018-07-12 09:42:05 +00005462 PartVarSpecDecl *ToPartial;
5463 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005464 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5465 VarTemplate, T, *TInfoOrErr,
5466 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005467 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005468
Balazs Keri3b30d652018-10-19 13:32:20 +00005469 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5470 FromPartial->getInstantiatedFromMember()))
5471 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5472 else
5473 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005474
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005475 if (FromPartial->isMemberSpecialization())
5476 ToPartial->setMemberSpecialization();
5477
5478 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005479
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005480 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005481 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5482 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5483 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005484 D->getStorageClass(), TemplateArgs))
5485 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005486 }
5487
Balazs Keri3b30d652018-10-19 13:32:20 +00005488 if (D->getPointOfInstantiation().isValid()) {
5489 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5490 D2->setPointOfInstantiation(*POIOrErr);
5491 else
5492 return POIOrErr.takeError();
5493 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005494
Larisse Voufo39a1e502013-08-06 01:03:05 +00005495 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005496 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005497
5498 // Add this specialization to the class template.
5499 VarTemplate->AddSpecialization(D2, InsertPos);
5500
5501 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005502 if (auto LocOrErr = import(D->getQualifierLoc()))
5503 D2->setQualifierInfo(*LocOrErr);
5504 else
5505 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005506
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005507 if (D->isConstexpr())
5508 D2->setConstexpr(true);
5509
Larisse Voufo39a1e502013-08-06 01:03:05 +00005510 // Add the specialization to this context.
5511 D2->setLexicalDeclContext(LexicalDC);
5512 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005513
5514 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005515 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005516
Balazs Keri3b30d652018-10-19 13:32:20 +00005517 if (Error Err = ImportInitializer(D, D2))
5518 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005519
5520 return D2;
5521}
5522
Balazs Keri3b30d652018-10-19 13:32:20 +00005523ExpectedDecl
5524ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005525 DeclContext *DC, *LexicalDC;
5526 DeclarationName Name;
5527 SourceLocation Loc;
5528 NamedDecl *ToD;
5529
Balazs Keri3b30d652018-10-19 13:32:20 +00005530 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5531 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005532
5533 if (ToD)
5534 return ToD;
5535
5536 // Try to find a function in our own ("to") context with the same name, same
5537 // type, and in the same context as the function we're importing.
5538 if (!LexicalDC->isFunctionOrMethod()) {
5539 unsigned IDNS = Decl::IDNS_Ordinary;
5540 SmallVector<NamedDecl *, 2> FoundDecls;
5541 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005542 for (auto *FoundDecl : FoundDecls) {
5543 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005544 continue;
5545
Balazs Keri3b30d652018-10-19 13:32:20 +00005546 if (auto *FoundFunction =
5547 dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005548 if (FoundFunction->hasExternalFormalLinkage() &&
5549 D->hasExternalFormalLinkage()) {
5550 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00005551 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005552 // FIXME: Actually try to merge the body and other attributes.
5553 return FoundFunction;
5554 }
5555 }
5556 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005557 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005558 }
5559 }
5560
Balazs Keri3b30d652018-10-19 13:32:20 +00005561 auto ParamsOrErr = ImportTemplateParameterList(
5562 D->getTemplateParameters());
5563 if (!ParamsOrErr)
5564 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005565
Balazs Keri3b30d652018-10-19 13:32:20 +00005566 FunctionDecl *TemplatedFD;
5567 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5568 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005569
Gabor Marton26f72a92018-07-12 09:42:05 +00005570 FunctionTemplateDecl *ToFunc;
5571 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005572 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005573 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005574
5575 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
5576 ToFunc->setAccess(D->getAccess());
5577 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005578
5579 LexicalDC->addDeclInternal(ToFunc);
5580 return ToFunc;
5581}
5582
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005583//----------------------------------------------------------------------------
5584// Import Statements
5585//----------------------------------------------------------------------------
5586
Balazs Keri3b30d652018-10-19 13:32:20 +00005587ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005588 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5589 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005590 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005591}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005592
Balazs Keri3b30d652018-10-19 13:32:20 +00005593
5594ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005595 SmallVector<IdentifierInfo *, 4> Names;
5596 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5597 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005598 // ToII is nullptr when no symbolic name is given for output operand
5599 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005600 Names.push_back(ToII);
5601 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005602
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005603 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5604 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005605 // ToII is nullptr when no symbolic name is given for input operand
5606 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005607 Names.push_back(ToII);
5608 }
5609
5610 SmallVector<StringLiteral *, 4> Clobbers;
5611 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005612 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5613 Clobbers.push_back(*ClobberOrErr);
5614 else
5615 return ClobberOrErr.takeError();
5616
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005617 }
5618
5619 SmallVector<StringLiteral *, 4> Constraints;
5620 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005621 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5622 Constraints.push_back(*OutputOrErr);
5623 else
5624 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005625 }
5626
5627 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005628 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5629 Constraints.push_back(*InputOrErr);
5630 else
5631 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005632 }
5633
5634 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005635 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5636 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005637
Balazs Keri3b30d652018-10-19 13:32:20 +00005638 if (Error Err = ImportArrayChecked(
5639 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5640 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005641
Balazs Keri3b30d652018-10-19 13:32:20 +00005642 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5643 if (!AsmLocOrErr)
5644 return AsmLocOrErr.takeError();
5645 auto AsmStrOrErr = import(S->getAsmString());
5646 if (!AsmStrOrErr)
5647 return AsmStrOrErr.takeError();
5648 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5649 if (!RParenLocOrErr)
5650 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005651
5652 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005653 Importer.getToContext(),
5654 *AsmLocOrErr,
5655 S->isSimple(),
5656 S->isVolatile(),
5657 S->getNumOutputs(),
5658 S->getNumInputs(),
5659 Names.data(),
5660 Constraints.data(),
5661 Exprs.data(),
5662 *AsmStrOrErr,
5663 S->getNumClobbers(),
5664 Clobbers.data(),
5665 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005666}
5667
Balazs Keri3b30d652018-10-19 13:32:20 +00005668ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5669 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5670 if (!Imp)
5671 return Imp.takeError();
5672
5673 DeclGroupRef ToDG;
5674 SourceLocation ToBeginLoc, ToEndLoc;
5675 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5676
5677 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005678}
5679
Balazs Keri3b30d652018-10-19 13:32:20 +00005680ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5681 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5682 if (!ToSemiLocOrErr)
5683 return ToSemiLocOrErr.takeError();
5684 return new (Importer.getToContext()) NullStmt(
5685 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005686}
5687
Balazs Keri3b30d652018-10-19 13:32:20 +00005688ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005689 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005690
Balazs Keri3b30d652018-10-19 13:32:20 +00005691 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5692 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005693
Balazs Keri3b30d652018-10-19 13:32:20 +00005694 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5695 if (!ToLBracLocOrErr)
5696 return ToLBracLocOrErr.takeError();
5697
5698 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5699 if (!ToRBracLocOrErr)
5700 return ToRBracLocOrErr.takeError();
5701
5702 return CompoundStmt::Create(
5703 Importer.getToContext(), ToStmts,
5704 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005705}
5706
Balazs Keri3b30d652018-10-19 13:32:20 +00005707ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5708 auto Imp = importSeq(
5709 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5710 S->getEllipsisLoc(), S->getColonLoc());
5711 if (!Imp)
5712 return Imp.takeError();
5713
5714 Expr *ToLHS, *ToRHS;
5715 Stmt *ToSubStmt;
5716 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5717 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5718 *Imp;
5719
Bruno Ricci5b30571752018-10-28 12:30:53 +00005720 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5721 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005722 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005723
Gabor Horvath480892b2017-10-18 09:25:18 +00005724 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005725}
5726
Balazs Keri3b30d652018-10-19 13:32:20 +00005727ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5728 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5729 if (!Imp)
5730 return Imp.takeError();
5731
5732 SourceLocation ToDefaultLoc, ToColonLoc;
5733 Stmt *ToSubStmt;
5734 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5735
5736 return new (Importer.getToContext()) DefaultStmt(
5737 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005738}
5739
Balazs Keri3b30d652018-10-19 13:32:20 +00005740ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5741 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5742 if (!Imp)
5743 return Imp.takeError();
5744
5745 SourceLocation ToIdentLoc;
5746 LabelDecl *ToLabelDecl;
5747 Stmt *ToSubStmt;
5748 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5749
5750 return new (Importer.getToContext()) LabelStmt(
5751 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005752}
5753
Balazs Keri3b30d652018-10-19 13:32:20 +00005754ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5755 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5756 if (!ToAttrLocOrErr)
5757 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005758 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5759 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005760 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5761 return std::move(Err);
5762 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5763 if (!ToSubStmtOrErr)
5764 return ToSubStmtOrErr.takeError();
5765
5766 return AttributedStmt::Create(
5767 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005768}
5769
Balazs Keri3b30d652018-10-19 13:32:20 +00005770ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5771 auto Imp = importSeq(
5772 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5773 S->getThen(), S->getElseLoc(), S->getElse());
5774 if (!Imp)
5775 return Imp.takeError();
5776
5777 SourceLocation ToIfLoc, ToElseLoc;
5778 Stmt *ToInit, *ToThen, *ToElse;
5779 VarDecl *ToConditionVariable;
5780 Expr *ToCond;
5781 std::tie(
5782 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5783 *Imp;
5784
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005785 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5786 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5787 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005788}
5789
Balazs Keri3b30d652018-10-19 13:32:20 +00005790ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5791 auto Imp = importSeq(
5792 S->getInit(), S->getConditionVariable(), S->getCond(),
5793 S->getBody(), S->getSwitchLoc());
5794 if (!Imp)
5795 return Imp.takeError();
5796
5797 Stmt *ToInit, *ToBody;
5798 VarDecl *ToConditionVariable;
5799 Expr *ToCond;
5800 SourceLocation ToSwitchLoc;
5801 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5802
Bruno Riccie2806f82018-10-29 16:12:37 +00005803 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5804 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005805 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005806 ToStmt->setSwitchLoc(ToSwitchLoc);
5807
Sean Callanan59721b32015-04-28 18:41:46 +00005808 // Now we have to re-chain the cases.
5809 SwitchCase *LastChainedSwitchCase = nullptr;
5810 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5811 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005812 Expected<SwitchCase *> ToSCOrErr = import(SC);
5813 if (!ToSCOrErr)
5814 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005815 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005816 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005817 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005818 ToStmt->setSwitchCaseList(*ToSCOrErr);
5819 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005820 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005821
Sean Callanan59721b32015-04-28 18:41:46 +00005822 return ToStmt;
5823}
5824
Balazs Keri3b30d652018-10-19 13:32:20 +00005825ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5826 auto Imp = importSeq(
5827 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5828 if (!Imp)
5829 return Imp.takeError();
5830
5831 VarDecl *ToConditionVariable;
5832 Expr *ToCond;
5833 Stmt *ToBody;
5834 SourceLocation ToWhileLoc;
5835 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5836
Bruno Riccibacf7512018-10-30 13:42:41 +00005837 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5838 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005839}
5840
Balazs Keri3b30d652018-10-19 13:32:20 +00005841ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5842 auto Imp = importSeq(
5843 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5844 S->getRParenLoc());
5845 if (!Imp)
5846 return Imp.takeError();
5847
5848 Stmt *ToBody;
5849 Expr *ToCond;
5850 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5851 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5852
5853 return new (Importer.getToContext()) DoStmt(
5854 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005855}
5856
Balazs Keri3b30d652018-10-19 13:32:20 +00005857ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5858 auto Imp = importSeq(
5859 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5860 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5861 if (!Imp)
5862 return Imp.takeError();
5863
5864 Stmt *ToInit;
5865 Expr *ToCond, *ToInc;
5866 VarDecl *ToConditionVariable;
5867 Stmt *ToBody;
5868 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5869 std::tie(
5870 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5871 ToLParenLoc, ToRParenLoc) = *Imp;
5872
5873 return new (Importer.getToContext()) ForStmt(
5874 Importer.getToContext(),
5875 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5876 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005877}
5878
Balazs Keri3b30d652018-10-19 13:32:20 +00005879ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5880 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5881 if (!Imp)
5882 return Imp.takeError();
5883
5884 LabelDecl *ToLabel;
5885 SourceLocation ToGotoLoc, ToLabelLoc;
5886 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5887
5888 return new (Importer.getToContext()) GotoStmt(
5889 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005890}
5891
Balazs Keri3b30d652018-10-19 13:32:20 +00005892ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5893 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5894 if (!Imp)
5895 return Imp.takeError();
5896
5897 SourceLocation ToGotoLoc, ToStarLoc;
5898 Expr *ToTarget;
5899 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5900
5901 return new (Importer.getToContext()) IndirectGotoStmt(
5902 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005903}
5904
Balazs Keri3b30d652018-10-19 13:32:20 +00005905ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5906 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5907 if (!ToContinueLocOrErr)
5908 return ToContinueLocOrErr.takeError();
5909 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005910}
5911
Balazs Keri3b30d652018-10-19 13:32:20 +00005912ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5913 auto ToBreakLocOrErr = import(S->getBreakLoc());
5914 if (!ToBreakLocOrErr)
5915 return ToBreakLocOrErr.takeError();
5916 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005917}
5918
Balazs Keri3b30d652018-10-19 13:32:20 +00005919ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5920 auto Imp = importSeq(
5921 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5922 if (!Imp)
5923 return Imp.takeError();
5924
5925 SourceLocation ToReturnLoc;
5926 Expr *ToRetValue;
5927 const VarDecl *ToNRVOCandidate;
5928 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5929
Bruno Ricci023b1d12018-10-30 14:40:49 +00005930 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5931 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005932}
5933
Balazs Keri3b30d652018-10-19 13:32:20 +00005934ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5935 auto Imp = importSeq(
5936 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5937 if (!Imp)
5938 return Imp.takeError();
5939
5940 SourceLocation ToCatchLoc;
5941 VarDecl *ToExceptionDecl;
5942 Stmt *ToHandlerBlock;
5943 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5944
5945 return new (Importer.getToContext()) CXXCatchStmt (
5946 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005947}
5948
Balazs Keri3b30d652018-10-19 13:32:20 +00005949ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5950 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5951 if (!ToTryLocOrErr)
5952 return ToTryLocOrErr.takeError();
5953
5954 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5955 if (!ToTryBlockOrErr)
5956 return ToTryBlockOrErr.takeError();
5957
Sean Callanan59721b32015-04-28 18:41:46 +00005958 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5959 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5960 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005961 if (auto ToHandlerOrErr = import(FromHandler))
5962 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005963 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005964 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005965 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005966
5967 return CXXTryStmt::Create(
5968 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005969}
5970
Balazs Keri3b30d652018-10-19 13:32:20 +00005971ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5972 auto Imp1 = importSeq(
5973 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5974 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5975 if (!Imp1)
5976 return Imp1.takeError();
5977 auto Imp2 = importSeq(
5978 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5979 if (!Imp2)
5980 return Imp2.takeError();
5981
5982 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5983 Expr *ToCond, *ToInc;
5984 Stmt *ToInit, *ToBody;
5985 std::tie(
5986 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5987 ToBody) = *Imp1;
5988 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5989 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5990
5991 return new (Importer.getToContext()) CXXForRangeStmt(
5992 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5993 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005994}
5995
Balazs Keri3b30d652018-10-19 13:32:20 +00005996ExpectedStmt
5997ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5998 auto Imp = importSeq(
5999 S->getElement(), S->getCollection(), S->getBody(),
6000 S->getForLoc(), S->getRParenLoc());
6001 if (!Imp)
6002 return Imp.takeError();
6003
6004 Stmt *ToElement, *ToBody;
6005 Expr *ToCollection;
6006 SourceLocation ToForLoc, ToRParenLoc;
6007 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6008
6009 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6010 ToCollection,
6011 ToBody,
6012 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006013 ToRParenLoc);
6014}
6015
Balazs Keri3b30d652018-10-19 13:32:20 +00006016ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6017 auto Imp = importSeq(
6018 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6019 S->getCatchBody());
6020 if (!Imp)
6021 return Imp.takeError();
6022
6023 SourceLocation ToAtCatchLoc, ToRParenLoc;
6024 VarDecl *ToCatchParamDecl;
6025 Stmt *ToCatchBody;
6026 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6027
6028 return new (Importer.getToContext()) ObjCAtCatchStmt (
6029 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006030}
6031
Balazs Keri3b30d652018-10-19 13:32:20 +00006032ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6033 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6034 if (!ToAtFinallyLocOrErr)
6035 return ToAtFinallyLocOrErr.takeError();
6036 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6037 if (!ToAtFinallyStmtOrErr)
6038 return ToAtFinallyStmtOrErr.takeError();
6039 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6040 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006041}
6042
Balazs Keri3b30d652018-10-19 13:32:20 +00006043ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6044 auto Imp = importSeq(
6045 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6046 if (!Imp)
6047 return Imp.takeError();
6048
6049 SourceLocation ToAtTryLoc;
6050 Stmt *ToTryBody, *ToFinallyStmt;
6051 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6052
Sean Callanan59721b32015-04-28 18:41:46 +00006053 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6054 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6055 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006056 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6057 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006058 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006059 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006060 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006061
Sean Callanan59721b32015-04-28 18:41:46 +00006062 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006063 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006064 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006065 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006066}
6067
Balazs Keri3b30d652018-10-19 13:32:20 +00006068ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006069 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006070 auto Imp = importSeq(
6071 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6072 if (!Imp)
6073 return Imp.takeError();
6074
6075 SourceLocation ToAtSynchronizedLoc;
6076 Expr *ToSynchExpr;
6077 Stmt *ToSynchBody;
6078 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6079
Sean Callanan59721b32015-04-28 18:41:46 +00006080 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6081 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6082}
6083
Balazs Keri3b30d652018-10-19 13:32:20 +00006084ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6085 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6086 if (!ToThrowLocOrErr)
6087 return ToThrowLocOrErr.takeError();
6088 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6089 if (!ToThrowExprOrErr)
6090 return ToThrowExprOrErr.takeError();
6091 return new (Importer.getToContext()) ObjCAtThrowStmt(
6092 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006093}
6094
Balazs Keri3b30d652018-10-19 13:32:20 +00006095ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6096 ObjCAutoreleasePoolStmt *S) {
6097 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6098 if (!ToAtLocOrErr)
6099 return ToAtLocOrErr.takeError();
6100 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6101 if (!ToSubStmtOrErr)
6102 return ToSubStmtOrErr.takeError();
6103 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6104 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006105}
6106
6107//----------------------------------------------------------------------------
6108// Import Expressions
6109//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006110ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006111 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6112 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006113 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006114}
6115
Balazs Keri3b30d652018-10-19 13:32:20 +00006116ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6117 auto Imp = importSeq(
6118 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6119 E->getRParenLoc(), E->getType());
6120 if (!Imp)
6121 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006122
Balazs Keri3b30d652018-10-19 13:32:20 +00006123 SourceLocation ToBuiltinLoc, ToRParenLoc;
6124 Expr *ToSubExpr;
6125 TypeSourceInfo *ToWrittenTypeInfo;
6126 QualType ToType;
6127 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6128 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006129
6130 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006131 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6132 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006133}
6134
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006135
Balazs Keri3b30d652018-10-19 13:32:20 +00006136ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6137 ExpectedType TypeOrErr = import(E->getType());
6138 if (!TypeOrErr)
6139 return TypeOrErr.takeError();
6140
6141 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6142 if (!BeginLocOrErr)
6143 return BeginLocOrErr.takeError();
6144
6145 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006146}
6147
Balazs Keri3b30d652018-10-19 13:32:20 +00006148ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6149 auto Imp = importSeq(
6150 E->getBeginLoc(), E->getType(), E->getFunctionName());
6151 if (!Imp)
6152 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006153
Balazs Keri3b30d652018-10-19 13:32:20 +00006154 SourceLocation ToBeginLoc;
6155 QualType ToType;
6156 StringLiteral *ToFunctionName;
6157 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006158
Bruno Ricci17ff0262018-10-27 19:21:19 +00006159 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6160 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006161}
6162
Balazs Keri3b30d652018-10-19 13:32:20 +00006163ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6164 auto Imp = importSeq(
6165 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6166 E->getLocation(), E->getType());
6167 if (!Imp)
6168 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006169
Balazs Keri3b30d652018-10-19 13:32:20 +00006170 NestedNameSpecifierLoc ToQualifierLoc;
6171 SourceLocation ToTemplateKeywordLoc, ToLocation;
6172 ValueDecl *ToDecl;
6173 QualType ToType;
6174 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6175 *Imp;
6176
6177 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006178 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006179 auto FoundDOrErr = import(E->getFoundDecl());
6180 if (!FoundDOrErr)
6181 return FoundDOrErr.takeError();
6182 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006183 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006184
Aleksei Sidorina693b372016-09-28 10:16:56 +00006185 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006186 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006187 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006188 if (Error Err =
6189 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6190 return std::move(Err);
6191 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006192 }
6193
Balazs Keri3b30d652018-10-19 13:32:20 +00006194 auto *ToE = DeclRefExpr::Create(
6195 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6196 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6197 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006198 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006199 ToE->setHadMultipleCandidates(true);
6200 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006201}
6202
Balazs Keri3b30d652018-10-19 13:32:20 +00006203ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6204 ExpectedType TypeOrErr = import(E->getType());
6205 if (!TypeOrErr)
6206 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006207
Balazs Keri3b30d652018-10-19 13:32:20 +00006208 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006209}
6210
Balazs Keri3b30d652018-10-19 13:32:20 +00006211ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6212 ExpectedExpr ToInitOrErr = import(E->getInit());
6213 if (!ToInitOrErr)
6214 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006215
Balazs Keri3b30d652018-10-19 13:32:20 +00006216 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6217 if (!ToEqualOrColonLocOrErr)
6218 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006219
Balazs Keri3b30d652018-10-19 13:32:20 +00006220 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006221 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006222 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6223 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6224 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006225 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006226 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006227 }
6228
Balazs Keri3b30d652018-10-19 13:32:20 +00006229 SmallVector<Designator, 4> ToDesignators(E->size());
6230 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6231 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006232
6233 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006234 Importer.getToContext(), ToDesignators,
6235 ToIndexExprs, *ToEqualOrColonLocOrErr,
6236 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006237}
6238
Balazs Keri3b30d652018-10-19 13:32:20 +00006239ExpectedStmt
6240ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6241 ExpectedType ToTypeOrErr = import(E->getType());
6242 if (!ToTypeOrErr)
6243 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006244
Balazs Keri3b30d652018-10-19 13:32:20 +00006245 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6246 if (!ToLocationOrErr)
6247 return ToLocationOrErr.takeError();
6248
6249 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6250 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006251}
6252
Balazs Keri3b30d652018-10-19 13:32:20 +00006253ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6254 ExpectedType ToTypeOrErr = import(E->getType());
6255 if (!ToTypeOrErr)
6256 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006257
Balazs Keri3b30d652018-10-19 13:32:20 +00006258 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6259 if (!ToLocationOrErr)
6260 return ToLocationOrErr.takeError();
6261
6262 return IntegerLiteral::Create(
6263 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006264}
6265
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006266
Balazs Keri3b30d652018-10-19 13:32:20 +00006267ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6268 ExpectedType ToTypeOrErr = import(E->getType());
6269 if (!ToTypeOrErr)
6270 return ToTypeOrErr.takeError();
6271
6272 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6273 if (!ToLocationOrErr)
6274 return ToLocationOrErr.takeError();
6275
6276 return FloatingLiteral::Create(
6277 Importer.getToContext(), E->getValue(), E->isExact(),
6278 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006279}
6280
Balazs Keri3b30d652018-10-19 13:32:20 +00006281ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6282 auto ToTypeOrErr = import(E->getType());
6283 if (!ToTypeOrErr)
6284 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006285
Balazs Keri3b30d652018-10-19 13:32:20 +00006286 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6287 if (!ToSubExprOrErr)
6288 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006289
Balazs Keri3b30d652018-10-19 13:32:20 +00006290 return new (Importer.getToContext()) ImaginaryLiteral(
6291 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006292}
6293
Balazs Keri3b30d652018-10-19 13:32:20 +00006294ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6295 ExpectedType ToTypeOrErr = import(E->getType());
6296 if (!ToTypeOrErr)
6297 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006298
Balazs Keri3b30d652018-10-19 13:32:20 +00006299 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6300 if (!ToLocationOrErr)
6301 return ToLocationOrErr.takeError();
6302
6303 return new (Importer.getToContext()) CharacterLiteral(
6304 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006305}
6306
Balazs Keri3b30d652018-10-19 13:32:20 +00006307ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6308 ExpectedType ToTypeOrErr = import(E->getType());
6309 if (!ToTypeOrErr)
6310 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006311
Balazs Keri3b30d652018-10-19 13:32:20 +00006312 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6313 if (Error Err = ImportArrayChecked(
6314 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6315 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006316
Balazs Keri3b30d652018-10-19 13:32:20 +00006317 return StringLiteral::Create(
6318 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6319 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006320}
6321
Balazs Keri3b30d652018-10-19 13:32:20 +00006322ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6323 auto Imp = importSeq(
6324 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6325 E->getInitializer());
6326 if (!Imp)
6327 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006328
Balazs Keri3b30d652018-10-19 13:32:20 +00006329 SourceLocation ToLParenLoc;
6330 TypeSourceInfo *ToTypeSourceInfo;
6331 QualType ToType;
6332 Expr *ToInitializer;
6333 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006334
6335 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006336 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6337 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006338}
6339
Balazs Keri3b30d652018-10-19 13:32:20 +00006340ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6341 auto Imp = importSeq(
6342 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6343 if (!Imp)
6344 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006345
Balazs Keri3b30d652018-10-19 13:32:20 +00006346 SourceLocation ToBuiltinLoc, ToRParenLoc;
6347 QualType ToType;
6348 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6349
6350 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6351 if (Error Err = ImportArrayChecked(
6352 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6353 ToExprs.begin()))
6354 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006355
6356 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006357 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006358}
6359
Balazs Keri3b30d652018-10-19 13:32:20 +00006360ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6361 auto Imp = importSeq(
6362 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6363 if (!Imp)
6364 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006365
Balazs Keri3b30d652018-10-19 13:32:20 +00006366 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6367 LabelDecl *ToLabel;
6368 QualType ToType;
6369 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006370
6371 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006372 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006373}
6374
Bill Wendling8003edc2018-11-09 00:41:36 +00006375ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6376 auto Imp = importSeq(E->getSubExpr());
6377 if (!Imp)
6378 return Imp.takeError();
6379
6380 Expr *ToSubExpr;
6381 std::tie(ToSubExpr) = *Imp;
6382
Nico Weber9f0246d2018-11-21 12:47:43 +00006383 return new (Importer.getToContext()) ConstantExpr(ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006384}
6385
Balazs Keri3b30d652018-10-19 13:32:20 +00006386ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6387 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6388 if (!Imp)
6389 return Imp.takeError();
6390
6391 SourceLocation ToLParen, ToRParen;
6392 Expr *ToSubExpr;
6393 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006394
Fangrui Song6907ce22018-07-30 19:24:48 +00006395 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006396 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006397}
6398
Balazs Keri3b30d652018-10-19 13:32:20 +00006399ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6400 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6401 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6402 return std::move(Err);
6403
6404 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6405 if (!ToLParenLocOrErr)
6406 return ToLParenLocOrErr.takeError();
6407
6408 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6409 if (!ToRParenLocOrErr)
6410 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006411
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006412 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6413 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006414}
6415
Balazs Keri3b30d652018-10-19 13:32:20 +00006416ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6417 auto Imp = importSeq(
6418 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6419 if (!Imp)
6420 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006421
Balazs Keri3b30d652018-10-19 13:32:20 +00006422 CompoundStmt *ToSubStmt;
6423 QualType ToType;
6424 SourceLocation ToLParenLoc, ToRParenLoc;
6425 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006426
Balazs Keri3b30d652018-10-19 13:32:20 +00006427 return new (Importer.getToContext()) StmtExpr(
6428 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006429}
6430
Balazs Keri3b30d652018-10-19 13:32:20 +00006431ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6432 auto Imp = importSeq(
6433 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6434 if (!Imp)
6435 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006436
Balazs Keri3b30d652018-10-19 13:32:20 +00006437 Expr *ToSubExpr;
6438 QualType ToType;
6439 SourceLocation ToOperatorLoc;
6440 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006441
Aaron Ballmana5038552018-01-09 13:07:03 +00006442 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006443 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6444 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006445}
6446
Balazs Keri3b30d652018-10-19 13:32:20 +00006447ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006448ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006449 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6450 if (!Imp)
6451 return Imp.takeError();
6452
6453 QualType ToType;
6454 SourceLocation ToOperatorLoc, ToRParenLoc;
6455 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006456
Douglas Gregord8552cd2010-02-19 01:24:23 +00006457 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006458 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6459 import(E->getArgumentTypeInfo());
6460 if (!ToArgumentTypeInfoOrErr)
6461 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006462
Balazs Keri3b30d652018-10-19 13:32:20 +00006463 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6464 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6465 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006466 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006467
Balazs Keri3b30d652018-10-19 13:32:20 +00006468 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6469 if (!ToArgumentExprOrErr)
6470 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006471
Balazs Keri3b30d652018-10-19 13:32:20 +00006472 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6473 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006474}
6475
Balazs Keri3b30d652018-10-19 13:32:20 +00006476ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6477 auto Imp = importSeq(
6478 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6479 if (!Imp)
6480 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006481
Balazs Keri3b30d652018-10-19 13:32:20 +00006482 Expr *ToLHS, *ToRHS;
6483 QualType ToType;
6484 SourceLocation ToOperatorLoc;
6485 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006486
Balazs Keri3b30d652018-10-19 13:32:20 +00006487 return new (Importer.getToContext()) BinaryOperator(
6488 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6489 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006490}
6491
Balazs Keri3b30d652018-10-19 13:32:20 +00006492ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6493 auto Imp = importSeq(
6494 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6495 E->getRHS(), E->getType());
6496 if (!Imp)
6497 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006498
Balazs Keri3b30d652018-10-19 13:32:20 +00006499 Expr *ToCond, *ToLHS, *ToRHS;
6500 SourceLocation ToQuestionLoc, ToColonLoc;
6501 QualType ToType;
6502 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006503
6504 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006505 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6506 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006507}
6508
Balazs Keri3b30d652018-10-19 13:32:20 +00006509ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006510 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006511 auto Imp = importSeq(
6512 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6513 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6514 if (!Imp)
6515 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006516
Balazs Keri3b30d652018-10-19 13:32:20 +00006517 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6518 OpaqueValueExpr *ToOpaqueValue;
6519 SourceLocation ToQuestionLoc, ToColonLoc;
6520 QualType ToType;
6521 std::tie(
6522 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6523 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006524
6525 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006526 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6527 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6528 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006529}
6530
Balazs Keri3b30d652018-10-19 13:32:20 +00006531ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6532 auto Imp = importSeq(
6533 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6534 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6535 if (!Imp)
6536 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006537
Balazs Keri3b30d652018-10-19 13:32:20 +00006538 SourceLocation ToBeginLoc, ToEndLoc;
6539 TypeSourceInfo *ToQueriedTypeSourceInfo;
6540 Expr *ToDimensionExpression;
6541 QualType ToType;
6542 std::tie(
6543 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6544 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006545
6546 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006547 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6548 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006549}
6550
Balazs Keri3b30d652018-10-19 13:32:20 +00006551ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6552 auto Imp = importSeq(
6553 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6554 if (!Imp)
6555 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006556
Balazs Keri3b30d652018-10-19 13:32:20 +00006557 SourceLocation ToBeginLoc, ToEndLoc;
6558 Expr *ToQueriedExpression;
6559 QualType ToType;
6560 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006561
6562 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006563 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6564 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006565}
6566
Balazs Keri3b30d652018-10-19 13:32:20 +00006567ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6568 auto Imp = importSeq(
6569 E->getLocation(), E->getType(), E->getSourceExpr());
6570 if (!Imp)
6571 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006572
Balazs Keri3b30d652018-10-19 13:32:20 +00006573 SourceLocation ToLocation;
6574 QualType ToType;
6575 Expr *ToSourceExpr;
6576 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006577
6578 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006579 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006580}
6581
Balazs Keri3b30d652018-10-19 13:32:20 +00006582ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6583 auto Imp = importSeq(
6584 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6585 if (!Imp)
6586 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006587
Balazs Keri3b30d652018-10-19 13:32:20 +00006588 Expr *ToLHS, *ToRHS;
6589 SourceLocation ToRBracketLoc;
6590 QualType ToType;
6591 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006592
6593 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006594 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6595 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006596}
6597
Balazs Keri3b30d652018-10-19 13:32:20 +00006598ExpectedStmt
6599ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6600 auto Imp = importSeq(
6601 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6602 E->getComputationResultType(), E->getOperatorLoc());
6603 if (!Imp)
6604 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006605
Balazs Keri3b30d652018-10-19 13:32:20 +00006606 Expr *ToLHS, *ToRHS;
6607 QualType ToType, ToComputationLHSType, ToComputationResultType;
6608 SourceLocation ToOperatorLoc;
6609 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6610 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006611
Balazs Keri3b30d652018-10-19 13:32:20 +00006612 return new (Importer.getToContext()) CompoundAssignOperator(
6613 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6614 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6615 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006616}
6617
Balazs Keri3b30d652018-10-19 13:32:20 +00006618Expected<CXXCastPath>
6619ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6620 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006621 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006622 if (auto SpecOrErr = import(*I))
6623 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006624 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006625 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006626 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006627 return Path;
John McCallcf142162010-08-07 06:22:56 +00006628}
6629
Balazs Keri3b30d652018-10-19 13:32:20 +00006630ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6631 ExpectedType ToTypeOrErr = import(E->getType());
6632 if (!ToTypeOrErr)
6633 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006634
Balazs Keri3b30d652018-10-19 13:32:20 +00006635 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6636 if (!ToSubExprOrErr)
6637 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006638
Balazs Keri3b30d652018-10-19 13:32:20 +00006639 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6640 if (!ToBasePathOrErr)
6641 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006642
Balazs Keri3b30d652018-10-19 13:32:20 +00006643 return ImplicitCastExpr::Create(
6644 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6645 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006646}
6647
Balazs Keri3b30d652018-10-19 13:32:20 +00006648ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6649 auto Imp1 = importSeq(
6650 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6651 if (!Imp1)
6652 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006653
Balazs Keri3b30d652018-10-19 13:32:20 +00006654 QualType ToType;
6655 Expr *ToSubExpr;
6656 TypeSourceInfo *ToTypeInfoAsWritten;
6657 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006658
Balazs Keri3b30d652018-10-19 13:32:20 +00006659 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6660 if (!ToBasePathOrErr)
6661 return ToBasePathOrErr.takeError();
6662 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006663
Aleksei Sidorina693b372016-09-28 10:16:56 +00006664 switch (E->getStmtClass()) {
6665 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006666 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006667 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6668 if (!ToLParenLocOrErr)
6669 return ToLParenLocOrErr.takeError();
6670 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6671 if (!ToRParenLocOrErr)
6672 return ToRParenLocOrErr.takeError();
6673 return CStyleCastExpr::Create(
6674 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6675 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6676 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006677 }
6678
6679 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006680 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006681 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6682 if (!ToLParenLocOrErr)
6683 return ToLParenLocOrErr.takeError();
6684 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6685 if (!ToRParenLocOrErr)
6686 return ToRParenLocOrErr.takeError();
6687 return CXXFunctionalCastExpr::Create(
6688 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6689 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6690 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006691 }
6692
6693 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006694 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6695 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6696 if (!ToLParenLocOrErr)
6697 return ToLParenLocOrErr.takeError();
6698 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6699 if (!ToBridgeKeywordLocOrErr)
6700 return ToBridgeKeywordLocOrErr.takeError();
6701 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6702 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6703 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006704 }
6705 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006706 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006707 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006708 }
6709}
6710
Balazs Keri3b30d652018-10-19 13:32:20 +00006711ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6712 SmallVector<OffsetOfNode, 4> ToNodes;
6713 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6714 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006715
Balazs Keri3b30d652018-10-19 13:32:20 +00006716 SourceLocation ToBeginLoc, ToEndLoc;
6717 if (FromNode.getKind() != OffsetOfNode::Base) {
6718 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6719 if (!Imp)
6720 return Imp.takeError();
6721 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6722 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006723
Balazs Keri3b30d652018-10-19 13:32:20 +00006724 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006725 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006726 ToNodes.push_back(
6727 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006728 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006730 auto ToBSOrErr = import(FromNode.getBase());
6731 if (!ToBSOrErr)
6732 return ToBSOrErr.takeError();
6733 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006734 break;
6735 }
6736 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006737 auto ToFieldOrErr = import(FromNode.getField());
6738 if (!ToFieldOrErr)
6739 return ToFieldOrErr.takeError();
6740 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006741 break;
6742 }
6743 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006744 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6745 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006746 break;
6747 }
6748 }
6749 }
6750
Balazs Keri3b30d652018-10-19 13:32:20 +00006751 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6752 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6753 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6754 if (!ToIndexExprOrErr)
6755 return ToIndexExprOrErr.takeError();
6756 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006757 }
6758
Balazs Keri3b30d652018-10-19 13:32:20 +00006759 auto Imp = importSeq(
6760 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6761 E->getRParenLoc());
6762 if (!Imp)
6763 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006764
Balazs Keri3b30d652018-10-19 13:32:20 +00006765 QualType ToType;
6766 TypeSourceInfo *ToTypeSourceInfo;
6767 SourceLocation ToOperatorLoc, ToRParenLoc;
6768 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6769
6770 return OffsetOfExpr::Create(
6771 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6772 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006773}
6774
Balazs Keri3b30d652018-10-19 13:32:20 +00006775ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6776 auto Imp = importSeq(
6777 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6778 if (!Imp)
6779 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006780
Balazs Keri3b30d652018-10-19 13:32:20 +00006781 QualType ToType;
6782 Expr *ToOperand;
6783 SourceLocation ToBeginLoc, ToEndLoc;
6784 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006785
Balazs Keri3b30d652018-10-19 13:32:20 +00006786 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006787 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006788 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006789 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006790 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006791
Balazs Keri3b30d652018-10-19 13:32:20 +00006792 return new (Importer.getToContext()) CXXNoexceptExpr(
6793 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006794}
6795
Balazs Keri3b30d652018-10-19 13:32:20 +00006796ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6797 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6798 if (!Imp)
6799 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006800
Balazs Keri3b30d652018-10-19 13:32:20 +00006801 Expr *ToSubExpr;
6802 QualType ToType;
6803 SourceLocation ToThrowLoc;
6804 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006805
6806 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006807 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006808}
6809
Balazs Keri3b30d652018-10-19 13:32:20 +00006810ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6811 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6812 if (!ToUsedLocOrErr)
6813 return ToUsedLocOrErr.takeError();
6814
6815 auto ToParamOrErr = import(E->getParam());
6816 if (!ToParamOrErr)
6817 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006818
6819 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006820 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006821}
6822
Balazs Keri3b30d652018-10-19 13:32:20 +00006823ExpectedStmt
6824ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6825 auto Imp = importSeq(
6826 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6827 if (!Imp)
6828 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006829
Balazs Keri3b30d652018-10-19 13:32:20 +00006830 QualType ToType;
6831 TypeSourceInfo *ToTypeSourceInfo;
6832 SourceLocation ToRParenLoc;
6833 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006834
6835 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006836 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006837}
6838
Balazs Keri3b30d652018-10-19 13:32:20 +00006839ExpectedStmt
6840ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6841 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6842 if (!ToSubExprOrErr)
6843 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006844
Balazs Keri3b30d652018-10-19 13:32:20 +00006845 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6846 if (!ToDtorOrErr)
6847 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006848
6849 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006850 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6851 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006852}
6853
Balazs Keri3b30d652018-10-19 13:32:20 +00006854ExpectedStmt
6855ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6856 auto Imp = importSeq(
6857 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6858 E->getParenOrBraceRange());
6859 if (!Imp)
6860 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006861
Balazs Keri3b30d652018-10-19 13:32:20 +00006862 CXXConstructorDecl *ToConstructor;
6863 QualType ToType;
6864 TypeSourceInfo *ToTypeSourceInfo;
6865 SourceRange ToParenOrBraceRange;
6866 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006867
Balazs Keri3b30d652018-10-19 13:32:20 +00006868 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6869 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6870 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006871
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006872 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006873 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6874 ToParenOrBraceRange, E->hadMultipleCandidates(),
6875 E->isListInitialization(), E->isStdInitListInitialization(),
6876 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006877}
6878
Balazs Keri3b30d652018-10-19 13:32:20 +00006879ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006880ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006881 auto Imp = importSeq(
6882 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6883 if (!Imp)
6884 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006885
Balazs Keri3b30d652018-10-19 13:32:20 +00006886 QualType ToType;
6887 Expr *ToTemporaryExpr;
6888 const ValueDecl *ToExtendingDecl;
6889 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006890
6891 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006892 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006893
6894 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006895 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006896 return ToMTE;
6897}
6898
Balazs Keri3b30d652018-10-19 13:32:20 +00006899ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6900 auto Imp = importSeq(
6901 E->getType(), E->getPattern(), E->getEllipsisLoc());
6902 if (!Imp)
6903 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006904
Balazs Keri3b30d652018-10-19 13:32:20 +00006905 QualType ToType;
6906 Expr *ToPattern;
6907 SourceLocation ToEllipsisLoc;
6908 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006909
6910 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006911 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006912}
6913
Balazs Keri3b30d652018-10-19 13:32:20 +00006914ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6915 auto Imp = importSeq(
6916 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6917 if (!Imp)
6918 return Imp.takeError();
6919
6920 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6921 NamedDecl *ToPack;
6922 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006923
6924 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006925 if (!E->isValueDependent())
6926 Length = E->getPackLength();
6927
Balazs Keri3b30d652018-10-19 13:32:20 +00006928 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006929 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006930 if (Error Err = ImportTemplateArguments(
6931 E->getPartialArguments().data(),
6932 E->getPartialArguments().size(),
6933 ToPartialArguments))
6934 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006935 }
6936
6937 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006938 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6939 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006940}
6941
Aleksei Sidorina693b372016-09-28 10:16:56 +00006942
Balazs Keri3b30d652018-10-19 13:32:20 +00006943ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6944 auto Imp = importSeq(
6945 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6946 E->getArraySize(), E->getInitializer(), E->getType(),
6947 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6948 E->getDirectInitRange());
6949 if (!Imp)
6950 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006951
Balazs Keri3b30d652018-10-19 13:32:20 +00006952 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6953 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6954 Expr *ToArraySize, *ToInitializer;
6955 QualType ToType;
6956 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6957 std::tie(
6958 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6959 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006960
Balazs Keri3b30d652018-10-19 13:32:20 +00006961 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6962 if (Error Err =
6963 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6964 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006965
6966 return new (Importer.getToContext()) CXXNewExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006967 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6968 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6969 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6970 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6971 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006972}
6973
Balazs Keri3b30d652018-10-19 13:32:20 +00006974ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6975 auto Imp = importSeq(
6976 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6977 if (!Imp)
6978 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006979
Balazs Keri3b30d652018-10-19 13:32:20 +00006980 QualType ToType;
6981 FunctionDecl *ToOperatorDelete;
6982 Expr *ToArgument;
6983 SourceLocation ToBeginLoc;
6984 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006985
6986 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006987 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6988 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6989 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006990}
6991
Balazs Keri3b30d652018-10-19 13:32:20 +00006992ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6993 auto Imp = importSeq(
6994 E->getType(), E->getLocation(), E->getConstructor(),
6995 E->getParenOrBraceRange());
6996 if (!Imp)
6997 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006998
Balazs Keri3b30d652018-10-19 13:32:20 +00006999 QualType ToType;
7000 SourceLocation ToLocation;
7001 CXXConstructorDecl *ToConstructor;
7002 SourceRange ToParenOrBraceRange;
7003 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007004
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007005 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007006 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7007 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007008
Balazs Keri3b30d652018-10-19 13:32:20 +00007009 return CXXConstructExpr::Create(
7010 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7011 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7012 E->isListInitialization(), E->isStdInitListInitialization(),
7013 E->requiresZeroInitialization(), E->getConstructionKind(),
7014 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007015}
7016
Balazs Keri3b30d652018-10-19 13:32:20 +00007017ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7018 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7019 if (!ToSubExprOrErr)
7020 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007021
Balazs Keri3b30d652018-10-19 13:32:20 +00007022 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7023 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7024 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007025
Balazs Keri3b30d652018-10-19 13:32:20 +00007026 return ExprWithCleanups::Create(
7027 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7028 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007029}
7030
Balazs Keri3b30d652018-10-19 13:32:20 +00007031ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7032 auto Imp = importSeq(
7033 E->getCallee(), E->getType(), E->getRParenLoc());
7034 if (!Imp)
7035 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007036
Balazs Keri3b30d652018-10-19 13:32:20 +00007037 Expr *ToCallee;
7038 QualType ToType;
7039 SourceLocation ToRParenLoc;
7040 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007041
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007042 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007043 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7044 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007045
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007046 return new (Importer.getToContext()) CXXMemberCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007047 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7048 ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007049}
7050
Balazs Keri3b30d652018-10-19 13:32:20 +00007051ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7052 ExpectedType ToTypeOrErr = import(E->getType());
7053 if (!ToTypeOrErr)
7054 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007055
Balazs Keri3b30d652018-10-19 13:32:20 +00007056 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7057 if (!ToLocationOrErr)
7058 return ToLocationOrErr.takeError();
7059
7060 return new (Importer.getToContext()) CXXThisExpr(
7061 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007062}
7063
Balazs Keri3b30d652018-10-19 13:32:20 +00007064ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7065 ExpectedType ToTypeOrErr = import(E->getType());
7066 if (!ToTypeOrErr)
7067 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007068
Balazs Keri3b30d652018-10-19 13:32:20 +00007069 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7070 if (!ToLocationOrErr)
7071 return ToLocationOrErr.takeError();
7072
7073 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7074 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007075}
7076
Balazs Keri3b30d652018-10-19 13:32:20 +00007077ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7078 auto Imp1 = importSeq(
7079 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7080 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7081 if (!Imp1)
7082 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007083
Balazs Keri3b30d652018-10-19 13:32:20 +00007084 Expr *ToBase;
7085 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7086 NestedNameSpecifierLoc ToQualifierLoc;
7087 ValueDecl *ToMemberDecl;
7088 QualType ToType;
7089 std::tie(
7090 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7091 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007092
Balazs Keri3b30d652018-10-19 13:32:20 +00007093 auto Imp2 = importSeq(
7094 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7095 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7096 if (!Imp2)
7097 return Imp2.takeError();
7098 NamedDecl *ToDecl;
7099 DeclarationName ToName;
7100 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7101 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007102
7103 DeclAccessPair ToFoundDecl =
7104 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007105
Balazs Keri3b30d652018-10-19 13:32:20 +00007106 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007107
7108 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007109 // FIXME: handle template arguments
7110 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Sean Callanan59721b32015-04-28 18:41:46 +00007111 }
7112
Balazs Keri3b30d652018-10-19 13:32:20 +00007113 return MemberExpr::Create(
7114 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7115 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
7116 ToMemberNameInfo, nullptr, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007117}
7118
Balazs Keri3b30d652018-10-19 13:32:20 +00007119ExpectedStmt
7120ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7121 auto Imp = importSeq(
7122 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7123 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7124 if (!Imp)
7125 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007126
Balazs Keri3b30d652018-10-19 13:32:20 +00007127 Expr *ToBase;
7128 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7129 NestedNameSpecifierLoc ToQualifierLoc;
7130 TypeSourceInfo *ToScopeTypeInfo;
7131 std::tie(
7132 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7133 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007134
7135 PseudoDestructorTypeStorage Storage;
7136 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7137 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007138 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7139 if (!ToDestroyedTypeLocOrErr)
7140 return ToDestroyedTypeLocOrErr.takeError();
7141 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007142 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007143 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7144 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7145 else
7146 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007147 }
7148
7149 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007150 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7151 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007152}
7153
Balazs Keri3b30d652018-10-19 13:32:20 +00007154ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007155 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007156 auto Imp = importSeq(
7157 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7158 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7159 if (!Imp)
7160 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007161
Balazs Keri3b30d652018-10-19 13:32:20 +00007162 QualType ToType;
7163 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7164 NestedNameSpecifierLoc ToQualifierLoc;
7165 NamedDecl *ToFirstQualifierFoundInScope;
7166 std::tie(
7167 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7168 ToFirstQualifierFoundInScope) = *Imp;
7169
7170 Expr *ToBase = nullptr;
7171 if (!E->isImplicitAccess()) {
7172 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7173 ToBase = *ToBaseOrErr;
7174 else
7175 return ToBaseOrErr.takeError();
7176 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007177
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007178 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007179 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007180 if (Error Err = ImportTemplateArgumentListInfo(
7181 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7182 ToTAInfo))
7183 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007184 ResInfo = &ToTAInfo;
7185 }
7186
Balazs Keri3b30d652018-10-19 13:32:20 +00007187 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7188 if (!ToMemberNameInfoOrErr)
7189 return ToMemberNameInfoOrErr.takeError();
7190 DeclarationNameInfo ToMemberNameInfo(
7191 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007192 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007193 if (Error Err = ImportDeclarationNameLoc(
7194 E->getMemberNameInfo(), ToMemberNameInfo))
7195 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007196
7197 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007198 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7199 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7200 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007201}
7202
Balazs Keri3b30d652018-10-19 13:32:20 +00007203ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007204ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007205 auto Imp = importSeq(
7206 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7207 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7208 if (!Imp)
7209 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007210
Balazs Keri3b30d652018-10-19 13:32:20 +00007211 NestedNameSpecifierLoc ToQualifierLoc;
7212 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7213 DeclarationName ToDeclName;
7214 std::tie(
7215 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7216 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007217
Balazs Keri3b30d652018-10-19 13:32:20 +00007218 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7219 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7220 return std::move(Err);
7221
7222 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007223 TemplateArgumentListInfo *ResInfo = nullptr;
7224 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007225 if (Error Err =
7226 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7227 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007228 ResInfo = &ToTAInfo;
7229 }
7230
7231 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007232 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7233 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007234}
7235
Balazs Keri3b30d652018-10-19 13:32:20 +00007236ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7237 CXXUnresolvedConstructExpr *E) {
7238 auto Imp = importSeq(
7239 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7240 if (!Imp)
7241 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007242
Balazs Keri3b30d652018-10-19 13:32:20 +00007243 SourceLocation ToLParenLoc, ToRParenLoc;
7244 TypeSourceInfo *ToTypeSourceInfo;
7245 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7246
7247 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7248 if (Error Err =
7249 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7250 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007251
7252 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007253 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7254 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007255}
7256
Balazs Keri3b30d652018-10-19 13:32:20 +00007257ExpectedStmt
7258ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7259 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7260 if (!ToNamingClassOrErr)
7261 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007262
Balazs Keri3b30d652018-10-19 13:32:20 +00007263 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7264 if (!ToQualifierLocOrErr)
7265 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007266
Balazs Keri3b30d652018-10-19 13:32:20 +00007267 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7268 if (!ToNameInfoOrErr)
7269 return ToNameInfoOrErr.takeError();
7270 DeclarationNameInfo ToNameInfo(
7271 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007272 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007273 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7274 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007275
7276 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007277 for (auto *D : E->decls())
7278 if (auto ToDOrErr = import(D))
7279 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007280 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007281 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007282
Balazs Keri3b30d652018-10-19 13:32:20 +00007283 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7284 TemplateArgumentListInfo ToTAInfo;
7285 if (Error Err = ImportTemplateArgumentListInfo(
7286 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7287 ToTAInfo))
7288 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007289
Balazs Keri3b30d652018-10-19 13:32:20 +00007290 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7291 if (!ToTemplateKeywordLocOrErr)
7292 return ToTemplateKeywordLocOrErr.takeError();
7293
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007294 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007295 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7296 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7297 ToDecls.begin(), ToDecls.end());
7298 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007299
7300 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007301 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7302 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7303 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007304}
7305
Balazs Keri3b30d652018-10-19 13:32:20 +00007306ExpectedStmt
7307ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7308 auto Imp1 = importSeq(
7309 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7310 E->getTemplateKeywordLoc());
7311 if (!Imp1)
7312 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007313
Balazs Keri3b30d652018-10-19 13:32:20 +00007314 QualType ToType;
7315 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7316 NestedNameSpecifierLoc ToQualifierLoc;
7317 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7318
7319 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7320 if (!Imp2)
7321 return Imp2.takeError();
7322 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7323 // Import additional name location/type info.
7324 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7325 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007326
7327 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007328 for (Decl *D : E->decls())
7329 if (auto ToDOrErr = import(D))
7330 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007331 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007332 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007333
7334 TemplateArgumentListInfo ToTAInfo;
7335 TemplateArgumentListInfo *ResInfo = nullptr;
7336 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007337 if (Error Err =
7338 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7339 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007340 ResInfo = &ToTAInfo;
7341 }
7342
Balazs Keri3b30d652018-10-19 13:32:20 +00007343 Expr *ToBase = nullptr;
7344 if (!E->isImplicitAccess()) {
7345 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7346 ToBase = *ToBaseOrErr;
7347 else
7348 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007349 }
7350
7351 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007352 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7353 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7354 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007355}
7356
Balazs Keri3b30d652018-10-19 13:32:20 +00007357ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7358 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7359 if (!Imp)
7360 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007361
Balazs Keri3b30d652018-10-19 13:32:20 +00007362 Expr *ToCallee;
7363 QualType ToType;
7364 SourceLocation ToRParenLoc;
7365 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007366
7367 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007368 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7369 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7370 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007371
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007372 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
7373 return new (Importer.getToContext()) CXXOperatorCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007374 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
7375 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007376 }
7377
Balazs Keri3b30d652018-10-19 13:32:20 +00007378 return new (Importer.getToContext()) CallExpr(
7379 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7380 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007381}
7382
Balazs Keri3b30d652018-10-19 13:32:20 +00007383ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7384 CXXRecordDecl *FromClass = E->getLambdaClass();
7385 auto ToClassOrErr = import(FromClass);
7386 if (!ToClassOrErr)
7387 return ToClassOrErr.takeError();
7388 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007389
7390 // NOTE: lambda classes are created with BeingDefined flag set up.
7391 // It means that ImportDefinition doesn't work for them and we should fill it
7392 // manually.
7393 if (ToClass->isBeingDefined()) {
7394 for (auto FromField : FromClass->fields()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007395 auto ToFieldOrErr = import(FromField);
7396 if (!ToFieldOrErr)
7397 return ToFieldOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007398 }
7399 }
7400
Balazs Keri3b30d652018-10-19 13:32:20 +00007401 auto ToCallOpOrErr = import(E->getCallOperator());
7402 if (!ToCallOpOrErr)
7403 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007404
7405 ToClass->completeDefinition();
7406
Balazs Keri3b30d652018-10-19 13:32:20 +00007407 SmallVector<LambdaCapture, 8> ToCaptures;
7408 ToCaptures.reserve(E->capture_size());
7409 for (const auto &FromCapture : E->captures()) {
7410 if (auto ToCaptureOrErr = import(FromCapture))
7411 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007412 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007413 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007414 }
7415
Balazs Keri3b30d652018-10-19 13:32:20 +00007416 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7417 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7418 return std::move(Err);
7419
7420 auto Imp = importSeq(
7421 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7422 if (!Imp)
7423 return Imp.takeError();
7424
7425 SourceRange ToIntroducerRange;
7426 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7427 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007428
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007429 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007430 Importer.getToContext(), ToClass, ToIntroducerRange,
7431 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7432 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7433 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007434}
7435
Sean Callanan8bca9962016-03-28 21:43:01 +00007436
Balazs Keri3b30d652018-10-19 13:32:20 +00007437ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7438 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7439 if (!Imp)
7440 return Imp.takeError();
7441
7442 SourceLocation ToLBraceLoc, ToRBraceLoc;
7443 QualType ToType;
7444 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7445
7446 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7447 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7448 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007449
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007450 ASTContext &ToCtx = Importer.getToContext();
7451 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007452 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7453 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007454
Balazs Keri3b30d652018-10-19 13:32:20 +00007455 if (E->hasArrayFiller()) {
7456 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7457 To->setArrayFiller(*ToFillerOrErr);
7458 else
7459 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007460 }
7461
Balazs Keri3b30d652018-10-19 13:32:20 +00007462 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7463 if (auto ToFDOrErr = import(FromFD))
7464 To->setInitializedFieldInUnion(*ToFDOrErr);
7465 else
7466 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007467 }
7468
Balazs Keri3b30d652018-10-19 13:32:20 +00007469 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7470 if (auto ToSyntFormOrErr = import(SyntForm))
7471 To->setSyntacticForm(*ToSyntFormOrErr);
7472 else
7473 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007474 }
7475
Gabor Martona20ce602018-09-03 13:10:53 +00007476 // Copy InitListExprBitfields, which are not handled in the ctor of
7477 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007478 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007479
7480 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007481}
7482
Balazs Keri3b30d652018-10-19 13:32:20 +00007483ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007484 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007485 ExpectedType ToTypeOrErr = import(E->getType());
7486 if (!ToTypeOrErr)
7487 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007488
Balazs Keri3b30d652018-10-19 13:32:20 +00007489 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7490 if (!ToSubExprOrErr)
7491 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007492
Balazs Keri3b30d652018-10-19 13:32:20 +00007493 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7494 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007495}
7496
Balazs Keri3b30d652018-10-19 13:32:20 +00007497ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007498 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007499 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7500 if (!Imp)
7501 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007502
Balazs Keri3b30d652018-10-19 13:32:20 +00007503 SourceLocation ToLocation;
7504 QualType ToType;
7505 CXXConstructorDecl *ToConstructor;
7506 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007507
7508 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007509 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7510 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007511}
7512
Balazs Keri3b30d652018-10-19 13:32:20 +00007513ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7514 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7515 if (!Imp)
7516 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007517
Balazs Keri3b30d652018-10-19 13:32:20 +00007518 QualType ToType;
7519 Expr *ToCommonExpr, *ToSubExpr;
7520 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007521
Balazs Keri3b30d652018-10-19 13:32:20 +00007522 return new (Importer.getToContext()) ArrayInitLoopExpr(
7523 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007524}
7525
Balazs Keri3b30d652018-10-19 13:32:20 +00007526ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7527 ExpectedType ToTypeOrErr = import(E->getType());
7528 if (!ToTypeOrErr)
7529 return ToTypeOrErr.takeError();
7530 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007531}
7532
Balazs Keri3b30d652018-10-19 13:32:20 +00007533ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7534 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7535 if (!ToBeginLocOrErr)
7536 return ToBeginLocOrErr.takeError();
7537
7538 auto ToFieldOrErr = import(E->getField());
7539 if (!ToFieldOrErr)
7540 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007541
7542 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007543 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007544}
7545
Balazs Keri3b30d652018-10-19 13:32:20 +00007546ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7547 auto Imp = importSeq(
7548 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7549 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7550 if (!Imp)
7551 return Imp.takeError();
7552
7553 QualType ToType;
7554 Expr *ToSubExpr;
7555 TypeSourceInfo *ToTypeInfoAsWritten;
7556 SourceLocation ToOperatorLoc, ToRParenLoc;
7557 SourceRange ToAngleBrackets;
7558 std::tie(
7559 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7560 ToAngleBrackets) = *Imp;
7561
Sean Callanandd2c1742016-05-16 20:48:03 +00007562 ExprValueKind VK = E->getValueKind();
7563 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007564 auto ToBasePathOrErr = ImportCastPath(E);
7565 if (!ToBasePathOrErr)
7566 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007567
Sean Callanandd2c1742016-05-16 20:48:03 +00007568 if (isa<CXXStaticCastExpr>(E)) {
7569 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007570 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7571 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007572 } else if (isa<CXXDynamicCastExpr>(E)) {
7573 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007574 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7575 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007576 } else if (isa<CXXReinterpretCastExpr>(E)) {
7577 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007578 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7579 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007580 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007581 return CXXConstCastExpr::Create(
7582 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7583 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007584 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007585 llvm_unreachable("Unknown cast type");
7586 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007587 }
7588}
7589
Balazs Keri3b30d652018-10-19 13:32:20 +00007590ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007591 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007592 auto Imp = importSeq(
7593 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7594 if (!Imp)
7595 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007596
Balazs Keri3b30d652018-10-19 13:32:20 +00007597 QualType ToType;
7598 SourceLocation ToExprLoc;
7599 NonTypeTemplateParmDecl *ToParameter;
7600 Expr *ToReplacement;
7601 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007602
7603 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007604 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007605}
7606
Balazs Keri3b30d652018-10-19 13:32:20 +00007607ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7608 auto Imp = importSeq(
7609 E->getType(), E->getBeginLoc(), E->getEndLoc());
7610 if (!Imp)
7611 return Imp.takeError();
7612
7613 QualType ToType;
7614 SourceLocation ToBeginLoc, ToEndLoc;
7615 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007616
7617 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007618 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7619 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007620
7621 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7622 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007623 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007624
7625 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007626 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7627 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007628}
7629
Balazs Keri3b30d652018-10-19 13:32:20 +00007630ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7631 ExpectedType ToTypeOrErr = import(E->getType());
7632 if (!ToTypeOrErr)
7633 return ToTypeOrErr.takeError();
7634
7635 auto ToSourceRangeOrErr = import(E->getSourceRange());
7636 if (!ToSourceRangeOrErr)
7637 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007638
7639 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007640 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7641 return new (Importer.getToContext()) CXXTypeidExpr(
7642 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7643 else
7644 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007645 }
7646
Balazs Keri3b30d652018-10-19 13:32:20 +00007647 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7648 if (!ToExprOperandOrErr)
7649 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007650
Balazs Keri3b30d652018-10-19 13:32:20 +00007651 return new (Importer.getToContext()) CXXTypeidExpr(
7652 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007653}
7654
Lang Hames19e07e12017-06-20 21:06:00 +00007655void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7656 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007657 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7658 if (auto ImportedOrErr = import(FromOverriddenMethod))
7659 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7660 (*ImportedOrErr)->getCanonicalDecl()));
7661 else
7662 consumeError(ImportedOrErr.takeError());
7663 }
Lang Hames19e07e12017-06-20 21:06:00 +00007664}
7665
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007666ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007667 ASTContext &FromContext, FileManager &FromFileManager,
7668 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007669 : ToContext(ToContext), FromContext(FromContext),
7670 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7671 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007672 ImportedDecls[FromContext.getTranslationUnitDecl()]
7673 = ToContext.getTranslationUnitDecl();
7674}
7675
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007676ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007677
7678QualType ASTImporter::Import(QualType FromT) {
7679 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007680 return {};
John McCall424cec92011-01-19 06:33:43 +00007681
Balazs Keri3b30d652018-10-19 13:32:20 +00007682 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007683
7684 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007685 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007686 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007687 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007688 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007689
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007690 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007691 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007692 ExpectedType ToTOrErr = Importer.Visit(FromTy);
7693 if (!ToTOrErr) {
7694 llvm::consumeError(ToTOrErr.takeError());
7695 return {};
7696 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007697
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007698 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007699 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007700
Balazs Keri3b30d652018-10-19 13:32:20 +00007701 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007702}
7703
Douglas Gregor62d311f2010-02-09 19:21:46 +00007704TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007705 if (!FromTSI)
7706 return FromTSI;
7707
7708 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007709 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007710 QualType T = Import(FromTSI->getType());
7711 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007712 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007713
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007714 return ToContext.getTrivialTypeSourceInfo(
7715 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007716}
7717
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007718Attr *ASTImporter::Import(const Attr *FromAttr) {
7719 Attr *ToAttr = FromAttr->clone(ToContext);
7720 ToAttr->setRange(Import(FromAttr->getRange()));
7721 return ToAttr;
7722}
7723
Sean Callanan59721b32015-04-28 18:41:46 +00007724Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7725 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7726 if (Pos != ImportedDecls.end()) {
7727 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007728 // FIXME: move this call to ImportDeclParts().
Balazs Keri3b30d652018-10-19 13:32:20 +00007729 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD))
7730 llvm::consumeError(std::move(Err));
Sean Callanan59721b32015-04-28 18:41:46 +00007731 return ToD;
7732 } else {
7733 return nullptr;
7734 }
7735}
7736
Douglas Gregor62d311f2010-02-09 19:21:46 +00007737Decl *ASTImporter::Import(Decl *FromD) {
7738 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007739 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007740
Douglas Gregord451ea92011-07-29 23:31:30 +00007741 ASTNodeImporter Importer(*this);
7742
Gabor Marton26f72a92018-07-12 09:42:05 +00007743 // Check whether we've already imported this declaration.
7744 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7745 if (ToD) {
7746 // If FromD has some updated flags after last import, apply it
7747 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007748 return ToD;
7749 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007750
7751 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007752 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
7753 if (!ToDOrErr) {
7754 llvm::consumeError(ToDOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007755 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007756 }
7757 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007758
Gabor Marton26f72a92018-07-12 09:42:05 +00007759 // Notify subclasses.
7760 Imported(FromD, ToD);
7761
Gabor Martonac3a5d62018-09-17 12:04:52 +00007762 updateFlags(FromD, ToD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007763 return ToD;
7764}
7765
Balazs Keri3b30d652018-10-19 13:32:20 +00007766Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007767 if (!FromDC)
7768 return FromDC;
7769
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007770 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007771 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007772 return nullptr;
7773
Fangrui Song6907ce22018-07-30 19:24:48 +00007774 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007775 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007776 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7777 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007778 if (ToRecord->isCompleteDefinition()) {
7779 // Do nothing.
7780 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007781 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7782 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7783 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007784 } else {
7785 CompleteDecl(ToRecord);
7786 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007787 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7788 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007789 if (ToEnum->isCompleteDefinition()) {
7790 // Do nothing.
7791 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007792 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7793 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7794 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007795 } else {
7796 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007797 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007798 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7799 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007800 if (ToClass->getDefinition()) {
7801 // Do nothing.
7802 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007803 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7804 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7805 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007806 } else {
7807 CompleteDecl(ToClass);
7808 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007809 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7810 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007811 if (ToProto->getDefinition()) {
7812 // Do nothing.
7813 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007814 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7815 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7816 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007817 } else {
7818 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007819 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007820 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007821
Douglas Gregor95d82832012-01-24 18:36:04 +00007822 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007823}
7824
7825Expr *ASTImporter::Import(Expr *FromE) {
7826 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007827 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007828
7829 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7830}
7831
7832Stmt *ASTImporter::Import(Stmt *FromS) {
7833 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007834 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007835
Fangrui Song6907ce22018-07-30 19:24:48 +00007836 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007837 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7838 if (Pos != ImportedStmts.end())
7839 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007840
Balazs Keri3b30d652018-10-19 13:32:20 +00007841 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007842 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007843 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
7844 if (!ToSOrErr) {
7845 llvm::consumeError(ToSOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007846 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007847 }
Craig Topper36250ad2014-05-12 05:36:57 +00007848
Balazs Keri3b30d652018-10-19 13:32:20 +00007849 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007850 auto *FromE = cast<Expr>(FromS);
7851 // Copy ExprBitfields, which may not be handled in Expr subclasses
7852 // constructors.
7853 ToE->setValueKind(FromE->getValueKind());
7854 ToE->setObjectKind(FromE->getObjectKind());
7855 ToE->setTypeDependent(FromE->isTypeDependent());
7856 ToE->setValueDependent(FromE->isValueDependent());
7857 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7858 ToE->setContainsUnexpandedParameterPack(
7859 FromE->containsUnexpandedParameterPack());
7860 }
7861
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007862 // Record the imported declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007863 ImportedStmts[FromS] = *ToSOrErr;
7864 return *ToSOrErr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007865}
7866
7867NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7868 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007869 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007870
Douglas Gregor90ebf252011-04-27 16:48:40 +00007871 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7872
7873 switch (FromNNS->getKind()) {
7874 case NestedNameSpecifier::Identifier:
7875 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7876 return NestedNameSpecifier::Create(ToContext, prefix, II);
7877 }
Craig Topper36250ad2014-05-12 05:36:57 +00007878 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007879
7880 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007881 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007882 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007883 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7884 }
Craig Topper36250ad2014-05-12 05:36:57 +00007885 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007886
7887 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007888 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007889 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007890 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7891 }
Craig Topper36250ad2014-05-12 05:36:57 +00007892 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007893
7894 case NestedNameSpecifier::Global:
7895 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7896
Nikola Smiljanic67860242014-09-26 00:28:20 +00007897 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007898 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007899 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007900 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7901 }
7902 return nullptr;
7903
Douglas Gregor90ebf252011-04-27 16:48:40 +00007904 case NestedNameSpecifier::TypeSpec:
7905 case NestedNameSpecifier::TypeSpecWithTemplate: {
7906 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7907 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007908 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007909 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007910 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007911 bTemplate, T.getTypePtr());
7912 }
7913 }
Craig Topper36250ad2014-05-12 05:36:57 +00007914 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007915 }
7916
7917 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007918}
7919
Douglas Gregor14454802011-02-25 02:25:35 +00007920NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007921 // Copied from NestedNameSpecifier mostly.
7922 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7923 NestedNameSpecifierLoc NNS = FromNNS;
7924
7925 // Push each of the nested-name-specifiers's onto a stack for
7926 // serialization in reverse order.
7927 while (NNS) {
7928 NestedNames.push_back(NNS);
7929 NNS = NNS.getPrefix();
7930 }
7931
7932 NestedNameSpecifierLocBuilder Builder;
7933
7934 while (!NestedNames.empty()) {
7935 NNS = NestedNames.pop_back_val();
7936 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7937 if (!Spec)
7938 return NestedNameSpecifierLoc();
7939
7940 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7941 switch (Kind) {
7942 case NestedNameSpecifier::Identifier:
7943 Builder.Extend(getToContext(),
7944 Spec->getAsIdentifier(),
7945 Import(NNS.getLocalBeginLoc()),
7946 Import(NNS.getLocalEndLoc()));
7947 break;
7948
7949 case NestedNameSpecifier::Namespace:
7950 Builder.Extend(getToContext(),
7951 Spec->getAsNamespace(),
7952 Import(NNS.getLocalBeginLoc()),
7953 Import(NNS.getLocalEndLoc()));
7954 break;
7955
7956 case NestedNameSpecifier::NamespaceAlias:
7957 Builder.Extend(getToContext(),
7958 Spec->getAsNamespaceAlias(),
7959 Import(NNS.getLocalBeginLoc()),
7960 Import(NNS.getLocalEndLoc()));
7961 break;
7962
7963 case NestedNameSpecifier::TypeSpec:
7964 case NestedNameSpecifier::TypeSpecWithTemplate: {
7965 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7966 QualType(Spec->getAsType(), 0));
7967 Builder.Extend(getToContext(),
7968 Import(NNS.getLocalBeginLoc()),
7969 TSI->getTypeLoc(),
7970 Import(NNS.getLocalEndLoc()));
7971 break;
7972 }
7973
7974 case NestedNameSpecifier::Global:
7975 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7976 break;
7977
7978 case NestedNameSpecifier::Super: {
7979 SourceRange ToRange = Import(NNS.getSourceRange());
7980 Builder.MakeSuper(getToContext(),
7981 Spec->getAsRecordDecl(),
7982 ToRange.getBegin(),
7983 ToRange.getEnd());
7984 }
7985 }
7986 }
7987
7988 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007989}
7990
Douglas Gregore2e50d332010-12-01 01:36:18 +00007991TemplateName ASTImporter::Import(TemplateName From) {
7992 switch (From.getKind()) {
7993 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007994 if (auto *ToTemplate =
7995 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007996 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007997
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007998 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007999
Douglas Gregore2e50d332010-12-01 01:36:18 +00008000 case TemplateName::OverloadedTemplate: {
8001 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8002 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008003 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008004 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00008005 ToTemplates.addDecl(To);
8006 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008007 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008008 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008009 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008010 ToTemplates.end());
8011 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008012
Douglas Gregore2e50d332010-12-01 01:36:18 +00008013 case TemplateName::QualifiedTemplate: {
8014 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
8015 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
8016 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008017 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008018
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008019 if (auto *ToTemplate =
8020 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00008021 return ToContext.getQualifiedTemplateName(Qualifier,
8022 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008023 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008024
8025 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008026 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008027
Douglas Gregore2e50d332010-12-01 01:36:18 +00008028 case TemplateName::DependentTemplate: {
8029 DependentTemplateName *DTN = From.getAsDependentTemplateName();
8030 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
8031 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008032 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008033
Douglas Gregore2e50d332010-12-01 01:36:18 +00008034 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008035 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008036 Import(DTN->getIdentifier()));
8037 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008038
Douglas Gregore2e50d332010-12-01 01:36:18 +00008039 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
8040 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008041
8042 case TemplateName::SubstTemplateTemplateParm: {
8043 SubstTemplateTemplateParmStorage *subst
8044 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008045 auto *param =
8046 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00008047 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008048 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00008049
8050 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008051 if (replacement.isNull())
8052 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008053
John McCalld9dfe3a2011-06-30 08:33:18 +00008054 return ToContext.getSubstTemplateTemplateParm(param, replacement);
8055 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008056
Douglas Gregor5590be02011-01-15 06:45:20 +00008057 case TemplateName::SubstTemplateTemplateParmPack: {
8058 SubstTemplateTemplateParmPackStorage *SubstPack
8059 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008060 auto *Param =
8061 cast_or_null<TemplateTemplateParmDecl>(
8062 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00008063 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008064 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008065
Douglas Gregor5590be02011-01-15 06:45:20 +00008066 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008067 Expected<TemplateArgument> ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00008068 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
Balazs Keri3b30d652018-10-19 13:32:20 +00008069 if (!ArgPack) {
8070 llvm::consumeError(ArgPack.takeError());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008071 return {};
Balazs Keri3b30d652018-10-19 13:32:20 +00008072 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008073
Balazs Keri3b30d652018-10-19 13:32:20 +00008074 return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00008075 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008076 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008077
Douglas Gregore2e50d332010-12-01 01:36:18 +00008078 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008079}
8080
Douglas Gregor62d311f2010-02-09 19:21:46 +00008081SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
8082 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008083 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008084
Douglas Gregor811663e2010-02-10 00:15:17 +00008085 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008086
Douglas Gregor811663e2010-02-10 00:15:17 +00008087 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00008088 FileID ToFileID = Import(Decomposed.first);
8089 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008090 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008091 SourceManager &ToSM = ToContext.getSourceManager();
8092 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00008093}
8094
8095SourceRange ASTImporter::Import(SourceRange FromRange) {
8096 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
8097}
8098
Douglas Gregor811663e2010-02-10 00:15:17 +00008099FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008100 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008101 if (Pos != ImportedFileIDs.end())
8102 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008103
Douglas Gregor811663e2010-02-10 00:15:17 +00008104 SourceManager &FromSM = FromContext.getSourceManager();
8105 SourceManager &ToSM = ToContext.getSourceManager();
8106 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008107
8108 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008109 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008110 if (FromSLoc.isExpansion()) {
8111 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
8112 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
8113 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
8114 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8115 SourceLocation MLoc;
8116 if (FromEx.isMacroArgExpansion()) {
8117 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
8118 } else {
8119 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
8120 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
8121 FromEx.isExpansionTokenRange());
8122 }
8123 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008124 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008125 // Include location of this file.
8126 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
8127
8128 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
8129 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8130 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8131 // disk again
8132 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8133 // than mmap the files several times.
8134 const FileEntry *Entry =
8135 ToFileManager.getFile(Cache->OrigEntry->getName());
8136 if (!Entry)
8137 return {};
8138 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
8139 FromSLoc.getFile().getFileCharacteristic());
8140 } else {
8141 // FIXME: We want to re-use the existing MemoryBuffer!
8142 const llvm::MemoryBuffer *FromBuf =
8143 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
8144 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8145 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8146 FromBuf->getBufferIdentifier());
8147 ToID = ToSM.createFileID(std::move(ToBuf),
8148 FromSLoc.getFile().getFileCharacteristic());
8149 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008150 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008151
Sebastian Redl99219f12010-09-30 01:03:06 +00008152 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008153 return ToID;
8154}
8155
Sean Callanandd2c1742016-05-16 20:48:03 +00008156CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8157 Expr *ToExpr = Import(From->getInit());
8158 if (!ToExpr && From->getInit())
8159 return nullptr;
8160
8161 if (From->isBaseInitializer()) {
8162 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8163 if (!ToTInfo && From->getTypeSourceInfo())
8164 return nullptr;
8165
8166 return new (ToContext) CXXCtorInitializer(
8167 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
8168 ToExpr, Import(From->getRParenLoc()),
8169 From->isPackExpansion() ? Import(From->getEllipsisLoc())
8170 : SourceLocation());
8171 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008172 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008173 if (!ToField && From->getMember())
8174 return nullptr;
8175
8176 return new (ToContext) CXXCtorInitializer(
8177 ToContext, ToField, Import(From->getMemberLocation()),
8178 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8179 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008180 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00008181 Import(From->getIndirectMember()));
8182 if (!ToIField && From->getIndirectMember())
8183 return nullptr;
8184
8185 return new (ToContext) CXXCtorInitializer(
8186 ToContext, ToIField, Import(From->getMemberLocation()),
8187 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8188 } else if (From->isDelegatingInitializer()) {
8189 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8190 if (!ToTInfo && From->getTypeSourceInfo())
8191 return nullptr;
8192
8193 return new (ToContext)
8194 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
8195 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008196 } else {
8197 return nullptr;
8198 }
8199}
8200
Aleksei Sidorina693b372016-09-28 10:16:56 +00008201CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
8202 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8203 if (Pos != ImportedCXXBaseSpecifiers.end())
8204 return Pos->second;
8205
8206 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
8207 Import(BaseSpec->getSourceRange()),
8208 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8209 BaseSpec->getAccessSpecifierAsWritten(),
8210 Import(BaseSpec->getTypeSourceInfo()),
8211 Import(BaseSpec->getEllipsisLoc()));
8212 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8213 return Imported;
8214}
8215
Balazs Keri3b30d652018-10-19 13:32:20 +00008216Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008217 Decl *To = Import(From);
8218 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008219 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008220
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008221 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008222 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008223
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008224 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00008225 if (!ToRecord->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008226 return Importer.ImportDefinition(
8227 cast<RecordDecl>(FromDC), ToRecord,
8228 ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008229 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00008230 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008231
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008232 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00008233 if (!ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008234 return Importer.ImportDefinition(
8235 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008236 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008237 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008238
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008239 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008240 if (!ToIFace->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008241 return Importer.ImportDefinition(
8242 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8243 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008244 }
8245 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008246
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008247 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008248 if (!ToProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008249 return Importer.ImportDefinition(
8250 cast<ObjCProtocolDecl>(FromDC), ToProto,
8251 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008252 }
8253 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008254
Balazs Keri3b30d652018-10-19 13:32:20 +00008255 return Importer.ImportDeclContext(FromDC, true);
Douglas Gregor0a791672011-01-18 03:11:38 +00008256 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008257
8258 return Error::success();
8259}
8260
8261void ASTImporter::ImportDefinition(Decl *From) {
8262 Error Err = ImportDefinition_New(From);
8263 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008264}
8265
Douglas Gregor96e578d2010-02-05 17:54:41 +00008266DeclarationName ASTImporter::Import(DeclarationName FromName) {
8267 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008268 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008269
8270 switch (FromName.getNameKind()) {
8271 case DeclarationName::Identifier:
8272 return Import(FromName.getAsIdentifierInfo());
8273
8274 case DeclarationName::ObjCZeroArgSelector:
8275 case DeclarationName::ObjCOneArgSelector:
8276 case DeclarationName::ObjCMultiArgSelector:
8277 return Import(FromName.getObjCSelector());
8278
8279 case DeclarationName::CXXConstructorName: {
8280 QualType T = Import(FromName.getCXXNameType());
8281 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008282 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008283
8284 return ToContext.DeclarationNames.getCXXConstructorName(
8285 ToContext.getCanonicalType(T));
8286 }
8287
8288 case DeclarationName::CXXDestructorName: {
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.getCXXDestructorName(
8294 ToContext.getCanonicalType(T));
8295 }
8296
Richard Smith35845152017-02-07 01:37:30 +00008297 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008298 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00008299 Import(FromName.getCXXDeductionGuideTemplate()));
8300 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008301 return {};
Richard Smith35845152017-02-07 01:37:30 +00008302 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
8303 }
8304
Douglas Gregor96e578d2010-02-05 17:54:41 +00008305 case DeclarationName::CXXConversionFunctionName: {
8306 QualType T = Import(FromName.getCXXNameType());
8307 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008308 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008309
8310 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8311 ToContext.getCanonicalType(T));
8312 }
8313
8314 case DeclarationName::CXXOperatorName:
8315 return ToContext.DeclarationNames.getCXXOperatorName(
8316 FromName.getCXXOverloadedOperator());
8317
8318 case DeclarationName::CXXLiteralOperatorName:
8319 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
8320 Import(FromName.getCXXLiteralIdentifier()));
8321
8322 case DeclarationName::CXXUsingDirective:
8323 // FIXME: STATICS!
8324 return DeclarationName::getUsingDirectiveName();
8325 }
8326
David Blaikiee4d798f2012-01-20 21:50:17 +00008327 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008328}
8329
Douglas Gregore2e50d332010-12-01 01:36:18 +00008330IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008331 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008332 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008333
Sean Callananf94ef1d2016-05-14 06:11:19 +00008334 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8335
8336 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8337 ToId->setBuiltinID(FromId->getBuiltinID());
8338
8339 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008340}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008341
Douglas Gregor43f54792010-02-17 02:12:47 +00008342Selector ASTImporter::Import(Selector FromSel) {
8343 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008344 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00008345
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008346 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008347 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8348 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8349 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8350 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8351}
8352
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008353DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8354 DeclContext *DC,
8355 unsigned IDNS,
8356 NamedDecl **Decls,
8357 unsigned NumDecls) {
8358 return Name;
8359}
8360
8361DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008362 if (LastDiagFromFrom)
8363 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8364 FromContext.getDiagnostics());
8365 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008366 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008367}
8368
8369DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008370 if (!LastDiagFromFrom)
8371 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8372 ToContext.getDiagnostics());
8373 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008374 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008375}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008376
Douglas Gregor2e15c842012-02-01 21:00:38 +00008377void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008378 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008379 if (!ID->getDefinition())
8380 ID->startDefinition();
8381 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008382 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008383 if (!PD->getDefinition())
8384 PD->startDefinition();
8385 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008386 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008387 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8388 TD->startDefinition();
8389 TD->setCompleteDefinition(true);
8390 }
8391 }
8392 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008393 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008394 }
8395}
8396
Gabor Marton26f72a92018-07-12 09:42:05 +00008397Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8398 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8399 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8400 "Try to import an already imported Decl");
8401 if (Pos != ImportedDecls.end())
8402 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008403 ImportedDecls[From] = To;
8404 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008405}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008406
Douglas Gregordd6006f2012-07-17 21:16:27 +00008407bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8408 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00008409 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00008410 = ImportedTypes.find(From.getTypePtr());
8411 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8412 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008413
Douglas Gregordd6006f2012-07-17 21:16:27 +00008414 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008415 getStructuralEquivalenceKind(*this), false,
8416 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008417 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008418}