blob: 200214b2445b4cb6324717acf040e0377f9801f3 [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";
92 default:
93 llvm_unreachable("Invalid error code.");
Balazs Keri2544b4b2018-08-08 09:40:57 +000094 }
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
125 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000126 }
127
Gabor Marton26f72a92018-07-12 09:42:05 +0000128 void updateFlags(const Decl *From, Decl *To) {
129 // Check if some flags or attrs are new in 'From' and copy into 'To'.
130 // FIXME: Other flags or attrs?
131 if (From->isUsed(false) && !To->isUsed(false))
132 To->setIsUsed();
133 }
134
Balazs Keri3b30d652018-10-19 13:32:20 +0000135 Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
136 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
137 "Try to get field index for non-field.");
138
139 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
140 if (!Owner)
141 return None;
142
143 unsigned Index = 0;
144 for (const auto *D : Owner->decls()) {
145 if (D == F)
146 return Index;
147
148 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
149 ++Index;
150 }
151
152 llvm_unreachable("Field was not found in its parent context.");
153
154 return None;
155 }
156
157 // FIXME: Temporary until every import returns Expected.
158 template <>
159 LLVM_NODISCARD Error
160 ASTImporter::importInto(SourceLocation &To, const SourceLocation &From) {
161 To = Import(From);
162 if (From.isValid() && To.isInvalid())
163 return llvm::make_error<ImportError>();
164 return Error::success();
165 }
166 // FIXME: Temporary until every import returns Expected.
167 template <>
168 LLVM_NODISCARD Error
169 ASTImporter::importInto(QualType &To, const QualType &From) {
170 To = Import(From);
171 if (!From.isNull() && To.isNull())
172 return llvm::make_error<ImportError>();
173 return Error::success();
174 }
175
176 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
177 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
178 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000180
Balazs Keri3b30d652018-10-19 13:32:20 +0000181 // Use this instead of Importer.importInto .
182 template <typename ImportT>
183 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
184 return Importer.importInto(To, From);
185 }
186
187 // Use this to import pointers of specific type.
188 template <typename ImportT>
189 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
190 auto ToI = Importer.Import(From);
191 if (!ToI && From)
192 return make_error<ImportError>();
193 To = cast_or_null<ImportT>(ToI);
194 return Error::success();
195 // FIXME: This should be the final code.
196 //auto ToOrErr = Importer.Import(From);
197 //if (ToOrErr) {
198 // To = cast_or_null<ImportT>(*ToOrErr);
199 //}
200 //return ToOrErr.takeError();
201 }
202
203 // Call the import function of ASTImporter for a baseclass of type `T` and
204 // cast the return value to `T`.
205 template <typename T>
206 Expected<T *> import(T *From) {
207 auto *To = Importer.Import(From);
208 if (!To && From)
209 return make_error<ImportError>();
210 return cast_or_null<T>(To);
211 // FIXME: This should be the final code.
212 //auto ToOrErr = Importer.Import(From);
213 //if (!ToOrErr)
214 // return ToOrErr.takeError();
215 //return cast_or_null<T>(*ToOrErr);
216 }
217
218 template <typename T>
219 Expected<T *> import(const T *From) {
220 return import(const_cast<T *>(From));
221 }
222
223 // Call the import function of ASTImporter for type `T`.
224 template <typename T>
225 Expected<T> import(const T &From) {
226 T To = Importer.Import(From);
227 T DefaultT;
228 if (To == DefaultT && !(From == DefaultT))
229 return make_error<ImportError>();
230 return To;
231 // FIXME: This should be the final code.
232 //return Importer.Import(From);
233 }
234
235 template <class T>
236 Expected<std::tuple<T>>
237 importSeq(const T &From) {
238 Expected<T> ToOrErr = import(From);
239 if (!ToOrErr)
240 return ToOrErr.takeError();
241 return std::make_tuple<T>(std::move(*ToOrErr));
242 }
243
244 // Import multiple objects with a single function call.
245 // This should work for every type for which a variant of `import` exists.
246 // The arguments are processed from left to right and import is stopped on
247 // first error.
248 template <class THead, class... TTail>
249 Expected<std::tuple<THead, TTail...>>
250 importSeq(const THead &FromHead, const TTail &...FromTail) {
251 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
252 if (!ToHeadOrErr)
253 return ToHeadOrErr.takeError();
254 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
255 if (!ToTailOrErr)
256 return ToTailOrErr.takeError();
257 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
258 }
259
260// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 template <typename ToDeclT> struct CallOverloadedCreateFun {
262 template <typename... Args>
263 auto operator()(Args &&... args)
264 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
265 return ToDeclT::Create(std::forward<Args>(args)...);
266 }
267 };
268
269 // Always use these functions to create a Decl during import. There are
270 // certain tasks which must be done after the Decl was created, e.g. we
271 // must immediately register that as an imported Decl. The parameter `ToD`
272 // will be set to the newly created Decl or if had been imported before
273 // then to the already imported Decl. Returns a bool value set to true if
274 // the `FromD` had been imported before.
275 template <typename ToDeclT, typename FromDeclT, typename... Args>
276 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
277 Args &&... args) {
278 // There may be several overloads of ToDeclT::Create. We must make sure
279 // to call the one which would be chosen by the arguments, thus we use a
280 // wrapper for the overload set.
281 CallOverloadedCreateFun<ToDeclT> OC;
282 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
283 std::forward<Args>(args)...);
284 }
285 // Use this overload if a special Type is needed to be created. E.g if we
286 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
287 // then:
288 // TypedefNameDecl *ToTypedef;
289 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
290 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
291 typename... Args>
292 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
293 Args &&... args) {
294 CallOverloadedCreateFun<NewDeclT> OC;
295 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
296 std::forward<Args>(args)...);
297 }
298 // Use this version if a special create function must be
299 // used, e.g. CXXRecordDecl::CreateLambda .
300 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
301 typename... Args>
302 LLVM_NODISCARD bool
303 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
304 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000305 // FIXME: This code is needed later.
306 //if (Importer.getImportDeclErrorIfAny(FromD)) {
307 // ToD = nullptr;
308 // return true; // Already imported but with error.
309 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000310 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
311 if (ToD)
312 return true; // Already imported.
313 ToD = CreateFun(std::forward<Args>(args)...);
314 InitializeImportedDecl(FromD, ToD);
315 return false; // A new Decl is created.
316 }
317
318 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
319 Importer.MapImported(FromD, ToD);
320 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
321 if (FromD->hasAttrs())
322 for (const Attr *FromAttr : FromD->getAttrs())
323 ToD->addAttr(Importer.Import(FromAttr));
324 if (FromD->isUsed())
325 ToD->setIsUsed();
326 if (FromD->isImplicit())
327 ToD->setImplicit();
328 }
329
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000331 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000332
Balazs Keri3b30d652018-10-19 13:32:20 +0000333 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
334 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
335 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336
337 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000338 ExpectedType VisitType(const Type *T);
339 ExpectedType VisitAtomicType(const AtomicType *T);
340 ExpectedType VisitBuiltinType(const BuiltinType *T);
341 ExpectedType VisitDecayedType(const DecayedType *T);
342 ExpectedType VisitComplexType(const ComplexType *T);
343 ExpectedType VisitPointerType(const PointerType *T);
344 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
345 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
346 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
347 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
348 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
349 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
350 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
351 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000352 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000353 ExpectedType VisitVectorType(const VectorType *T);
354 ExpectedType VisitExtVectorType(const ExtVectorType *T);
355 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
356 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
357 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
358 ExpectedType VisitParenType(const ParenType *T);
359 ExpectedType VisitTypedefType(const TypedefType *T);
360 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000361 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000362 ExpectedType VisitTypeOfType(const TypeOfType *T);
363 ExpectedType VisitDecltypeType(const DecltypeType *T);
364 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
365 ExpectedType VisitAutoType(const AutoType *T);
366 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000368 ExpectedType VisitRecordType(const RecordType *T);
369 ExpectedType VisitEnumType(const EnumType *T);
370 ExpectedType VisitAttributedType(const AttributedType *T);
371 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
372 ExpectedType VisitSubstTemplateTypeParmType(
373 const SubstTemplateTypeParmType *T);
374 ExpectedType VisitTemplateSpecializationType(
375 const TemplateSpecializationType *T);
376 ExpectedType VisitElaboratedType(const ElaboratedType *T);
377 ExpectedType VisitDependentNameType(const DependentNameType *T);
378 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
379 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000380 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
382 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
383 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000384
385 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000386 Error ImportDeclParts(
387 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
388 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
389 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
390 Error ImportDeclarationNameLoc(
391 const DeclarationNameInfo &From, DeclarationNameInfo &To);
392 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
393 Error ImportDeclContext(
394 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
395 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000396
Balazs Keri3b30d652018-10-19 13:32:20 +0000397 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000398
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000399 using Designator = DesignatedInitExpr::Designator;
400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000401 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000402 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000403 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000404 /// nothing (if minimal import is set) or might be everything (if minimal
405 /// import is not set).
406 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000407 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000408 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000410 /// DeclContext.
411 IDK_Basic
412 };
413
Douglas Gregor2e15c842012-02-01 21:00:38 +0000414 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
415 return IDK == IDK_Everything ||
416 (IDK == IDK_Default && !Importer.isMinimalImport());
417 }
418
Balazs Keri3b30d652018-10-19 13:32:20 +0000419 Error ImportInitializer(VarDecl *From, VarDecl *To);
420 Error ImportDefinition(
421 RecordDecl *From, RecordDecl *To,
422 ImportDefinitionKind Kind = IDK_Default);
423 Error ImportDefinition(
424 EnumDecl *From, EnumDecl *To,
425 ImportDefinitionKind Kind = IDK_Default);
426 Error ImportDefinition(
427 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
428 ImportDefinitionKind Kind = IDK_Default);
429 Error ImportDefinition(
430 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
431 ImportDefinitionKind Kind = IDK_Default);
432 Expected<TemplateParameterList *> ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000433 TemplateParameterList *Params);
Balazs Keri3b30d652018-10-19 13:32:20 +0000434 Error ImportTemplateArguments(
435 const TemplateArgument *FromArgs, unsigned NumFromArgs,
436 SmallVectorImpl<TemplateArgument> &ToArgs);
437 Expected<TemplateArgument>
438 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000439
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000440 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000441 Error ImportTemplateArgumentListInfo(
442 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000443
444 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000445 Error ImportTemplateArgumentListInfo(
446 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
447 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000448
Gabor Marton5254e642018-06-27 13:32:50 +0000449 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000450 using FunctionTemplateAndArgsTy =
451 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
452 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000453 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
454 FunctionDecl *FromFD);
455
Balazs Keri3b30d652018-10-19 13:32:20 +0000456 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000457
Gabor Marton950fb572018-07-17 12:39:27 +0000458 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000459 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
460 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000461 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
462 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000463 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000464 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000465 bool IsStructuralMatch(FunctionTemplateDecl *From,
466 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000467 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000468 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000469 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000470 ExpectedDecl VisitDecl(Decl *D);
471 ExpectedDecl VisitImportDecl(ImportDecl *D);
472 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
473 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
474 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
475 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
476 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
477 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
479 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
480 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
481 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
482 ExpectedDecl VisitLabelDecl(LabelDecl *D);
483 ExpectedDecl VisitEnumDecl(EnumDecl *D);
484 ExpectedDecl VisitRecordDecl(RecordDecl *D);
485 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
486 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
487 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
488 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
489 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
490 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
491 ExpectedDecl VisitFieldDecl(FieldDecl *D);
492 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
493 ExpectedDecl VisitFriendDecl(FriendDecl *D);
494 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
495 ExpectedDecl VisitVarDecl(VarDecl *D);
496 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
497 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
498 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
499 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
500 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
501 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
502 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
503 ExpectedDecl VisitUsingDecl(UsingDecl *D);
504 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
505 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
506 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
507 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000508
Balazs Keri3b30d652018-10-19 13:32:20 +0000509 Expected<ObjCTypeParamList *>
510 ImportObjCTypeParamList(ObjCTypeParamList *list);
511
512 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
513 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
514 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
515 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
516 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
517 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
518 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
519 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
520 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
521 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000522 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000523 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
524 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
525 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000526
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000527 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000528 ExpectedStmt VisitStmt(Stmt *S);
529 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
530 ExpectedStmt VisitDeclStmt(DeclStmt *S);
531 ExpectedStmt VisitNullStmt(NullStmt *S);
532 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
533 ExpectedStmt VisitCaseStmt(CaseStmt *S);
534 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
535 ExpectedStmt VisitLabelStmt(LabelStmt *S);
536 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
537 ExpectedStmt VisitIfStmt(IfStmt *S);
538 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
539 ExpectedStmt VisitWhileStmt(WhileStmt *S);
540 ExpectedStmt VisitDoStmt(DoStmt *S);
541 ExpectedStmt VisitForStmt(ForStmt *S);
542 ExpectedStmt VisitGotoStmt(GotoStmt *S);
543 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
544 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
545 ExpectedStmt VisitBreakStmt(BreakStmt *S);
546 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000547 // FIXME: MSAsmStmt
548 // FIXME: SEHExceptStmt
549 // FIXME: SEHFinallyStmt
550 // FIXME: SEHTryStmt
551 // FIXME: SEHLeaveStmt
552 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000553 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
554 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
555 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000556 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000557 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
558 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
559 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
560 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
561 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
562 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
563 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000564
565 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000566 ExpectedStmt VisitExpr(Expr *E);
567 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
568 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
569 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
570 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
571 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
572 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
573 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
574 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
575 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
576 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
577 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
578 ExpectedStmt VisitStringLiteral(StringLiteral *E);
579 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
580 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
581 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
582 ExpectedStmt VisitParenExpr(ParenExpr *E);
583 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
584 ExpectedStmt VisitStmtExpr(StmtExpr *E);
585 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
586 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
587 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
588 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
589 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
590 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
591 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
592 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
593 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
594 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
595 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
596 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
597 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
598 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
599 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
600 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
601 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
602 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
603 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
604 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
605 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
606 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
607 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
608 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
609 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
610 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
611 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
612 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
613 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
614 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
615 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
616 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
617 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
618 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
619 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
620 ExpectedStmt VisitMemberExpr(MemberExpr *E);
621 ExpectedStmt VisitCallExpr(CallExpr *E);
622 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
623 ExpectedStmt VisitInitListExpr(InitListExpr *E);
624 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
625 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
626 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
627 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
628 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
629 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
630 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
631 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
632 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000633
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000634 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000635 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000636 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000637 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
638 Expected<ItemT> ToOrErr = import(*Ibegin);
639 if (!ToOrErr)
640 return ToOrErr.takeError();
641 *Obegin = *ToOrErr;
642 }
643 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000644 }
645
Balazs Keri3b30d652018-10-19 13:32:20 +0000646 // Import every item from a container structure into an output container.
647 // If error occurs, stops at first error and returns the error.
648 // The output container should have space for all needed elements (it is not
649 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000650 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000651 Error ImportContainerChecked(
652 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
653 return ImportArrayChecked(
654 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000655 }
656
657 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000658 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000659 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
660 }
Lang Hames19e07e12017-06-20 21:06:00 +0000661
Lang Hames19e07e12017-06-20 21:06:00 +0000662 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000663
Balazs Keri3b30d652018-10-19 13:32:20 +0000664 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
665 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000666 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000667
Balazs Keri3b30d652018-10-19 13:32:20 +0000668// FIXME: Temporary until every import returns Expected.
669template <>
670Expected<TemplateName> ASTNodeImporter::import(const TemplateName &From) {
671 TemplateName To = Importer.Import(From);
672 if (To.isNull() && !From.isNull())
673 return make_error<ImportError>();
674 return To;
675}
676
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000677template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000678Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000679 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
680 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000681 auto ToLAngleLocOrErr = import(FromLAngleLoc);
682 if (!ToLAngleLocOrErr)
683 return ToLAngleLocOrErr.takeError();
684 auto ToRAngleLocOrErr = import(FromRAngleLoc);
685 if (!ToRAngleLocOrErr)
686 return ToRAngleLocOrErr.takeError();
687
688 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
689 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
690 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000691 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000692 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000693}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000694
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000695template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000696Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000697 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
698 return ImportTemplateArgumentListInfo(
699 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
700}
701
702template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000703Error ASTNodeImporter::ImportTemplateArgumentListInfo<
704 ASTTemplateArgumentListInfo>(
705 const ASTTemplateArgumentListInfo &From,
706 TemplateArgumentListInfo &Result) {
707 return ImportTemplateArgumentListInfo(
708 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000709}
710
Balazs Keri3b30d652018-10-19 13:32:20 +0000711Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000712ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
713 FunctionDecl *FromFD) {
714 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000715 FunctionDecl::TK_FunctionTemplateSpecialization);
716
717 FunctionTemplateAndArgsTy Result;
718
Gabor Marton5254e642018-06-27 13:32:50 +0000719 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000720 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
721 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000722
723 // Import template arguments.
724 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000725 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
726 std::get<1>(Result)))
727 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000728
Balazs Keri3b30d652018-10-19 13:32:20 +0000729 return Result;
730}
731
732template <>
733Expected<TemplateParameterList *>
734ASTNodeImporter::import(TemplateParameterList *From) {
735 SmallVector<NamedDecl *, 4> To(From->size());
736 if (Error Err = ImportContainerChecked(*From, To))
737 return std::move(Err);
738
739 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
740 if (!ToRequiresClause)
741 return ToRequiresClause.takeError();
742
743 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
744 if (!ToTemplateLocOrErr)
745 return ToTemplateLocOrErr.takeError();
746 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
747 if (!ToLAngleLocOrErr)
748 return ToLAngleLocOrErr.takeError();
749 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
750 if (!ToRAngleLocOrErr)
751 return ToRAngleLocOrErr.takeError();
752
753 return TemplateParameterList::Create(
754 Importer.getToContext(),
755 *ToTemplateLocOrErr,
756 *ToLAngleLocOrErr,
757 To,
758 *ToRAngleLocOrErr,
759 *ToRequiresClause);
760}
761
762template <>
763Expected<TemplateArgument>
764ASTNodeImporter::import(const TemplateArgument &From) {
765 switch (From.getKind()) {
766 case TemplateArgument::Null:
767 return TemplateArgument();
768
769 case TemplateArgument::Type: {
770 ExpectedType ToTypeOrErr = import(From.getAsType());
771 if (!ToTypeOrErr)
772 return ToTypeOrErr.takeError();
773 return TemplateArgument(*ToTypeOrErr);
774 }
775
776 case TemplateArgument::Integral: {
777 ExpectedType ToTypeOrErr = import(From.getIntegralType());
778 if (!ToTypeOrErr)
779 return ToTypeOrErr.takeError();
780 return TemplateArgument(From, *ToTypeOrErr);
781 }
782
783 case TemplateArgument::Declaration: {
784 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
785 if (!ToOrErr)
786 return ToOrErr.takeError();
787 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
788 if (!ToTypeOrErr)
789 return ToTypeOrErr.takeError();
790 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
791 }
792
793 case TemplateArgument::NullPtr: {
794 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
795 if (!ToTypeOrErr)
796 return ToTypeOrErr.takeError();
797 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
798 }
799
800 case TemplateArgument::Template: {
801 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
802 if (!ToTemplateOrErr)
803 return ToTemplateOrErr.takeError();
804
805 return TemplateArgument(*ToTemplateOrErr);
806 }
807
808 case TemplateArgument::TemplateExpansion: {
809 Expected<TemplateName> ToTemplateOrErr =
810 import(From.getAsTemplateOrTemplatePattern());
811 if (!ToTemplateOrErr)
812 return ToTemplateOrErr.takeError();
813
814 return TemplateArgument(
815 *ToTemplateOrErr, From.getNumTemplateExpansions());
816 }
817
818 case TemplateArgument::Expression:
819 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
820 return TemplateArgument(*ToExpr);
821 else
822 return ToExpr.takeError();
823
824 case TemplateArgument::Pack: {
825 SmallVector<TemplateArgument, 2> ToPack;
826 ToPack.reserve(From.pack_size());
827 if (Error Err = ImportTemplateArguments(
828 From.pack_begin(), From.pack_size(), ToPack))
829 return std::move(Err);
830
831 return TemplateArgument(
832 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
833 }
834 }
835
836 llvm_unreachable("Invalid template argument kind");
837}
838
839template <>
840Expected<TemplateArgumentLoc>
841ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
842 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
843 if (!ArgOrErr)
844 return ArgOrErr.takeError();
845 TemplateArgument Arg = *ArgOrErr;
846
847 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
848
849 TemplateArgumentLocInfo ToInfo;
850 if (Arg.getKind() == TemplateArgument::Expression) {
851 ExpectedExpr E = import(FromInfo.getAsExpr());
852 if (!E)
853 return E.takeError();
854 ToInfo = TemplateArgumentLocInfo(*E);
855 } else if (Arg.getKind() == TemplateArgument::Type) {
856 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
857 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
858 else
859 return TSIOrErr.takeError();
860 } else {
861 auto ToTemplateQualifierLocOrErr =
862 import(FromInfo.getTemplateQualifierLoc());
863 if (!ToTemplateQualifierLocOrErr)
864 return ToTemplateQualifierLocOrErr.takeError();
865 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
866 if (!ToTemplateNameLocOrErr)
867 return ToTemplateNameLocOrErr.takeError();
868 auto ToTemplateEllipsisLocOrErr =
869 import(FromInfo.getTemplateEllipsisLoc());
870 if (!ToTemplateEllipsisLocOrErr)
871 return ToTemplateEllipsisLocOrErr.takeError();
872
873 ToInfo = TemplateArgumentLocInfo(
874 *ToTemplateQualifierLocOrErr,
875 *ToTemplateNameLocOrErr,
876 *ToTemplateEllipsisLocOrErr);
877 }
878
879 return TemplateArgumentLoc(Arg, ToInfo);
880}
881
882template <>
883Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
884 if (DG.isNull())
885 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
886 size_t NumDecls = DG.end() - DG.begin();
887 SmallVector<Decl *, 1> ToDecls;
888 ToDecls.reserve(NumDecls);
889 for (Decl *FromD : DG) {
890 if (auto ToDOrErr = import(FromD))
891 ToDecls.push_back(*ToDOrErr);
892 else
893 return ToDOrErr.takeError();
894 }
895 return DeclGroupRef::Create(Importer.getToContext(),
896 ToDecls.begin(),
897 NumDecls);
898}
899
900template <>
901Expected<ASTNodeImporter::Designator>
902ASTNodeImporter::import(const Designator &D) {
903 if (D.isFieldDesignator()) {
904 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
905
906 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
907 if (!ToDotLocOrErr)
908 return ToDotLocOrErr.takeError();
909
910 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
911 if (!ToFieldLocOrErr)
912 return ToFieldLocOrErr.takeError();
913
914 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
915 }
916
917 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
918 if (!ToLBracketLocOrErr)
919 return ToLBracketLocOrErr.takeError();
920
921 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
922 if (!ToRBracketLocOrErr)
923 return ToRBracketLocOrErr.takeError();
924
925 if (D.isArrayDesignator())
926 return Designator(D.getFirstExprIndex(),
927 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
928
929 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
930 if (!ToEllipsisLocOrErr)
931 return ToEllipsisLocOrErr.takeError();
932
933 assert(D.isArrayRangeDesignator());
934 return Designator(
935 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
936 *ToRBracketLocOrErr);
937}
938
939template <>
940Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
941 VarDecl *Var = nullptr;
942 if (From.capturesVariable()) {
943 if (auto VarOrErr = import(From.getCapturedVar()))
944 Var = *VarOrErr;
945 else
946 return VarOrErr.takeError();
947 }
948
949 auto LocationOrErr = import(From.getLocation());
950 if (!LocationOrErr)
951 return LocationOrErr.takeError();
952
953 SourceLocation EllipsisLoc;
954 if (From.isPackExpansion())
955 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
956 return std::move(Err);
957
958 return LambdaCapture(
959 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
960 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000961}
962
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000963} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000964
Douglas Gregor3996e242010-02-15 22:01:00 +0000965//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000966// Import Types
967//----------------------------------------------------------------------------
968
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000969using namespace clang;
970
Balazs Keri3b30d652018-10-19 13:32:20 +0000971ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000972 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
973 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000974 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000975}
976
Balazs Keri3b30d652018-10-19 13:32:20 +0000977ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
978 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
979 if (!UnderlyingTypeOrErr)
980 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000981
Balazs Keri3b30d652018-10-19 13:32:20 +0000982 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000983}
984
Balazs Keri3b30d652018-10-19 13:32:20 +0000985ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000986 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000987#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
988 case BuiltinType::Id: \
989 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000990#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000991#define SHARED_SINGLETON_TYPE(Expansion)
992#define BUILTIN_TYPE(Id, SingletonId) \
993 case BuiltinType::Id: return Importer.getToContext().SingletonId;
994#include "clang/AST/BuiltinTypes.def"
995
996 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
997 // context supports C++.
998
999 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1000 // context supports ObjC.
1001
Douglas Gregor96e578d2010-02-05 17:54:41 +00001002 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +00001003 // The context we're importing from has an unsigned 'char'. If we're
1004 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001005 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001006 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001007 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001008
Douglas Gregor96e578d2010-02-05 17:54:41 +00001009 return Importer.getToContext().CharTy;
1010
Douglas Gregor96e578d2010-02-05 17:54:41 +00001011 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +00001012 // The context we're importing from has an unsigned 'char'. If we're
1013 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001015 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001016 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001017
Douglas Gregor96e578d2010-02-05 17:54:41 +00001018 return Importer.getToContext().CharTy;
1019
Chris Lattnerad3467e2010-12-25 23:25:43 +00001020 case BuiltinType::WChar_S:
1021 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001022 // FIXME: If not in C++, shall we translate to the C equivalent of
1023 // wchar_t?
1024 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001025 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001026
1027 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001028}
1029
Balazs Keri3b30d652018-10-19 13:32:20 +00001030ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1031 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1032 if (!ToOriginalTypeOrErr)
1033 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001036}
1037
Balazs Keri3b30d652018-10-19 13:32:20 +00001038ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1039 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1040 if (!ToElementTypeOrErr)
1041 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001042
Balazs Keri3b30d652018-10-19 13:32:20 +00001043 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001044}
1045
Balazs Keri3b30d652018-10-19 13:32:20 +00001046ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1047 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1048 if (!ToPointeeTypeOrErr)
1049 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001050
Balazs Keri3b30d652018-10-19 13:32:20 +00001051 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001052}
1053
Balazs Keri3b30d652018-10-19 13:32:20 +00001054ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001055 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001056 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1057 if (!ToPointeeTypeOrErr)
1058 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001059
Balazs Keri3b30d652018-10-19 13:32:20 +00001060 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001061}
1062
Balazs Keri3b30d652018-10-19 13:32:20 +00001063ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001064ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001065 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001066 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1067 if (!ToPointeeTypeOrErr)
1068 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001069
Balazs Keri3b30d652018-10-19 13:32:20 +00001070 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001071}
1072
Balazs Keri3b30d652018-10-19 13:32:20 +00001073ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001074ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001075 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001076 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1077 if (!ToPointeeTypeOrErr)
1078 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001079
Balazs Keri3b30d652018-10-19 13:32:20 +00001080 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001081}
1082
Balazs Keri3b30d652018-10-19 13:32:20 +00001083ExpectedType
1084ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001085 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001086 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1087 if (!ToPointeeTypeOrErr)
1088 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001089
Balazs Keri3b30d652018-10-19 13:32:20 +00001090 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1091 if (!ClassTypeOrErr)
1092 return ClassTypeOrErr.takeError();
1093
1094 return Importer.getToContext().getMemberPointerType(
1095 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001096}
1097
Balazs Keri3b30d652018-10-19 13:32:20 +00001098ExpectedType
1099ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1100 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1101 if (!ToElementTypeOrErr)
1102 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001103
Balazs Keri3b30d652018-10-19 13:32:20 +00001104 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001105 T->getSize(),
1106 T->getSizeModifier(),
1107 T->getIndexTypeCVRQualifiers());
1108}
1109
Balazs Keri3b30d652018-10-19 13:32:20 +00001110ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001111ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001112 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1113 if (!ToElementTypeOrErr)
1114 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001115
Balazs Keri3b30d652018-10-19 13:32:20 +00001116 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001117 T->getSizeModifier(),
1118 T->getIndexTypeCVRQualifiers());
1119}
1120
Balazs Keri3b30d652018-10-19 13:32:20 +00001121ExpectedType
1122ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1123 QualType ToElementType;
1124 Expr *ToSizeExpr;
1125 SourceRange ToBracketsRange;
1126 if (auto Imp = importSeq(
1127 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1128 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1129 else
1130 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001131
Balazs Keri3b30d652018-10-19 13:32:20 +00001132 return Importer.getToContext().getVariableArrayType(
1133 ToElementType, ToSizeExpr, T->getSizeModifier(),
1134 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001135}
1136
Balazs Keri3b30d652018-10-19 13:32:20 +00001137ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001138 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001139 QualType ToElementType;
1140 Expr *ToSizeExpr;
1141 SourceRange ToBracketsRange;
1142 if (auto Imp = importSeq(
1143 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1144 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1145 else
1146 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001147 // SizeExpr may be null if size is not specified directly.
1148 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001149
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001150 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001151 ToElementType, ToSizeExpr, T->getSizeModifier(),
1152 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001153}
1154
Balazs Keri3b30d652018-10-19 13:32:20 +00001155ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1156 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1157 if (!ToElementTypeOrErr)
1158 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001159
Balazs Keri3b30d652018-10-19 13:32:20 +00001160 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001161 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001162 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001163}
1164
Balazs Keri3b30d652018-10-19 13:32:20 +00001165ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1166 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1167 if (!ToElementTypeOrErr)
1168 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001169
Balazs Keri3b30d652018-10-19 13:32:20 +00001170 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001171 T->getNumElements());
1172}
1173
Balazs Keri3b30d652018-10-19 13:32:20 +00001174ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001175ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001176 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001177 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001178 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1179 if (!ToReturnTypeOrErr)
1180 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001181
Balazs Keri3b30d652018-10-19 13:32:20 +00001182 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001183 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001184}
1185
Balazs Keri3b30d652018-10-19 13:32:20 +00001186ExpectedType
1187ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1188 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1189 if (!ToReturnTypeOrErr)
1190 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001191
Douglas Gregor96e578d2010-02-05 17:54:41 +00001192 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001193 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001194 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001195 ExpectedType TyOrErr = import(A);
1196 if (!TyOrErr)
1197 return TyOrErr.takeError();
1198 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001199 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001200
Douglas Gregor96e578d2010-02-05 17:54:41 +00001201 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001202 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001203 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001204 ExpectedType TyOrErr = import(E);
1205 if (!TyOrErr)
1206 return TyOrErr.takeError();
1207 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001208 }
John McCalldb40c7f2010-12-14 08:05:40 +00001209
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001210 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1211 FunctionProtoType::ExtProtoInfo ToEPI;
1212
Balazs Keri3b30d652018-10-19 13:32:20 +00001213 auto Imp = importSeq(
1214 FromEPI.ExceptionSpec.NoexceptExpr,
1215 FromEPI.ExceptionSpec.SourceDecl,
1216 FromEPI.ExceptionSpec.SourceTemplate);
1217 if (!Imp)
1218 return Imp.takeError();
1219
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001220 ToEPI.ExtInfo = FromEPI.ExtInfo;
1221 ToEPI.Variadic = FromEPI.Variadic;
1222 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1223 ToEPI.TypeQuals = FromEPI.TypeQuals;
1224 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001225 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1226 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001227 std::tie(
1228 ToEPI.ExceptionSpec.NoexceptExpr,
1229 ToEPI.ExceptionSpec.SourceDecl,
1230 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001231
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 return Importer.getToContext().getFunctionType(
1233 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001234}
1235
Balazs Keri3b30d652018-10-19 13:32:20 +00001236ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001237 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001238 UnresolvedUsingTypenameDecl *ToD;
1239 Decl *ToPrevD;
1240 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1241 std::tie(ToD, ToPrevD) = *Imp;
1242 else
1243 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001244
Balazs Keri3b30d652018-10-19 13:32:20 +00001245 return Importer.getToContext().getTypeDeclType(
1246 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001247}
1248
Balazs Keri3b30d652018-10-19 13:32:20 +00001249ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1250 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1251 if (!ToInnerTypeOrErr)
1252 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001253
Balazs Keri3b30d652018-10-19 13:32:20 +00001254 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001255}
1256
Balazs Keri3b30d652018-10-19 13:32:20 +00001257ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1258 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1259 if (!ToDeclOrErr)
1260 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001263}
1264
Balazs Keri3b30d652018-10-19 13:32:20 +00001265ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1266 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1267 if (!ToExprOrErr)
1268 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001269
Balazs Keri3b30d652018-10-19 13:32:20 +00001270 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001271}
1272
Balazs Keri3b30d652018-10-19 13:32:20 +00001273ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1274 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1275 if (!ToUnderlyingTypeOrErr)
1276 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001277
Balazs Keri3b30d652018-10-19 13:32:20 +00001278 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001279}
1280
Balazs Keri3b30d652018-10-19 13:32:20 +00001281ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001282 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001283 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1284 if (!ToExprOrErr)
1285 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001286
Balazs Keri3b30d652018-10-19 13:32:20 +00001287 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1288 if (!ToUnderlyingTypeOrErr)
1289 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001290
Balazs Keri3b30d652018-10-19 13:32:20 +00001291 return Importer.getToContext().getDecltypeType(
1292 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001293}
1294
Balazs Keri3b30d652018-10-19 13:32:20 +00001295ExpectedType
1296ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1297 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1298 if (!ToBaseTypeOrErr)
1299 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001300
Balazs Keri3b30d652018-10-19 13:32:20 +00001301 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1302 if (!ToUnderlyingTypeOrErr)
1303 return ToUnderlyingTypeOrErr.takeError();
1304
1305 return Importer.getToContext().getUnaryTransformType(
1306 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001307}
1308
Balazs Keri3b30d652018-10-19 13:32:20 +00001309ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001310 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001311 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1312 if (!ToDeducedTypeOrErr)
1313 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001314
Balazs Keri3b30d652018-10-19 13:32:20 +00001315 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1316 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001317 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001318}
1319
Balazs Keri3b30d652018-10-19 13:32:20 +00001320ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001321 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001322 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1323 if (!ToDeclOrErr)
1324 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001325
Balazs Keri3b30d652018-10-19 13:32:20 +00001326 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1327 if (!ToInjTypeOrErr)
1328 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001329
1330 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1331 // See comments in InjectedClassNameType definition for details
1332 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1333 enum {
1334 TypeAlignmentInBits = 4,
1335 TypeAlignment = 1 << TypeAlignmentInBits
1336 };
1337
1338 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001339 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001340}
1341
Balazs Keri3b30d652018-10-19 13:32:20 +00001342ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1343 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1344 if (!ToDeclOrErr)
1345 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001346
Balazs Keri3b30d652018-10-19 13:32:20 +00001347 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001348}
1349
Balazs Keri3b30d652018-10-19 13:32:20 +00001350ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1351 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1352 if (!ToDeclOrErr)
1353 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001354
Balazs Keri3b30d652018-10-19 13:32:20 +00001355 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001356}
1357
Balazs Keri3b30d652018-10-19 13:32:20 +00001358ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1359 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1360 if (!ToModifiedTypeOrErr)
1361 return ToModifiedTypeOrErr.takeError();
1362 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1363 if (!ToEquivalentTypeOrErr)
1364 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001365
1366 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001367 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001368}
1369
Balazs Keri3b30d652018-10-19 13:32:20 +00001370ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001371 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001372 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1373 if (!ToDeclOrErr)
1374 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001375
1376 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001377 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001378}
1379
Balazs Keri3b30d652018-10-19 13:32:20 +00001380ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001381 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001382 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1383 if (!ReplacedOrErr)
1384 return ReplacedOrErr.takeError();
1385 const TemplateTypeParmType *Replaced =
1386 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001387
Balazs Keri3b30d652018-10-19 13:32:20 +00001388 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1389 if (!ToReplacementTypeOrErr)
1390 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001391
1392 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001394}
1395
Balazs Keri3b30d652018-10-19 13:32:20 +00001396ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001397 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 auto ToTemplateOrErr = import(T->getTemplateName());
1399 if (!ToTemplateOrErr)
1400 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001401
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001402 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001403 if (Error Err = ImportTemplateArguments(
1404 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1405 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Douglas Gregore2e50d332010-12-01 01:36:18 +00001407 QualType ToCanonType;
1408 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001409 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001410 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001411 if (ExpectedType TyOrErr = import(FromCanonType))
1412 ToCanonType = *TyOrErr;
1413 else
1414 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001415 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001416 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001417 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001418 ToCanonType);
1419}
1420
Balazs Keri3b30d652018-10-19 13:32:20 +00001421ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001422 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001423 auto ToQualifierOrErr = import(T->getQualifier());
1424 if (!ToQualifierOrErr)
1425 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001426
Balazs Keri3b30d652018-10-19 13:32:20 +00001427 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1428 if (!ToNamedTypeOrErr)
1429 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001430
Balazs Keri3b30d652018-10-19 13:32:20 +00001431 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1432 if (!ToOwnedTagDeclOrErr)
1433 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001434
Abramo Bagnara6150c882010-05-11 21:36:43 +00001435 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 *ToQualifierOrErr,
1437 *ToNamedTypeOrErr,
1438 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001439}
1440
Balazs Keri3b30d652018-10-19 13:32:20 +00001441ExpectedType
1442ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1443 ExpectedType ToPatternOrErr = import(T->getPattern());
1444 if (!ToPatternOrErr)
1445 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001446
Balazs Keri3b30d652018-10-19 13:32:20 +00001447 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001448 T->getNumExpansions());
1449}
1450
Balazs Keri3b30d652018-10-19 13:32:20 +00001451ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001452 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001453 auto ToQualifierOrErr = import(T->getQualifier());
1454 if (!ToQualifierOrErr)
1455 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001456
Balazs Keri3b30d652018-10-19 13:32:20 +00001457 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001458
1459 SmallVector<TemplateArgument, 2> ToPack;
1460 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001461 if (Error Err = ImportTemplateArguments(
1462 T->getArgs(), T->getNumArgs(), ToPack))
1463 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001464
1465 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001466 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001467}
1468
Balazs Keri3b30d652018-10-19 13:32:20 +00001469ExpectedType
1470ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1471 auto ToQualifierOrErr = import(T->getQualifier());
1472 if (!ToQualifierOrErr)
1473 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001474
1475 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001476
Balazs Keri3b30d652018-10-19 13:32:20 +00001477 QualType Canon;
1478 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1479 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1480 Canon = (*TyOrErr).getCanonicalType();
1481 else
1482 return TyOrErr.takeError();
1483 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001484
Balazs Keri3b30d652018-10-19 13:32:20 +00001485 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1486 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001487 Name, Canon);
1488}
1489
Balazs Keri3b30d652018-10-19 13:32:20 +00001490ExpectedType
1491ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1492 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1493 if (!ToDeclOrErr)
1494 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495
Balazs Keri3b30d652018-10-19 13:32:20 +00001496 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001497}
1498
Balazs Keri3b30d652018-10-19 13:32:20 +00001499ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1500 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1501 if (!ToBaseTypeOrErr)
1502 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001503
Douglas Gregore9d95f12015-07-07 03:57:35 +00001504 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001505 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001506 if (ExpectedType TyOrErr = import(TypeArg))
1507 TypeArgs.push_back(*TyOrErr);
1508 else
1509 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001510 }
1511
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001512 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001513 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001514 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1515 Protocols.push_back(*ProtocolOrErr);
1516 else
1517 return ProtocolOrErr.takeError();
1518
Douglas Gregor96e578d2010-02-05 17:54:41 +00001519 }
1520
Balazs Keri3b30d652018-10-19 13:32:20 +00001521 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001522 Protocols,
1523 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524}
1525
Balazs Keri3b30d652018-10-19 13:32:20 +00001526ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001527ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001528 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1529 if (!ToPointeeTypeOrErr)
1530 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001531
Balazs Keri3b30d652018-10-19 13:32:20 +00001532 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001533}
1534
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001535//----------------------------------------------------------------------------
1536// Import Declarations
1537//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001538Error ASTNodeImporter::ImportDeclParts(
1539 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1540 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001541 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1542 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1543 DeclContext *OrigDC = D->getDeclContext();
1544 FunctionDecl *FunDecl;
1545 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1546 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001547 auto getLeafPointeeType = [](const Type *T) {
1548 while (T->isPointerType() || T->isArrayType()) {
1549 T = T->getPointeeOrArrayElementType();
1550 }
1551 return T;
1552 };
1553 for (const ParmVarDecl *P : FunDecl->parameters()) {
1554 const Type *LeafT =
1555 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1556 auto *RT = dyn_cast<RecordType>(LeafT);
1557 if (RT && RT->getDecl() == D) {
1558 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1559 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001560 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001561 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001562 }
1563 }
1564
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001565 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001566 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1567 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001568
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001569 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001570 if (Error Err = importInto(Name, D->getDeclName()))
1571 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001572
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001573 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001574 if (Error Err = importInto(Loc, D->getLocation()))
1575 return Err;
1576
Sean Callanan59721b32015-04-28 18:41:46 +00001577 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Balazs Keri3b30d652018-10-19 13:32:20 +00001578
1579 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001580}
1581
Balazs Keri3b30d652018-10-19 13:32:20 +00001582Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001583 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001584 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001585
Balazs Keri3b30d652018-10-19 13:32:20 +00001586 if (!ToD)
1587 if (Error Err = importInto(ToD, FromD))
1588 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001589
Balazs Keri3b30d652018-10-19 13:32:20 +00001590 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1591 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1592 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1593 !ToRecord->getDefinition()) {
1594 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1595 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001596 }
1597 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001598 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001599 }
1600
Balazs Keri3b30d652018-10-19 13:32:20 +00001601 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1602 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001603 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001604 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1605 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001606 }
1607 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001608 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001609 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001610
1611 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001612}
1613
Balazs Keri3b30d652018-10-19 13:32:20 +00001614Error
1615ASTNodeImporter::ImportDeclarationNameLoc(
1616 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001617 // NOTE: To.Name and To.Loc are already imported.
1618 // We only have to import To.LocInfo.
1619 switch (To.getName().getNameKind()) {
1620 case DeclarationName::Identifier:
1621 case DeclarationName::ObjCZeroArgSelector:
1622 case DeclarationName::ObjCOneArgSelector:
1623 case DeclarationName::ObjCMultiArgSelector:
1624 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001625 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001626 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001627
1628 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001629 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1630 To.setCXXOperatorNameRange(*ToRangeOrErr);
1631 else
1632 return ToRangeOrErr.takeError();
1633 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001634 }
1635 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001636 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1637 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1638 else
1639 return LocOrErr.takeError();
1640 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001641 }
1642 case DeclarationName::CXXConstructorName:
1643 case DeclarationName::CXXDestructorName:
1644 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001645 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1646 To.setNamedTypeInfo(*ToTInfoOrErr);
1647 else
1648 return ToTInfoOrErr.takeError();
1649 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001650 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001651 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001652 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001653}
1654
Balazs Keri3b30d652018-10-19 13:32:20 +00001655Error
1656ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001657 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001658 auto ToDCOrErr = Importer.ImportContext(FromDC);
1659 return ToDCOrErr.takeError();
1660 }
1661 llvm::SmallVector<Decl *, 8> ImportedDecls;
1662 for (auto *From : FromDC->decls()) {
1663 ExpectedDecl ImportedOrErr = import(From);
1664 if (!ImportedOrErr)
1665 // Ignore the error, continue with next Decl.
1666 // FIXME: Handle this case somehow better.
1667 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001668 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001669
Balazs Keri3b30d652018-10-19 13:32:20 +00001670 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001671}
1672
Balazs Keri3b30d652018-10-19 13:32:20 +00001673Error ASTNodeImporter::ImportDeclContext(
1674 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1675 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1676 if (!ToDCOrErr)
1677 return ToDCOrErr.takeError();
1678 ToDC = *ToDCOrErr;
1679
1680 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1681 auto ToLexicalDCOrErr = Importer.ImportContext(
1682 FromD->getLexicalDeclContext());
1683 if (!ToLexicalDCOrErr)
1684 return ToLexicalDCOrErr.takeError();
1685 ToLexicalDC = *ToLexicalDCOrErr;
1686 } else
1687 ToLexicalDC = ToDC;
1688
1689 return Error::success();
1690}
1691
1692Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001693 const CXXRecordDecl *From, CXXRecordDecl *To) {
1694 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1695 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001696
Balazs Keri1d20cc22018-07-16 12:16:39 +00001697 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001698 if (FromM->isImplicit()) {
1699 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1700 if (!ToMOrErr)
1701 return ToMOrErr.takeError();
1702 }
1703
1704 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001705}
1706
Balazs Keri3b30d652018-10-19 13:32:20 +00001707static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1708 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001709 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001710 Decl *ToTypedef = Importer.Import(FromTypedef);
1711 if (!ToTypedef)
1712 return make_error<ImportError>();
1713 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToTypedef));
1714 // FIXME: This should be the final code.
1715 //if (Expected<Decl *> ToTypedefOrErr = Importer.Import(FromTypedef))
1716 // To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1717 //else
1718 // return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001719 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001720 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001721}
1722
Balazs Keri3b30d652018-10-19 13:32:20 +00001723Error ASTNodeImporter::ImportDefinition(
1724 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001725 if (To->getDefinition() || To->isBeingDefined()) {
1726 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001727 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001728
Balazs Keri3b30d652018-10-19 13:32:20 +00001729 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001730 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001731
Douglas Gregore2e50d332010-12-01 01:36:18 +00001732 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001733
Balazs Keri3b30d652018-10-19 13:32:20 +00001734 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1735 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001736
Douglas Gregore2e50d332010-12-01 01:36:18 +00001737 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001738 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1739 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001740
1741 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1742 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1743 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001744 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001745 ToData.Aggregate = FromData.Aggregate;
1746 ToData.PlainOldData = FromData.PlainOldData;
1747 ToData.Empty = FromData.Empty;
1748 ToData.Polymorphic = FromData.Polymorphic;
1749 ToData.Abstract = FromData.Abstract;
1750 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001751 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1752 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1753 ToData.HasBasesWithNonStaticDataMembers =
1754 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001755 ToData.HasPrivateFields = FromData.HasPrivateFields;
1756 ToData.HasProtectedFields = FromData.HasProtectedFields;
1757 ToData.HasPublicFields = FromData.HasPublicFields;
1758 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001759 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001760 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001761 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001762 ToData.HasUninitializedReferenceMember
1763 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001764 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001765 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1766 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001767 ToData.NeedOverloadResolutionForCopyConstructor
1768 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001769 ToData.NeedOverloadResolutionForMoveConstructor
1770 = FromData.NeedOverloadResolutionForMoveConstructor;
1771 ToData.NeedOverloadResolutionForMoveAssignment
1772 = FromData.NeedOverloadResolutionForMoveAssignment;
1773 ToData.NeedOverloadResolutionForDestructor
1774 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001775 ToData.DefaultedCopyConstructorIsDeleted
1776 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001777 ToData.DefaultedMoveConstructorIsDeleted
1778 = FromData.DefaultedMoveConstructorIsDeleted;
1779 ToData.DefaultedMoveAssignmentIsDeleted
1780 = FromData.DefaultedMoveAssignmentIsDeleted;
1781 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001782 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1783 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001784 ToData.HasConstexprNonCopyMoveConstructor
1785 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001786 ToData.HasDefaultedDefaultConstructor
1787 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001788 ToData.DefaultedDefaultConstructorIsConstexpr
1789 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001790 ToData.HasConstexprDefaultConstructor
1791 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001792 ToData.HasNonLiteralTypeFieldsOrBases
1793 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001794 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001795 ToData.UserProvidedDefaultConstructor
1796 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001797 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001798 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1799 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1800 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1801 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001802 ToData.ImplicitCopyAssignmentHasConstParam
1803 = FromData.ImplicitCopyAssignmentHasConstParam;
1804 ToData.HasDeclaredCopyConstructorWithConstParam
1805 = FromData.HasDeclaredCopyConstructorWithConstParam;
1806 ToData.HasDeclaredCopyAssignmentWithConstParam
1807 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001808
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001809 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001810 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001811 ExpectedType TyOrErr = import(Base1.getType());
1812 if (!TyOrErr)
1813 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001814
1815 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001816 if (Base1.isPackExpansion()) {
1817 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1818 EllipsisLoc = *LocOrErr;
1819 else
1820 return LocOrErr.takeError();
1821 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001822
1823 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001824 if (Error Err =
1825 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1826 return Err;
1827
1828 auto RangeOrErr = import(Base1.getSourceRange());
1829 if (!RangeOrErr)
1830 return RangeOrErr.takeError();
1831
1832 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1833 if (!TSIOrErr)
1834 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001835
Douglas Gregore2e50d332010-12-01 01:36:18 +00001836 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001837 new (Importer.getToContext()) CXXBaseSpecifier(
1838 *RangeOrErr,
1839 Base1.isVirtual(),
1840 Base1.isBaseOfClass(),
1841 Base1.getAccessSpecifierAsWritten(),
1842 *TSIOrErr,
1843 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001844 }
1845 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001846 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001847 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001848
Douglas Gregor2e15c842012-02-01 21:00:38 +00001849 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001850 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1851 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001852
Douglas Gregore2e50d332010-12-01 01:36:18 +00001853 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001854 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001855}
1856
Balazs Keri3b30d652018-10-19 13:32:20 +00001857Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001858 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001859 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001860
Gabor Martonac3a5d62018-09-17 12:04:52 +00001861 Expr *FromInit = From->getInit();
1862 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001863 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001864
Balazs Keri3b30d652018-10-19 13:32:20 +00001865 ExpectedExpr ToInitOrErr = import(FromInit);
1866 if (!ToInitOrErr)
1867 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001868
Balazs Keri3b30d652018-10-19 13:32:20 +00001869 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001870 if (From->isInitKnownICE()) {
1871 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1872 Eval->CheckedICE = true;
1873 Eval->IsICE = From->isInitICE();
1874 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001875
1876 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001877 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001878}
1879
Balazs Keri3b30d652018-10-19 13:32:20 +00001880Error ASTNodeImporter::ImportDefinition(
1881 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001882 if (To->getDefinition() || To->isBeingDefined()) {
1883 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001884 return ImportDeclContext(From, /*ForceImport=*/true);
1885 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001886 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001887
Douglas Gregord451ea92011-07-29 23:31:30 +00001888 To->startDefinition();
1889
Balazs Keri3b30d652018-10-19 13:32:20 +00001890 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1891 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001892
Balazs Keri3b30d652018-10-19 13:32:20 +00001893 ExpectedType ToTypeOrErr =
1894 import(Importer.getFromContext().getTypeDeclType(From));
1895 if (!ToTypeOrErr)
1896 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001897
Balazs Keri3b30d652018-10-19 13:32:20 +00001898 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1899 if (!ToPromotionTypeOrErr)
1900 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001901
1902 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001903 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1904 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001905
Douglas Gregord451ea92011-07-29 23:31:30 +00001906 // FIXME: we might need to merge the number of positive or negative bits
1907 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001908 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001909 From->getNumPositiveBits(),
1910 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001911 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001912}
1913
Balazs Keri3b30d652018-10-19 13:32:20 +00001914// FIXME: Remove this, use `import` instead.
1915Expected<TemplateParameterList *> ASTNodeImporter::ImportTemplateParameterList(
1916 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001917 SmallVector<NamedDecl *, 4> ToParams(Params->size());
Balazs Keri3b30d652018-10-19 13:32:20 +00001918 if (Error Err = ImportContainerChecked(*Params, ToParams))
1919 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001920
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001921 Expr *ToRequiresClause;
1922 if (Expr *const R = Params->getRequiresClause()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001923 if (Error Err = importInto(ToRequiresClause, R))
1924 return std::move(Err);
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001925 } else {
1926 ToRequiresClause = nullptr;
1927 }
1928
Balazs Keri3b30d652018-10-19 13:32:20 +00001929 auto ToTemplateLocOrErr = import(Params->getTemplateLoc());
1930 if (!ToTemplateLocOrErr)
1931 return ToTemplateLocOrErr.takeError();
1932 auto ToLAngleLocOrErr = import(Params->getLAngleLoc());
1933 if (!ToLAngleLocOrErr)
1934 return ToLAngleLocOrErr.takeError();
1935 auto ToRAngleLocOrErr = import(Params->getRAngleLoc());
1936 if (!ToRAngleLocOrErr)
1937 return ToRAngleLocOrErr.takeError();
1938
1939 return TemplateParameterList::Create(
1940 Importer.getToContext(),
1941 *ToTemplateLocOrErr,
1942 *ToLAngleLocOrErr,
1943 ToParams,
1944 *ToRAngleLocOrErr,
1945 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001946}
1947
Balazs Keri3b30d652018-10-19 13:32:20 +00001948Error ASTNodeImporter::ImportTemplateArguments(
1949 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1950 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001951 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001952 if (auto ToOrErr = import(FromArgs[I]))
1953 ToArgs.push_back(*ToOrErr);
1954 else
1955 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001956 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001957
Balazs Keri3b30d652018-10-19 13:32:20 +00001958 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001959}
1960
Balazs Keri3b30d652018-10-19 13:32:20 +00001961// FIXME: Do not forget to remove this and use only 'import'.
1962Expected<TemplateArgument>
1963ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1964 return import(From);
1965}
1966
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001967template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001968Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001969 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1970 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001971 if (auto ToLocOrErr = import(FromLoc))
1972 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001973 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001974 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001975 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001976 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001977}
1978
Gabor Marton26f72a92018-07-12 09:42:05 +00001979static StructuralEquivalenceKind
1980getStructuralEquivalenceKind(const ASTImporter &Importer) {
1981 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1982 : StructuralEquivalenceKind::Default;
1983}
1984
Gabor Marton950fb572018-07-17 12:39:27 +00001985bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1986 StructuralEquivalenceContext Ctx(
1987 Importer.getFromContext(), Importer.getToContext(),
1988 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1989 false, Complain);
1990 return Ctx.IsEquivalent(From, To);
1991}
1992
1993bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001994 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001995 // Eliminate a potential failure point where we attempt to re-import
1996 // something we're trying to import while completing ToRecord.
1997 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1998 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001999 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002000 if (ToOriginRecord)
2001 ToRecord = ToOriginRecord;
2002 }
2003
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002004 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002005 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002006 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002007 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002008 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002009 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002010}
2011
Larisse Voufo39a1e502013-08-06 01:03:05 +00002012bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2013 bool Complain) {
2014 StructuralEquivalenceContext Ctx(
2015 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002016 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2017 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002018 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002019}
2020
Douglas Gregor98c10182010-02-12 22:17:39 +00002021bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002022 StructuralEquivalenceContext Ctx(
2023 Importer.getFromContext(), Importer.getToContext(),
2024 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002025 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002026}
2027
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002028bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2029 FunctionTemplateDecl *To) {
2030 StructuralEquivalenceContext Ctx(
2031 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002032 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2033 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002034 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002035}
2036
Balazs Keric7797c42018-07-11 09:37:24 +00002037bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2038 StructuralEquivalenceContext Ctx(
2039 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002040 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2041 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002042 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002043}
2044
Douglas Gregor91155082012-11-14 22:29:20 +00002045bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002046 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002047 const llvm::APSInt &FromVal = FromEC->getInitVal();
2048 const llvm::APSInt &ToVal = ToEC->getInitVal();
2049
2050 return FromVal.isSigned() == ToVal.isSigned() &&
2051 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2052 FromVal == ToVal;
2053}
2054
2055bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002056 ClassTemplateDecl *To) {
2057 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2058 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002059 Importer.getNonEquivalentDecls(),
2060 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002061 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002062}
2063
Larisse Voufo39a1e502013-08-06 01:03:05 +00002064bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2065 VarTemplateDecl *To) {
2066 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2067 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002068 Importer.getNonEquivalentDecls(),
2069 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002070 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002071}
2072
Balazs Keri3b30d652018-10-19 13:32:20 +00002073ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002074 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002075 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002076 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002077}
2078
Balazs Keri3b30d652018-10-19 13:32:20 +00002079ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2080 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2081 << D->getDeclKindName();
2082 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2083}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002084
Balazs Keri3b30d652018-10-19 13:32:20 +00002085ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2086 // Import the context of this declaration.
2087 DeclContext *DC, *LexicalDC;
2088 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2089 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002090
2091 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002092 ExpectedSLoc LocOrErr = import(D->getLocation());
2093 if (!LocOrErr)
2094 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002095
Gabor Marton26f72a92018-07-12 09:42:05 +00002096 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002097 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002098 return ToD;
2099
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002100 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002101 LexicalDC->addDeclInternal(ToD);
2102 return ToD;
2103}
2104
Balazs Keri3b30d652018-10-19 13:32:20 +00002105ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002106 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002107 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002108
Gabor Marton26f72a92018-07-12 09:42:05 +00002109 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002110
Sean Callanan65198272011-11-17 23:20:56 +00002111 return ToD;
2112}
2113
Balazs Keri3b30d652018-10-19 13:32:20 +00002114ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2115 ExpectedSLoc LocOrErr = import(D->getLocation());
2116 if (!LocOrErr)
2117 return LocOrErr.takeError();
2118 auto ColonLocOrErr = import(D->getColonLoc());
2119 if (!ColonLocOrErr)
2120 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002121
2122 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002123 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2124 if (!DCOrErr)
2125 return DCOrErr.takeError();
2126 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002127
Gabor Marton26f72a92018-07-12 09:42:05 +00002128 AccessSpecDecl *ToD;
2129 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002130 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002131 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002132
2133 // Lexical DeclContext and Semantic DeclContext
2134 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002135 ToD->setLexicalDeclContext(DC);
2136 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002137
Gabor Marton26f72a92018-07-12 09:42:05 +00002138 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002139}
2140
Balazs Keri3b30d652018-10-19 13:32:20 +00002141ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2142 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2143 if (!DCOrErr)
2144 return DCOrErr.takeError();
2145 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002146 DeclContext *LexicalDC = DC;
2147
Balazs Keri3b30d652018-10-19 13:32:20 +00002148 SourceLocation ToLocation, ToRParenLoc;
2149 Expr *ToAssertExpr;
2150 StringLiteral *ToMessage;
2151 if (auto Imp = importSeq(
2152 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2153 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2154 else
2155 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002156
Gabor Marton26f72a92018-07-12 09:42:05 +00002157 StaticAssertDecl *ToD;
2158 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002159 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2160 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002161 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002162
2163 ToD->setLexicalDeclContext(LexicalDC);
2164 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002165 return ToD;
2166}
2167
Balazs Keri3b30d652018-10-19 13:32:20 +00002168ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002169 // Import the major distinguishing characteristics of this namespace.
2170 DeclContext *DC, *LexicalDC;
2171 DeclarationName Name;
2172 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002173 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002174 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2175 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002176 if (ToD)
2177 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002178
2179 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002180 if (!Name) {
2181 // This is an anonymous namespace. Adopt an existing anonymous
2182 // namespace if we can.
2183 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002184 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002185 MergeWithNamespace = TU->getAnonymousNamespace();
2186 else
2187 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2188 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002189 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002190 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002191 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002192 for (auto *FoundDecl : FoundDecls) {
2193 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002194 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002195
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002196 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002197 MergeWithNamespace = FoundNS;
2198 ConflictingDecls.clear();
2199 break;
2200 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002201
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002202 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002203 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002204
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002205 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002206 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002207 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002208 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002209 if (!Name)
2210 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002211 }
2212 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002213
Balazs Keri3b30d652018-10-19 13:32:20 +00002214 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2215 if (!BeginLocOrErr)
2216 return BeginLocOrErr.takeError();
2217
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002218 // Create the "to" namespace, if needed.
2219 NamespaceDecl *ToNamespace = MergeWithNamespace;
2220 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002221 if (GetImportedOrCreateDecl(
2222 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002223 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002224 /*PrevDecl=*/nullptr))
2225 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002226 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002227 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002228
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002229 // If this is an anonymous namespace, register it as the anonymous
2230 // namespace within its context.
2231 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002232 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002233 TU->setAnonymousNamespace(ToNamespace);
2234 else
2235 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2236 }
2237 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002238 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002239
Balazs Keri3b30d652018-10-19 13:32:20 +00002240 if (Error Err = ImportDeclContext(D))
2241 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002242
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002243 return ToNamespace;
2244}
2245
Balazs Keri3b30d652018-10-19 13:32:20 +00002246ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002247 // Import the major distinguishing characteristics of this namespace.
2248 DeclContext *DC, *LexicalDC;
2249 DeclarationName Name;
2250 SourceLocation Loc;
2251 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002252 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2253 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002254 if (LookupD)
2255 return LookupD;
2256
2257 // NOTE: No conflict resolution is done for namespace aliases now.
2258
Balazs Keri3b30d652018-10-19 13:32:20 +00002259 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2260 NestedNameSpecifierLoc ToQualifierLoc;
2261 NamespaceDecl *ToNamespace;
2262 if (auto Imp = importSeq(
2263 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2264 D->getTargetNameLoc(), D->getNamespace()))
2265 std::tie(
2266 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2267 ToNamespace) = *Imp;
2268 else
2269 return Imp.takeError();
2270 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002271
Gabor Marton26f72a92018-07-12 09:42:05 +00002272 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002273 if (GetImportedOrCreateDecl(
2274 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2275 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002276 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002277
2278 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002279 LexicalDC->addDeclInternal(ToD);
2280
2281 return ToD;
2282}
2283
Balazs Keri3b30d652018-10-19 13:32:20 +00002284ExpectedDecl
2285ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002286 // Import the major distinguishing characteristics of this typedef.
2287 DeclContext *DC, *LexicalDC;
2288 DeclarationName Name;
2289 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002290 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002291 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2292 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002293 if (ToD)
2294 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002295
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002296 // If this typedef is not in block scope, determine whether we've
2297 // seen a typedef with the same name (that we can merge with) or any
2298 // other entity by that name (which name lookup could conflict with).
2299 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002300 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002301 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002302 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002303 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002304 for (auto *FoundDecl : FoundDecls) {
2305 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002306 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002307 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002308 if (Importer.IsStructurallyEquivalent(
2309 D->getUnderlyingType(), FoundTypedef->getUnderlyingType()))
2310 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002311 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002312
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002313 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002314 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002315
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002316 if (!ConflictingDecls.empty()) {
2317 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002318 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002319 ConflictingDecls.size());
2320 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002321 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002322 }
2323 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002324
Balazs Keri3b30d652018-10-19 13:32:20 +00002325 QualType ToUnderlyingType;
2326 TypeSourceInfo *ToTypeSourceInfo;
2327 SourceLocation ToBeginLoc;
2328 if (auto Imp = importSeq(
2329 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2330 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2331 else
2332 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002333
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002334 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002335 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002336 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002337 if (IsAlias) {
2338 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002339 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2340 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002341 return ToTypedef;
2342 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002343 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2344 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002345 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002346
Douglas Gregordd483172010-02-22 17:42:47 +00002347 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002348 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002349
2350 // Templated declarations should not appear in DeclContext.
2351 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2352 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2353 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002354
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002355 return ToTypedef;
2356}
2357
Balazs Keri3b30d652018-10-19 13:32:20 +00002358ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002359 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2360}
2361
Balazs Keri3b30d652018-10-19 13:32:20 +00002362ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002363 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2364}
2365
Balazs Keri3b30d652018-10-19 13:32:20 +00002366ExpectedDecl
2367ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002368 // Import the major distinguishing characteristics of this typedef.
2369 DeclContext *DC, *LexicalDC;
2370 DeclarationName Name;
2371 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002372 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002373 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2374 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002375 if (FoundD)
2376 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002377
2378 // If this typedef is not in block scope, determine whether we've
2379 // seen a typedef with the same name (that we can merge with) or any
2380 // other entity by that name (which name lookup could conflict with).
2381 if (!DC->isFunctionOrMethod()) {
2382 SmallVector<NamedDecl *, 4> ConflictingDecls;
2383 unsigned IDNS = Decl::IDNS_Ordinary;
2384 SmallVector<NamedDecl *, 2> FoundDecls;
2385 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002386 for (auto *FoundDecl : FoundDecls) {
2387 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002388 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002389 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002390 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002391 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002392 }
2393
2394 if (!ConflictingDecls.empty()) {
2395 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2396 ConflictingDecls.data(),
2397 ConflictingDecls.size());
2398 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002399 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002400 }
2401 }
2402
Balazs Keri3b30d652018-10-19 13:32:20 +00002403 TemplateParameterList *ToTemplateParameters;
2404 TypeAliasDecl *ToTemplatedDecl;
2405 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2406 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2407 else
2408 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002409
Gabor Marton26f72a92018-07-12 09:42:05 +00002410 TypeAliasTemplateDecl *ToAlias;
2411 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002412 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002413 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002414
Balazs Keri3b30d652018-10-19 13:32:20 +00002415 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002416
Gabor Horvath7a91c082017-11-14 11:30:38 +00002417 ToAlias->setAccess(D->getAccess());
2418 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002419 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002420 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002421}
2422
Balazs Keri3b30d652018-10-19 13:32:20 +00002423ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002424 // Import the major distinguishing characteristics of this label.
2425 DeclContext *DC, *LexicalDC;
2426 DeclarationName Name;
2427 SourceLocation Loc;
2428 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002429 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2430 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002431 if (ToD)
2432 return ToD;
2433
2434 assert(LexicalDC->isFunctionOrMethod());
2435
Gabor Marton26f72a92018-07-12 09:42:05 +00002436 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002437 if (D->isGnuLocal()) {
2438 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2439 if (!BeginLocOrErr)
2440 return BeginLocOrErr.takeError();
2441 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2442 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2443 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002444
Balazs Keri3b30d652018-10-19 13:32:20 +00002445 } else {
2446 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2447 Name.getAsIdentifierInfo()))
2448 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002449
Balazs Keri3b30d652018-10-19 13:32:20 +00002450 }
2451
2452 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2453 if (!ToStmtOrErr)
2454 return ToStmtOrErr.takeError();
2455
2456 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002457 ToLabel->setLexicalDeclContext(LexicalDC);
2458 LexicalDC->addDeclInternal(ToLabel);
2459 return ToLabel;
2460}
2461
Balazs Keri3b30d652018-10-19 13:32:20 +00002462ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002463 // Import the major distinguishing characteristics of this enum.
2464 DeclContext *DC, *LexicalDC;
2465 DeclarationName Name;
2466 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002467 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002468 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2469 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002470 if (ToD)
2471 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002472
Douglas Gregor98c10182010-02-12 22:17:39 +00002473 // Figure out what enum name we're looking for.
2474 unsigned IDNS = Decl::IDNS_Tag;
2475 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002476 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002477 if (Error Err = importInto(
2478 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2479 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002480 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002481 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002482 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002483
Douglas Gregor98c10182010-02-12 22:17:39 +00002484 // We may already have an enum of the same name; try to find and match it.
2485 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002486 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002487 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002488 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002489 for (auto *FoundDecl : FoundDecls) {
2490 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002491 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002492
Balazs Keri3b30d652018-10-19 13:32:20 +00002493 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002494 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002495 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002496 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002497
Balazs Keri3b30d652018-10-19 13:32:20 +00002498 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002499 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002500 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002501 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002502
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002503 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002504 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002505
Douglas Gregor98c10182010-02-12 22:17:39 +00002506 if (!ConflictingDecls.empty()) {
2507 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002508 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002509 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002510 if (!Name)
2511 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002512 }
2513 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002514
Balazs Keri3b30d652018-10-19 13:32:20 +00002515 SourceLocation ToBeginLoc;
2516 NestedNameSpecifierLoc ToQualifierLoc;
2517 QualType ToIntegerType;
2518 if (auto Imp = importSeq(
2519 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2520 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2521 else
2522 return Imp.takeError();
2523
Douglas Gregor98c10182010-02-12 22:17:39 +00002524 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002525 EnumDecl *D2;
2526 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002527 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002528 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2529 D->isScopedUsingClassTag(), D->isFixed()))
2530 return D2;
2531
Balazs Keri3b30d652018-10-19 13:32:20 +00002532 D2->setQualifierInfo(ToQualifierLoc);
2533 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002534 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002535 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002536 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002537
Douglas Gregor98c10182010-02-12 22:17:39 +00002538 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002539 if (D->isCompleteDefinition())
2540 if (Error Err = ImportDefinition(D, D2))
2541 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002542
Douglas Gregor3996e242010-02-15 22:01:00 +00002543 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002544}
2545
Balazs Keri3b30d652018-10-19 13:32:20 +00002546ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002547 bool IsFriendTemplate = false;
2548 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2549 IsFriendTemplate =
2550 DCXX->getDescribedClassTemplate() &&
2551 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2552 Decl::FOK_None;
2553 }
2554
Douglas Gregor5c73e912010-02-11 00:48:18 +00002555 // If this record has a definition in the translation unit we're coming from,
2556 // but this particular declaration is not that definition, import the
2557 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002558 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002559 if (Definition && Definition != D &&
Balazs Keri0c23dc52018-08-13 13:08:37 +00002560 // Friend template declaration must be imported on its own.
2561 !IsFriendTemplate &&
Gabor Martona3af5672018-05-23 14:24:02 +00002562 // In contrast to a normal CXXRecordDecl, the implicit
2563 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2564 // The definition of the implicit CXXRecordDecl in this case is the
2565 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2566 // condition in order to be able to import the implict Decl.
2567 !D->isImplicit()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002568 ExpectedDecl ImportedDefOrErr = import(Definition);
2569 if (!ImportedDefOrErr)
2570 return ImportedDefOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002571
Balazs Keri3b30d652018-10-19 13:32:20 +00002572 return Importer.MapImported(D, *ImportedDefOrErr);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002573 }
Gabor Martona3af5672018-05-23 14:24:02 +00002574
Douglas Gregor5c73e912010-02-11 00:48:18 +00002575 // Import the major distinguishing characteristics of this record.
2576 DeclContext *DC, *LexicalDC;
2577 DeclarationName Name;
2578 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002579 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002580 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2581 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002582 if (ToD)
2583 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002584
Douglas Gregor5c73e912010-02-11 00:48:18 +00002585 // Figure out what structure name we're looking for.
2586 unsigned IDNS = Decl::IDNS_Tag;
2587 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002588 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002589 if (Error Err = importInto(
2590 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2591 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002592 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002593 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002594 IDNS |= Decl::IDNS_Ordinary;
2595
2596 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002597 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002598 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002599 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002600 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002601 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002602 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002603
2604 if (!FoundDecls.empty()) {
2605 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2606 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2607 D->getASTContext().getExternalSource()->CompleteType(D);
2608 }
2609
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002610 for (auto *FoundDecl : FoundDecls) {
2611 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002612 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002613
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002614 Decl *Found = FoundDecl;
2615 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2616 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002617 Found = Tag->getDecl();
2618 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002619
2620 if (D->getDescribedTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002621 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Martona0df7a92018-05-30 09:19:26 +00002622 Found = Template->getTemplatedDecl();
Balazs Keri3b30d652018-10-19 13:32:20 +00002623 } else {
2624 ConflictingDecls.push_back(FoundDecl);
Gabor Martona0df7a92018-05-30 09:19:26 +00002625 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002626 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002627 }
2628
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002629 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002630 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002631 if (!IsStructuralMatch(D, FoundRecord, false))
2632 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002633 } else {
2634 if (!IsStructuralMatch(D, FoundRecord)) {
2635 ConflictingDecls.push_back(FoundDecl);
2636 continue;
2637 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002638 }
2639
Sean Callanan9092d472017-05-13 00:46:33 +00002640 PrevDecl = FoundRecord;
2641
Douglas Gregor25791052010-02-12 00:09:27 +00002642 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002643 if ((SearchName && !D->isCompleteDefinition() && !IsFriendTemplate)
Douglas Gregordd6006f2012-07-17 21:16:27 +00002644 || (D->isCompleteDefinition() &&
2645 D->isAnonymousStructOrUnion()
Balazs Keri3b30d652018-10-19 13:32:20 +00002646 == FoundDef->isAnonymousStructOrUnion())) {
Douglas Gregor25791052010-02-12 00:09:27 +00002647 // The record types structurally match, or the "from" translation
2648 // unit only had a forward declaration anyway; call it the same
2649 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002650 // FIXME: Structural equivalence check should check for same
2651 // user-defined methods.
2652 Importer.MapImported(D, FoundDef);
2653 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2654 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2655 assert(FoundCXX && "Record type mismatch");
2656
2657 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2658 // FoundDef may not have every implicit method that D has
2659 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002660 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2661 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002662 }
2663 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002664 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002665 if (IsFriendTemplate)
2666 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002667 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002668 // We have a forward declaration of this type, so adopt that forward
2669 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002670
Sean Callananc94711c2014-03-04 18:11:50 +00002671 // If one or both can be completed from external storage then try one
2672 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002673
Sean Callananc94711c2014-03-04 18:11:50 +00002674 if (FoundRecord->hasExternalLexicalStorage() &&
2675 !FoundRecord->isCompleteDefinition())
2676 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2677 if (D->hasExternalLexicalStorage())
2678 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002679
Sean Callananc94711c2014-03-04 18:11:50 +00002680 if (FoundRecord->isCompleteDefinition() &&
2681 D->isCompleteDefinition() &&
Balazs Keri3b30d652018-10-19 13:32:20 +00002682 !IsStructuralMatch(D, FoundRecord)) {
2683 ConflictingDecls.push_back(FoundDecl);
Sean Callananc94711c2014-03-04 18:11:50 +00002684 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002685 }
Balazs Keri0c23dc52018-08-13 13:08:37 +00002686
Douglas Gregor25791052010-02-12 00:09:27 +00002687 AdoptDecl = FoundRecord;
2688 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002689 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002690
2691 continue;
2692 } else if (isa<ValueDecl>(Found))
2693 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002694
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002695 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002696 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002697
Douglas Gregordd6006f2012-07-17 21:16:27 +00002698 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002699 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002700 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002701 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002702 if (!Name)
2703 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002704 }
2705 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002706
Balazs Keri3b30d652018-10-19 13:32:20 +00002707 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2708 if (!BeginLocOrErr)
2709 return BeginLocOrErr.takeError();
2710
Douglas Gregor5c73e912010-02-11 00:48:18 +00002711 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002712 RecordDecl *D2 = AdoptDecl;
2713 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002714 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002715 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002716 if (DCXX->isLambda()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002717 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2718 if (!TInfoOrErr)
2719 return TInfoOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00002720 if (GetImportedOrCreateSpecialDecl(
2721 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002722 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002723 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2724 return D2CXX;
Balazs Keri3b30d652018-10-19 13:32:20 +00002725 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2726 if (!CDeclOrErr)
2727 return CDeclOrErr.takeError();
2728 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002729 } else if (DCXX->isInjectedClassName()) {
2730 // We have to be careful to do a similar dance to the one in
2731 // Sema::ActOnStartCXXMemberDeclarations
2732 CXXRecordDecl *const PrevDecl = nullptr;
2733 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002734 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002735 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002736 Name.getAsIdentifierInfo(), PrevDecl,
2737 DelayTypeCreation))
2738 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002739 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002740 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002741 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002742 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002743 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002744 Name.getAsIdentifierInfo(),
2745 cast_or_null<CXXRecordDecl>(PrevDecl)))
2746 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002747 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002748
Douglas Gregor3996e242010-02-15 22:01:00 +00002749 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002750 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002751 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002752 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002753 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002754
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002755 if (ClassTemplateDecl *FromDescribed =
2756 DCXX->getDescribedClassTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002757 ClassTemplateDecl *ToDescribed;
2758 if (Error Err = importInto(ToDescribed, FromDescribed))
2759 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002760 D2CXX->setDescribedClassTemplate(ToDescribed);
Balazs Keri0c23dc52018-08-13 13:08:37 +00002761 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
Gabor Marton5915777e2018-06-26 13:44:24 +00002762 // In a record describing a template the type should be an
2763 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2764 // previously set type to the correct value here (ToDescribed is not
2765 // available at record create).
2766 // FIXME: The previous type is cleared but not removed from
2767 // ASTContext's internal storage.
2768 CXXRecordDecl *Injected = nullptr;
2769 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2770 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2771 if (Record && Record->isInjectedClassName()) {
2772 Injected = Record;
2773 break;
2774 }
2775 }
2776 D2CXX->setTypeForDecl(nullptr);
2777 Importer.getToContext().getInjectedClassNameType(D2CXX,
2778 ToDescribed->getInjectedClassNameSpecialization());
2779 if (Injected) {
2780 Injected->setTypeForDecl(nullptr);
2781 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2782 }
2783 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002784 } else if (MemberSpecializationInfo *MemberInfo =
2785 DCXX->getMemberSpecializationInfo()) {
2786 TemplateSpecializationKind SK =
2787 MemberInfo->getTemplateSpecializationKind();
2788 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002789
2790 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2791 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2792 else
2793 return ToInstOrErr.takeError();
2794
2795 if (ExpectedSLoc POIOrErr =
2796 import(MemberInfo->getPointOfInstantiation()))
2797 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2798 *POIOrErr);
2799 else
2800 return POIOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002801 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002802
Douglas Gregor25791052010-02-12 00:09:27 +00002803 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002804 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002805 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002806 Name.getAsIdentifierInfo(), PrevDecl))
2807 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002808 D2->setLexicalDeclContext(LexicalDC);
2809 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002810 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002811
Balazs Keri3b30d652018-10-19 13:32:20 +00002812 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2813 D2->setQualifierInfo(*QualifierLocOrErr);
2814 else
2815 return QualifierLocOrErr.takeError();
2816
Douglas Gregordd6006f2012-07-17 21:16:27 +00002817 if (D->isAnonymousStructOrUnion())
2818 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002819 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002820
2821 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002822
Balazs Keri3b30d652018-10-19 13:32:20 +00002823 if (D->isCompleteDefinition())
2824 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2825 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002826
Douglas Gregor3996e242010-02-15 22:01:00 +00002827 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002828}
2829
Balazs Keri3b30d652018-10-19 13:32:20 +00002830ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002831 // Import the major distinguishing characteristics of this enumerator.
2832 DeclContext *DC, *LexicalDC;
2833 DeclarationName Name;
2834 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002835 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002836 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2837 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002838 if (ToD)
2839 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002840
Fangrui Song6907ce22018-07-30 19:24:48 +00002841 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002842 // in the same context.
2843 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002844 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002845 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002846 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002847 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002848 for (auto *FoundDecl : FoundDecls) {
2849 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002850 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002851
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002852 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002853 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002854 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002855 }
2856
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002857 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002858 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002859
Douglas Gregor98c10182010-02-12 22:17:39 +00002860 if (!ConflictingDecls.empty()) {
2861 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002862 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002863 ConflictingDecls.size());
2864 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002865 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002866 }
2867 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002868
Balazs Keri3b30d652018-10-19 13:32:20 +00002869 ExpectedType TypeOrErr = import(D->getType());
2870 if (!TypeOrErr)
2871 return TypeOrErr.takeError();
2872
2873 ExpectedExpr InitOrErr = import(D->getInitExpr());
2874 if (!InitOrErr)
2875 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002876
Gabor Marton26f72a92018-07-12 09:42:05 +00002877 EnumConstantDecl *ToEnumerator;
2878 if (GetImportedOrCreateDecl(
2879 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002880 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002881 return ToEnumerator;
2882
Douglas Gregordd483172010-02-22 17:42:47 +00002883 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002884 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002885 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002886 return ToEnumerator;
2887}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002888
Balazs Keri3b30d652018-10-19 13:32:20 +00002889Error ASTNodeImporter::ImportTemplateInformation(
2890 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002891 switch (FromFD->getTemplatedKind()) {
2892 case FunctionDecl::TK_NonTemplate:
2893 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002894 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002895
2896 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002897 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002898
2899 if (Expected<FunctionDecl *> InstFDOrErr =
2900 import(FromFD->getInstantiatedFromMemberFunction()))
2901 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2902 else
2903 return InstFDOrErr.takeError();
2904
2905 if (ExpectedSLoc POIOrErr = import(
2906 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2907 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2908 else
2909 return POIOrErr.takeError();
2910
2911 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002912 }
2913
2914 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002915 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002916 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002917 if (!FunctionAndArgsOrErr)
2918 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002919
2920 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002921 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002922
Gabor Marton5254e642018-06-27 13:32:50 +00002923 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002924 TemplateArgumentListInfo ToTAInfo;
2925 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002926 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002927 if (Error Err = ImportTemplateArgumentListInfo(
2928 *FromTAArgsAsWritten, ToTAInfo))
2929 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002930
Balazs Keri3b30d652018-10-19 13:32:20 +00002931 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2932 if (!POIOrErr)
2933 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002934
Gabor Marton5254e642018-06-27 13:32:50 +00002935 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002936 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002937 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2938 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2939 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002940 }
2941
2942 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2943 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2944 UnresolvedSet<8> TemplDecls;
2945 unsigned NumTemplates = FromInfo->getNumTemplates();
2946 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002947 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2948 import(FromInfo->getTemplate(I)))
2949 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002950 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002951 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002952 }
2953
2954 // Import TemplateArgumentListInfo.
2955 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002956 if (Error Err = ImportTemplateArgumentListInfo(
2957 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2958 llvm::makeArrayRef(
2959 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2960 ToTAInfo))
2961 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002962
2963 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2964 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002965 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002966 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002967 }
Sam McCallfdc32072018-01-26 12:06:44 +00002968 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002969}
2970
Balazs Keri3b30d652018-10-19 13:32:20 +00002971Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002972ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002973 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002974 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002975 if (!FunctionAndArgsOrErr)
2976 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002977
Balazs Keri3b30d652018-10-19 13:32:20 +00002978 FunctionTemplateDecl *Template;
2979 TemplateArgsTy ToTemplArgs;
2980 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002981 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002982 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002983 return FoundSpec;
2984}
2985
Balazs Keri3b30d652018-10-19 13:32:20 +00002986ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002987
Balazs Keri3b30d652018-10-19 13:32:20 +00002988 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002989 auto RedeclIt = Redecls.begin();
2990 // Import the first part of the decl chain. I.e. import all previous
2991 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002992 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2993 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2994 if (!ToRedeclOrErr)
2995 return ToRedeclOrErr.takeError();
2996 }
Gabor Marton5254e642018-06-27 13:32:50 +00002997 assert(*RedeclIt == D);
2998
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002999 // Import the major distinguishing characteristics of this function.
3000 DeclContext *DC, *LexicalDC;
3001 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003002 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003003 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003004 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3005 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003006 if (ToD)
3007 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003008
Gabor Marton5254e642018-06-27 13:32:50 +00003009 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003010 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003011
Gabor Marton5254e642018-06-27 13:32:50 +00003012 // If this is a function template specialization, then try to find the same
3013 // existing specialization in the "to" context. The localUncachedLookup
3014 // below will not find any specialization, but would find the primary
3015 // template; thus, we have to skip normal lookup in case of specializations.
3016 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3017 if (D->getTemplatedKind() ==
3018 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003019 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3020 if (!FoundFunctionOrErr)
3021 return FoundFunctionOrErr.takeError();
3022 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
3023 if (D->doesThisDeclarationHaveABody() && FoundFunction->hasBody())
3024 return Importer.MapImported(D, FoundFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003025 FoundByLookup = FoundFunction;
3026 }
3027 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003028 // Try to find a function in our own ("to") context with the same name, same
3029 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003030 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003031 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003032 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003033 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003034 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003035 for (auto *FoundDecl : FoundDecls) {
3036 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003037 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003038
Balazs Keria35798d2018-07-17 09:52:41 +00003039 // If template was found, look at the templated function.
3040 if (FromFT) {
3041 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
3042 FoundDecl = Template->getTemplatedDecl();
3043 else
3044 continue;
3045 }
3046
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003047 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003048 if (FoundFunction->hasExternalFormalLinkage() &&
3049 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00003050 if (IsStructuralMatch(D, FoundFunction)) {
3051 const FunctionDecl *Definition = nullptr;
3052 if (D->doesThisDeclarationHaveABody() &&
3053 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003054 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00003055 D, const_cast<FunctionDecl *>(Definition));
3056 }
3057 FoundByLookup = FoundFunction;
3058 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003059 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003060
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003061 // FIXME: Check for overloading more carefully, e.g., by boosting
3062 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003063
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003064 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003065 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003066 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003067
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003068 // Complain about inconsistent function types.
3069 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003070 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003071 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003072 diag::note_odr_value_here)
3073 << FoundFunction->getType();
3074 }
3075 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003076
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003077 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003078 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003079
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003080 if (!ConflictingDecls.empty()) {
3081 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003082 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003083 ConflictingDecls.size());
3084 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003085 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003086 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003087 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003088
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003089 DeclarationNameInfo NameInfo(Name, Loc);
3090 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003091 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3092 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003093
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003094 QualType FromTy = D->getType();
3095 bool usedDifferentExceptionSpec = false;
3096
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003097 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003098 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3099 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3100 // FunctionDecl that we are importing the FunctionProtoType for.
3101 // To avoid an infinite recursion when importing, create the FunctionDecl
3102 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003103 if (FromEPI.ExceptionSpec.SourceDecl ||
3104 FromEPI.ExceptionSpec.SourceTemplate ||
3105 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003106 FunctionProtoType::ExtProtoInfo DefaultEPI;
3107 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003108 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003109 usedDifferentExceptionSpec = true;
3110 }
3111 }
3112
Balazs Keri3b30d652018-10-19 13:32:20 +00003113 QualType T;
3114 TypeSourceInfo *TInfo;
3115 SourceLocation ToInnerLocStart, ToEndLoc;
3116 NestedNameSpecifierLoc ToQualifierLoc;
3117 if (auto Imp = importSeq(
3118 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3119 D->getQualifierLoc(), D->getEndLoc()))
3120 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3121 else
3122 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003123
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003124 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003125 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003126 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003127 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3128 Parameters.push_back(*ToPOrErr);
3129 else
3130 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003131 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003132
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003133 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003134 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003135 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003136 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003137 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3138 ToInnerLocStart, NameInfo, T, TInfo,
3139 FromConstructor->isExplicit(),
3140 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003141 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00003142 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003143 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3144 // Import first, then allocate memory and copy if there was no error.
3145 if (Error Err = ImportContainerChecked(
3146 FromConstructor->inits(), CtorInitializers))
3147 return std::move(Err);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003148 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00003149 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3150 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003151 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00003152 ToCtor->setCtorInitializers(Memory);
3153 ToCtor->setNumCtorInitializers(NumInitializers);
3154 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003155 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003156 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003157 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3158 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3159 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003160 return ToFunction;
3161 } else if (CXXConversionDecl *FromConversion =
3162 dyn_cast<CXXConversionDecl>(D)) {
3163 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3164 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003165 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003166 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3167 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003168 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003169 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3170 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003171 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003172 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3173 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003174 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003175 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003176 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003177 D->getStorageClass(), D->isInlineSpecified(),
3178 D->hasWrittenPrototype(), D->isConstexpr()))
3179 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003180 }
John McCall3e11ebe2010-03-15 10:12:16 +00003181
Balazs Keri3b30d652018-10-19 13:32:20 +00003182 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003183 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003184 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003185 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3186 ToFunction->setTrivial(D->isTrivial());
3187 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003188 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003189
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003190 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003191 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003192 Param->setOwningFunction(ToFunction);
3193 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003194 }
David Blaikie9c70e042011-09-21 18:16:56 +00003195 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003196
Gabor Marton5254e642018-06-27 13:32:50 +00003197 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003198 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00003199 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003200 ToFunction->setPreviousDecl(Recent);
3201 }
3202
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003203 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3204 // params it refers to.
3205 if (TInfo) {
3206 if (auto ProtoLoc =
3207 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3208 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3209 ProtoLoc.setParam(I, Parameters[I]);
3210 }
3211 }
3212
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003213 if (usedDifferentExceptionSpec) {
3214 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003215 if (ExpectedType TyOrErr = import(D->getType()))
3216 ToFunction->setType(*TyOrErr);
3217 else
3218 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003219 }
3220
Balazs Keria35798d2018-07-17 09:52:41 +00003221 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003222 if (FromFT) {
3223 auto ToFTOrErr = import(FromFT);
3224 if (!ToFTOrErr)
3225 return ToFTOrErr.takeError();
3226 }
Balazs Keria35798d2018-07-17 09:52:41 +00003227
Gabor Marton5254e642018-06-27 13:32:50 +00003228 if (D->doesThisDeclarationHaveABody()) {
3229 if (Stmt *FromBody = D->getBody()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003230 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3231 ToFunction->setBody(*ToBodyOrErr);
3232 else
3233 return ToBodyOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00003234 }
3235 }
3236
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003237 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003238
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003239 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003240 if (Error Err = ImportTemplateInformation(D, ToFunction))
3241 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003242
Gabor Marton5254e642018-06-27 13:32:50 +00003243 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3244
3245 // TODO Can we generalize this approach to other AST nodes as well?
3246 if (D->getDeclContext()->containsDeclAndLoad(D))
3247 DC->addDeclInternal(ToFunction);
3248 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003249 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003250
Gabor Marton5254e642018-06-27 13:32:50 +00003251 // Friend declaration's lexical context is the befriending class, but the
3252 // semantic context is the enclosing scope of the befriending class.
3253 // We want the friend functions to be found in the semantic context by lookup.
3254 // FIXME should we handle this generically in VisitFriendDecl?
3255 // In Other cases when LexicalDC != DC we don't want it to be added,
3256 // e.g out-of-class definitions like void B::f() {} .
3257 if (LexicalDC != DC && IsFriend) {
3258 DC->makeDeclVisibleInContext(ToFunction);
3259 }
3260
3261 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003262 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3263 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3264 if (!ToRedeclOrErr)
3265 return ToRedeclOrErr.takeError();
3266 }
Gabor Marton5254e642018-06-27 13:32:50 +00003267
Lang Hames19e07e12017-06-20 21:06:00 +00003268 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3269 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3270
Douglas Gregor43f54792010-02-17 02:12:47 +00003271 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003272}
3273
Balazs Keri3b30d652018-10-19 13:32:20 +00003274ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003275 return VisitFunctionDecl(D);
3276}
3277
Balazs Keri3b30d652018-10-19 13:32:20 +00003278ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003279 return VisitCXXMethodDecl(D);
3280}
3281
Balazs Keri3b30d652018-10-19 13:32:20 +00003282ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003283 return VisitCXXMethodDecl(D);
3284}
3285
Balazs Keri3b30d652018-10-19 13:32:20 +00003286ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003287 return VisitCXXMethodDecl(D);
3288}
3289
Balazs Keri3b30d652018-10-19 13:32:20 +00003290ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003291 // Import the major distinguishing characteristics of a variable.
3292 DeclContext *DC, *LexicalDC;
3293 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003294 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003295 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003296 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3297 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003298 if (ToD)
3299 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003300
Fangrui Song6907ce22018-07-30 19:24:48 +00003301 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003302 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003303 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003304 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003305 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003306 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003307 if (!Name &&
3308 ASTImporter::getFieldIndex(D) !=
3309 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003310 continue;
3311
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003312 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003313 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003314 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003315 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3316 // initializer of a FieldDecl might not had been instantiated in the
3317 // "To" context. However, the "From" context might instantiated that,
3318 // thus we have to merge that.
3319 if (Expr *FromInitializer = D->getInClassInitializer()) {
3320 // We don't have yet the initializer set.
3321 if (FoundField->hasInClassInitializer() &&
3322 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003323 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3324 FoundField->setInClassInitializer(*ToInitializerOrErr);
3325 else {
3326 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003327 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003328 // FIXME: warning message?
3329 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003330 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003331 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003332 }
3333 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003334 return FoundField;
3335 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003336
Balazs Keri3b30d652018-10-19 13:32:20 +00003337 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003338 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3339 << Name << D->getType() << FoundField->getType();
3340 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3341 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003342
3343 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003344 }
3345 }
3346
Balazs Keri3b30d652018-10-19 13:32:20 +00003347 QualType ToType;
3348 TypeSourceInfo *ToTInfo;
3349 Expr *ToBitWidth;
3350 SourceLocation ToInnerLocStart;
3351 Expr *ToInitializer;
3352 if (auto Imp = importSeq(
3353 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3354 D->getInnerLocStart(), D->getInClassInitializer()))
3355 std::tie(
3356 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3357 else
3358 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003359
Gabor Marton26f72a92018-07-12 09:42:05 +00003360 FieldDecl *ToField;
3361 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003362 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3363 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3364 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003365 return ToField;
3366
Douglas Gregordd483172010-02-22 17:42:47 +00003367 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003368 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003369 if (ToInitializer)
3370 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003371 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003372 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003373 return ToField;
3374}
3375
Balazs Keri3b30d652018-10-19 13:32:20 +00003376ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003377 // Import the major distinguishing characteristics of a variable.
3378 DeclContext *DC, *LexicalDC;
3379 DeclarationName Name;
3380 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003381 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003382 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3383 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003384 if (ToD)
3385 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003386
Fangrui Song6907ce22018-07-30 19:24:48 +00003387 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003388 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003389 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003390 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003391 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003392 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003393 if (!Name &&
3394 ASTImporter::getFieldIndex(D) !=
3395 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003396 continue;
3397
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003398 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003399 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003400 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003401 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003402 return FoundField;
3403 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003404
3405 // If there are more anonymous fields to check, continue.
3406 if (!Name && I < N-1)
3407 continue;
3408
Balazs Keri3b30d652018-10-19 13:32:20 +00003409 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003410 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3411 << Name << D->getType() << FoundField->getType();
3412 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3413 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003414
3415 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003416 }
3417 }
3418
Francois Pichet783dd6e2010-11-21 06:08:52 +00003419 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003420 auto TypeOrErr = import(D->getType());
3421 if (!TypeOrErr)
3422 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003423
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003424 auto **NamedChain =
3425 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003426
3427 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003428 for (auto *PI : D->chain())
3429 if (Expected<NamedDecl *> ToD = import(PI))
3430 NamedChain[i++] = *ToD;
3431 else
3432 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003433
Gabor Marton26f72a92018-07-12 09:42:05 +00003434 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3435 IndirectFieldDecl *ToIndirectField;
3436 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003437 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003438 // FIXME here we leak `NamedChain` which is allocated before
3439 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003440
Balazs Keri3b30d652018-10-19 13:32:20 +00003441 for (const auto *Attr : D->attrs())
3442 ToIndirectField->addAttr(Importer.Import(Attr));
Aaron Ballman260995b2014-10-15 16:58:18 +00003443
Francois Pichet783dd6e2010-11-21 06:08:52 +00003444 ToIndirectField->setAccess(D->getAccess());
3445 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003446 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003447 return ToIndirectField;
3448}
3449
Balazs Keri3b30d652018-10-19 13:32:20 +00003450ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003451 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003452 DeclContext *DC, *LexicalDC;
3453 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3454 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003455
3456 // Determine whether we've already imported this decl.
3457 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3458 auto *RD = cast<CXXRecordDecl>(DC);
3459 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003460
3461 while (ImportedFriend) {
3462 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003463 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3464 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003465 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003466
3467 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3468 if (Importer.IsStructurallyEquivalent(
3469 D->getFriendType()->getType(),
3470 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003471 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003472 }
3473 ImportedFriend = ImportedFriend->getNextFriend();
3474 }
3475
3476 // Not found. Create it.
3477 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003478 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003479 NamedDecl *ToFriendD;
3480 if (Error Err = importInto(ToFriendD, FriendD))
3481 return std::move(Err);
3482
3483 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003484 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3485 ToFriendD->setObjectOfFriendDecl(false);
3486
3487 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003488 } else { // The friend is a type, not a decl.
3489 if (auto TSIOrErr = import(D->getFriendType()))
3490 ToFU = *TSIOrErr;
3491 else
3492 return TSIOrErr.takeError();
3493 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003494
3495 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003496 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003497 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003498 if (auto ListOrErr = ImportTemplateParameterList(FromTPLists[I]))
3499 ToTPLists[I] = *ListOrErr;
3500 else
3501 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003502 }
3503
Balazs Keri3b30d652018-10-19 13:32:20 +00003504 auto LocationOrErr = import(D->getLocation());
3505 if (!LocationOrErr)
3506 return LocationOrErr.takeError();
3507 auto FriendLocOrErr = import(D->getFriendLoc());
3508 if (!FriendLocOrErr)
3509 return FriendLocOrErr.takeError();
3510
Gabor Marton26f72a92018-07-12 09:42:05 +00003511 FriendDecl *FrD;
3512 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003513 *LocationOrErr, ToFU,
3514 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003515 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003516
3517 FrD->setAccess(D->getAccess());
3518 FrD->setLexicalDeclContext(LexicalDC);
3519 LexicalDC->addDeclInternal(FrD);
3520 return FrD;
3521}
3522
Balazs Keri3b30d652018-10-19 13:32:20 +00003523ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003524 // Import the major distinguishing characteristics of an ivar.
3525 DeclContext *DC, *LexicalDC;
3526 DeclarationName Name;
3527 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003528 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003529 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3530 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003531 if (ToD)
3532 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003533
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003534 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003535 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003536 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003537 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003538 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003539 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003540 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003541 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003542 return FoundIvar;
3543 }
3544
3545 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3546 << Name << D->getType() << FoundIvar->getType();
3547 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3548 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003549
3550 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003551 }
3552 }
3553
Balazs Keri3b30d652018-10-19 13:32:20 +00003554 QualType ToType;
3555 TypeSourceInfo *ToTypeSourceInfo;
3556 Expr *ToBitWidth;
3557 SourceLocation ToInnerLocStart;
3558 if (auto Imp = importSeq(
3559 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3560 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3561 else
3562 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003563
Gabor Marton26f72a92018-07-12 09:42:05 +00003564 ObjCIvarDecl *ToIvar;
3565 if (GetImportedOrCreateDecl(
3566 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003567 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3568 ToType, ToTypeSourceInfo,
3569 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003570 return ToIvar;
3571
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003572 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003573 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003574 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003575}
3576
Balazs Keri3b30d652018-10-19 13:32:20 +00003577ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003578
3579 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3580 auto RedeclIt = Redecls.begin();
3581 // Import the first part of the decl chain. I.e. import all previous
3582 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003583 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3584 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3585 if (!RedeclOrErr)
3586 return RedeclOrErr.takeError();
3587 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003588 assert(*RedeclIt == D);
3589
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003590 // Import the major distinguishing characteristics of a variable.
3591 DeclContext *DC, *LexicalDC;
3592 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003593 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003594 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003595 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3596 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003597 if (ToD)
3598 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003599
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003600 // Try to find a variable in our own ("to") context with the same name and
3601 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003602 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003603 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003604 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003605 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003606 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003607 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003608 for (auto *FoundDecl : FoundDecls) {
3609 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003610 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003611
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003612 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003613 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003614 if (FoundVar->hasExternalFormalLinkage() &&
3615 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003616 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003617 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003618
3619 // The VarDecl in the "From" context has a definition, but in the
3620 // "To" context we already have a definition.
3621 VarDecl *FoundDef = FoundVar->getDefinition();
3622 if (D->isThisDeclarationADefinition() && FoundDef)
3623 // FIXME Check for ODR error if the two definitions have
3624 // different initializers?
3625 return Importer.MapImported(D, FoundDef);
3626
3627 // The VarDecl in the "From" context has an initializer, but in the
3628 // "To" context we already have an initializer.
3629 const VarDecl *FoundDInit = nullptr;
3630 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3631 // FIXME Diagnose ODR error if the two initializers are different?
3632 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3633
3634 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003635 break;
3636 }
3637
Douglas Gregor56521c52010-02-12 17:23:39 +00003638 const ArrayType *FoundArray
3639 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3640 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003641 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003642 if (FoundArray && TArray) {
3643 if (isa<IncompleteArrayType>(FoundArray) &&
3644 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003645 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003646 if (auto TyOrErr = import(D->getType()))
3647 FoundVar->setType(*TyOrErr);
3648 else
3649 return TyOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003650
Gabor Martonac3a5d62018-09-17 12:04:52 +00003651 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003652 break;
3653 } else if (isa<IncompleteArrayType>(TArray) &&
3654 isa<ConstantArrayType>(FoundArray)) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003655 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003656 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003657 }
3658 }
3659
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003660 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003661 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003662 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3663 << FoundVar->getType();
3664 }
3665 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003666
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003667 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003668 }
3669
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003670 if (!ConflictingDecls.empty()) {
3671 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003672 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003673 ConflictingDecls.size());
3674 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003675 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003676 }
3677 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003678
Balazs Keri3b30d652018-10-19 13:32:20 +00003679 QualType ToType;
3680 TypeSourceInfo *ToTypeSourceInfo;
3681 SourceLocation ToInnerLocStart;
3682 NestedNameSpecifierLoc ToQualifierLoc;
3683 if (auto Imp = importSeq(
3684 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3685 D->getQualifierLoc()))
3686 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3687 else
3688 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003689
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003690 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003691 VarDecl *ToVar;
3692 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003693 ToInnerLocStart, Loc,
3694 Name.getAsIdentifierInfo(),
3695 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003696 D->getStorageClass()))
3697 return ToVar;
3698
Balazs Keri3b30d652018-10-19 13:32:20 +00003699 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003700 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003701 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003702
Gabor Martonac3a5d62018-09-17 12:04:52 +00003703 if (FoundByLookup) {
3704 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3705 ToVar->setPreviousDecl(Recent);
3706 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003707
Balazs Keri3b30d652018-10-19 13:32:20 +00003708 if (Error Err = ImportInitializer(D, ToVar))
3709 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003710
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003711 if (D->isConstexpr())
3712 ToVar->setConstexpr(true);
3713
Gabor Martonac3a5d62018-09-17 12:04:52 +00003714 if (D->getDeclContext()->containsDeclAndLoad(D))
3715 DC->addDeclInternal(ToVar);
3716 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3717 LexicalDC->addDeclInternal(ToVar);
3718
3719 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003720 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3721 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3722 if (!RedeclOrErr)
3723 return RedeclOrErr.takeError();
3724 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003725
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003726 return ToVar;
3727}
3728
Balazs Keri3b30d652018-10-19 13:32:20 +00003729ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003730 // Parameters are created in the translation unit's context, then moved
3731 // into the function declaration's context afterward.
3732 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003733
Balazs Keri3b30d652018-10-19 13:32:20 +00003734 DeclarationName ToDeclName;
3735 SourceLocation ToLocation;
3736 QualType ToType;
3737 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3738 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3739 else
3740 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003741
Douglas Gregor8b228d72010-02-17 21:22:52 +00003742 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003743 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003744 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3745 ToLocation, ToDeclName.getAsIdentifierInfo(),
3746 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003747 return ToParm;
3748 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003749}
3750
Balazs Keri3b30d652018-10-19 13:32:20 +00003751ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003752 // Parameters are created in the translation unit's context, then moved
3753 // into the function declaration's context afterward.
3754 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003755
Balazs Keri3b30d652018-10-19 13:32:20 +00003756 DeclarationName ToDeclName;
3757 SourceLocation ToLocation, ToInnerLocStart;
3758 QualType ToType;
3759 TypeSourceInfo *ToTypeSourceInfo;
3760 if (auto Imp = importSeq(
3761 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3762 D->getTypeSourceInfo()))
3763 std::tie(
3764 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3765 ToTypeSourceInfo) = *Imp;
3766 else
3767 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003768
Gabor Marton26f72a92018-07-12 09:42:05 +00003769 ParmVarDecl *ToParm;
3770 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003771 ToInnerLocStart, ToLocation,
3772 ToDeclName.getAsIdentifierInfo(), ToType,
3773 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003774 /*DefaultArg*/ nullptr))
3775 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003776
3777 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003778 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003779 ToParm->setKNRPromoted(D->isKNRPromoted());
3780
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003781 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003782 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3783 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3784 else
3785 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003786 } else if (D->hasUnparsedDefaultArg()) {
3787 ToParm->setUnparsedDefaultArg();
3788 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003789 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3790 ToParm->setDefaultArg(*ToDefArgOrErr);
3791 else
3792 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003793 }
Sean Callanan59721b32015-04-28 18:41:46 +00003794
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003795 if (D->isObjCMethodParameter()) {
3796 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3797 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3798 } else {
3799 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3800 D->getFunctionScopeIndex());
3801 }
3802
Gabor Marton26f72a92018-07-12 09:42:05 +00003803 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003804}
3805
Balazs Keri3b30d652018-10-19 13:32:20 +00003806ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003807 // Import the major distinguishing characteristics of a method.
3808 DeclContext *DC, *LexicalDC;
3809 DeclarationName Name;
3810 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003811 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003812 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3813 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003814 if (ToD)
3815 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003816
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003817 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003818 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003819 for (auto *FoundDecl : FoundDecls) {
3820 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003821 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3822 continue;
3823
3824 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003825 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3826 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003827 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003828 << D->isInstanceMethod() << Name << D->getReturnType()
3829 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003830 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003831 diag::note_odr_objc_method_here)
3832 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003833
3834 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003835 }
3836
3837 // Check the number of parameters.
3838 if (D->param_size() != FoundMethod->param_size()) {
3839 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3840 << D->isInstanceMethod() << Name
3841 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003842 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003843 diag::note_odr_objc_method_here)
3844 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003845
3846 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003847 }
3848
3849 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003850 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003851 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3852 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003853 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003854 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003855 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003856 diag::err_odr_objc_method_param_type_inconsistent)
3857 << D->isInstanceMethod() << Name
3858 << (*P)->getType() << (*FoundP)->getType();
3859 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3860 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003861
3862 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003863 }
3864 }
3865
3866 // Check variadic/non-variadic.
3867 // Check the number of parameters.
3868 if (D->isVariadic() != FoundMethod->isVariadic()) {
3869 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3870 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003871 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003872 diag::note_odr_objc_method_here)
3873 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003874
3875 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003876 }
3877
3878 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003879 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003880 }
3881 }
3882
Balazs Keri3b30d652018-10-19 13:32:20 +00003883 SourceLocation ToEndLoc;
3884 QualType ToReturnType;
3885 TypeSourceInfo *ToReturnTypeSourceInfo;
3886 if (auto Imp = importSeq(
3887 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3888 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3889 else
3890 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003891
Gabor Marton26f72a92018-07-12 09:42:05 +00003892 ObjCMethodDecl *ToMethod;
3893 if (GetImportedOrCreateDecl(
3894 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003895 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3896 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003897 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3898 D->getImplementationControl(), D->hasRelatedResultType()))
3899 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003900
3901 // FIXME: When we decide to merge method definitions, we'll need to
3902 // deal with implicit parameters.
3903
3904 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003905 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003906 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003907 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3908 ToParams.push_back(*ToPOrErr);
3909 else
3910 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003911 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003912
Douglas Gregor43f54792010-02-17 02:12:47 +00003913 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003914 for (auto *ToParam : ToParams) {
3915 ToParam->setOwningFunction(ToMethod);
3916 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003917 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003918
Balazs Keri3b30d652018-10-19 13:32:20 +00003919 SmallVector<SourceLocation, 12> FromSelLocs;
3920 D->getSelectorLocs(FromSelLocs);
3921 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3922 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3923 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003924
Balazs Keri3b30d652018-10-19 13:32:20 +00003925 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003926
3927 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003928 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003929 return ToMethod;
3930}
3931
Balazs Keri3b30d652018-10-19 13:32:20 +00003932ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003933 // Import the major distinguishing characteristics of a category.
3934 DeclContext *DC, *LexicalDC;
3935 DeclarationName Name;
3936 SourceLocation Loc;
3937 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003938 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3939 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003940 if (ToD)
3941 return ToD;
3942
Balazs Keri3b30d652018-10-19 13:32:20 +00003943 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3944 TypeSourceInfo *ToTypeSourceInfo;
3945 if (auto Imp = importSeq(
3946 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3947 D->getTypeSourceInfo()))
3948 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3949 else
3950 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003951
Gabor Marton26f72a92018-07-12 09:42:05 +00003952 ObjCTypeParamDecl *Result;
3953 if (GetImportedOrCreateDecl(
3954 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003955 ToVarianceLoc, D->getIndex(),
3956 ToLocation, Name.getAsIdentifierInfo(),
3957 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003958 return Result;
3959
Douglas Gregor85f3f952015-07-07 03:57:15 +00003960 Result->setLexicalDeclContext(LexicalDC);
3961 return Result;
3962}
3963
Balazs Keri3b30d652018-10-19 13:32:20 +00003964ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003965 // Import the major distinguishing characteristics of a category.
3966 DeclContext *DC, *LexicalDC;
3967 DeclarationName Name;
3968 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003969 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003970 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3971 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003972 if (ToD)
3973 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003974
Balazs Keri3b30d652018-10-19 13:32:20 +00003975 ObjCInterfaceDecl *ToInterface;
3976 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3977 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003978
Douglas Gregor84c51c32010-02-18 01:47:50 +00003979 // Determine if we've already encountered this category.
3980 ObjCCategoryDecl *MergeWithCategory
3981 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3982 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3983 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003984 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3985 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3986 if (auto Imp = importSeq(
3987 D->getAtStartLoc(), D->getCategoryNameLoc(),
3988 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3989 std::tie(
3990 ToAtStartLoc, ToCategoryNameLoc,
3991 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3992 else
3993 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003994
3995 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003996 ToAtStartLoc, Loc,
3997 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003998 Name.getAsIdentifierInfo(), ToInterface,
3999 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004000 ToIvarLBraceLoc,
4001 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004002 return ToCategory;
4003
Douglas Gregor84c51c32010-02-18 01:47:50 +00004004 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004005 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004006 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004007 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004008 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4009 ToCategory->setTypeParamList(*PListOrErr);
4010 else
4011 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004012
Douglas Gregor84c51c32010-02-18 01:47:50 +00004013 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004014 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4015 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004016 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4017 = D->protocol_loc_begin();
4018 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4019 FromProtoEnd = D->protocol_end();
4020 FromProto != FromProtoEnd;
4021 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004022 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4023 Protocols.push_back(*ToProtoOrErr);
4024 else
4025 return ToProtoOrErr.takeError();
4026
4027 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4028 ProtocolLocs.push_back(*ToProtoLocOrErr);
4029 else
4030 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004031 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004032
Douglas Gregor84c51c32010-02-18 01:47:50 +00004033 // FIXME: If we're merging, make sure that the protocol list is the same.
4034 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4035 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004036
Douglas Gregor84c51c32010-02-18 01:47:50 +00004037 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004038 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004039 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004040
Douglas Gregor84c51c32010-02-18 01:47:50 +00004041 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004042 if (Error Err = ImportDeclContext(D))
4043 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004044
Douglas Gregor84c51c32010-02-18 01:47:50 +00004045 // If we have an implementation, import it as well.
4046 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004047 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4048 import(D->getImplementation()))
4049 ToCategory->setImplementation(*ToImplOrErr);
4050 else
4051 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004052 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004053
Douglas Gregor84c51c32010-02-18 01:47:50 +00004054 return ToCategory;
4055}
4056
Balazs Keri3b30d652018-10-19 13:32:20 +00004057Error ASTNodeImporter::ImportDefinition(
4058 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004059 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004060 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004061 if (Error Err = ImportDeclContext(From))
4062 return Err;
4063 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004064 }
4065
4066 // Start the protocol definition
4067 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004068
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 // Import protocols
4070 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4071 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004072 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4073 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004074 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4075 FromProtoEnd = From->protocol_end();
4076 FromProto != FromProtoEnd;
4077 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004078 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4079 Protocols.push_back(*ToProtoOrErr);
4080 else
4081 return ToProtoOrErr.takeError();
4082
4083 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4084 ProtocolLocs.push_back(*ToProtoLocOrErr);
4085 else
4086 return ToProtoLocOrErr.takeError();
4087
Douglas Gregor2aa53772012-01-24 17:42:07 +00004088 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004089
Douglas Gregor2aa53772012-01-24 17:42:07 +00004090 // FIXME: If we're merging, make sure that the protocol list is the same.
4091 To->setProtocolList(Protocols.data(), Protocols.size(),
4092 ProtocolLocs.data(), Importer.getToContext());
4093
Douglas Gregor2e15c842012-02-01 21:00:38 +00004094 if (shouldForceImportDeclContext(Kind)) {
4095 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004096 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4097 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004098 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004099 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004100}
4101
Balazs Keri3b30d652018-10-19 13:32:20 +00004102ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004103 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004104 // from, but this particular declaration is not that definition, import the
4105 // definition and map to that.
4106 ObjCProtocolDecl *Definition = D->getDefinition();
4107 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004108 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4109 return Importer.MapImported(D, *ImportedDefOrErr);
4110 else
4111 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004112 }
4113
Douglas Gregor84c51c32010-02-18 01:47:50 +00004114 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004115 DeclContext *DC, *LexicalDC;
4116 DeclarationName Name;
4117 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004118 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004119 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4120 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004121 if (ToD)
4122 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004123
Craig Topper36250ad2014-05-12 05:36:57 +00004124 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004125 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004126 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004127 for (auto *FoundDecl : FoundDecls) {
4128 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004129 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004130
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004131 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004132 break;
4133 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004134
Douglas Gregor98d156a2010-02-17 16:12:00 +00004135 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004136 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004137 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4138 if (!ToAtBeginLocOrErr)
4139 return ToAtBeginLocOrErr.takeError();
4140
Gabor Marton26f72a92018-07-12 09:42:05 +00004141 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4142 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004143 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004144 /*PrevDecl=*/nullptr))
4145 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004146 ToProto->setLexicalDeclContext(LexicalDC);
4147 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004148 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004149
4150 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004151
Balazs Keri3b30d652018-10-19 13:32:20 +00004152 if (D->isThisDeclarationADefinition())
4153 if (Error Err = ImportDefinition(D, ToProto))
4154 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004155
Douglas Gregor98d156a2010-02-17 16:12:00 +00004156 return ToProto;
4157}
4158
Balazs Keri3b30d652018-10-19 13:32:20 +00004159ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4160 DeclContext *DC, *LexicalDC;
4161 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4162 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004163
Balazs Keri3b30d652018-10-19 13:32:20 +00004164 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4165 if (!ExternLocOrErr)
4166 return ExternLocOrErr.takeError();
4167
4168 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4169 if (!LangLocOrErr)
4170 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004171
4172 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004173
4174 LinkageSpecDecl *ToLinkageSpec;
4175 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004176 *ExternLocOrErr, *LangLocOrErr,
4177 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004178 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004179
4180 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004181 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4182 if (!RBraceLocOrErr)
4183 return RBraceLocOrErr.takeError();
4184 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004185 }
4186
4187 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4188 LexicalDC->addDeclInternal(ToLinkageSpec);
4189
Sean Callanan0aae0412014-12-10 00:00:37 +00004190 return ToLinkageSpec;
4191}
4192
Balazs Keri3b30d652018-10-19 13:32:20 +00004193ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004194 DeclContext *DC, *LexicalDC;
4195 DeclarationName Name;
4196 SourceLocation Loc;
4197 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004198 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4199 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004200 if (ToD)
4201 return ToD;
4202
Balazs Keri3b30d652018-10-19 13:32:20 +00004203 SourceLocation ToLoc, ToUsingLoc;
4204 NestedNameSpecifierLoc ToQualifierLoc;
4205 if (auto Imp = importSeq(
4206 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4207 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4208 else
4209 return Imp.takeError();
4210
4211 DeclarationNameInfo NameInfo(Name, ToLoc);
4212 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4213 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004214
Gabor Marton26f72a92018-07-12 09:42:05 +00004215 UsingDecl *ToUsing;
4216 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004217 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004218 D->hasTypename()))
4219 return ToUsing;
4220
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004221 ToUsing->setLexicalDeclContext(LexicalDC);
4222 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004223
4224 if (NamedDecl *FromPattern =
4225 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004226 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4227 Importer.getToContext().setInstantiatedFromUsingDecl(
4228 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004229 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004230 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004231 }
4232
Balazs Keri3b30d652018-10-19 13:32:20 +00004233 for (UsingShadowDecl *FromShadow : D->shadows()) {
4234 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4235 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004236 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004237 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004238 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004239 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004240 }
4241 return ToUsing;
4242}
4243
Balazs Keri3b30d652018-10-19 13:32:20 +00004244ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004245 DeclContext *DC, *LexicalDC;
4246 DeclarationName Name;
4247 SourceLocation Loc;
4248 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004249 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4250 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004251 if (ToD)
4252 return ToD;
4253
Balazs Keri3b30d652018-10-19 13:32:20 +00004254 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4255 if (!ToUsingOrErr)
4256 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004257
Balazs Keri3b30d652018-10-19 13:32:20 +00004258 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4259 if (!ToTargetOrErr)
4260 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004261
Gabor Marton26f72a92018-07-12 09:42:05 +00004262 UsingShadowDecl *ToShadow;
4263 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004264 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004265 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004266
4267 ToShadow->setLexicalDeclContext(LexicalDC);
4268 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004269
4270 if (UsingShadowDecl *FromPattern =
4271 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004272 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4273 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4274 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004275 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004276 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004277 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004278 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004279 }
4280
4281 LexicalDC->addDeclInternal(ToShadow);
4282
4283 return ToShadow;
4284}
4285
Balazs Keri3b30d652018-10-19 13:32:20 +00004286ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004287 DeclContext *DC, *LexicalDC;
4288 DeclarationName Name;
4289 SourceLocation Loc;
4290 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004291 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4292 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004293 if (ToD)
4294 return ToD;
4295
Balazs Keri3b30d652018-10-19 13:32:20 +00004296 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4297 if (!ToComAncestorOrErr)
4298 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004299
Balazs Keri3b30d652018-10-19 13:32:20 +00004300 NamespaceDecl *ToNominatedNamespace;
4301 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4302 NestedNameSpecifierLoc ToQualifierLoc;
4303 if (auto Imp = importSeq(
4304 D->getNominatedNamespace(), D->getUsingLoc(),
4305 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4306 D->getIdentLocation()))
4307 std::tie(
4308 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4309 ToQualifierLoc, ToIdentLocation) = *Imp;
4310 else
4311 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004312
Gabor Marton26f72a92018-07-12 09:42:05 +00004313 UsingDirectiveDecl *ToUsingDir;
4314 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004315 ToUsingLoc,
4316 ToNamespaceKeyLocation,
4317 ToQualifierLoc,
4318 ToIdentLocation,
4319 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004320 return ToUsingDir;
4321
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004322 ToUsingDir->setLexicalDeclContext(LexicalDC);
4323 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004324
4325 return ToUsingDir;
4326}
4327
Balazs Keri3b30d652018-10-19 13:32:20 +00004328ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004329 UnresolvedUsingValueDecl *D) {
4330 DeclContext *DC, *LexicalDC;
4331 DeclarationName Name;
4332 SourceLocation Loc;
4333 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004334 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4335 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004336 if (ToD)
4337 return ToD;
4338
Balazs Keri3b30d652018-10-19 13:32:20 +00004339 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4340 NestedNameSpecifierLoc ToQualifierLoc;
4341 if (auto Imp = importSeq(
4342 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4343 D->getEllipsisLoc()))
4344 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4345 else
4346 return Imp.takeError();
4347
4348 DeclarationNameInfo NameInfo(Name, ToLoc);
4349 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4350 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004351
Gabor Marton26f72a92018-07-12 09:42:05 +00004352 UnresolvedUsingValueDecl *ToUsingValue;
4353 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004354 ToUsingLoc, ToQualifierLoc, NameInfo,
4355 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004356 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004357
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004358 ToUsingValue->setAccess(D->getAccess());
4359 ToUsingValue->setLexicalDeclContext(LexicalDC);
4360 LexicalDC->addDeclInternal(ToUsingValue);
4361
4362 return ToUsingValue;
4363}
4364
Balazs Keri3b30d652018-10-19 13:32:20 +00004365ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004366 UnresolvedUsingTypenameDecl *D) {
4367 DeclContext *DC, *LexicalDC;
4368 DeclarationName Name;
4369 SourceLocation Loc;
4370 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004371 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4372 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004373 if (ToD)
4374 return ToD;
4375
Balazs Keri3b30d652018-10-19 13:32:20 +00004376 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4377 NestedNameSpecifierLoc ToQualifierLoc;
4378 if (auto Imp = importSeq(
4379 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4380 D->getEllipsisLoc()))
4381 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4382 else
4383 return Imp.takeError();
4384
Gabor Marton26f72a92018-07-12 09:42:05 +00004385 UnresolvedUsingTypenameDecl *ToUsing;
4386 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004387 ToUsingLoc, ToTypenameLoc,
4388 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004389 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004390
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004391 ToUsing->setAccess(D->getAccess());
4392 ToUsing->setLexicalDeclContext(LexicalDC);
4393 LexicalDC->addDeclInternal(ToUsing);
4394
4395 return ToUsing;
4396}
4397
Balazs Keri3b30d652018-10-19 13:32:20 +00004398
4399Error ASTNodeImporter::ImportDefinition(
4400 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004401 if (To->getDefinition()) {
4402 // Check consistency of superclass.
4403 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4404 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004405 if (auto FromSuperOrErr = import(FromSuper))
4406 FromSuper = *FromSuperOrErr;
4407 else
4408 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004409 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004410
4411 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004412 if ((bool)FromSuper != (bool)ToSuper ||
4413 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004414 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004415 diag::err_odr_objc_superclass_inconsistent)
4416 << To->getDeclName();
4417 if (ToSuper)
4418 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4419 << To->getSuperClass()->getDeclName();
4420 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004421 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004422 diag::note_odr_objc_missing_superclass);
4423 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004424 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004425 diag::note_odr_objc_superclass)
4426 << From->getSuperClass()->getDeclName();
4427 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004428 Importer.FromDiag(From->getLocation(),
4429 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004430 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004431
Douglas Gregor2e15c842012-02-01 21:00:38 +00004432 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004433 if (Error Err = ImportDeclContext(From))
4434 return Err;
4435 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004436 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004437
Douglas Gregor2aa53772012-01-24 17:42:07 +00004438 // Start the definition.
4439 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004440
Douglas Gregor2aa53772012-01-24 17:42:07 +00004441 // If this class has a superclass, import it.
4442 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004443 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4444 To->setSuperClass(*SuperTInfoOrErr);
4445 else
4446 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004447 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004448
Douglas Gregor2aa53772012-01-24 17:42:07 +00004449 // Import protocols
4450 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4451 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004452 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4453 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004454
Douglas Gregor2aa53772012-01-24 17:42:07 +00004455 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4456 FromProtoEnd = From->protocol_end();
4457 FromProto != FromProtoEnd;
4458 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004459 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4460 Protocols.push_back(*ToProtoOrErr);
4461 else
4462 return ToProtoOrErr.takeError();
4463
4464 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4465 ProtocolLocs.push_back(*ToProtoLocOrErr);
4466 else
4467 return ToProtoLocOrErr.takeError();
4468
Douglas Gregor2aa53772012-01-24 17:42:07 +00004469 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004470
Douglas Gregor2aa53772012-01-24 17:42:07 +00004471 // FIXME: If we're merging, make sure that the protocol list is the same.
4472 To->setProtocolList(Protocols.data(), Protocols.size(),
4473 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004474
Douglas Gregor2aa53772012-01-24 17:42:07 +00004475 // Import categories. When the categories themselves are imported, they'll
4476 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004477 for (auto *Cat : From->known_categories()) {
4478 auto ToCatOrErr = import(Cat);
4479 if (!ToCatOrErr)
4480 return ToCatOrErr.takeError();
4481 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004482
Douglas Gregor2aa53772012-01-24 17:42:07 +00004483 // If we have an @implementation, import it as well.
4484 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004485 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4486 import(From->getImplementation()))
4487 To->setImplementation(*ToImplOrErr);
4488 else
4489 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004490 }
4491
Douglas Gregor2e15c842012-02-01 21:00:38 +00004492 if (shouldForceImportDeclContext(Kind)) {
4493 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004494 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4495 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004496 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004497 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004498}
4499
Balazs Keri3b30d652018-10-19 13:32:20 +00004500Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004501ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4502 if (!list)
4503 return nullptr;
4504
4505 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004506 for (auto *fromTypeParam : *list) {
4507 if (auto toTypeParamOrErr = import(fromTypeParam))
4508 toTypeParams.push_back(*toTypeParamOrErr);
4509 else
4510 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004511 }
4512
Balazs Keri3b30d652018-10-19 13:32:20 +00004513 auto LAngleLocOrErr = import(list->getLAngleLoc());
4514 if (!LAngleLocOrErr)
4515 return LAngleLocOrErr.takeError();
4516
4517 auto RAngleLocOrErr = import(list->getRAngleLoc());
4518 if (!RAngleLocOrErr)
4519 return RAngleLocOrErr.takeError();
4520
Douglas Gregor85f3f952015-07-07 03:57:15 +00004521 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004522 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004523 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004524 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004525}
4526
Balazs Keri3b30d652018-10-19 13:32:20 +00004527ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004528 // If this class has a definition in the translation unit we're coming from,
4529 // but this particular declaration is not that definition, import the
4530 // definition and map to that.
4531 ObjCInterfaceDecl *Definition = D->getDefinition();
4532 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004533 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4534 return Importer.MapImported(D, *ImportedDefOrErr);
4535 else
4536 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004537 }
4538
Douglas Gregor45635322010-02-16 01:20:57 +00004539 // Import the major distinguishing characteristics of an @interface.
4540 DeclContext *DC, *LexicalDC;
4541 DeclarationName Name;
4542 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004543 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004544 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4545 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004546 if (ToD)
4547 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004548
Douglas Gregor2aa53772012-01-24 17:42:07 +00004549 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004550 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004551 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004552 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004553 for (auto *FoundDecl : FoundDecls) {
4554 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004555 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004556
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004557 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004558 break;
4559 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004560
Douglas Gregor2aa53772012-01-24 17:42:07 +00004561 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004562 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004563 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004564 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4565 if (!AtBeginLocOrErr)
4566 return AtBeginLocOrErr.takeError();
4567
Gabor Marton26f72a92018-07-12 09:42:05 +00004568 if (GetImportedOrCreateDecl(
4569 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004570 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004571 /*TypeParamList=*/nullptr,
4572 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4573 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004574 ToIface->setLexicalDeclContext(LexicalDC);
4575 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004576 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004577 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004578 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004579 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004580 if (auto ToPListOrErr =
4581 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4582 ToIface->setTypeParamList(*ToPListOrErr);
4583 else
4584 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004585
Balazs Keri3b30d652018-10-19 13:32:20 +00004586 if (D->isThisDeclarationADefinition())
4587 if (Error Err = ImportDefinition(D, ToIface))
4588 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004589
Douglas Gregor98d156a2010-02-17 16:12:00 +00004590 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004591}
4592
Balazs Keri3b30d652018-10-19 13:32:20 +00004593ExpectedDecl
4594ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4595 ObjCCategoryDecl *Category;
4596 if (Error Err = importInto(Category, D->getCategoryDecl()))
4597 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004598
Douglas Gregor4da9d682010-12-07 15:32:12 +00004599 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4600 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004601 DeclContext *DC, *LexicalDC;
4602 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4603 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004604
Balazs Keri3b30d652018-10-19 13:32:20 +00004605 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4606 if (auto Imp = importSeq(
4607 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4608 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4609 else
4610 return Imp.takeError();
4611
Gabor Marton26f72a92018-07-12 09:42:05 +00004612 if (GetImportedOrCreateDecl(
4613 ToImpl, D, Importer.getToContext(), DC,
4614 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004615 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004616 return ToImpl;
4617
Balazs Keri3b30d652018-10-19 13:32:20 +00004618 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004619 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004620 Category->setImplementation(ToImpl);
4621 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004622
Gabor Marton26f72a92018-07-12 09:42:05 +00004623 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004624 if (Error Err = ImportDeclContext(D))
4625 return std::move(Err);
4626
Douglas Gregor4da9d682010-12-07 15:32:12 +00004627 return ToImpl;
4628}
4629
Balazs Keri3b30d652018-10-19 13:32:20 +00004630ExpectedDecl
4631ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004632 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004633 ObjCInterfaceDecl *Iface;
4634 if (Error Err = importInto(Iface, D->getClassInterface()))
4635 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004636
4637 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004638 ObjCInterfaceDecl *Super;
4639 if (Error Err = importInto(Super, D->getSuperClass()))
4640 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004641
4642 ObjCImplementationDecl *Impl = Iface->getImplementation();
4643 if (!Impl) {
4644 // We haven't imported an implementation yet. Create a new @implementation
4645 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004646 DeclContext *DC, *LexicalDC;
4647 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4648 return std::move(Err);
4649
4650 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4651 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4652 if (auto Imp = importSeq(
4653 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4654 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4655 std::tie(
4656 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4657 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4658 else
4659 return Imp.takeError();
4660
Gabor Marton26f72a92018-07-12 09:42:05 +00004661 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004662 DC, Iface, Super,
4663 ToLocation,
4664 ToAtStartLoc,
4665 ToSuperClassLoc,
4666 ToIvarLBraceLoc,
4667 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004668 return Impl;
4669
Balazs Keri3b30d652018-10-19 13:32:20 +00004670 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004671
Douglas Gregorda8025c2010-12-07 01:26:03 +00004672 // Associate the implementation with the class it implements.
4673 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004674 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004675 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004676 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004677
4678 // Verify that the existing @implementation has the same superclass.
4679 if ((Super && !Impl->getSuperClass()) ||
4680 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004681 (Super && Impl->getSuperClass() &&
4682 !declaresSameEntity(Super->getCanonicalDecl(),
4683 Impl->getSuperClass()))) {
4684 Importer.ToDiag(Impl->getLocation(),
4685 diag::err_odr_objc_superclass_inconsistent)
4686 << Iface->getDeclName();
4687 // FIXME: It would be nice to have the location of the superclass
4688 // below.
4689 if (Impl->getSuperClass())
4690 Importer.ToDiag(Impl->getLocation(),
4691 diag::note_odr_objc_superclass)
4692 << Impl->getSuperClass()->getDeclName();
4693 else
4694 Importer.ToDiag(Impl->getLocation(),
4695 diag::note_odr_objc_missing_superclass);
4696 if (D->getSuperClass())
4697 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004698 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004699 << D->getSuperClass()->getDeclName();
4700 else
4701 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004702 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004703
4704 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004705 }
4706 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004707
Douglas Gregorda8025c2010-12-07 01:26:03 +00004708 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004709 if (Error Err = ImportDeclContext(D))
4710 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004711
4712 return Impl;
4713}
4714
Balazs Keri3b30d652018-10-19 13:32:20 +00004715ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004716 // Import the major distinguishing characteristics of an @property.
4717 DeclContext *DC, *LexicalDC;
4718 DeclarationName Name;
4719 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004720 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004721 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4722 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004723 if (ToD)
4724 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004725
4726 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004727 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004728 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004729 for (auto *FoundDecl : FoundDecls) {
4730 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004731 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004732 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004733 FoundProp->getType())) {
4734 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4735 << Name << D->getType() << FoundProp->getType();
4736 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4737 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004738
4739 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004740 }
4741
4742 // FIXME: Check property attributes, getters, setters, etc.?
4743
4744 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004745 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004746 return FoundProp;
4747 }
4748 }
4749
Balazs Keri3b30d652018-10-19 13:32:20 +00004750 QualType ToType;
4751 TypeSourceInfo *ToTypeSourceInfo;
4752 SourceLocation ToAtLoc, ToLParenLoc;
4753 if (auto Imp = importSeq(
4754 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4755 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4756 else
4757 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004758
4759 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004760 ObjCPropertyDecl *ToProperty;
4761 if (GetImportedOrCreateDecl(
4762 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004763 Name.getAsIdentifierInfo(), ToAtLoc,
4764 ToLParenLoc, ToType,
4765 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004766 return ToProperty;
4767
Balazs Keri3b30d652018-10-19 13:32:20 +00004768 Selector ToGetterName, ToSetterName;
4769 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4770 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4771 ObjCIvarDecl *ToPropertyIvarDecl;
4772 if (auto Imp = importSeq(
4773 D->getGetterName(), D->getSetterName(),
4774 D->getGetterNameLoc(), D->getSetterNameLoc(),
4775 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4776 D->getPropertyIvarDecl()))
4777 std::tie(
4778 ToGetterName, ToSetterName,
4779 ToGetterNameLoc, ToSetterNameLoc,
4780 ToGetterMethodDecl, ToSetterMethodDecl,
4781 ToPropertyIvarDecl) = *Imp;
4782 else
4783 return Imp.takeError();
4784
Douglas Gregora11c4582010-02-17 18:02:10 +00004785 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004786 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004787
4788 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004789 ToProperty->setPropertyAttributesAsWritten(
4790 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004791 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4792 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4793 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4794 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4795 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004796 return ToProperty;
4797}
4798
Balazs Keri3b30d652018-10-19 13:32:20 +00004799ExpectedDecl
4800ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4801 ObjCPropertyDecl *Property;
4802 if (Error Err = importInto(Property, D->getPropertyDecl()))
4803 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004804
Balazs Keri3b30d652018-10-19 13:32:20 +00004805 DeclContext *DC, *LexicalDC;
4806 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4807 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004808
Balazs Keri3b30d652018-10-19 13:32:20 +00004809 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004810
4811 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004812 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004813 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4814 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004815
4816 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004817 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4818 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004819 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004820 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4821 if (auto Imp = importSeq(
4822 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4823 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4824 else
4825 return Imp.takeError();
4826
Gabor Marton26f72a92018-07-12 09:42:05 +00004827 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004828 ToBeginLoc,
4829 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004830 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004831 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004832 return ToImpl;
4833
Douglas Gregor14a49e22010-12-07 18:32:03 +00004834 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004835 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004836 } else {
4837 // Check that we have the same kind of property implementation (@synthesize
4838 // vs. @dynamic).
4839 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004840 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004841 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004842 << Property->getDeclName()
4843 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004844 == ObjCPropertyImplDecl::Dynamic);
4845 Importer.FromDiag(D->getLocation(),
4846 diag::note_odr_objc_property_impl_kind)
4847 << D->getPropertyDecl()->getDeclName()
4848 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004849
4850 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004851 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004852
4853 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004854 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4855 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004856 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004857 diag::err_odr_objc_synthesize_ivar_inconsistent)
4858 << Property->getDeclName()
4859 << ToImpl->getPropertyIvarDecl()->getDeclName()
4860 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004861 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004862 diag::note_odr_objc_synthesize_ivar_here)
4863 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004864
4865 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004866 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004867
Douglas Gregor14a49e22010-12-07 18:32:03 +00004868 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004869 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004870 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004871
Douglas Gregor14a49e22010-12-07 18:32:03 +00004872 return ToImpl;
4873}
4874
Balazs Keri3b30d652018-10-19 13:32:20 +00004875ExpectedDecl
4876ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004877 // For template arguments, we adopt the translation unit as our declaration
4878 // context. This context will be fixed when the actual template declaration
4879 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004880
Douglas Gregora082a492010-11-30 19:14:50 +00004881 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004882
4883 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4884 if (!BeginLocOrErr)
4885 return BeginLocOrErr.takeError();
4886
4887 ExpectedSLoc LocationOrErr = import(D->getLocation());
4888 if (!LocationOrErr)
4889 return LocationOrErr.takeError();
4890
Gabor Marton26f72a92018-07-12 09:42:05 +00004891 TemplateTypeParmDecl *ToD = nullptr;
4892 (void)GetImportedOrCreateDecl(
4893 ToD, D, Importer.getToContext(),
4894 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004895 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004896 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4897 D->wasDeclaredWithTypename(), D->isParameterPack());
4898 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004899}
4900
Balazs Keri3b30d652018-10-19 13:32:20 +00004901ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004902ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004903 DeclarationName ToDeclName;
4904 SourceLocation ToLocation, ToInnerLocStart;
4905 QualType ToType;
4906 TypeSourceInfo *ToTypeSourceInfo;
4907 if (auto Imp = importSeq(
4908 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4909 D->getInnerLocStart()))
4910 std::tie(
4911 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4912 ToInnerLocStart) = *Imp;
4913 else
4914 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004915
Douglas Gregora082a492010-11-30 19:14:50 +00004916 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004917
4918 NonTypeTemplateParmDecl *ToD = nullptr;
4919 (void)GetImportedOrCreateDecl(
4920 ToD, D, Importer.getToContext(),
4921 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004922 ToInnerLocStart, ToLocation, D->getDepth(),
4923 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4924 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004925 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004926}
4927
Balazs Keri3b30d652018-10-19 13:32:20 +00004928ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004929ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4930 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004931 auto NameOrErr = import(D->getDeclName());
4932 if (!NameOrErr)
4933 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004934
Douglas Gregora082a492010-11-30 19:14:50 +00004935 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004936 ExpectedSLoc LocationOrErr = import(D->getLocation());
4937 if (!LocationOrErr)
4938 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004939
Douglas Gregora082a492010-11-30 19:14:50 +00004940 // Import template parameters.
Balazs Keri3b30d652018-10-19 13:32:20 +00004941 auto TemplateParamsOrErr = ImportTemplateParameterList(
4942 D->getTemplateParameters());
4943 if (!TemplateParamsOrErr)
4944 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004945
Douglas Gregora082a492010-11-30 19:14:50 +00004946 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004947
4948 TemplateTemplateParmDecl *ToD = nullptr;
4949 (void)GetImportedOrCreateDecl(
4950 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004951 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4952 D->getDepth(), D->getPosition(), D->isParameterPack(),
4953 (*NameOrErr).getAsIdentifierInfo(),
4954 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004955 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004956}
4957
Gabor Marton9581c332018-05-23 13:53:36 +00004958// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4959// it has any definition in the redecl chain.
4960static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4961 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4962 if (!ToTemplatedDef)
4963 return nullptr;
4964 ClassTemplateDecl *TemplateWithDef =
4965 ToTemplatedDef->getDescribedClassTemplate();
4966 return TemplateWithDef;
4967}
4968
Balazs Keri3b30d652018-10-19 13:32:20 +00004969ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004970 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4971
Douglas Gregora082a492010-11-30 19:14:50 +00004972 // If this record has a definition in the translation unit we're coming from,
4973 // but this particular declaration is not that definition, import the
4974 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004975 auto *Definition =
4976 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Balazs Keri0c23dc52018-08-13 13:08:37 +00004977 if (Definition && Definition != D->getTemplatedDecl() && !IsFriend) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004978 if (ExpectedDecl ImportedDefOrErr = import(
4979 Definition->getDescribedClassTemplate()))
4980 return Importer.MapImported(D, *ImportedDefOrErr);
4981 else
4982 return ImportedDefOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00004983 }
Gabor Marton9581c332018-05-23 13:53:36 +00004984
Douglas Gregora082a492010-11-30 19:14:50 +00004985 // Import the major distinguishing characteristics of this class template.
4986 DeclContext *DC, *LexicalDC;
4987 DeclarationName Name;
4988 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004989 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004990 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4991 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004992 if (ToD)
4993 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004994
Douglas Gregora082a492010-11-30 19:14:50 +00004995 // We may already have a template of the same name; try to find and match it.
4996 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004997 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004998 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004999 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005000 for (auto *FoundDecl : FoundDecls) {
5001 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00005002 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005003
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005004 Decl *Found = FoundDecl;
5005 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00005006
5007 // The class to be imported is a definition.
5008 if (D->isThisDeclarationADefinition()) {
5009 // Lookup will find the fwd decl only if that is more recent than the
5010 // definition. So, try to get the definition if that is available in
5011 // the redecl chain.
5012 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005013 if (TemplateWithDef)
5014 FoundTemplate = TemplateWithDef;
5015 else
Gabor Marton9581c332018-05-23 13:53:36 +00005016 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005017 }
5018
Douglas Gregora082a492010-11-30 19:14:50 +00005019 if (IsStructuralMatch(D, FoundTemplate)) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005020 if (!IsFriend) {
5021 Importer.MapImported(D->getTemplatedDecl(),
5022 FoundTemplate->getTemplatedDecl());
5023 return Importer.MapImported(D, FoundTemplate);
5024 }
Aleksei Sidorin761c2242018-05-15 11:09:07 +00005025
Balazs Keri0c23dc52018-08-13 13:08:37 +00005026 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005027 }
Douglas Gregora082a492010-11-30 19:14:50 +00005028 }
Gabor Marton9581c332018-05-23 13:53:36 +00005029
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005030 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005031 }
Gabor Marton9581c332018-05-23 13:53:36 +00005032
Douglas Gregora082a492010-11-30 19:14:50 +00005033 if (!ConflictingDecls.empty()) {
5034 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005035 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005036 ConflictingDecls.size());
5037 }
Gabor Marton9581c332018-05-23 13:53:36 +00005038
Douglas Gregora082a492010-11-30 19:14:50 +00005039 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005040 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005041 }
5042
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005043 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5044
Douglas Gregora082a492010-11-30 19:14:50 +00005045 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005046 CXXRecordDecl *ToTemplated;
5047 if (Error Err = importInto(ToTemplated, FromTemplated))
5048 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005049
Douglas Gregora082a492010-11-30 19:14:50 +00005050 // Create the class template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005051 auto TemplateParamsOrErr = ImportTemplateParameterList(
5052 D->getTemplateParameters());
5053 if (!TemplateParamsOrErr)
5054 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005055
Gabor Marton26f72a92018-07-12 09:42:05 +00005056 ClassTemplateDecl *D2;
5057 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005058 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005059 return D2;
5060
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005061 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005062
Balazs Keri0c23dc52018-08-13 13:08:37 +00005063 if (ToTemplated->getPreviousDecl()) {
5064 assert(
5065 ToTemplated->getPreviousDecl()->getDescribedClassTemplate() &&
5066 "Missing described template");
5067 D2->setPreviousDecl(
5068 ToTemplated->getPreviousDecl()->getDescribedClassTemplate());
5069 }
Douglas Gregora082a492010-11-30 19:14:50 +00005070 D2->setAccess(D->getAccess());
5071 D2->setLexicalDeclContext(LexicalDC);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005072 if (!IsFriend)
5073 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005074
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005075 if (FromTemplated->isCompleteDefinition() &&
5076 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005077 // FIXME: Import definition!
5078 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005079
Douglas Gregora082a492010-11-30 19:14:50 +00005080 return D2;
5081}
5082
Balazs Keri3b30d652018-10-19 13:32:20 +00005083ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005084 ClassTemplateSpecializationDecl *D) {
5085 // If this record has a definition in the translation unit we're coming from,
5086 // but this particular declaration is not that definition, import the
5087 // definition and map to that.
5088 TagDecl *Definition = D->getDefinition();
5089 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005090 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5091 return Importer.MapImported(D, *ImportedDefOrErr);
5092 else
5093 return ImportedDefOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005094 }
5095
Balazs Keri3b30d652018-10-19 13:32:20 +00005096 ClassTemplateDecl *ClassTemplate;
5097 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5098 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005099
Douglas Gregore2e50d332010-12-01 01:36:18 +00005100 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005101 DeclContext *DC, *LexicalDC;
5102 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5103 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005104
5105 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005106 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005107 if (Error Err = ImportTemplateArguments(
5108 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5109 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005110
Douglas Gregore2e50d332010-12-01 01:36:18 +00005111 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005112 void *InsertPos = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005113 ClassTemplateSpecializationDecl *D2 = nullptr;
5114 ClassTemplatePartialSpecializationDecl *PartialSpec =
5115 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5116 if (PartialSpec)
5117 D2 = ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
5118 else
5119 D2 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
5120 ClassTemplateSpecializationDecl * const PrevDecl = D2;
5121 RecordDecl *FoundDef = D2 ? D2->getDefinition() : nullptr;
5122 if (FoundDef) {
5123 if (!D->isCompleteDefinition()) {
5124 // The "From" translation unit only had a forward declaration; call it
5125 // the same declaration.
5126 // TODO Handle the redecl chain properly!
5127 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005128 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005129
5130 if (IsStructuralMatch(D, FoundDef)) {
5131
5132 Importer.MapImported(D, FoundDef);
5133
5134 // Import those those default field initializers which have been
5135 // instantiated in the "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005136 for (auto *FromField : D->fields()) {
5137 auto ToOrErr = import(FromField);
5138 if (!ToOrErr)
5139 // FIXME: return the error?
5140 consumeError(ToOrErr.takeError());
5141 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005142
5143 // Import those methods which have been instantiated in the
5144 // "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005145 for (CXXMethodDecl *FromM : D->methods()) {
5146 auto ToOrErr = import(FromM);
5147 if (!ToOrErr)
5148 // FIXME: return the error?
5149 consumeError(ToOrErr.takeError());
5150 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005151
5152 // TODO Import instantiated default arguments.
5153 // TODO Import instantiated exception specifications.
5154 //
5155 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint what
5156 // else could be fused during an AST merge.
5157
5158 return FoundDef;
5159 }
5160 } else { // We either couldn't find any previous specialization in the "To"
5161 // context, or we found one but without definition. Let's create a
5162 // new specialization and register that at the class template.
Balazs Keri3b30d652018-10-19 13:32:20 +00005163
5164 // Import the location of this declaration.
5165 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5166 if (!BeginLocOrErr)
5167 return BeginLocOrErr.takeError();
5168 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5169 if (!IdLocOrErr)
5170 return IdLocOrErr.takeError();
5171
Gabor Marton42e15de2018-08-22 11:52:14 +00005172 if (PartialSpec) {
5173 // Import TemplateArgumentListInfo.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005174 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005175 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Balazs Keri3b30d652018-10-19 13:32:20 +00005176 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5177 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005178
Balazs Keri3b30d652018-10-19 13:32:20 +00005179 QualType CanonInjType;
5180 if (Error Err = importInto(
5181 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5182 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005183 CanonInjType = CanonInjType.getCanonicalType();
5184
Balazs Keri3b30d652018-10-19 13:32:20 +00005185 auto ToTPListOrErr = ImportTemplateParameterList(
5186 PartialSpec->getTemplateParameters());
5187 if (!ToTPListOrErr)
5188 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005189
Gabor Marton26f72a92018-07-12 09:42:05 +00005190 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00005191 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5192 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
Gabor Marton26f72a92018-07-12 09:42:05 +00005193 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
Gabor Marton42e15de2018-08-22 11:52:14 +00005194 ToTAInfo, CanonInjType,
5195 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
Gabor Marton26f72a92018-07-12 09:42:05 +00005196 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005197
Gabor Marton42e15de2018-08-22 11:52:14 +00005198 // Update InsertPos, because preceding import calls may have invalidated
5199 // it by adding new specializations.
5200 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5201 // Add this partial specialization to the class template.
5202 ClassTemplate->AddPartialSpecialization(
5203 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
5204
5205 } else { // Not a partial specialization.
Gabor Marton26f72a92018-07-12 09:42:05 +00005206 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00005207 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5208 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5209 PrevDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00005210 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005211
5212 // Update InsertPos, because preceding import calls may have invalidated
5213 // it by adding new specializations.
5214 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5215 // Add this specialization to the class template.
5216 ClassTemplate->AddSpecialization(D2, InsertPos);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005217 }
5218
Douglas Gregore2e50d332010-12-01 01:36:18 +00005219 D2->setSpecializationKind(D->getSpecializationKind());
5220
Douglas Gregore2e50d332010-12-01 01:36:18 +00005221 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005222 if (auto LocOrErr = import(D->getQualifierLoc()))
5223 D2->setQualifierInfo(*LocOrErr);
5224 else
5225 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005226
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005227 if (auto *TSI = D->getTypeAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005228 if (auto TInfoOrErr = import(TSI))
5229 D2->setTypeAsWritten(*TInfoOrErr);
5230 else
5231 return TInfoOrErr.takeError();
5232
5233 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5234 D2->setTemplateKeywordLoc(*LocOrErr);
5235 else
5236 return LocOrErr.takeError();
5237
5238 if (auto LocOrErr = import(D->getExternLoc()))
5239 D2->setExternLoc(*LocOrErr);
5240 else
5241 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005242 }
5243
Balazs Keri3b30d652018-10-19 13:32:20 +00005244 if (D->getPointOfInstantiation().isValid()) {
5245 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5246 D2->setPointOfInstantiation(*POIOrErr);
5247 else
5248 return POIOrErr.takeError();
5249 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005250
5251 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5252
Gabor Martonb14056b2018-05-25 11:21:24 +00005253 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00005254 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00005255
5256 // Add to the DC only if it was an explicit specialization/instantiation.
5257 if (D2->isExplicitInstantiationOrSpecialization()) {
5258 LexicalDC->addDeclInternal(D2);
5259 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005260 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005261 if (D->isCompleteDefinition())
5262 if (Error Err = ImportDefinition(D, D2))
5263 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005264
Douglas Gregore2e50d332010-12-01 01:36:18 +00005265 return D2;
5266}
5267
Balazs Keri3b30d652018-10-19 13:32:20 +00005268ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005269 // If this variable has a definition in the translation unit we're coming
5270 // from,
5271 // but this particular declaration is not that definition, import the
5272 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005273 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005274 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5275 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005276 if (ExpectedDecl ImportedDefOrErr = import(
5277 Definition->getDescribedVarTemplate()))
5278 return Importer.MapImported(D, *ImportedDefOrErr);
5279 else
5280 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005281 }
5282
5283 // Import the major distinguishing characteristics of this variable template.
5284 DeclContext *DC, *LexicalDC;
5285 DeclarationName Name;
5286 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005287 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005288 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5289 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005290 if (ToD)
5291 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005292
5293 // We may already have a template of the same name; try to find and match it.
5294 assert(!DC->isFunctionOrMethod() &&
5295 "Variable templates cannot be declared at function scope");
5296 SmallVector<NamedDecl *, 4> ConflictingDecls;
5297 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005298 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005299 for (auto *FoundDecl : FoundDecls) {
5300 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005301 continue;
5302
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005303 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005304 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005305 if (IsStructuralMatch(D, FoundTemplate)) {
5306 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005307 Importer.MapImported(D->getTemplatedDecl(),
5308 FoundTemplate->getTemplatedDecl());
5309 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005310 }
5311 }
5312
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005313 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005314 }
5315
5316 if (!ConflictingDecls.empty()) {
5317 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5318 ConflictingDecls.data(),
5319 ConflictingDecls.size());
5320 }
5321
5322 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005323 // FIXME: Is it possible to get other error than name conflict?
5324 // (Put this `if` into the previous `if`?)
5325 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005326
5327 VarDecl *DTemplated = D->getTemplatedDecl();
5328
5329 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005330 // FIXME: Value not used?
5331 ExpectedType TypeOrErr = import(DTemplated->getType());
5332 if (!TypeOrErr)
5333 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005334
5335 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005336 VarDecl *ToTemplated;
5337 if (Error Err = importInto(ToTemplated, DTemplated))
5338 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005339
5340 // Create the variable template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005341 auto TemplateParamsOrErr = ImportTemplateParameterList(
5342 D->getTemplateParameters());
5343 if (!TemplateParamsOrErr)
5344 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005345
Gabor Marton26f72a92018-07-12 09:42:05 +00005346 VarTemplateDecl *ToVarTD;
5347 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005348 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005349 return ToVarTD;
5350
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005351 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005352
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005353 ToVarTD->setAccess(D->getAccess());
5354 ToVarTD->setLexicalDeclContext(LexicalDC);
5355 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005356
Larisse Voufo39a1e502013-08-06 01:03:05 +00005357 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005358 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005359 // FIXME: Import definition!
5360 }
5361
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005362 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005363}
5364
Balazs Keri3b30d652018-10-19 13:32:20 +00005365ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005366 VarTemplateSpecializationDecl *D) {
5367 // If this record has a definition in the translation unit we're coming from,
5368 // but this particular declaration is not that definition, import the
5369 // definition and map to that.
5370 VarDecl *Definition = D->getDefinition();
5371 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005372 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5373 return Importer.MapImported(D, *ImportedDefOrErr);
5374 else
5375 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005376 }
5377
Balazs Keri3b30d652018-10-19 13:32:20 +00005378 VarTemplateDecl *VarTemplate;
5379 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5380 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005381
5382 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005383 DeclContext *DC, *LexicalDC;
5384 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5385 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005386
5387 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005388 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5389 if (!BeginLocOrErr)
5390 return BeginLocOrErr.takeError();
5391
5392 auto IdLocOrErr = import(D->getLocation());
5393 if (!IdLocOrErr)
5394 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005395
5396 // Import template arguments.
5397 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005398 if (Error Err = ImportTemplateArguments(
5399 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5400 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005401
5402 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005403 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005404 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005405 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005406 if (D2) {
5407 // We already have a variable template specialization with these template
5408 // arguments.
5409
5410 // FIXME: Check for specialization vs. instantiation errors.
5411
5412 if (VarDecl *FoundDef = D2->getDefinition()) {
5413 if (!D->isThisDeclarationADefinition() ||
5414 IsStructuralMatch(D, FoundDef)) {
5415 // The record types structurally match, or the "from" translation
5416 // unit only had a forward declaration anyway; call it the same
5417 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005418 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005419 }
5420 }
5421 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005422 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005423 QualType T;
5424 if (Error Err = importInto(T, D->getType()))
5425 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005426
Balazs Keri3b30d652018-10-19 13:32:20 +00005427 auto TInfoOrErr = import(D->getTypeSourceInfo());
5428 if (!TInfoOrErr)
5429 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005430
5431 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005432 if (Error Err = ImportTemplateArgumentListInfo(
5433 D->getTemplateArgsInfo(), ToTAInfo))
5434 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005435
5436 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005437 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005438 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5439 // Import TemplateArgumentListInfo
5440 TemplateArgumentListInfo ArgInfos;
5441 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5442 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005443 if (Error Err = ImportTemplateArgumentListInfo(
5444 *FromTAArgsAsWritten, ArgInfos))
5445 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005446
Balazs Keri3b30d652018-10-19 13:32:20 +00005447 auto ToTPListOrErr = ImportTemplateParameterList(
5448 FromPartial->getTemplateParameters());
5449 if (!ToTPListOrErr)
5450 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005451
Gabor Marton26f72a92018-07-12 09:42:05 +00005452 PartVarSpecDecl *ToPartial;
5453 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005454 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5455 VarTemplate, T, *TInfoOrErr,
5456 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005457 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005458
Balazs Keri3b30d652018-10-19 13:32:20 +00005459 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5460 FromPartial->getInstantiatedFromMember()))
5461 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5462 else
5463 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005464
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005465 if (FromPartial->isMemberSpecialization())
5466 ToPartial->setMemberSpecialization();
5467
5468 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005469
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005470 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005471 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5472 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5473 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005474 D->getStorageClass(), TemplateArgs))
5475 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005476 }
5477
Balazs Keri3b30d652018-10-19 13:32:20 +00005478 if (D->getPointOfInstantiation().isValid()) {
5479 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5480 D2->setPointOfInstantiation(*POIOrErr);
5481 else
5482 return POIOrErr.takeError();
5483 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005484
Larisse Voufo39a1e502013-08-06 01:03:05 +00005485 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005486 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005487
5488 // Add this specialization to the class template.
5489 VarTemplate->AddSpecialization(D2, InsertPos);
5490
5491 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005492 if (auto LocOrErr = import(D->getQualifierLoc()))
5493 D2->setQualifierInfo(*LocOrErr);
5494 else
5495 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005496
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005497 if (D->isConstexpr())
5498 D2->setConstexpr(true);
5499
Larisse Voufo39a1e502013-08-06 01:03:05 +00005500 // Add the specialization to this context.
5501 D2->setLexicalDeclContext(LexicalDC);
5502 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005503
5504 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005505 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005506
Balazs Keri3b30d652018-10-19 13:32:20 +00005507 if (Error Err = ImportInitializer(D, D2))
5508 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005509
5510 return D2;
5511}
5512
Balazs Keri3b30d652018-10-19 13:32:20 +00005513ExpectedDecl
5514ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005515 DeclContext *DC, *LexicalDC;
5516 DeclarationName Name;
5517 SourceLocation Loc;
5518 NamedDecl *ToD;
5519
Balazs Keri3b30d652018-10-19 13:32:20 +00005520 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5521 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005522
5523 if (ToD)
5524 return ToD;
5525
5526 // Try to find a function in our own ("to") context with the same name, same
5527 // type, and in the same context as the function we're importing.
5528 if (!LexicalDC->isFunctionOrMethod()) {
5529 unsigned IDNS = Decl::IDNS_Ordinary;
5530 SmallVector<NamedDecl *, 2> FoundDecls;
5531 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005532 for (auto *FoundDecl : FoundDecls) {
5533 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005534 continue;
5535
Balazs Keri3b30d652018-10-19 13:32:20 +00005536 if (auto *FoundFunction =
5537 dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005538 if (FoundFunction->hasExternalFormalLinkage() &&
5539 D->hasExternalFormalLinkage()) {
5540 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00005541 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005542 // FIXME: Actually try to merge the body and other attributes.
5543 return FoundFunction;
5544 }
5545 }
5546 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005547 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005548 }
5549 }
5550
Balazs Keri3b30d652018-10-19 13:32:20 +00005551 auto ParamsOrErr = ImportTemplateParameterList(
5552 D->getTemplateParameters());
5553 if (!ParamsOrErr)
5554 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005555
Balazs Keri3b30d652018-10-19 13:32:20 +00005556 FunctionDecl *TemplatedFD;
5557 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5558 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005559
Gabor Marton26f72a92018-07-12 09:42:05 +00005560 FunctionTemplateDecl *ToFunc;
5561 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005562 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005563 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005564
5565 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
5566 ToFunc->setAccess(D->getAccess());
5567 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005568
5569 LexicalDC->addDeclInternal(ToFunc);
5570 return ToFunc;
5571}
5572
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005573//----------------------------------------------------------------------------
5574// Import Statements
5575//----------------------------------------------------------------------------
5576
Balazs Keri3b30d652018-10-19 13:32:20 +00005577ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005578 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5579 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005580 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005581}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005582
Balazs Keri3b30d652018-10-19 13:32:20 +00005583
5584ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005585 SmallVector<IdentifierInfo *, 4> Names;
5586 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5587 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005588 // ToII is nullptr when no symbolic name is given for output operand
5589 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005590 Names.push_back(ToII);
5591 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005592
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005593 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5594 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005595 // ToII is nullptr when no symbolic name is given for input operand
5596 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005597 Names.push_back(ToII);
5598 }
5599
5600 SmallVector<StringLiteral *, 4> Clobbers;
5601 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005602 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5603 Clobbers.push_back(*ClobberOrErr);
5604 else
5605 return ClobberOrErr.takeError();
5606
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005607 }
5608
5609 SmallVector<StringLiteral *, 4> Constraints;
5610 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005611 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5612 Constraints.push_back(*OutputOrErr);
5613 else
5614 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005615 }
5616
5617 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005618 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5619 Constraints.push_back(*InputOrErr);
5620 else
5621 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005622 }
5623
5624 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005625 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5626 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005627
Balazs Keri3b30d652018-10-19 13:32:20 +00005628 if (Error Err = ImportArrayChecked(
5629 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5630 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005631
Balazs Keri3b30d652018-10-19 13:32:20 +00005632 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5633 if (!AsmLocOrErr)
5634 return AsmLocOrErr.takeError();
5635 auto AsmStrOrErr = import(S->getAsmString());
5636 if (!AsmStrOrErr)
5637 return AsmStrOrErr.takeError();
5638 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5639 if (!RParenLocOrErr)
5640 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005641
5642 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005643 Importer.getToContext(),
5644 *AsmLocOrErr,
5645 S->isSimple(),
5646 S->isVolatile(),
5647 S->getNumOutputs(),
5648 S->getNumInputs(),
5649 Names.data(),
5650 Constraints.data(),
5651 Exprs.data(),
5652 *AsmStrOrErr,
5653 S->getNumClobbers(),
5654 Clobbers.data(),
5655 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005656}
5657
Balazs Keri3b30d652018-10-19 13:32:20 +00005658ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5659 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5660 if (!Imp)
5661 return Imp.takeError();
5662
5663 DeclGroupRef ToDG;
5664 SourceLocation ToBeginLoc, ToEndLoc;
5665 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5666
5667 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005668}
5669
Balazs Keri3b30d652018-10-19 13:32:20 +00005670ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5671 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5672 if (!ToSemiLocOrErr)
5673 return ToSemiLocOrErr.takeError();
5674 return new (Importer.getToContext()) NullStmt(
5675 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005676}
5677
Balazs Keri3b30d652018-10-19 13:32:20 +00005678ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005679 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005680
Balazs Keri3b30d652018-10-19 13:32:20 +00005681 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5682 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005683
Balazs Keri3b30d652018-10-19 13:32:20 +00005684 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5685 if (!ToLBracLocOrErr)
5686 return ToLBracLocOrErr.takeError();
5687
5688 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5689 if (!ToRBracLocOrErr)
5690 return ToRBracLocOrErr.takeError();
5691
5692 return CompoundStmt::Create(
5693 Importer.getToContext(), ToStmts,
5694 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005695}
5696
Balazs Keri3b30d652018-10-19 13:32:20 +00005697ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5698 auto Imp = importSeq(
5699 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5700 S->getEllipsisLoc(), S->getColonLoc());
5701 if (!Imp)
5702 return Imp.takeError();
5703
5704 Expr *ToLHS, *ToRHS;
5705 Stmt *ToSubStmt;
5706 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5707 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5708 *Imp;
5709
5710 auto *ToStmt = new (Importer.getToContext()) CaseStmt(
5711 ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005712 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005713
Gabor Horvath480892b2017-10-18 09:25:18 +00005714 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005715}
5716
Balazs Keri3b30d652018-10-19 13:32:20 +00005717ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5718 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5719 if (!Imp)
5720 return Imp.takeError();
5721
5722 SourceLocation ToDefaultLoc, ToColonLoc;
5723 Stmt *ToSubStmt;
5724 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5725
5726 return new (Importer.getToContext()) DefaultStmt(
5727 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005728}
5729
Balazs Keri3b30d652018-10-19 13:32:20 +00005730ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5731 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5732 if (!Imp)
5733 return Imp.takeError();
5734
5735 SourceLocation ToIdentLoc;
5736 LabelDecl *ToLabelDecl;
5737 Stmt *ToSubStmt;
5738 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5739
5740 return new (Importer.getToContext()) LabelStmt(
5741 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005742}
5743
Balazs Keri3b30d652018-10-19 13:32:20 +00005744ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5745 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5746 if (!ToAttrLocOrErr)
5747 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005748 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5749 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005750 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5751 return std::move(Err);
5752 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5753 if (!ToSubStmtOrErr)
5754 return ToSubStmtOrErr.takeError();
5755
5756 return AttributedStmt::Create(
5757 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005758}
5759
Balazs Keri3b30d652018-10-19 13:32:20 +00005760ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5761 auto Imp = importSeq(
5762 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5763 S->getThen(), S->getElseLoc(), S->getElse());
5764 if (!Imp)
5765 return Imp.takeError();
5766
5767 SourceLocation ToIfLoc, ToElseLoc;
5768 Stmt *ToInit, *ToThen, *ToElse;
5769 VarDecl *ToConditionVariable;
5770 Expr *ToCond;
5771 std::tie(
5772 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5773 *Imp;
5774
5775 return new (Importer.getToContext()) IfStmt(
5776 Importer.getToContext(),
5777 ToIfLoc, S->isConstexpr(), ToInit, ToConditionVariable, ToCond,
5778 ToThen, ToElseLoc, ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005779}
5780
Balazs Keri3b30d652018-10-19 13:32:20 +00005781ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5782 auto Imp = importSeq(
5783 S->getInit(), S->getConditionVariable(), S->getCond(),
5784 S->getBody(), S->getSwitchLoc());
5785 if (!Imp)
5786 return Imp.takeError();
5787
5788 Stmt *ToInit, *ToBody;
5789 VarDecl *ToConditionVariable;
5790 Expr *ToCond;
5791 SourceLocation ToSwitchLoc;
5792 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5793
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005794 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005795 Importer.getToContext(), ToInit, ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005796 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005797 ToStmt->setSwitchLoc(ToSwitchLoc);
5798
Sean Callanan59721b32015-04-28 18:41:46 +00005799 // Now we have to re-chain the cases.
5800 SwitchCase *LastChainedSwitchCase = nullptr;
5801 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5802 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005803 Expected<SwitchCase *> ToSCOrErr = import(SC);
5804 if (!ToSCOrErr)
5805 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005806 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005807 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005808 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005809 ToStmt->setSwitchCaseList(*ToSCOrErr);
5810 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005811 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005812
Sean Callanan59721b32015-04-28 18:41:46 +00005813 return ToStmt;
5814}
5815
Balazs Keri3b30d652018-10-19 13:32:20 +00005816ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5817 auto Imp = importSeq(
5818 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5819 if (!Imp)
5820 return Imp.takeError();
5821
5822 VarDecl *ToConditionVariable;
5823 Expr *ToCond;
5824 Stmt *ToBody;
5825 SourceLocation ToWhileLoc;
5826 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5827
5828 return new (Importer.getToContext()) WhileStmt(
5829 Importer.getToContext(),
5830 ToConditionVariable, ToCond, ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005831}
5832
Balazs Keri3b30d652018-10-19 13:32:20 +00005833ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5834 auto Imp = importSeq(
5835 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5836 S->getRParenLoc());
5837 if (!Imp)
5838 return Imp.takeError();
5839
5840 Stmt *ToBody;
5841 Expr *ToCond;
5842 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5843 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5844
5845 return new (Importer.getToContext()) DoStmt(
5846 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005847}
5848
Balazs Keri3b30d652018-10-19 13:32:20 +00005849ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5850 auto Imp = importSeq(
5851 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5852 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5853 if (!Imp)
5854 return Imp.takeError();
5855
5856 Stmt *ToInit;
5857 Expr *ToCond, *ToInc;
5858 VarDecl *ToConditionVariable;
5859 Stmt *ToBody;
5860 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5861 std::tie(
5862 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5863 ToLParenLoc, ToRParenLoc) = *Imp;
5864
5865 return new (Importer.getToContext()) ForStmt(
5866 Importer.getToContext(),
5867 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5868 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005869}
5870
Balazs Keri3b30d652018-10-19 13:32:20 +00005871ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5872 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5873 if (!Imp)
5874 return Imp.takeError();
5875
5876 LabelDecl *ToLabel;
5877 SourceLocation ToGotoLoc, ToLabelLoc;
5878 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5879
5880 return new (Importer.getToContext()) GotoStmt(
5881 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005882}
5883
Balazs Keri3b30d652018-10-19 13:32:20 +00005884ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5885 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5886 if (!Imp)
5887 return Imp.takeError();
5888
5889 SourceLocation ToGotoLoc, ToStarLoc;
5890 Expr *ToTarget;
5891 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5892
5893 return new (Importer.getToContext()) IndirectGotoStmt(
5894 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005895}
5896
Balazs Keri3b30d652018-10-19 13:32:20 +00005897ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5898 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5899 if (!ToContinueLocOrErr)
5900 return ToContinueLocOrErr.takeError();
5901 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005902}
5903
Balazs Keri3b30d652018-10-19 13:32:20 +00005904ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5905 auto ToBreakLocOrErr = import(S->getBreakLoc());
5906 if (!ToBreakLocOrErr)
5907 return ToBreakLocOrErr.takeError();
5908 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005909}
5910
Balazs Keri3b30d652018-10-19 13:32:20 +00005911ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5912 auto Imp = importSeq(
5913 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5914 if (!Imp)
5915 return Imp.takeError();
5916
5917 SourceLocation ToReturnLoc;
5918 Expr *ToRetValue;
5919 const VarDecl *ToNRVOCandidate;
5920 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5921
5922 return new (Importer.getToContext()) ReturnStmt(
5923 ToReturnLoc, ToRetValue, ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005924}
5925
Balazs Keri3b30d652018-10-19 13:32:20 +00005926ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5927 auto Imp = importSeq(
5928 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5929 if (!Imp)
5930 return Imp.takeError();
5931
5932 SourceLocation ToCatchLoc;
5933 VarDecl *ToExceptionDecl;
5934 Stmt *ToHandlerBlock;
5935 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5936
5937 return new (Importer.getToContext()) CXXCatchStmt (
5938 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005939}
5940
Balazs Keri3b30d652018-10-19 13:32:20 +00005941ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5942 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5943 if (!ToTryLocOrErr)
5944 return ToTryLocOrErr.takeError();
5945
5946 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5947 if (!ToTryBlockOrErr)
5948 return ToTryBlockOrErr.takeError();
5949
Sean Callanan59721b32015-04-28 18:41:46 +00005950 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5951 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5952 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005953 if (auto ToHandlerOrErr = import(FromHandler))
5954 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005955 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005956 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005957 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005958
5959 return CXXTryStmt::Create(
5960 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005961}
5962
Balazs Keri3b30d652018-10-19 13:32:20 +00005963ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5964 auto Imp1 = importSeq(
5965 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5966 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5967 if (!Imp1)
5968 return Imp1.takeError();
5969 auto Imp2 = importSeq(
5970 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5971 if (!Imp2)
5972 return Imp2.takeError();
5973
5974 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5975 Expr *ToCond, *ToInc;
5976 Stmt *ToInit, *ToBody;
5977 std::tie(
5978 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5979 ToBody) = *Imp1;
5980 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5981 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5982
5983 return new (Importer.getToContext()) CXXForRangeStmt(
5984 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5985 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005986}
5987
Balazs Keri3b30d652018-10-19 13:32:20 +00005988ExpectedStmt
5989ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5990 auto Imp = importSeq(
5991 S->getElement(), S->getCollection(), S->getBody(),
5992 S->getForLoc(), S->getRParenLoc());
5993 if (!Imp)
5994 return Imp.takeError();
5995
5996 Stmt *ToElement, *ToBody;
5997 Expr *ToCollection;
5998 SourceLocation ToForLoc, ToRParenLoc;
5999 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6000
6001 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6002 ToCollection,
6003 ToBody,
6004 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006005 ToRParenLoc);
6006}
6007
Balazs Keri3b30d652018-10-19 13:32:20 +00006008ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6009 auto Imp = importSeq(
6010 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6011 S->getCatchBody());
6012 if (!Imp)
6013 return Imp.takeError();
6014
6015 SourceLocation ToAtCatchLoc, ToRParenLoc;
6016 VarDecl *ToCatchParamDecl;
6017 Stmt *ToCatchBody;
6018 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6019
6020 return new (Importer.getToContext()) ObjCAtCatchStmt (
6021 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006022}
6023
Balazs Keri3b30d652018-10-19 13:32:20 +00006024ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6025 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6026 if (!ToAtFinallyLocOrErr)
6027 return ToAtFinallyLocOrErr.takeError();
6028 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6029 if (!ToAtFinallyStmtOrErr)
6030 return ToAtFinallyStmtOrErr.takeError();
6031 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6032 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006033}
6034
Balazs Keri3b30d652018-10-19 13:32:20 +00006035ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6036 auto Imp = importSeq(
6037 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6038 if (!Imp)
6039 return Imp.takeError();
6040
6041 SourceLocation ToAtTryLoc;
6042 Stmt *ToTryBody, *ToFinallyStmt;
6043 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6044
Sean Callanan59721b32015-04-28 18:41:46 +00006045 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6046 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6047 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006048 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6049 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006050 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006051 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006052 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006053
Sean Callanan59721b32015-04-28 18:41:46 +00006054 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006055 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006056 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006057 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006058}
6059
Balazs Keri3b30d652018-10-19 13:32:20 +00006060ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006061 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006062 auto Imp = importSeq(
6063 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6064 if (!Imp)
6065 return Imp.takeError();
6066
6067 SourceLocation ToAtSynchronizedLoc;
6068 Expr *ToSynchExpr;
6069 Stmt *ToSynchBody;
6070 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6071
Sean Callanan59721b32015-04-28 18:41:46 +00006072 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6073 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6074}
6075
Balazs Keri3b30d652018-10-19 13:32:20 +00006076ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6077 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6078 if (!ToThrowLocOrErr)
6079 return ToThrowLocOrErr.takeError();
6080 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6081 if (!ToThrowExprOrErr)
6082 return ToThrowExprOrErr.takeError();
6083 return new (Importer.getToContext()) ObjCAtThrowStmt(
6084 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006085}
6086
Balazs Keri3b30d652018-10-19 13:32:20 +00006087ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6088 ObjCAutoreleasePoolStmt *S) {
6089 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6090 if (!ToAtLocOrErr)
6091 return ToAtLocOrErr.takeError();
6092 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6093 if (!ToSubStmtOrErr)
6094 return ToSubStmtOrErr.takeError();
6095 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6096 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006097}
6098
6099//----------------------------------------------------------------------------
6100// Import Expressions
6101//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006102ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006103 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6104 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006105 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006106}
6107
Balazs Keri3b30d652018-10-19 13:32:20 +00006108ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6109 auto Imp = importSeq(
6110 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6111 E->getRParenLoc(), E->getType());
6112 if (!Imp)
6113 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006114
Balazs Keri3b30d652018-10-19 13:32:20 +00006115 SourceLocation ToBuiltinLoc, ToRParenLoc;
6116 Expr *ToSubExpr;
6117 TypeSourceInfo *ToWrittenTypeInfo;
6118 QualType ToType;
6119 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6120 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006121
6122 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006123 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6124 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006125}
6126
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006127
Balazs Keri3b30d652018-10-19 13:32:20 +00006128ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6129 ExpectedType TypeOrErr = import(E->getType());
6130 if (!TypeOrErr)
6131 return TypeOrErr.takeError();
6132
6133 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6134 if (!BeginLocOrErr)
6135 return BeginLocOrErr.takeError();
6136
6137 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006138}
6139
Balazs Keri3b30d652018-10-19 13:32:20 +00006140ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6141 auto Imp = importSeq(
6142 E->getBeginLoc(), E->getType(), E->getFunctionName());
6143 if (!Imp)
6144 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006145
Balazs Keri3b30d652018-10-19 13:32:20 +00006146 SourceLocation ToBeginLoc;
6147 QualType ToType;
6148 StringLiteral *ToFunctionName;
6149 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006150
6151 return new (Importer.getToContext()) PredefinedExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006152 ToBeginLoc, ToType, E->getIdentType(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006153}
6154
Balazs Keri3b30d652018-10-19 13:32:20 +00006155ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6156 auto Imp = importSeq(
6157 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6158 E->getLocation(), E->getType());
6159 if (!Imp)
6160 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006161
Balazs Keri3b30d652018-10-19 13:32:20 +00006162 NestedNameSpecifierLoc ToQualifierLoc;
6163 SourceLocation ToTemplateKeywordLoc, ToLocation;
6164 ValueDecl *ToDecl;
6165 QualType ToType;
6166 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6167 *Imp;
6168
6169 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006170 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006171 auto FoundDOrErr = import(E->getFoundDecl());
6172 if (!FoundDOrErr)
6173 return FoundDOrErr.takeError();
6174 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006175 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006176
Aleksei Sidorina693b372016-09-28 10:16:56 +00006177 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006178 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006179 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006180 if (Error Err =
6181 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6182 return std::move(Err);
6183 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006184 }
6185
Balazs Keri3b30d652018-10-19 13:32:20 +00006186 auto *ToE = DeclRefExpr::Create(
6187 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6188 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6189 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006190 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006191 ToE->setHadMultipleCandidates(true);
6192 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006193}
6194
Balazs Keri3b30d652018-10-19 13:32:20 +00006195ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6196 ExpectedType TypeOrErr = import(E->getType());
6197 if (!TypeOrErr)
6198 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006199
Balazs Keri3b30d652018-10-19 13:32:20 +00006200 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006201}
6202
Balazs Keri3b30d652018-10-19 13:32:20 +00006203ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6204 ExpectedExpr ToInitOrErr = import(E->getInit());
6205 if (!ToInitOrErr)
6206 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006207
Balazs Keri3b30d652018-10-19 13:32:20 +00006208 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6209 if (!ToEqualOrColonLocOrErr)
6210 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006211
Balazs Keri3b30d652018-10-19 13:32:20 +00006212 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006213 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006214 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6215 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6216 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006217 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006218 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006219 }
6220
Balazs Keri3b30d652018-10-19 13:32:20 +00006221 SmallVector<Designator, 4> ToDesignators(E->size());
6222 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6223 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006224
6225 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006226 Importer.getToContext(), ToDesignators,
6227 ToIndexExprs, *ToEqualOrColonLocOrErr,
6228 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006229}
6230
Balazs Keri3b30d652018-10-19 13:32:20 +00006231ExpectedStmt
6232ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6233 ExpectedType ToTypeOrErr = import(E->getType());
6234 if (!ToTypeOrErr)
6235 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006236
Balazs Keri3b30d652018-10-19 13:32:20 +00006237 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6238 if (!ToLocationOrErr)
6239 return ToLocationOrErr.takeError();
6240
6241 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6242 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006243}
6244
Balazs Keri3b30d652018-10-19 13:32:20 +00006245ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6246 ExpectedType ToTypeOrErr = import(E->getType());
6247 if (!ToTypeOrErr)
6248 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006249
Balazs Keri3b30d652018-10-19 13:32:20 +00006250 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6251 if (!ToLocationOrErr)
6252 return ToLocationOrErr.takeError();
6253
6254 return IntegerLiteral::Create(
6255 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006256}
6257
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006258
Balazs Keri3b30d652018-10-19 13:32:20 +00006259ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6260 ExpectedType ToTypeOrErr = import(E->getType());
6261 if (!ToTypeOrErr)
6262 return ToTypeOrErr.takeError();
6263
6264 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6265 if (!ToLocationOrErr)
6266 return ToLocationOrErr.takeError();
6267
6268 return FloatingLiteral::Create(
6269 Importer.getToContext(), E->getValue(), E->isExact(),
6270 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006271}
6272
Balazs Keri3b30d652018-10-19 13:32:20 +00006273ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6274 auto ToTypeOrErr = import(E->getType());
6275 if (!ToTypeOrErr)
6276 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006277
Balazs Keri3b30d652018-10-19 13:32:20 +00006278 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6279 if (!ToSubExprOrErr)
6280 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006281
Balazs Keri3b30d652018-10-19 13:32:20 +00006282 return new (Importer.getToContext()) ImaginaryLiteral(
6283 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006284}
6285
Balazs Keri3b30d652018-10-19 13:32:20 +00006286ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6287 ExpectedType ToTypeOrErr = import(E->getType());
6288 if (!ToTypeOrErr)
6289 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006290
Balazs Keri3b30d652018-10-19 13:32:20 +00006291 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6292 if (!ToLocationOrErr)
6293 return ToLocationOrErr.takeError();
6294
6295 return new (Importer.getToContext()) CharacterLiteral(
6296 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006297}
6298
Balazs Keri3b30d652018-10-19 13:32:20 +00006299ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6300 ExpectedType ToTypeOrErr = import(E->getType());
6301 if (!ToTypeOrErr)
6302 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006303
Balazs Keri3b30d652018-10-19 13:32:20 +00006304 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6305 if (Error Err = ImportArrayChecked(
6306 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6307 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006308
Balazs Keri3b30d652018-10-19 13:32:20 +00006309 return StringLiteral::Create(
6310 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6311 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006312}
6313
Balazs Keri3b30d652018-10-19 13:32:20 +00006314ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6315 auto Imp = importSeq(
6316 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6317 E->getInitializer());
6318 if (!Imp)
6319 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006320
Balazs Keri3b30d652018-10-19 13:32:20 +00006321 SourceLocation ToLParenLoc;
6322 TypeSourceInfo *ToTypeSourceInfo;
6323 QualType ToType;
6324 Expr *ToInitializer;
6325 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006326
6327 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006328 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6329 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006330}
6331
Balazs Keri3b30d652018-10-19 13:32:20 +00006332ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6333 auto Imp = importSeq(
6334 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6335 if (!Imp)
6336 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006337
Balazs Keri3b30d652018-10-19 13:32:20 +00006338 SourceLocation ToBuiltinLoc, ToRParenLoc;
6339 QualType ToType;
6340 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6341
6342 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6343 if (Error Err = ImportArrayChecked(
6344 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6345 ToExprs.begin()))
6346 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006347
6348 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006349 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006350}
6351
Balazs Keri3b30d652018-10-19 13:32:20 +00006352ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6353 auto Imp = importSeq(
6354 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6355 if (!Imp)
6356 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006357
Balazs Keri3b30d652018-10-19 13:32:20 +00006358 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6359 LabelDecl *ToLabel;
6360 QualType ToType;
6361 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006362
6363 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006364 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006365}
6366
Balazs Keri3b30d652018-10-19 13:32:20 +00006367ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6368 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6369 if (!Imp)
6370 return Imp.takeError();
6371
6372 SourceLocation ToLParen, ToRParen;
6373 Expr *ToSubExpr;
6374 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006375
Fangrui Song6907ce22018-07-30 19:24:48 +00006376 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006377 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006378}
6379
Balazs Keri3b30d652018-10-19 13:32:20 +00006380ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6381 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6382 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6383 return std::move(Err);
6384
6385 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6386 if (!ToLParenLocOrErr)
6387 return ToLParenLocOrErr.takeError();
6388
6389 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6390 if (!ToRParenLocOrErr)
6391 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006392
6393 return new (Importer.getToContext()) ParenListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006394 Importer.getToContext(), *ToLParenLocOrErr, ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006395}
6396
Balazs Keri3b30d652018-10-19 13:32:20 +00006397ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6398 auto Imp = importSeq(
6399 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6400 if (!Imp)
6401 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006402
Balazs Keri3b30d652018-10-19 13:32:20 +00006403 CompoundStmt *ToSubStmt;
6404 QualType ToType;
6405 SourceLocation ToLParenLoc, ToRParenLoc;
6406 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006407
Balazs Keri3b30d652018-10-19 13:32:20 +00006408 return new (Importer.getToContext()) StmtExpr(
6409 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006410}
6411
Balazs Keri3b30d652018-10-19 13:32:20 +00006412ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6413 auto Imp = importSeq(
6414 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6415 if (!Imp)
6416 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006417
Balazs Keri3b30d652018-10-19 13:32:20 +00006418 Expr *ToSubExpr;
6419 QualType ToType;
6420 SourceLocation ToOperatorLoc;
6421 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006422
Aaron Ballmana5038552018-01-09 13:07:03 +00006423 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006424 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6425 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006426}
6427
Balazs Keri3b30d652018-10-19 13:32:20 +00006428ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006429ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006430 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6431 if (!Imp)
6432 return Imp.takeError();
6433
6434 QualType ToType;
6435 SourceLocation ToOperatorLoc, ToRParenLoc;
6436 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006437
Douglas Gregord8552cd2010-02-19 01:24:23 +00006438 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006439 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6440 import(E->getArgumentTypeInfo());
6441 if (!ToArgumentTypeInfoOrErr)
6442 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006443
Balazs Keri3b30d652018-10-19 13:32:20 +00006444 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6445 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6446 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006447 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006448
Balazs Keri3b30d652018-10-19 13:32:20 +00006449 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6450 if (!ToArgumentExprOrErr)
6451 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006452
Balazs Keri3b30d652018-10-19 13:32:20 +00006453 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6454 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006455}
6456
Balazs Keri3b30d652018-10-19 13:32:20 +00006457ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6458 auto Imp = importSeq(
6459 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6460 if (!Imp)
6461 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006462
Balazs Keri3b30d652018-10-19 13:32:20 +00006463 Expr *ToLHS, *ToRHS;
6464 QualType ToType;
6465 SourceLocation ToOperatorLoc;
6466 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006467
Balazs Keri3b30d652018-10-19 13:32:20 +00006468 return new (Importer.getToContext()) BinaryOperator(
6469 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6470 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006471}
6472
Balazs Keri3b30d652018-10-19 13:32:20 +00006473ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6474 auto Imp = importSeq(
6475 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6476 E->getRHS(), E->getType());
6477 if (!Imp)
6478 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006479
Balazs Keri3b30d652018-10-19 13:32:20 +00006480 Expr *ToCond, *ToLHS, *ToRHS;
6481 SourceLocation ToQuestionLoc, ToColonLoc;
6482 QualType ToType;
6483 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006484
6485 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006486 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6487 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006488}
6489
Balazs Keri3b30d652018-10-19 13:32:20 +00006490ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006491 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006492 auto Imp = importSeq(
6493 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6494 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6495 if (!Imp)
6496 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006497
Balazs Keri3b30d652018-10-19 13:32:20 +00006498 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6499 OpaqueValueExpr *ToOpaqueValue;
6500 SourceLocation ToQuestionLoc, ToColonLoc;
6501 QualType ToType;
6502 std::tie(
6503 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6504 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006505
6506 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006507 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6508 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6509 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006510}
6511
Balazs Keri3b30d652018-10-19 13:32:20 +00006512ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6513 auto Imp = importSeq(
6514 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6515 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6516 if (!Imp)
6517 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006518
Balazs Keri3b30d652018-10-19 13:32:20 +00006519 SourceLocation ToBeginLoc, ToEndLoc;
6520 TypeSourceInfo *ToQueriedTypeSourceInfo;
6521 Expr *ToDimensionExpression;
6522 QualType ToType;
6523 std::tie(
6524 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6525 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006526
6527 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006528 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6529 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006530}
6531
Balazs Keri3b30d652018-10-19 13:32:20 +00006532ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6533 auto Imp = importSeq(
6534 E->getBeginLoc(), E->getQueriedExpression(), 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 Expr *ToQueriedExpression;
6540 QualType ToType;
6541 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006542
6543 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006544 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6545 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006546}
6547
Balazs Keri3b30d652018-10-19 13:32:20 +00006548ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6549 auto Imp = importSeq(
6550 E->getLocation(), E->getType(), E->getSourceExpr());
6551 if (!Imp)
6552 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006553
Balazs Keri3b30d652018-10-19 13:32:20 +00006554 SourceLocation ToLocation;
6555 QualType ToType;
6556 Expr *ToSourceExpr;
6557 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006558
6559 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006560 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006561}
6562
Balazs Keri3b30d652018-10-19 13:32:20 +00006563ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6564 auto Imp = importSeq(
6565 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6566 if (!Imp)
6567 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006568
Balazs Keri3b30d652018-10-19 13:32:20 +00006569 Expr *ToLHS, *ToRHS;
6570 SourceLocation ToRBracketLoc;
6571 QualType ToType;
6572 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006573
6574 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006575 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6576 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006577}
6578
Balazs Keri3b30d652018-10-19 13:32:20 +00006579ExpectedStmt
6580ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6581 auto Imp = importSeq(
6582 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6583 E->getComputationResultType(), E->getOperatorLoc());
6584 if (!Imp)
6585 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006586
Balazs Keri3b30d652018-10-19 13:32:20 +00006587 Expr *ToLHS, *ToRHS;
6588 QualType ToType, ToComputationLHSType, ToComputationResultType;
6589 SourceLocation ToOperatorLoc;
6590 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6591 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006592
Balazs Keri3b30d652018-10-19 13:32:20 +00006593 return new (Importer.getToContext()) CompoundAssignOperator(
6594 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6595 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6596 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006597}
6598
Balazs Keri3b30d652018-10-19 13:32:20 +00006599Expected<CXXCastPath>
6600ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6601 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006602 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006603 if (auto SpecOrErr = import(*I))
6604 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006605 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006606 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006607 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006608 return Path;
John McCallcf142162010-08-07 06:22:56 +00006609}
6610
Balazs Keri3b30d652018-10-19 13:32:20 +00006611ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6612 ExpectedType ToTypeOrErr = import(E->getType());
6613 if (!ToTypeOrErr)
6614 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006615
Balazs Keri3b30d652018-10-19 13:32:20 +00006616 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6617 if (!ToSubExprOrErr)
6618 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006619
Balazs Keri3b30d652018-10-19 13:32:20 +00006620 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6621 if (!ToBasePathOrErr)
6622 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006623
Balazs Keri3b30d652018-10-19 13:32:20 +00006624 return ImplicitCastExpr::Create(
6625 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6626 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006627}
6628
Balazs Keri3b30d652018-10-19 13:32:20 +00006629ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6630 auto Imp1 = importSeq(
6631 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6632 if (!Imp1)
6633 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006634
Balazs Keri3b30d652018-10-19 13:32:20 +00006635 QualType ToType;
6636 Expr *ToSubExpr;
6637 TypeSourceInfo *ToTypeInfoAsWritten;
6638 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006639
Balazs Keri3b30d652018-10-19 13:32:20 +00006640 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6641 if (!ToBasePathOrErr)
6642 return ToBasePathOrErr.takeError();
6643 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006644
Aleksei Sidorina693b372016-09-28 10:16:56 +00006645 switch (E->getStmtClass()) {
6646 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006647 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006648 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6649 if (!ToLParenLocOrErr)
6650 return ToLParenLocOrErr.takeError();
6651 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6652 if (!ToRParenLocOrErr)
6653 return ToRParenLocOrErr.takeError();
6654 return CStyleCastExpr::Create(
6655 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6656 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6657 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006658 }
6659
6660 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006661 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006662 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6663 if (!ToLParenLocOrErr)
6664 return ToLParenLocOrErr.takeError();
6665 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6666 if (!ToRParenLocOrErr)
6667 return ToRParenLocOrErr.takeError();
6668 return CXXFunctionalCastExpr::Create(
6669 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6670 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6671 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006672 }
6673
6674 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006675 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6676 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6677 if (!ToLParenLocOrErr)
6678 return ToLParenLocOrErr.takeError();
6679 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6680 if (!ToBridgeKeywordLocOrErr)
6681 return ToBridgeKeywordLocOrErr.takeError();
6682 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6683 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6684 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006685 }
6686 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006687 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006688 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006689 }
6690}
6691
Balazs Keri3b30d652018-10-19 13:32:20 +00006692ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6693 SmallVector<OffsetOfNode, 4> ToNodes;
6694 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6695 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006696
Balazs Keri3b30d652018-10-19 13:32:20 +00006697 SourceLocation ToBeginLoc, ToEndLoc;
6698 if (FromNode.getKind() != OffsetOfNode::Base) {
6699 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6700 if (!Imp)
6701 return Imp.takeError();
6702 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6703 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006704
Balazs Keri3b30d652018-10-19 13:32:20 +00006705 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006706 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006707 ToNodes.push_back(
6708 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006709 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006710 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006711 auto ToBSOrErr = import(FromNode.getBase());
6712 if (!ToBSOrErr)
6713 return ToBSOrErr.takeError();
6714 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006715 break;
6716 }
6717 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006718 auto ToFieldOrErr = import(FromNode.getField());
6719 if (!ToFieldOrErr)
6720 return ToFieldOrErr.takeError();
6721 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006722 break;
6723 }
6724 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006725 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6726 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006727 break;
6728 }
6729 }
6730 }
6731
Balazs Keri3b30d652018-10-19 13:32:20 +00006732 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6733 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6734 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6735 if (!ToIndexExprOrErr)
6736 return ToIndexExprOrErr.takeError();
6737 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006738 }
6739
Balazs Keri3b30d652018-10-19 13:32:20 +00006740 auto Imp = importSeq(
6741 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6742 E->getRParenLoc());
6743 if (!Imp)
6744 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006745
Balazs Keri3b30d652018-10-19 13:32:20 +00006746 QualType ToType;
6747 TypeSourceInfo *ToTypeSourceInfo;
6748 SourceLocation ToOperatorLoc, ToRParenLoc;
6749 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6750
6751 return OffsetOfExpr::Create(
6752 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6753 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006754}
6755
Balazs Keri3b30d652018-10-19 13:32:20 +00006756ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6757 auto Imp = importSeq(
6758 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6759 if (!Imp)
6760 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006761
Balazs Keri3b30d652018-10-19 13:32:20 +00006762 QualType ToType;
6763 Expr *ToOperand;
6764 SourceLocation ToBeginLoc, ToEndLoc;
6765 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006766
Balazs Keri3b30d652018-10-19 13:32:20 +00006767 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006768 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006769 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006770 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006771 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006772
Balazs Keri3b30d652018-10-19 13:32:20 +00006773 return new (Importer.getToContext()) CXXNoexceptExpr(
6774 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006775}
6776
Balazs Keri3b30d652018-10-19 13:32:20 +00006777ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6778 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6779 if (!Imp)
6780 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006781
Balazs Keri3b30d652018-10-19 13:32:20 +00006782 Expr *ToSubExpr;
6783 QualType ToType;
6784 SourceLocation ToThrowLoc;
6785 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006786
6787 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006788 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006789}
6790
Balazs Keri3b30d652018-10-19 13:32:20 +00006791ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6792 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6793 if (!ToUsedLocOrErr)
6794 return ToUsedLocOrErr.takeError();
6795
6796 auto ToParamOrErr = import(E->getParam());
6797 if (!ToParamOrErr)
6798 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006799
6800 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006801 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006802}
6803
Balazs Keri3b30d652018-10-19 13:32:20 +00006804ExpectedStmt
6805ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6806 auto Imp = importSeq(
6807 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6808 if (!Imp)
6809 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006810
Balazs Keri3b30d652018-10-19 13:32:20 +00006811 QualType ToType;
6812 TypeSourceInfo *ToTypeSourceInfo;
6813 SourceLocation ToRParenLoc;
6814 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006815
6816 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006817 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006818}
6819
Balazs Keri3b30d652018-10-19 13:32:20 +00006820ExpectedStmt
6821ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6822 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6823 if (!ToSubExprOrErr)
6824 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006825
Balazs Keri3b30d652018-10-19 13:32:20 +00006826 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6827 if (!ToDtorOrErr)
6828 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006829
6830 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006831 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6832 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006833}
6834
Balazs Keri3b30d652018-10-19 13:32:20 +00006835ExpectedStmt
6836ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6837 auto Imp = importSeq(
6838 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6839 E->getParenOrBraceRange());
6840 if (!Imp)
6841 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006842
Balazs Keri3b30d652018-10-19 13:32:20 +00006843 CXXConstructorDecl *ToConstructor;
6844 QualType ToType;
6845 TypeSourceInfo *ToTypeSourceInfo;
6846 SourceRange ToParenOrBraceRange;
6847 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006848
Balazs Keri3b30d652018-10-19 13:32:20 +00006849 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6850 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6851 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006852
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006853 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006854 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6855 ToParenOrBraceRange, E->hadMultipleCandidates(),
6856 E->isListInitialization(), E->isStdInitListInitialization(),
6857 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006858}
6859
Balazs Keri3b30d652018-10-19 13:32:20 +00006860ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006861ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006862 auto Imp = importSeq(
6863 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6864 if (!Imp)
6865 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006866
Balazs Keri3b30d652018-10-19 13:32:20 +00006867 QualType ToType;
6868 Expr *ToTemporaryExpr;
6869 const ValueDecl *ToExtendingDecl;
6870 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006871
6872 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006873 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006874
6875 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006876 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006877 return ToMTE;
6878}
6879
Balazs Keri3b30d652018-10-19 13:32:20 +00006880ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6881 auto Imp = importSeq(
6882 E->getType(), E->getPattern(), E->getEllipsisLoc());
6883 if (!Imp)
6884 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006885
Balazs Keri3b30d652018-10-19 13:32:20 +00006886 QualType ToType;
6887 Expr *ToPattern;
6888 SourceLocation ToEllipsisLoc;
6889 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006890
6891 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006892 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006893}
6894
Balazs Keri3b30d652018-10-19 13:32:20 +00006895ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6896 auto Imp = importSeq(
6897 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6898 if (!Imp)
6899 return Imp.takeError();
6900
6901 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6902 NamedDecl *ToPack;
6903 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006904
6905 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006906 if (!E->isValueDependent())
6907 Length = E->getPackLength();
6908
Balazs Keri3b30d652018-10-19 13:32:20 +00006909 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006910 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006911 if (Error Err = ImportTemplateArguments(
6912 E->getPartialArguments().data(),
6913 E->getPartialArguments().size(),
6914 ToPartialArguments))
6915 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006916 }
6917
6918 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006919 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6920 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006921}
6922
Aleksei Sidorina693b372016-09-28 10:16:56 +00006923
Balazs Keri3b30d652018-10-19 13:32:20 +00006924ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6925 auto Imp = importSeq(
6926 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6927 E->getArraySize(), E->getInitializer(), E->getType(),
6928 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6929 E->getDirectInitRange());
6930 if (!Imp)
6931 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006932
Balazs Keri3b30d652018-10-19 13:32:20 +00006933 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6934 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6935 Expr *ToArraySize, *ToInitializer;
6936 QualType ToType;
6937 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6938 std::tie(
6939 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6940 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006941
Balazs Keri3b30d652018-10-19 13:32:20 +00006942 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6943 if (Error Err =
6944 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6945 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006946
6947 return new (Importer.getToContext()) CXXNewExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006948 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6949 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6950 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6951 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6952 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006953}
6954
Balazs Keri3b30d652018-10-19 13:32:20 +00006955ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6956 auto Imp = importSeq(
6957 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6958 if (!Imp)
6959 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006960
Balazs Keri3b30d652018-10-19 13:32:20 +00006961 QualType ToType;
6962 FunctionDecl *ToOperatorDelete;
6963 Expr *ToArgument;
6964 SourceLocation ToBeginLoc;
6965 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006966
6967 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006968 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6969 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6970 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006971}
6972
Balazs Keri3b30d652018-10-19 13:32:20 +00006973ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6974 auto Imp = importSeq(
6975 E->getType(), E->getLocation(), E->getConstructor(),
6976 E->getParenOrBraceRange());
6977 if (!Imp)
6978 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006979
Balazs Keri3b30d652018-10-19 13:32:20 +00006980 QualType ToType;
6981 SourceLocation ToLocation;
6982 CXXConstructorDecl *ToConstructor;
6983 SourceRange ToParenOrBraceRange;
6984 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006985
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006986 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006987 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6988 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006989
Balazs Keri3b30d652018-10-19 13:32:20 +00006990 return CXXConstructExpr::Create(
6991 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6992 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6993 E->isListInitialization(), E->isStdInitListInitialization(),
6994 E->requiresZeroInitialization(), E->getConstructionKind(),
6995 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006996}
6997
Balazs Keri3b30d652018-10-19 13:32:20 +00006998ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
6999 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7000 if (!ToSubExprOrErr)
7001 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007002
Balazs Keri3b30d652018-10-19 13:32:20 +00007003 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7004 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7005 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007006
Balazs Keri3b30d652018-10-19 13:32:20 +00007007 return ExprWithCleanups::Create(
7008 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7009 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007010}
7011
Balazs Keri3b30d652018-10-19 13:32:20 +00007012ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7013 auto Imp = importSeq(
7014 E->getCallee(), E->getType(), E->getRParenLoc());
7015 if (!Imp)
7016 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007017
Balazs Keri3b30d652018-10-19 13:32:20 +00007018 Expr *ToCallee;
7019 QualType ToType;
7020 SourceLocation ToRParenLoc;
7021 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007022
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007023 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007024 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7025 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007026
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007027 return new (Importer.getToContext()) CXXMemberCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007028 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7029 ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007030}
7031
Balazs Keri3b30d652018-10-19 13:32:20 +00007032ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7033 ExpectedType ToTypeOrErr = import(E->getType());
7034 if (!ToTypeOrErr)
7035 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007036
Balazs Keri3b30d652018-10-19 13:32:20 +00007037 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7038 if (!ToLocationOrErr)
7039 return ToLocationOrErr.takeError();
7040
7041 return new (Importer.getToContext()) CXXThisExpr(
7042 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007043}
7044
Balazs Keri3b30d652018-10-19 13:32:20 +00007045ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7046 ExpectedType ToTypeOrErr = import(E->getType());
7047 if (!ToTypeOrErr)
7048 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007049
Balazs Keri3b30d652018-10-19 13:32:20 +00007050 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7051 if (!ToLocationOrErr)
7052 return ToLocationOrErr.takeError();
7053
7054 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7055 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007056}
7057
Balazs Keri3b30d652018-10-19 13:32:20 +00007058ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7059 auto Imp1 = importSeq(
7060 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7061 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7062 if (!Imp1)
7063 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007064
Balazs Keri3b30d652018-10-19 13:32:20 +00007065 Expr *ToBase;
7066 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7067 NestedNameSpecifierLoc ToQualifierLoc;
7068 ValueDecl *ToMemberDecl;
7069 QualType ToType;
7070 std::tie(
7071 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7072 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007073
Balazs Keri3b30d652018-10-19 13:32:20 +00007074 auto Imp2 = importSeq(
7075 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7076 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7077 if (!Imp2)
7078 return Imp2.takeError();
7079 NamedDecl *ToDecl;
7080 DeclarationName ToName;
7081 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7082 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007083
7084 DeclAccessPair ToFoundDecl =
7085 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007086
Balazs Keri3b30d652018-10-19 13:32:20 +00007087 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007088
7089 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007090 // FIXME: handle template arguments
7091 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Sean Callanan59721b32015-04-28 18:41:46 +00007092 }
7093
Balazs Keri3b30d652018-10-19 13:32:20 +00007094 return MemberExpr::Create(
7095 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7096 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
7097 ToMemberNameInfo, nullptr, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007098}
7099
Balazs Keri3b30d652018-10-19 13:32:20 +00007100ExpectedStmt
7101ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7102 auto Imp = importSeq(
7103 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7104 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7105 if (!Imp)
7106 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007107
Balazs Keri3b30d652018-10-19 13:32:20 +00007108 Expr *ToBase;
7109 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7110 NestedNameSpecifierLoc ToQualifierLoc;
7111 TypeSourceInfo *ToScopeTypeInfo;
7112 std::tie(
7113 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7114 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007115
7116 PseudoDestructorTypeStorage Storage;
7117 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7118 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007119 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7120 if (!ToDestroyedTypeLocOrErr)
7121 return ToDestroyedTypeLocOrErr.takeError();
7122 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007123 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007124 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7125 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7126 else
7127 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007128 }
7129
7130 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007131 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7132 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007133}
7134
Balazs Keri3b30d652018-10-19 13:32:20 +00007135ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007136 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007137 auto Imp = importSeq(
7138 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7139 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7140 if (!Imp)
7141 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007142
Balazs Keri3b30d652018-10-19 13:32:20 +00007143 QualType ToType;
7144 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7145 NestedNameSpecifierLoc ToQualifierLoc;
7146 NamedDecl *ToFirstQualifierFoundInScope;
7147 std::tie(
7148 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7149 ToFirstQualifierFoundInScope) = *Imp;
7150
7151 Expr *ToBase = nullptr;
7152 if (!E->isImplicitAccess()) {
7153 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7154 ToBase = *ToBaseOrErr;
7155 else
7156 return ToBaseOrErr.takeError();
7157 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007158
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007159 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007160 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007161 if (Error Err = ImportTemplateArgumentListInfo(
7162 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7163 ToTAInfo))
7164 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007165 ResInfo = &ToTAInfo;
7166 }
7167
Balazs Keri3b30d652018-10-19 13:32:20 +00007168 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7169 if (!ToMemberNameInfoOrErr)
7170 return ToMemberNameInfoOrErr.takeError();
7171 DeclarationNameInfo ToMemberNameInfo(
7172 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007173 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007174 if (Error Err = ImportDeclarationNameLoc(
7175 E->getMemberNameInfo(), ToMemberNameInfo))
7176 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007177
7178 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007179 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7180 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7181 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007182}
7183
Balazs Keri3b30d652018-10-19 13:32:20 +00007184ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007185ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007186 auto Imp = importSeq(
7187 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7188 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7189 if (!Imp)
7190 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007191
Balazs Keri3b30d652018-10-19 13:32:20 +00007192 NestedNameSpecifierLoc ToQualifierLoc;
7193 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7194 DeclarationName ToDeclName;
7195 std::tie(
7196 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7197 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007198
Balazs Keri3b30d652018-10-19 13:32:20 +00007199 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7200 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7201 return std::move(Err);
7202
7203 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007204 TemplateArgumentListInfo *ResInfo = nullptr;
7205 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007206 if (Error Err =
7207 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7208 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007209 ResInfo = &ToTAInfo;
7210 }
7211
7212 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007213 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7214 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007215}
7216
Balazs Keri3b30d652018-10-19 13:32:20 +00007217ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7218 CXXUnresolvedConstructExpr *E) {
7219 auto Imp = importSeq(
7220 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7221 if (!Imp)
7222 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007223
Balazs Keri3b30d652018-10-19 13:32:20 +00007224 SourceLocation ToLParenLoc, ToRParenLoc;
7225 TypeSourceInfo *ToTypeSourceInfo;
7226 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7227
7228 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7229 if (Error Err =
7230 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7231 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007232
7233 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007234 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7235 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007236}
7237
Balazs Keri3b30d652018-10-19 13:32:20 +00007238ExpectedStmt
7239ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7240 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7241 if (!ToNamingClassOrErr)
7242 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007243
Balazs Keri3b30d652018-10-19 13:32:20 +00007244 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7245 if (!ToQualifierLocOrErr)
7246 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007247
Balazs Keri3b30d652018-10-19 13:32:20 +00007248 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7249 if (!ToNameInfoOrErr)
7250 return ToNameInfoOrErr.takeError();
7251 DeclarationNameInfo ToNameInfo(
7252 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007253 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007254 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7255 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007256
7257 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007258 for (auto *D : E->decls())
7259 if (auto ToDOrErr = import(D))
7260 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007261 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007262 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007263
Balazs Keri3b30d652018-10-19 13:32:20 +00007264 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7265 TemplateArgumentListInfo ToTAInfo;
7266 if (Error Err = ImportTemplateArgumentListInfo(
7267 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7268 ToTAInfo))
7269 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007270
Balazs Keri3b30d652018-10-19 13:32:20 +00007271 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7272 if (!ToTemplateKeywordLocOrErr)
7273 return ToTemplateKeywordLocOrErr.takeError();
7274
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007275 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007276 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7277 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7278 ToDecls.begin(), ToDecls.end());
7279 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007280
7281 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007282 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7283 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7284 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007285}
7286
Balazs Keri3b30d652018-10-19 13:32:20 +00007287ExpectedStmt
7288ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7289 auto Imp1 = importSeq(
7290 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7291 E->getTemplateKeywordLoc());
7292 if (!Imp1)
7293 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007294
Balazs Keri3b30d652018-10-19 13:32:20 +00007295 QualType ToType;
7296 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7297 NestedNameSpecifierLoc ToQualifierLoc;
7298 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7299
7300 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7301 if (!Imp2)
7302 return Imp2.takeError();
7303 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7304 // Import additional name location/type info.
7305 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7306 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007307
7308 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007309 for (Decl *D : E->decls())
7310 if (auto ToDOrErr = import(D))
7311 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007312 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007313 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007314
7315 TemplateArgumentListInfo ToTAInfo;
7316 TemplateArgumentListInfo *ResInfo = nullptr;
7317 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007318 if (Error Err =
7319 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7320 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007321 ResInfo = &ToTAInfo;
7322 }
7323
Balazs Keri3b30d652018-10-19 13:32:20 +00007324 Expr *ToBase = nullptr;
7325 if (!E->isImplicitAccess()) {
7326 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7327 ToBase = *ToBaseOrErr;
7328 else
7329 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007330 }
7331
7332 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007333 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7334 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7335 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007336}
7337
Balazs Keri3b30d652018-10-19 13:32:20 +00007338ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7339 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7340 if (!Imp)
7341 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007342
Balazs Keri3b30d652018-10-19 13:32:20 +00007343 Expr *ToCallee;
7344 QualType ToType;
7345 SourceLocation ToRParenLoc;
7346 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007347
7348 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007349 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7350 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7351 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007352
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007353 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
7354 return new (Importer.getToContext()) CXXOperatorCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007355 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
7356 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007357 }
7358
Balazs Keri3b30d652018-10-19 13:32:20 +00007359 return new (Importer.getToContext()) CallExpr(
7360 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7361 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007362}
7363
Balazs Keri3b30d652018-10-19 13:32:20 +00007364ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7365 CXXRecordDecl *FromClass = E->getLambdaClass();
7366 auto ToClassOrErr = import(FromClass);
7367 if (!ToClassOrErr)
7368 return ToClassOrErr.takeError();
7369 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007370
7371 // NOTE: lambda classes are created with BeingDefined flag set up.
7372 // It means that ImportDefinition doesn't work for them and we should fill it
7373 // manually.
7374 if (ToClass->isBeingDefined()) {
7375 for (auto FromField : FromClass->fields()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007376 auto ToFieldOrErr = import(FromField);
7377 if (!ToFieldOrErr)
7378 return ToFieldOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007379 }
7380 }
7381
Balazs Keri3b30d652018-10-19 13:32:20 +00007382 auto ToCallOpOrErr = import(E->getCallOperator());
7383 if (!ToCallOpOrErr)
7384 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007385
7386 ToClass->completeDefinition();
7387
Balazs Keri3b30d652018-10-19 13:32:20 +00007388 SmallVector<LambdaCapture, 8> ToCaptures;
7389 ToCaptures.reserve(E->capture_size());
7390 for (const auto &FromCapture : E->captures()) {
7391 if (auto ToCaptureOrErr = import(FromCapture))
7392 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007393 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007394 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007395 }
7396
Balazs Keri3b30d652018-10-19 13:32:20 +00007397 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7398 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7399 return std::move(Err);
7400
7401 auto Imp = importSeq(
7402 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7403 if (!Imp)
7404 return Imp.takeError();
7405
7406 SourceRange ToIntroducerRange;
7407 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7408 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007409
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007410 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007411 Importer.getToContext(), ToClass, ToIntroducerRange,
7412 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7413 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7414 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007415}
7416
Sean Callanan8bca9962016-03-28 21:43:01 +00007417
Balazs Keri3b30d652018-10-19 13:32:20 +00007418ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7419 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7420 if (!Imp)
7421 return Imp.takeError();
7422
7423 SourceLocation ToLBraceLoc, ToRBraceLoc;
7424 QualType ToType;
7425 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7426
7427 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7428 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7429 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007430
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007431 ASTContext &ToCtx = Importer.getToContext();
7432 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007433 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7434 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007435
Balazs Keri3b30d652018-10-19 13:32:20 +00007436 if (E->hasArrayFiller()) {
7437 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7438 To->setArrayFiller(*ToFillerOrErr);
7439 else
7440 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007441 }
7442
Balazs Keri3b30d652018-10-19 13:32:20 +00007443 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7444 if (auto ToFDOrErr = import(FromFD))
7445 To->setInitializedFieldInUnion(*ToFDOrErr);
7446 else
7447 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007448 }
7449
Balazs Keri3b30d652018-10-19 13:32:20 +00007450 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7451 if (auto ToSyntFormOrErr = import(SyntForm))
7452 To->setSyntacticForm(*ToSyntFormOrErr);
7453 else
7454 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007455 }
7456
Gabor Martona20ce602018-09-03 13:10:53 +00007457 // Copy InitListExprBitfields, which are not handled in the ctor of
7458 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007459 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007460
7461 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007462}
7463
Balazs Keri3b30d652018-10-19 13:32:20 +00007464ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007465 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007466 ExpectedType ToTypeOrErr = import(E->getType());
7467 if (!ToTypeOrErr)
7468 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007469
Balazs Keri3b30d652018-10-19 13:32:20 +00007470 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7471 if (!ToSubExprOrErr)
7472 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007473
Balazs Keri3b30d652018-10-19 13:32:20 +00007474 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7475 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007476}
7477
Balazs Keri3b30d652018-10-19 13:32:20 +00007478ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007479 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007480 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7481 if (!Imp)
7482 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007483
Balazs Keri3b30d652018-10-19 13:32:20 +00007484 SourceLocation ToLocation;
7485 QualType ToType;
7486 CXXConstructorDecl *ToConstructor;
7487 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007488
7489 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007490 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7491 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007492}
7493
Balazs Keri3b30d652018-10-19 13:32:20 +00007494ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7495 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7496 if (!Imp)
7497 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007498
Balazs Keri3b30d652018-10-19 13:32:20 +00007499 QualType ToType;
7500 Expr *ToCommonExpr, *ToSubExpr;
7501 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007502
Balazs Keri3b30d652018-10-19 13:32:20 +00007503 return new (Importer.getToContext()) ArrayInitLoopExpr(
7504 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007505}
7506
Balazs Keri3b30d652018-10-19 13:32:20 +00007507ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7508 ExpectedType ToTypeOrErr = import(E->getType());
7509 if (!ToTypeOrErr)
7510 return ToTypeOrErr.takeError();
7511 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007512}
7513
Balazs Keri3b30d652018-10-19 13:32:20 +00007514ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7515 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7516 if (!ToBeginLocOrErr)
7517 return ToBeginLocOrErr.takeError();
7518
7519 auto ToFieldOrErr = import(E->getField());
7520 if (!ToFieldOrErr)
7521 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007522
7523 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007524 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007525}
7526
Balazs Keri3b30d652018-10-19 13:32:20 +00007527ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7528 auto Imp = importSeq(
7529 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7530 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7531 if (!Imp)
7532 return Imp.takeError();
7533
7534 QualType ToType;
7535 Expr *ToSubExpr;
7536 TypeSourceInfo *ToTypeInfoAsWritten;
7537 SourceLocation ToOperatorLoc, ToRParenLoc;
7538 SourceRange ToAngleBrackets;
7539 std::tie(
7540 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7541 ToAngleBrackets) = *Imp;
7542
Sean Callanandd2c1742016-05-16 20:48:03 +00007543 ExprValueKind VK = E->getValueKind();
7544 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007545 auto ToBasePathOrErr = ImportCastPath(E);
7546 if (!ToBasePathOrErr)
7547 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007548
Sean Callanandd2c1742016-05-16 20:48:03 +00007549 if (isa<CXXStaticCastExpr>(E)) {
7550 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007551 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7552 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007553 } else if (isa<CXXDynamicCastExpr>(E)) {
7554 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007555 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7556 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007557 } else if (isa<CXXReinterpretCastExpr>(E)) {
7558 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007559 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7560 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007561 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007562 return CXXConstCastExpr::Create(
7563 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7564 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007565 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007566 llvm_unreachable("Unknown cast type");
7567 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007568 }
7569}
7570
Balazs Keri3b30d652018-10-19 13:32:20 +00007571ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007572 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007573 auto Imp = importSeq(
7574 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7575 if (!Imp)
7576 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007577
Balazs Keri3b30d652018-10-19 13:32:20 +00007578 QualType ToType;
7579 SourceLocation ToExprLoc;
7580 NonTypeTemplateParmDecl *ToParameter;
7581 Expr *ToReplacement;
7582 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007583
7584 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007585 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007586}
7587
Balazs Keri3b30d652018-10-19 13:32:20 +00007588ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7589 auto Imp = importSeq(
7590 E->getType(), E->getBeginLoc(), E->getEndLoc());
7591 if (!Imp)
7592 return Imp.takeError();
7593
7594 QualType ToType;
7595 SourceLocation ToBeginLoc, ToEndLoc;
7596 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007597
7598 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007599 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7600 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007601
7602 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7603 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007604 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007605
7606 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007607 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7608 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007609}
7610
Balazs Keri3b30d652018-10-19 13:32:20 +00007611ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7612 ExpectedType ToTypeOrErr = import(E->getType());
7613 if (!ToTypeOrErr)
7614 return ToTypeOrErr.takeError();
7615
7616 auto ToSourceRangeOrErr = import(E->getSourceRange());
7617 if (!ToSourceRangeOrErr)
7618 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007619
7620 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007621 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7622 return new (Importer.getToContext()) CXXTypeidExpr(
7623 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7624 else
7625 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007626 }
7627
Balazs Keri3b30d652018-10-19 13:32:20 +00007628 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7629 if (!ToExprOperandOrErr)
7630 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007631
Balazs Keri3b30d652018-10-19 13:32:20 +00007632 return new (Importer.getToContext()) CXXTypeidExpr(
7633 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007634}
7635
Lang Hames19e07e12017-06-20 21:06:00 +00007636void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7637 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007638 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7639 if (auto ImportedOrErr = import(FromOverriddenMethod))
7640 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7641 (*ImportedOrErr)->getCanonicalDecl()));
7642 else
7643 consumeError(ImportedOrErr.takeError());
7644 }
Lang Hames19e07e12017-06-20 21:06:00 +00007645}
7646
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007647ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007648 ASTContext &FromContext, FileManager &FromFileManager,
7649 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007650 : ToContext(ToContext), FromContext(FromContext),
7651 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7652 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007653 ImportedDecls[FromContext.getTranslationUnitDecl()]
7654 = ToContext.getTranslationUnitDecl();
7655}
7656
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007657ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007658
7659QualType ASTImporter::Import(QualType FromT) {
7660 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007661 return {};
John McCall424cec92011-01-19 06:33:43 +00007662
Balazs Keri3b30d652018-10-19 13:32:20 +00007663 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007664
7665 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007666 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007667 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007668 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007669 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007670
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007671 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007672 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007673 ExpectedType ToTOrErr = Importer.Visit(FromTy);
7674 if (!ToTOrErr) {
7675 llvm::consumeError(ToTOrErr.takeError());
7676 return {};
7677 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007678
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007679 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007680 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007681
Balazs Keri3b30d652018-10-19 13:32:20 +00007682 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007683}
7684
Douglas Gregor62d311f2010-02-09 19:21:46 +00007685TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007686 if (!FromTSI)
7687 return FromTSI;
7688
7689 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007690 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007691 QualType T = Import(FromTSI->getType());
7692 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007693 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007694
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007695 return ToContext.getTrivialTypeSourceInfo(
7696 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007697}
7698
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007699Attr *ASTImporter::Import(const Attr *FromAttr) {
7700 Attr *ToAttr = FromAttr->clone(ToContext);
7701 ToAttr->setRange(Import(FromAttr->getRange()));
7702 return ToAttr;
7703}
7704
Sean Callanan59721b32015-04-28 18:41:46 +00007705Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7706 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7707 if (Pos != ImportedDecls.end()) {
7708 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007709 // FIXME: move this call to ImportDeclParts().
Balazs Keri3b30d652018-10-19 13:32:20 +00007710 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD))
7711 llvm::consumeError(std::move(Err));
Sean Callanan59721b32015-04-28 18:41:46 +00007712 return ToD;
7713 } else {
7714 return nullptr;
7715 }
7716}
7717
Douglas Gregor62d311f2010-02-09 19:21:46 +00007718Decl *ASTImporter::Import(Decl *FromD) {
7719 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007720 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007721
Douglas Gregord451ea92011-07-29 23:31:30 +00007722 ASTNodeImporter Importer(*this);
7723
Gabor Marton26f72a92018-07-12 09:42:05 +00007724 // Check whether we've already imported this declaration.
7725 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7726 if (ToD) {
7727 // If FromD has some updated flags after last import, apply it
7728 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007729 return ToD;
7730 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007731
7732 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007733 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
7734 if (!ToDOrErr) {
7735 llvm::consumeError(ToDOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007736 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007737 }
7738 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007739
Gabor Marton26f72a92018-07-12 09:42:05 +00007740 // Notify subclasses.
7741 Imported(FromD, ToD);
7742
Gabor Martonac3a5d62018-09-17 12:04:52 +00007743 updateFlags(FromD, ToD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007744 return ToD;
7745}
7746
Balazs Keri3b30d652018-10-19 13:32:20 +00007747Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007748 if (!FromDC)
7749 return FromDC;
7750
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007751 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007752 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007753 return nullptr;
7754
Fangrui Song6907ce22018-07-30 19:24:48 +00007755 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007756 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007757 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7758 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007759 if (ToRecord->isCompleteDefinition()) {
7760 // Do nothing.
7761 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007762 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7763 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7764 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007765 } else {
7766 CompleteDecl(ToRecord);
7767 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007768 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7769 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007770 if (ToEnum->isCompleteDefinition()) {
7771 // Do nothing.
7772 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007773 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7774 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7775 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007776 } else {
7777 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007778 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007779 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7780 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007781 if (ToClass->getDefinition()) {
7782 // Do nothing.
7783 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007784 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7785 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7786 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007787 } else {
7788 CompleteDecl(ToClass);
7789 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007790 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7791 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007792 if (ToProto->getDefinition()) {
7793 // Do nothing.
7794 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007795 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7796 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7797 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007798 } else {
7799 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007800 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007801 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007802
Douglas Gregor95d82832012-01-24 18:36:04 +00007803 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007804}
7805
7806Expr *ASTImporter::Import(Expr *FromE) {
7807 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007808 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007809
7810 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7811}
7812
7813Stmt *ASTImporter::Import(Stmt *FromS) {
7814 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007815 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007816
Fangrui Song6907ce22018-07-30 19:24:48 +00007817 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007818 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7819 if (Pos != ImportedStmts.end())
7820 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007821
Balazs Keri3b30d652018-10-19 13:32:20 +00007822 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007823 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007824 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
7825 if (!ToSOrErr) {
7826 llvm::consumeError(ToSOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007827 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007828 }
Craig Topper36250ad2014-05-12 05:36:57 +00007829
Balazs Keri3b30d652018-10-19 13:32:20 +00007830 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007831 auto *FromE = cast<Expr>(FromS);
7832 // Copy ExprBitfields, which may not be handled in Expr subclasses
7833 // constructors.
7834 ToE->setValueKind(FromE->getValueKind());
7835 ToE->setObjectKind(FromE->getObjectKind());
7836 ToE->setTypeDependent(FromE->isTypeDependent());
7837 ToE->setValueDependent(FromE->isValueDependent());
7838 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7839 ToE->setContainsUnexpandedParameterPack(
7840 FromE->containsUnexpandedParameterPack());
7841 }
7842
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007843 // Record the imported declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007844 ImportedStmts[FromS] = *ToSOrErr;
7845 return *ToSOrErr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007846}
7847
7848NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7849 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007850 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007851
Douglas Gregor90ebf252011-04-27 16:48:40 +00007852 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7853
7854 switch (FromNNS->getKind()) {
7855 case NestedNameSpecifier::Identifier:
7856 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7857 return NestedNameSpecifier::Create(ToContext, prefix, II);
7858 }
Craig Topper36250ad2014-05-12 05:36:57 +00007859 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007860
7861 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007862 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007863 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007864 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7865 }
Craig Topper36250ad2014-05-12 05:36:57 +00007866 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007867
7868 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007869 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007870 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007871 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7872 }
Craig Topper36250ad2014-05-12 05:36:57 +00007873 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007874
7875 case NestedNameSpecifier::Global:
7876 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7877
Nikola Smiljanic67860242014-09-26 00:28:20 +00007878 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007879 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007880 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007881 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7882 }
7883 return nullptr;
7884
Douglas Gregor90ebf252011-04-27 16:48:40 +00007885 case NestedNameSpecifier::TypeSpec:
7886 case NestedNameSpecifier::TypeSpecWithTemplate: {
7887 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7888 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007889 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007890 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007891 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007892 bTemplate, T.getTypePtr());
7893 }
7894 }
Craig Topper36250ad2014-05-12 05:36:57 +00007895 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007896 }
7897
7898 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007899}
7900
Douglas Gregor14454802011-02-25 02:25:35 +00007901NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007902 // Copied from NestedNameSpecifier mostly.
7903 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7904 NestedNameSpecifierLoc NNS = FromNNS;
7905
7906 // Push each of the nested-name-specifiers's onto a stack for
7907 // serialization in reverse order.
7908 while (NNS) {
7909 NestedNames.push_back(NNS);
7910 NNS = NNS.getPrefix();
7911 }
7912
7913 NestedNameSpecifierLocBuilder Builder;
7914
7915 while (!NestedNames.empty()) {
7916 NNS = NestedNames.pop_back_val();
7917 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7918 if (!Spec)
7919 return NestedNameSpecifierLoc();
7920
7921 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7922 switch (Kind) {
7923 case NestedNameSpecifier::Identifier:
7924 Builder.Extend(getToContext(),
7925 Spec->getAsIdentifier(),
7926 Import(NNS.getLocalBeginLoc()),
7927 Import(NNS.getLocalEndLoc()));
7928 break;
7929
7930 case NestedNameSpecifier::Namespace:
7931 Builder.Extend(getToContext(),
7932 Spec->getAsNamespace(),
7933 Import(NNS.getLocalBeginLoc()),
7934 Import(NNS.getLocalEndLoc()));
7935 break;
7936
7937 case NestedNameSpecifier::NamespaceAlias:
7938 Builder.Extend(getToContext(),
7939 Spec->getAsNamespaceAlias(),
7940 Import(NNS.getLocalBeginLoc()),
7941 Import(NNS.getLocalEndLoc()));
7942 break;
7943
7944 case NestedNameSpecifier::TypeSpec:
7945 case NestedNameSpecifier::TypeSpecWithTemplate: {
7946 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7947 QualType(Spec->getAsType(), 0));
7948 Builder.Extend(getToContext(),
7949 Import(NNS.getLocalBeginLoc()),
7950 TSI->getTypeLoc(),
7951 Import(NNS.getLocalEndLoc()));
7952 break;
7953 }
7954
7955 case NestedNameSpecifier::Global:
7956 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7957 break;
7958
7959 case NestedNameSpecifier::Super: {
7960 SourceRange ToRange = Import(NNS.getSourceRange());
7961 Builder.MakeSuper(getToContext(),
7962 Spec->getAsRecordDecl(),
7963 ToRange.getBegin(),
7964 ToRange.getEnd());
7965 }
7966 }
7967 }
7968
7969 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007970}
7971
Douglas Gregore2e50d332010-12-01 01:36:18 +00007972TemplateName ASTImporter::Import(TemplateName From) {
7973 switch (From.getKind()) {
7974 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007975 if (auto *ToTemplate =
7976 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007977 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007978
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007979 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007980
Douglas Gregore2e50d332010-12-01 01:36:18 +00007981 case TemplateName::OverloadedTemplate: {
7982 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7983 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007984 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007985 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007986 ToTemplates.addDecl(To);
7987 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007988 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007989 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007990 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00007991 ToTemplates.end());
7992 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007993
Douglas Gregore2e50d332010-12-01 01:36:18 +00007994 case TemplateName::QualifiedTemplate: {
7995 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7996 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7997 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007998 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007999
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008000 if (auto *ToTemplate =
8001 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00008002 return ToContext.getQualifiedTemplateName(Qualifier,
8003 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008004 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008005
8006 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008007 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008008
Douglas Gregore2e50d332010-12-01 01:36:18 +00008009 case TemplateName::DependentTemplate: {
8010 DependentTemplateName *DTN = From.getAsDependentTemplateName();
8011 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
8012 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008013 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008014
Douglas Gregore2e50d332010-12-01 01:36:18 +00008015 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008016 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008017 Import(DTN->getIdentifier()));
8018 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008019
Douglas Gregore2e50d332010-12-01 01:36:18 +00008020 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
8021 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008022
8023 case TemplateName::SubstTemplateTemplateParm: {
8024 SubstTemplateTemplateParmStorage *subst
8025 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008026 auto *param =
8027 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00008028 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008029 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00008030
8031 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008032 if (replacement.isNull())
8033 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008034
John McCalld9dfe3a2011-06-30 08:33:18 +00008035 return ToContext.getSubstTemplateTemplateParm(param, replacement);
8036 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008037
Douglas Gregor5590be02011-01-15 06:45:20 +00008038 case TemplateName::SubstTemplateTemplateParmPack: {
8039 SubstTemplateTemplateParmPackStorage *SubstPack
8040 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008041 auto *Param =
8042 cast_or_null<TemplateTemplateParmDecl>(
8043 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00008044 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008045 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008046
Douglas Gregor5590be02011-01-15 06:45:20 +00008047 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008048 Expected<TemplateArgument> ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00008049 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
Balazs Keri3b30d652018-10-19 13:32:20 +00008050 if (!ArgPack) {
8051 llvm::consumeError(ArgPack.takeError());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008052 return {};
Balazs Keri3b30d652018-10-19 13:32:20 +00008053 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008054
Balazs Keri3b30d652018-10-19 13:32:20 +00008055 return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00008056 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008057 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008058
Douglas Gregore2e50d332010-12-01 01:36:18 +00008059 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008060}
8061
Douglas Gregor62d311f2010-02-09 19:21:46 +00008062SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
8063 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008064 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008065
Douglas Gregor811663e2010-02-10 00:15:17 +00008066 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008067
Douglas Gregor811663e2010-02-10 00:15:17 +00008068 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00008069 FileID ToFileID = Import(Decomposed.first);
8070 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008071 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008072 SourceManager &ToSM = ToContext.getSourceManager();
8073 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00008074}
8075
8076SourceRange ASTImporter::Import(SourceRange FromRange) {
8077 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
8078}
8079
Douglas Gregor811663e2010-02-10 00:15:17 +00008080FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008081 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008082 if (Pos != ImportedFileIDs.end())
8083 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008084
Douglas Gregor811663e2010-02-10 00:15:17 +00008085 SourceManager &FromSM = FromContext.getSourceManager();
8086 SourceManager &ToSM = ToContext.getSourceManager();
8087 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008088
8089 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008090 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008091 if (FromSLoc.isExpansion()) {
8092 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
8093 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
8094 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
8095 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8096 SourceLocation MLoc;
8097 if (FromEx.isMacroArgExpansion()) {
8098 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
8099 } else {
8100 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
8101 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
8102 FromEx.isExpansionTokenRange());
8103 }
8104 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008105 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008106 // Include location of this file.
8107 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
8108
8109 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
8110 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8111 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8112 // disk again
8113 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8114 // than mmap the files several times.
8115 const FileEntry *Entry =
8116 ToFileManager.getFile(Cache->OrigEntry->getName());
8117 if (!Entry)
8118 return {};
8119 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
8120 FromSLoc.getFile().getFileCharacteristic());
8121 } else {
8122 // FIXME: We want to re-use the existing MemoryBuffer!
8123 const llvm::MemoryBuffer *FromBuf =
8124 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
8125 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8126 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8127 FromBuf->getBufferIdentifier());
8128 ToID = ToSM.createFileID(std::move(ToBuf),
8129 FromSLoc.getFile().getFileCharacteristic());
8130 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008131 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008132
Sebastian Redl99219f12010-09-30 01:03:06 +00008133 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008134 return ToID;
8135}
8136
Sean Callanandd2c1742016-05-16 20:48:03 +00008137CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8138 Expr *ToExpr = Import(From->getInit());
8139 if (!ToExpr && From->getInit())
8140 return nullptr;
8141
8142 if (From->isBaseInitializer()) {
8143 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8144 if (!ToTInfo && From->getTypeSourceInfo())
8145 return nullptr;
8146
8147 return new (ToContext) CXXCtorInitializer(
8148 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
8149 ToExpr, Import(From->getRParenLoc()),
8150 From->isPackExpansion() ? Import(From->getEllipsisLoc())
8151 : SourceLocation());
8152 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008153 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008154 if (!ToField && From->getMember())
8155 return nullptr;
8156
8157 return new (ToContext) CXXCtorInitializer(
8158 ToContext, ToField, Import(From->getMemberLocation()),
8159 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8160 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008161 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00008162 Import(From->getIndirectMember()));
8163 if (!ToIField && From->getIndirectMember())
8164 return nullptr;
8165
8166 return new (ToContext) CXXCtorInitializer(
8167 ToContext, ToIField, Import(From->getMemberLocation()),
8168 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8169 } else if (From->isDelegatingInitializer()) {
8170 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8171 if (!ToTInfo && From->getTypeSourceInfo())
8172 return nullptr;
8173
8174 return new (ToContext)
8175 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
8176 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008177 } else {
8178 return nullptr;
8179 }
8180}
8181
Aleksei Sidorina693b372016-09-28 10:16:56 +00008182CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
8183 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8184 if (Pos != ImportedCXXBaseSpecifiers.end())
8185 return Pos->second;
8186
8187 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
8188 Import(BaseSpec->getSourceRange()),
8189 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8190 BaseSpec->getAccessSpecifierAsWritten(),
8191 Import(BaseSpec->getTypeSourceInfo()),
8192 Import(BaseSpec->getEllipsisLoc()));
8193 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8194 return Imported;
8195}
8196
Balazs Keri3b30d652018-10-19 13:32:20 +00008197Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008198 Decl *To = Import(From);
8199 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008200 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008201
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008202 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008203 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008204
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008205 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00008206 if (!ToRecord->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008207 return Importer.ImportDefinition(
8208 cast<RecordDecl>(FromDC), ToRecord,
8209 ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008210 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00008211 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008212
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008213 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00008214 if (!ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008215 return Importer.ImportDefinition(
8216 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008217 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008218 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008219
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008220 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008221 if (!ToIFace->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008222 return Importer.ImportDefinition(
8223 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8224 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008225 }
8226 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008227
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008228 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008229 if (!ToProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008230 return Importer.ImportDefinition(
8231 cast<ObjCProtocolDecl>(FromDC), ToProto,
8232 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008233 }
8234 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008235
Balazs Keri3b30d652018-10-19 13:32:20 +00008236 return Importer.ImportDeclContext(FromDC, true);
Douglas Gregor0a791672011-01-18 03:11:38 +00008237 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008238
8239 return Error::success();
8240}
8241
8242void ASTImporter::ImportDefinition(Decl *From) {
8243 Error Err = ImportDefinition_New(From);
8244 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008245}
8246
Douglas Gregor96e578d2010-02-05 17:54:41 +00008247DeclarationName ASTImporter::Import(DeclarationName FromName) {
8248 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008249 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008250
8251 switch (FromName.getNameKind()) {
8252 case DeclarationName::Identifier:
8253 return Import(FromName.getAsIdentifierInfo());
8254
8255 case DeclarationName::ObjCZeroArgSelector:
8256 case DeclarationName::ObjCOneArgSelector:
8257 case DeclarationName::ObjCMultiArgSelector:
8258 return Import(FromName.getObjCSelector());
8259
8260 case DeclarationName::CXXConstructorName: {
8261 QualType T = Import(FromName.getCXXNameType());
8262 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008263 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008264
8265 return ToContext.DeclarationNames.getCXXConstructorName(
8266 ToContext.getCanonicalType(T));
8267 }
8268
8269 case DeclarationName::CXXDestructorName: {
8270 QualType T = Import(FromName.getCXXNameType());
8271 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008272 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008273
8274 return ToContext.DeclarationNames.getCXXDestructorName(
8275 ToContext.getCanonicalType(T));
8276 }
8277
Richard Smith35845152017-02-07 01:37:30 +00008278 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008279 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00008280 Import(FromName.getCXXDeductionGuideTemplate()));
8281 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008282 return {};
Richard Smith35845152017-02-07 01:37:30 +00008283 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
8284 }
8285
Douglas Gregor96e578d2010-02-05 17:54:41 +00008286 case DeclarationName::CXXConversionFunctionName: {
8287 QualType T = Import(FromName.getCXXNameType());
8288 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008289 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008290
8291 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8292 ToContext.getCanonicalType(T));
8293 }
8294
8295 case DeclarationName::CXXOperatorName:
8296 return ToContext.DeclarationNames.getCXXOperatorName(
8297 FromName.getCXXOverloadedOperator());
8298
8299 case DeclarationName::CXXLiteralOperatorName:
8300 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
8301 Import(FromName.getCXXLiteralIdentifier()));
8302
8303 case DeclarationName::CXXUsingDirective:
8304 // FIXME: STATICS!
8305 return DeclarationName::getUsingDirectiveName();
8306 }
8307
David Blaikiee4d798f2012-01-20 21:50:17 +00008308 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008309}
8310
Douglas Gregore2e50d332010-12-01 01:36:18 +00008311IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008312 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008313 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008314
Sean Callananf94ef1d2016-05-14 06:11:19 +00008315 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8316
8317 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8318 ToId->setBuiltinID(FromId->getBuiltinID());
8319
8320 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008321}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008322
Douglas Gregor43f54792010-02-17 02:12:47 +00008323Selector ASTImporter::Import(Selector FromSel) {
8324 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008325 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00008326
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008327 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008328 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8329 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8330 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8331 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8332}
8333
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008334DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8335 DeclContext *DC,
8336 unsigned IDNS,
8337 NamedDecl **Decls,
8338 unsigned NumDecls) {
8339 return Name;
8340}
8341
8342DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008343 if (LastDiagFromFrom)
8344 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8345 FromContext.getDiagnostics());
8346 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008347 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008348}
8349
8350DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008351 if (!LastDiagFromFrom)
8352 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8353 ToContext.getDiagnostics());
8354 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008355 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008356}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008357
Douglas Gregor2e15c842012-02-01 21:00:38 +00008358void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008359 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008360 if (!ID->getDefinition())
8361 ID->startDefinition();
8362 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008363 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008364 if (!PD->getDefinition())
8365 PD->startDefinition();
8366 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008367 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008368 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8369 TD->startDefinition();
8370 TD->setCompleteDefinition(true);
8371 }
8372 }
8373 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008374 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008375 }
8376}
8377
Gabor Marton26f72a92018-07-12 09:42:05 +00008378Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8379 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8380 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8381 "Try to import an already imported Decl");
8382 if (Pos != ImportedDecls.end())
8383 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008384 ImportedDecls[From] = To;
8385 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008386}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008387
Douglas Gregordd6006f2012-07-17 21:16:27 +00008388bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8389 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00008390 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00008391 = ImportedTypes.find(From.getTypePtr());
8392 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8393 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008394
Douglas Gregordd6006f2012-07-17 21:16:27 +00008395 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008396 getStructuralEquivalenceKind(*this), false,
8397 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008398 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008399}