blob: 49623d0e676b15eac6ae129fe9ffa18ad49f9d8c [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000014
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Balazs Keri3b30d652018-10-19 13:32:20 +000074 using llvm::make_error;
75 using llvm::Error;
76 using llvm::Expected;
77 using ExpectedType = llvm::Expected<QualType>;
78 using ExpectedStmt = llvm::Expected<Stmt *>;
79 using ExpectedExpr = llvm::Expected<Expr *>;
80 using ExpectedDecl = llvm::Expected<Decl *>;
81 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000082
Balazs Keri3b30d652018-10-19 13:32:20 +000083 std::string ImportError::toString() const {
84 // FIXME: Improve error texts.
85 switch (Error) {
86 case NameConflict:
87 return "NameConflict";
88 case UnsupportedConstruct:
89 return "UnsupportedConstruct";
90 case Unknown:
91 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000092 }
Balazs Keri2a13d662018-10-19 15:16:51 +000093 llvm_unreachable("Invalid error code.");
94 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
125 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000126 }
127
Gabor Marton26f72a92018-07-12 09:42:05 +0000128 void updateFlags(const Decl *From, Decl *To) {
129 // Check if some flags or attrs are new in 'From' and copy into 'To'.
130 // FIXME: Other flags or attrs?
131 if (From->isUsed(false) && !To->isUsed(false))
132 To->setIsUsed();
133 }
134
Balazs Keri3b30d652018-10-19 13:32:20 +0000135 Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
136 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
137 "Try to get field index for non-field.");
138
139 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
140 if (!Owner)
141 return None;
142
143 unsigned Index = 0;
144 for (const auto *D : Owner->decls()) {
145 if (D == F)
146 return Index;
147
148 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
149 ++Index;
150 }
151
152 llvm_unreachable("Field was not found in its parent context.");
153
154 return None;
155 }
156
157 // FIXME: Temporary until every import returns Expected.
158 template <>
159 LLVM_NODISCARD Error
160 ASTImporter::importInto(SourceLocation &To, const SourceLocation &From) {
161 To = Import(From);
162 if (From.isValid() && To.isInvalid())
163 return llvm::make_error<ImportError>();
164 return Error::success();
165 }
166 // FIXME: Temporary until every import returns Expected.
167 template <>
168 LLVM_NODISCARD Error
169 ASTImporter::importInto(QualType &To, const QualType &From) {
170 To = Import(From);
171 if (!From.isNull() && To.isNull())
172 return llvm::make_error<ImportError>();
173 return Error::success();
174 }
175
176 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
177 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
178 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000180
Balazs Keri3b30d652018-10-19 13:32:20 +0000181 // Use this instead of Importer.importInto .
182 template <typename ImportT>
183 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
184 return Importer.importInto(To, From);
185 }
186
187 // Use this to import pointers of specific type.
188 template <typename ImportT>
189 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
190 auto ToI = Importer.Import(From);
191 if (!ToI && From)
192 return make_error<ImportError>();
193 To = cast_or_null<ImportT>(ToI);
194 return Error::success();
195 // FIXME: This should be the final code.
196 //auto ToOrErr = Importer.Import(From);
197 //if (ToOrErr) {
198 // To = cast_or_null<ImportT>(*ToOrErr);
199 //}
200 //return ToOrErr.takeError();
201 }
202
203 // Call the import function of ASTImporter for a baseclass of type `T` and
204 // cast the return value to `T`.
205 template <typename T>
206 Expected<T *> import(T *From) {
207 auto *To = Importer.Import(From);
208 if (!To && From)
209 return make_error<ImportError>();
210 return cast_or_null<T>(To);
211 // FIXME: This should be the final code.
212 //auto ToOrErr = Importer.Import(From);
213 //if (!ToOrErr)
214 // return ToOrErr.takeError();
215 //return cast_or_null<T>(*ToOrErr);
216 }
217
218 template <typename T>
219 Expected<T *> import(const T *From) {
220 return import(const_cast<T *>(From));
221 }
222
223 // Call the import function of ASTImporter for type `T`.
224 template <typename T>
225 Expected<T> import(const T &From) {
226 T To = Importer.Import(From);
227 T DefaultT;
228 if (To == DefaultT && !(From == DefaultT))
229 return make_error<ImportError>();
230 return To;
231 // FIXME: This should be the final code.
232 //return Importer.Import(From);
233 }
234
235 template <class T>
236 Expected<std::tuple<T>>
237 importSeq(const T &From) {
238 Expected<T> ToOrErr = import(From);
239 if (!ToOrErr)
240 return ToOrErr.takeError();
241 return std::make_tuple<T>(std::move(*ToOrErr));
242 }
243
244 // Import multiple objects with a single function call.
245 // This should work for every type for which a variant of `import` exists.
246 // The arguments are processed from left to right and import is stopped on
247 // first error.
248 template <class THead, class... TTail>
249 Expected<std::tuple<THead, TTail...>>
250 importSeq(const THead &FromHead, const TTail &...FromTail) {
251 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
252 if (!ToHeadOrErr)
253 return ToHeadOrErr.takeError();
254 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
255 if (!ToTailOrErr)
256 return ToTailOrErr.takeError();
257 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
258 }
259
260// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 template <typename ToDeclT> struct CallOverloadedCreateFun {
262 template <typename... Args>
263 auto operator()(Args &&... args)
264 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
265 return ToDeclT::Create(std::forward<Args>(args)...);
266 }
267 };
268
269 // Always use these functions to create a Decl during import. There are
270 // certain tasks which must be done after the Decl was created, e.g. we
271 // must immediately register that as an imported Decl. The parameter `ToD`
272 // will be set to the newly created Decl or if had been imported before
273 // then to the already imported Decl. Returns a bool value set to true if
274 // the `FromD` had been imported before.
275 template <typename ToDeclT, typename FromDeclT, typename... Args>
276 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
277 Args &&... args) {
278 // There may be several overloads of ToDeclT::Create. We must make sure
279 // to call the one which would be chosen by the arguments, thus we use a
280 // wrapper for the overload set.
281 CallOverloadedCreateFun<ToDeclT> OC;
282 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
283 std::forward<Args>(args)...);
284 }
285 // Use this overload if a special Type is needed to be created. E.g if we
286 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
287 // then:
288 // TypedefNameDecl *ToTypedef;
289 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
290 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
291 typename... Args>
292 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
293 Args &&... args) {
294 CallOverloadedCreateFun<NewDeclT> OC;
295 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
296 std::forward<Args>(args)...);
297 }
298 // Use this version if a special create function must be
299 // used, e.g. CXXRecordDecl::CreateLambda .
300 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
301 typename... Args>
302 LLVM_NODISCARD bool
303 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
304 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000305 // FIXME: This code is needed later.
306 //if (Importer.getImportDeclErrorIfAny(FromD)) {
307 // ToD = nullptr;
308 // return true; // Already imported but with error.
309 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000310 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
311 if (ToD)
312 return true; // Already imported.
313 ToD = CreateFun(std::forward<Args>(args)...);
314 InitializeImportedDecl(FromD, ToD);
315 return false; // A new Decl is created.
316 }
317
318 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
319 Importer.MapImported(FromD, ToD);
320 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
321 if (FromD->hasAttrs())
322 for (const Attr *FromAttr : FromD->getAttrs())
323 ToD->addAttr(Importer.Import(FromAttr));
324 if (FromD->isUsed())
325 ToD->setIsUsed();
326 if (FromD->isImplicit())
327 ToD->setImplicit();
328 }
329
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000331 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000332
Balazs Keri3b30d652018-10-19 13:32:20 +0000333 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
334 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
335 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336
337 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000338 ExpectedType VisitType(const Type *T);
339 ExpectedType VisitAtomicType(const AtomicType *T);
340 ExpectedType VisitBuiltinType(const BuiltinType *T);
341 ExpectedType VisitDecayedType(const DecayedType *T);
342 ExpectedType VisitComplexType(const ComplexType *T);
343 ExpectedType VisitPointerType(const PointerType *T);
344 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
345 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
346 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
347 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
348 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
349 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
350 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
351 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000352 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000353 ExpectedType VisitVectorType(const VectorType *T);
354 ExpectedType VisitExtVectorType(const ExtVectorType *T);
355 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
356 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
357 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
358 ExpectedType VisitParenType(const ParenType *T);
359 ExpectedType VisitTypedefType(const TypedefType *T);
360 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000361 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000362 ExpectedType VisitTypeOfType(const TypeOfType *T);
363 ExpectedType VisitDecltypeType(const DecltypeType *T);
364 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
365 ExpectedType VisitAutoType(const AutoType *T);
366 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000368 ExpectedType VisitRecordType(const RecordType *T);
369 ExpectedType VisitEnumType(const EnumType *T);
370 ExpectedType VisitAttributedType(const AttributedType *T);
371 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
372 ExpectedType VisitSubstTemplateTypeParmType(
373 const SubstTemplateTypeParmType *T);
374 ExpectedType VisitTemplateSpecializationType(
375 const TemplateSpecializationType *T);
376 ExpectedType VisitElaboratedType(const ElaboratedType *T);
377 ExpectedType VisitDependentNameType(const DependentNameType *T);
378 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
379 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000380 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
382 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
383 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000384
385 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000386 Error ImportDeclParts(
387 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
388 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
389 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
390 Error ImportDeclarationNameLoc(
391 const DeclarationNameInfo &From, DeclarationNameInfo &To);
392 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
393 Error ImportDeclContext(
394 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
395 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000396
Balazs Keri3b30d652018-10-19 13:32:20 +0000397 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000398
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000399 using Designator = DesignatedInitExpr::Designator;
400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000401 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000402 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000403 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000404 /// nothing (if minimal import is set) or might be everything (if minimal
405 /// import is not set).
406 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000407 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000408 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000410 /// DeclContext.
411 IDK_Basic
412 };
413
Douglas Gregor2e15c842012-02-01 21:00:38 +0000414 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
415 return IDK == IDK_Everything ||
416 (IDK == IDK_Default && !Importer.isMinimalImport());
417 }
418
Balazs Keri3b30d652018-10-19 13:32:20 +0000419 Error ImportInitializer(VarDecl *From, VarDecl *To);
420 Error ImportDefinition(
421 RecordDecl *From, RecordDecl *To,
422 ImportDefinitionKind Kind = IDK_Default);
423 Error ImportDefinition(
424 EnumDecl *From, EnumDecl *To,
425 ImportDefinitionKind Kind = IDK_Default);
426 Error ImportDefinition(
427 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
428 ImportDefinitionKind Kind = IDK_Default);
429 Error ImportDefinition(
430 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
431 ImportDefinitionKind Kind = IDK_Default);
432 Expected<TemplateParameterList *> ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000433 TemplateParameterList *Params);
Balazs Keri3b30d652018-10-19 13:32:20 +0000434 Error ImportTemplateArguments(
435 const TemplateArgument *FromArgs, unsigned NumFromArgs,
436 SmallVectorImpl<TemplateArgument> &ToArgs);
437 Expected<TemplateArgument>
438 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000439
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000440 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000441 Error ImportTemplateArgumentListInfo(
442 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000443
444 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000445 Error ImportTemplateArgumentListInfo(
446 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
447 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000448
Gabor Marton5254e642018-06-27 13:32:50 +0000449 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000450 using FunctionTemplateAndArgsTy =
451 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
452 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000453 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
454 FunctionDecl *FromFD);
455
Balazs Keri3b30d652018-10-19 13:32:20 +0000456 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000457
Gabor Marton950fb572018-07-17 12:39:27 +0000458 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000459 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
460 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000461 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
462 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000463 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000464 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000465 bool IsStructuralMatch(FunctionTemplateDecl *From,
466 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000467 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000468 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000469 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000470 ExpectedDecl VisitDecl(Decl *D);
471 ExpectedDecl VisitImportDecl(ImportDecl *D);
472 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
473 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
474 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
475 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
476 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
477 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
479 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
480 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
481 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
482 ExpectedDecl VisitLabelDecl(LabelDecl *D);
483 ExpectedDecl VisitEnumDecl(EnumDecl *D);
484 ExpectedDecl VisitRecordDecl(RecordDecl *D);
485 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
486 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
487 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
488 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
489 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
490 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
491 ExpectedDecl VisitFieldDecl(FieldDecl *D);
492 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
493 ExpectedDecl VisitFriendDecl(FriendDecl *D);
494 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
495 ExpectedDecl VisitVarDecl(VarDecl *D);
496 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
497 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
498 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
499 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
500 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
501 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
502 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
503 ExpectedDecl VisitUsingDecl(UsingDecl *D);
504 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
505 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
506 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
507 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000508
Balazs Keri3b30d652018-10-19 13:32:20 +0000509 Expected<ObjCTypeParamList *>
510 ImportObjCTypeParamList(ObjCTypeParamList *list);
511
512 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
513 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
514 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
515 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
516 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
517 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
518 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
519 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
520 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
521 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000522 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000523 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
524 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
525 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000526
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000527 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000528 ExpectedStmt VisitStmt(Stmt *S);
529 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
530 ExpectedStmt VisitDeclStmt(DeclStmt *S);
531 ExpectedStmt VisitNullStmt(NullStmt *S);
532 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
533 ExpectedStmt VisitCaseStmt(CaseStmt *S);
534 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
535 ExpectedStmt VisitLabelStmt(LabelStmt *S);
536 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
537 ExpectedStmt VisitIfStmt(IfStmt *S);
538 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
539 ExpectedStmt VisitWhileStmt(WhileStmt *S);
540 ExpectedStmt VisitDoStmt(DoStmt *S);
541 ExpectedStmt VisitForStmt(ForStmt *S);
542 ExpectedStmt VisitGotoStmt(GotoStmt *S);
543 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
544 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
545 ExpectedStmt VisitBreakStmt(BreakStmt *S);
546 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000547 // FIXME: MSAsmStmt
548 // FIXME: SEHExceptStmt
549 // FIXME: SEHFinallyStmt
550 // FIXME: SEHTryStmt
551 // FIXME: SEHLeaveStmt
552 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000553 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
554 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
555 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000556 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000557 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
558 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
559 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
560 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
561 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
562 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
563 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000564
565 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000566 ExpectedStmt VisitExpr(Expr *E);
567 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
568 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
569 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
570 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
571 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
572 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
573 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
574 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
575 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
576 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
577 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
578 ExpectedStmt VisitStringLiteral(StringLiteral *E);
579 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
580 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
581 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
582 ExpectedStmt VisitParenExpr(ParenExpr *E);
583 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
584 ExpectedStmt VisitStmtExpr(StmtExpr *E);
585 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
586 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
587 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
588 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
589 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
590 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
591 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
592 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
593 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
594 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
595 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
596 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
597 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
598 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
599 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
600 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
601 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
602 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
603 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
604 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
605 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
606 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
607 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
608 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
609 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
610 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
611 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
612 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
613 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
614 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
615 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
616 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
617 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
618 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
619 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
620 ExpectedStmt VisitMemberExpr(MemberExpr *E);
621 ExpectedStmt VisitCallExpr(CallExpr *E);
622 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
623 ExpectedStmt VisitInitListExpr(InitListExpr *E);
624 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
625 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
626 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
627 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
628 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
629 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
630 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
631 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
632 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000633
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000634 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000635 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000636 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000637 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
638 Expected<ItemT> ToOrErr = import(*Ibegin);
639 if (!ToOrErr)
640 return ToOrErr.takeError();
641 *Obegin = *ToOrErr;
642 }
643 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000644 }
645
Balazs Keri3b30d652018-10-19 13:32:20 +0000646 // Import every item from a container structure into an output container.
647 // If error occurs, stops at first error and returns the error.
648 // The output container should have space for all needed elements (it is not
649 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000650 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000651 Error ImportContainerChecked(
652 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
653 return ImportArrayChecked(
654 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000655 }
656
657 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000658 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000659 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
660 }
Lang Hames19e07e12017-06-20 21:06:00 +0000661
Lang Hames19e07e12017-06-20 21:06:00 +0000662 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000663
Balazs Keri3b30d652018-10-19 13:32:20 +0000664 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
665 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000666 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000667
Balazs Keri3b30d652018-10-19 13:32:20 +0000668// FIXME: Temporary until every import returns Expected.
669template <>
670Expected<TemplateName> ASTNodeImporter::import(const TemplateName &From) {
671 TemplateName To = Importer.Import(From);
672 if (To.isNull() && !From.isNull())
673 return make_error<ImportError>();
674 return To;
675}
676
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000677template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000678Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000679 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
680 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000681 auto ToLAngleLocOrErr = import(FromLAngleLoc);
682 if (!ToLAngleLocOrErr)
683 return ToLAngleLocOrErr.takeError();
684 auto ToRAngleLocOrErr = import(FromRAngleLoc);
685 if (!ToRAngleLocOrErr)
686 return ToRAngleLocOrErr.takeError();
687
688 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
689 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
690 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000691 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000692 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000693}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000694
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000695template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000696Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000697 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
698 return ImportTemplateArgumentListInfo(
699 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
700}
701
702template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000703Error ASTNodeImporter::ImportTemplateArgumentListInfo<
704 ASTTemplateArgumentListInfo>(
705 const ASTTemplateArgumentListInfo &From,
706 TemplateArgumentListInfo &Result) {
707 return ImportTemplateArgumentListInfo(
708 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000709}
710
Balazs Keri3b30d652018-10-19 13:32:20 +0000711Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000712ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
713 FunctionDecl *FromFD) {
714 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000715 FunctionDecl::TK_FunctionTemplateSpecialization);
716
717 FunctionTemplateAndArgsTy Result;
718
Gabor Marton5254e642018-06-27 13:32:50 +0000719 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000720 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
721 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000722
723 // Import template arguments.
724 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000725 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
726 std::get<1>(Result)))
727 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000728
Balazs Keri3b30d652018-10-19 13:32:20 +0000729 return Result;
730}
731
732template <>
733Expected<TemplateParameterList *>
734ASTNodeImporter::import(TemplateParameterList *From) {
735 SmallVector<NamedDecl *, 4> To(From->size());
736 if (Error Err = ImportContainerChecked(*From, To))
737 return std::move(Err);
738
739 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
740 if (!ToRequiresClause)
741 return ToRequiresClause.takeError();
742
743 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
744 if (!ToTemplateLocOrErr)
745 return ToTemplateLocOrErr.takeError();
746 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
747 if (!ToLAngleLocOrErr)
748 return ToLAngleLocOrErr.takeError();
749 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
750 if (!ToRAngleLocOrErr)
751 return ToRAngleLocOrErr.takeError();
752
753 return TemplateParameterList::Create(
754 Importer.getToContext(),
755 *ToTemplateLocOrErr,
756 *ToLAngleLocOrErr,
757 To,
758 *ToRAngleLocOrErr,
759 *ToRequiresClause);
760}
761
762template <>
763Expected<TemplateArgument>
764ASTNodeImporter::import(const TemplateArgument &From) {
765 switch (From.getKind()) {
766 case TemplateArgument::Null:
767 return TemplateArgument();
768
769 case TemplateArgument::Type: {
770 ExpectedType ToTypeOrErr = import(From.getAsType());
771 if (!ToTypeOrErr)
772 return ToTypeOrErr.takeError();
773 return TemplateArgument(*ToTypeOrErr);
774 }
775
776 case TemplateArgument::Integral: {
777 ExpectedType ToTypeOrErr = import(From.getIntegralType());
778 if (!ToTypeOrErr)
779 return ToTypeOrErr.takeError();
780 return TemplateArgument(From, *ToTypeOrErr);
781 }
782
783 case TemplateArgument::Declaration: {
784 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
785 if (!ToOrErr)
786 return ToOrErr.takeError();
787 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
788 if (!ToTypeOrErr)
789 return ToTypeOrErr.takeError();
790 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
791 }
792
793 case TemplateArgument::NullPtr: {
794 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
795 if (!ToTypeOrErr)
796 return ToTypeOrErr.takeError();
797 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
798 }
799
800 case TemplateArgument::Template: {
801 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
802 if (!ToTemplateOrErr)
803 return ToTemplateOrErr.takeError();
804
805 return TemplateArgument(*ToTemplateOrErr);
806 }
807
808 case TemplateArgument::TemplateExpansion: {
809 Expected<TemplateName> ToTemplateOrErr =
810 import(From.getAsTemplateOrTemplatePattern());
811 if (!ToTemplateOrErr)
812 return ToTemplateOrErr.takeError();
813
814 return TemplateArgument(
815 *ToTemplateOrErr, From.getNumTemplateExpansions());
816 }
817
818 case TemplateArgument::Expression:
819 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
820 return TemplateArgument(*ToExpr);
821 else
822 return ToExpr.takeError();
823
824 case TemplateArgument::Pack: {
825 SmallVector<TemplateArgument, 2> ToPack;
826 ToPack.reserve(From.pack_size());
827 if (Error Err = ImportTemplateArguments(
828 From.pack_begin(), From.pack_size(), ToPack))
829 return std::move(Err);
830
831 return TemplateArgument(
832 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
833 }
834 }
835
836 llvm_unreachable("Invalid template argument kind");
837}
838
839template <>
840Expected<TemplateArgumentLoc>
841ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
842 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
843 if (!ArgOrErr)
844 return ArgOrErr.takeError();
845 TemplateArgument Arg = *ArgOrErr;
846
847 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
848
849 TemplateArgumentLocInfo ToInfo;
850 if (Arg.getKind() == TemplateArgument::Expression) {
851 ExpectedExpr E = import(FromInfo.getAsExpr());
852 if (!E)
853 return E.takeError();
854 ToInfo = TemplateArgumentLocInfo(*E);
855 } else if (Arg.getKind() == TemplateArgument::Type) {
856 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
857 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
858 else
859 return TSIOrErr.takeError();
860 } else {
861 auto ToTemplateQualifierLocOrErr =
862 import(FromInfo.getTemplateQualifierLoc());
863 if (!ToTemplateQualifierLocOrErr)
864 return ToTemplateQualifierLocOrErr.takeError();
865 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
866 if (!ToTemplateNameLocOrErr)
867 return ToTemplateNameLocOrErr.takeError();
868 auto ToTemplateEllipsisLocOrErr =
869 import(FromInfo.getTemplateEllipsisLoc());
870 if (!ToTemplateEllipsisLocOrErr)
871 return ToTemplateEllipsisLocOrErr.takeError();
872
873 ToInfo = TemplateArgumentLocInfo(
874 *ToTemplateQualifierLocOrErr,
875 *ToTemplateNameLocOrErr,
876 *ToTemplateEllipsisLocOrErr);
877 }
878
879 return TemplateArgumentLoc(Arg, ToInfo);
880}
881
882template <>
883Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
884 if (DG.isNull())
885 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
886 size_t NumDecls = DG.end() - DG.begin();
887 SmallVector<Decl *, 1> ToDecls;
888 ToDecls.reserve(NumDecls);
889 for (Decl *FromD : DG) {
890 if (auto ToDOrErr = import(FromD))
891 ToDecls.push_back(*ToDOrErr);
892 else
893 return ToDOrErr.takeError();
894 }
895 return DeclGroupRef::Create(Importer.getToContext(),
896 ToDecls.begin(),
897 NumDecls);
898}
899
900template <>
901Expected<ASTNodeImporter::Designator>
902ASTNodeImporter::import(const Designator &D) {
903 if (D.isFieldDesignator()) {
904 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
905
906 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
907 if (!ToDotLocOrErr)
908 return ToDotLocOrErr.takeError();
909
910 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
911 if (!ToFieldLocOrErr)
912 return ToFieldLocOrErr.takeError();
913
914 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
915 }
916
917 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
918 if (!ToLBracketLocOrErr)
919 return ToLBracketLocOrErr.takeError();
920
921 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
922 if (!ToRBracketLocOrErr)
923 return ToRBracketLocOrErr.takeError();
924
925 if (D.isArrayDesignator())
926 return Designator(D.getFirstExprIndex(),
927 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
928
929 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
930 if (!ToEllipsisLocOrErr)
931 return ToEllipsisLocOrErr.takeError();
932
933 assert(D.isArrayRangeDesignator());
934 return Designator(
935 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
936 *ToRBracketLocOrErr);
937}
938
939template <>
940Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
941 VarDecl *Var = nullptr;
942 if (From.capturesVariable()) {
943 if (auto VarOrErr = import(From.getCapturedVar()))
944 Var = *VarOrErr;
945 else
946 return VarOrErr.takeError();
947 }
948
949 auto LocationOrErr = import(From.getLocation());
950 if (!LocationOrErr)
951 return LocationOrErr.takeError();
952
953 SourceLocation EllipsisLoc;
954 if (From.isPackExpansion())
955 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
956 return std::move(Err);
957
958 return LambdaCapture(
959 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
960 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000961}
962
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000963} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000964
Douglas Gregor3996e242010-02-15 22:01:00 +0000965//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000966// Import Types
967//----------------------------------------------------------------------------
968
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000969using namespace clang;
970
Balazs Keri3b30d652018-10-19 13:32:20 +0000971ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000972 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
973 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000974 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000975}
976
Balazs Keri3b30d652018-10-19 13:32:20 +0000977ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
978 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
979 if (!UnderlyingTypeOrErr)
980 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000981
Balazs Keri3b30d652018-10-19 13:32:20 +0000982 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000983}
984
Balazs Keri3b30d652018-10-19 13:32:20 +0000985ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000986 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000987#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
988 case BuiltinType::Id: \
989 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000990#include "clang/Basic/OpenCLImageTypes.def"
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
Gabor Marton7a0841e2018-10-29 10:18:28 +00003261 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3262 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3263
Gabor Marton5254e642018-06-27 13:32:50 +00003264 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003265 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3266 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3267 if (!ToRedeclOrErr)
3268 return ToRedeclOrErr.takeError();
3269 }
Gabor Marton5254e642018-06-27 13:32:50 +00003270
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
Bruno Ricci5b30571752018-10-28 12:30:53 +00005710 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5711 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
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005775 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5776 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5777 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005778}
5779
Balazs Keri3b30d652018-10-19 13:32:20 +00005780ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5781 auto Imp = importSeq(
5782 S->getInit(), S->getConditionVariable(), S->getCond(),
5783 S->getBody(), S->getSwitchLoc());
5784 if (!Imp)
5785 return Imp.takeError();
5786
5787 Stmt *ToInit, *ToBody;
5788 VarDecl *ToConditionVariable;
5789 Expr *ToCond;
5790 SourceLocation ToSwitchLoc;
5791 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5792
Bruno Riccie2806f82018-10-29 16:12:37 +00005793 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5794 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005795 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005796 ToStmt->setSwitchLoc(ToSwitchLoc);
5797
Sean Callanan59721b32015-04-28 18:41:46 +00005798 // Now we have to re-chain the cases.
5799 SwitchCase *LastChainedSwitchCase = nullptr;
5800 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5801 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005802 Expected<SwitchCase *> ToSCOrErr = import(SC);
5803 if (!ToSCOrErr)
5804 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005805 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005806 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005807 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005808 ToStmt->setSwitchCaseList(*ToSCOrErr);
5809 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005810 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005811
Sean Callanan59721b32015-04-28 18:41:46 +00005812 return ToStmt;
5813}
5814
Balazs Keri3b30d652018-10-19 13:32:20 +00005815ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5816 auto Imp = importSeq(
5817 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5818 if (!Imp)
5819 return Imp.takeError();
5820
5821 VarDecl *ToConditionVariable;
5822 Expr *ToCond;
5823 Stmt *ToBody;
5824 SourceLocation ToWhileLoc;
5825 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5826
5827 return new (Importer.getToContext()) WhileStmt(
5828 Importer.getToContext(),
5829 ToConditionVariable, ToCond, ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005830}
5831
Balazs Keri3b30d652018-10-19 13:32:20 +00005832ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5833 auto Imp = importSeq(
5834 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5835 S->getRParenLoc());
5836 if (!Imp)
5837 return Imp.takeError();
5838
5839 Stmt *ToBody;
5840 Expr *ToCond;
5841 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5842 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5843
5844 return new (Importer.getToContext()) DoStmt(
5845 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005846}
5847
Balazs Keri3b30d652018-10-19 13:32:20 +00005848ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5849 auto Imp = importSeq(
5850 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5851 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5852 if (!Imp)
5853 return Imp.takeError();
5854
5855 Stmt *ToInit;
5856 Expr *ToCond, *ToInc;
5857 VarDecl *ToConditionVariable;
5858 Stmt *ToBody;
5859 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5860 std::tie(
5861 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5862 ToLParenLoc, ToRParenLoc) = *Imp;
5863
5864 return new (Importer.getToContext()) ForStmt(
5865 Importer.getToContext(),
5866 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5867 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005868}
5869
Balazs Keri3b30d652018-10-19 13:32:20 +00005870ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5871 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5872 if (!Imp)
5873 return Imp.takeError();
5874
5875 LabelDecl *ToLabel;
5876 SourceLocation ToGotoLoc, ToLabelLoc;
5877 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5878
5879 return new (Importer.getToContext()) GotoStmt(
5880 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005881}
5882
Balazs Keri3b30d652018-10-19 13:32:20 +00005883ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5884 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5885 if (!Imp)
5886 return Imp.takeError();
5887
5888 SourceLocation ToGotoLoc, ToStarLoc;
5889 Expr *ToTarget;
5890 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5891
5892 return new (Importer.getToContext()) IndirectGotoStmt(
5893 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005894}
5895
Balazs Keri3b30d652018-10-19 13:32:20 +00005896ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5897 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5898 if (!ToContinueLocOrErr)
5899 return ToContinueLocOrErr.takeError();
5900 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005901}
5902
Balazs Keri3b30d652018-10-19 13:32:20 +00005903ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5904 auto ToBreakLocOrErr = import(S->getBreakLoc());
5905 if (!ToBreakLocOrErr)
5906 return ToBreakLocOrErr.takeError();
5907 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005908}
5909
Balazs Keri3b30d652018-10-19 13:32:20 +00005910ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5911 auto Imp = importSeq(
5912 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5913 if (!Imp)
5914 return Imp.takeError();
5915
5916 SourceLocation ToReturnLoc;
5917 Expr *ToRetValue;
5918 const VarDecl *ToNRVOCandidate;
5919 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5920
5921 return new (Importer.getToContext()) ReturnStmt(
5922 ToReturnLoc, ToRetValue, ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005923}
5924
Balazs Keri3b30d652018-10-19 13:32:20 +00005925ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5926 auto Imp = importSeq(
5927 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5928 if (!Imp)
5929 return Imp.takeError();
5930
5931 SourceLocation ToCatchLoc;
5932 VarDecl *ToExceptionDecl;
5933 Stmt *ToHandlerBlock;
5934 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5935
5936 return new (Importer.getToContext()) CXXCatchStmt (
5937 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005938}
5939
Balazs Keri3b30d652018-10-19 13:32:20 +00005940ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5941 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5942 if (!ToTryLocOrErr)
5943 return ToTryLocOrErr.takeError();
5944
5945 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5946 if (!ToTryBlockOrErr)
5947 return ToTryBlockOrErr.takeError();
5948
Sean Callanan59721b32015-04-28 18:41:46 +00005949 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5950 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5951 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005952 if (auto ToHandlerOrErr = import(FromHandler))
5953 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005954 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005955 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005956 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005957
5958 return CXXTryStmt::Create(
5959 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005960}
5961
Balazs Keri3b30d652018-10-19 13:32:20 +00005962ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5963 auto Imp1 = importSeq(
5964 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5965 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5966 if (!Imp1)
5967 return Imp1.takeError();
5968 auto Imp2 = importSeq(
5969 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5970 if (!Imp2)
5971 return Imp2.takeError();
5972
5973 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5974 Expr *ToCond, *ToInc;
5975 Stmt *ToInit, *ToBody;
5976 std::tie(
5977 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5978 ToBody) = *Imp1;
5979 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5980 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5981
5982 return new (Importer.getToContext()) CXXForRangeStmt(
5983 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5984 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005985}
5986
Balazs Keri3b30d652018-10-19 13:32:20 +00005987ExpectedStmt
5988ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5989 auto Imp = importSeq(
5990 S->getElement(), S->getCollection(), S->getBody(),
5991 S->getForLoc(), S->getRParenLoc());
5992 if (!Imp)
5993 return Imp.takeError();
5994
5995 Stmt *ToElement, *ToBody;
5996 Expr *ToCollection;
5997 SourceLocation ToForLoc, ToRParenLoc;
5998 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5999
6000 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6001 ToCollection,
6002 ToBody,
6003 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006004 ToRParenLoc);
6005}
6006
Balazs Keri3b30d652018-10-19 13:32:20 +00006007ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6008 auto Imp = importSeq(
6009 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6010 S->getCatchBody());
6011 if (!Imp)
6012 return Imp.takeError();
6013
6014 SourceLocation ToAtCatchLoc, ToRParenLoc;
6015 VarDecl *ToCatchParamDecl;
6016 Stmt *ToCatchBody;
6017 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6018
6019 return new (Importer.getToContext()) ObjCAtCatchStmt (
6020 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006021}
6022
Balazs Keri3b30d652018-10-19 13:32:20 +00006023ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6024 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6025 if (!ToAtFinallyLocOrErr)
6026 return ToAtFinallyLocOrErr.takeError();
6027 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6028 if (!ToAtFinallyStmtOrErr)
6029 return ToAtFinallyStmtOrErr.takeError();
6030 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6031 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006032}
6033
Balazs Keri3b30d652018-10-19 13:32:20 +00006034ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6035 auto Imp = importSeq(
6036 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6037 if (!Imp)
6038 return Imp.takeError();
6039
6040 SourceLocation ToAtTryLoc;
6041 Stmt *ToTryBody, *ToFinallyStmt;
6042 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6043
Sean Callanan59721b32015-04-28 18:41:46 +00006044 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6045 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6046 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006047 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6048 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006049 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006050 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006051 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006052
Sean Callanan59721b32015-04-28 18:41:46 +00006053 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006054 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006055 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006056 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006057}
6058
Balazs Keri3b30d652018-10-19 13:32:20 +00006059ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006060 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006061 auto Imp = importSeq(
6062 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6063 if (!Imp)
6064 return Imp.takeError();
6065
6066 SourceLocation ToAtSynchronizedLoc;
6067 Expr *ToSynchExpr;
6068 Stmt *ToSynchBody;
6069 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6070
Sean Callanan59721b32015-04-28 18:41:46 +00006071 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6072 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6073}
6074
Balazs Keri3b30d652018-10-19 13:32:20 +00006075ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6076 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6077 if (!ToThrowLocOrErr)
6078 return ToThrowLocOrErr.takeError();
6079 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6080 if (!ToThrowExprOrErr)
6081 return ToThrowExprOrErr.takeError();
6082 return new (Importer.getToContext()) ObjCAtThrowStmt(
6083 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006084}
6085
Balazs Keri3b30d652018-10-19 13:32:20 +00006086ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6087 ObjCAutoreleasePoolStmt *S) {
6088 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6089 if (!ToAtLocOrErr)
6090 return ToAtLocOrErr.takeError();
6091 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6092 if (!ToSubStmtOrErr)
6093 return ToSubStmtOrErr.takeError();
6094 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6095 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006096}
6097
6098//----------------------------------------------------------------------------
6099// Import Expressions
6100//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006101ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006102 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6103 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006104 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006105}
6106
Balazs Keri3b30d652018-10-19 13:32:20 +00006107ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6108 auto Imp = importSeq(
6109 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6110 E->getRParenLoc(), E->getType());
6111 if (!Imp)
6112 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006113
Balazs Keri3b30d652018-10-19 13:32:20 +00006114 SourceLocation ToBuiltinLoc, ToRParenLoc;
6115 Expr *ToSubExpr;
6116 TypeSourceInfo *ToWrittenTypeInfo;
6117 QualType ToType;
6118 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6119 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006120
6121 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006122 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6123 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006124}
6125
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006126
Balazs Keri3b30d652018-10-19 13:32:20 +00006127ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6128 ExpectedType TypeOrErr = import(E->getType());
6129 if (!TypeOrErr)
6130 return TypeOrErr.takeError();
6131
6132 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6133 if (!BeginLocOrErr)
6134 return BeginLocOrErr.takeError();
6135
6136 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006137}
6138
Balazs Keri3b30d652018-10-19 13:32:20 +00006139ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6140 auto Imp = importSeq(
6141 E->getBeginLoc(), E->getType(), E->getFunctionName());
6142 if (!Imp)
6143 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006144
Balazs Keri3b30d652018-10-19 13:32:20 +00006145 SourceLocation ToBeginLoc;
6146 QualType ToType;
6147 StringLiteral *ToFunctionName;
6148 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006149
Bruno Ricci17ff0262018-10-27 19:21:19 +00006150 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6151 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006152}
6153
Balazs Keri3b30d652018-10-19 13:32:20 +00006154ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6155 auto Imp = importSeq(
6156 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6157 E->getLocation(), E->getType());
6158 if (!Imp)
6159 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006160
Balazs Keri3b30d652018-10-19 13:32:20 +00006161 NestedNameSpecifierLoc ToQualifierLoc;
6162 SourceLocation ToTemplateKeywordLoc, ToLocation;
6163 ValueDecl *ToDecl;
6164 QualType ToType;
6165 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6166 *Imp;
6167
6168 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006169 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006170 auto FoundDOrErr = import(E->getFoundDecl());
6171 if (!FoundDOrErr)
6172 return FoundDOrErr.takeError();
6173 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006174 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006175
Aleksei Sidorina693b372016-09-28 10:16:56 +00006176 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006177 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006178 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006179 if (Error Err =
6180 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6181 return std::move(Err);
6182 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006183 }
6184
Balazs Keri3b30d652018-10-19 13:32:20 +00006185 auto *ToE = DeclRefExpr::Create(
6186 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6187 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6188 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006189 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006190 ToE->setHadMultipleCandidates(true);
6191 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006192}
6193
Balazs Keri3b30d652018-10-19 13:32:20 +00006194ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6195 ExpectedType TypeOrErr = import(E->getType());
6196 if (!TypeOrErr)
6197 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006198
Balazs Keri3b30d652018-10-19 13:32:20 +00006199 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006200}
6201
Balazs Keri3b30d652018-10-19 13:32:20 +00006202ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6203 ExpectedExpr ToInitOrErr = import(E->getInit());
6204 if (!ToInitOrErr)
6205 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006206
Balazs Keri3b30d652018-10-19 13:32:20 +00006207 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6208 if (!ToEqualOrColonLocOrErr)
6209 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006210
Balazs Keri3b30d652018-10-19 13:32:20 +00006211 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006212 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006213 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6214 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6215 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006216 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006217 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006218 }
6219
Balazs Keri3b30d652018-10-19 13:32:20 +00006220 SmallVector<Designator, 4> ToDesignators(E->size());
6221 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6222 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006223
6224 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006225 Importer.getToContext(), ToDesignators,
6226 ToIndexExprs, *ToEqualOrColonLocOrErr,
6227 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006228}
6229
Balazs Keri3b30d652018-10-19 13:32:20 +00006230ExpectedStmt
6231ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6232 ExpectedType ToTypeOrErr = import(E->getType());
6233 if (!ToTypeOrErr)
6234 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006235
Balazs Keri3b30d652018-10-19 13:32:20 +00006236 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6237 if (!ToLocationOrErr)
6238 return ToLocationOrErr.takeError();
6239
6240 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6241 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006242}
6243
Balazs Keri3b30d652018-10-19 13:32:20 +00006244ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6245 ExpectedType ToTypeOrErr = import(E->getType());
6246 if (!ToTypeOrErr)
6247 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006248
Balazs Keri3b30d652018-10-19 13:32:20 +00006249 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6250 if (!ToLocationOrErr)
6251 return ToLocationOrErr.takeError();
6252
6253 return IntegerLiteral::Create(
6254 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006255}
6256
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006257
Balazs Keri3b30d652018-10-19 13:32:20 +00006258ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6259 ExpectedType ToTypeOrErr = import(E->getType());
6260 if (!ToTypeOrErr)
6261 return ToTypeOrErr.takeError();
6262
6263 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6264 if (!ToLocationOrErr)
6265 return ToLocationOrErr.takeError();
6266
6267 return FloatingLiteral::Create(
6268 Importer.getToContext(), E->getValue(), E->isExact(),
6269 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006270}
6271
Balazs Keri3b30d652018-10-19 13:32:20 +00006272ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6273 auto ToTypeOrErr = import(E->getType());
6274 if (!ToTypeOrErr)
6275 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006276
Balazs Keri3b30d652018-10-19 13:32:20 +00006277 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6278 if (!ToSubExprOrErr)
6279 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006280
Balazs Keri3b30d652018-10-19 13:32:20 +00006281 return new (Importer.getToContext()) ImaginaryLiteral(
6282 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006283}
6284
Balazs Keri3b30d652018-10-19 13:32:20 +00006285ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6286 ExpectedType ToTypeOrErr = import(E->getType());
6287 if (!ToTypeOrErr)
6288 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006289
Balazs Keri3b30d652018-10-19 13:32:20 +00006290 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6291 if (!ToLocationOrErr)
6292 return ToLocationOrErr.takeError();
6293
6294 return new (Importer.getToContext()) CharacterLiteral(
6295 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006296}
6297
Balazs Keri3b30d652018-10-19 13:32:20 +00006298ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6299 ExpectedType ToTypeOrErr = import(E->getType());
6300 if (!ToTypeOrErr)
6301 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006302
Balazs Keri3b30d652018-10-19 13:32:20 +00006303 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6304 if (Error Err = ImportArrayChecked(
6305 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6306 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006307
Balazs Keri3b30d652018-10-19 13:32:20 +00006308 return StringLiteral::Create(
6309 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6310 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006311}
6312
Balazs Keri3b30d652018-10-19 13:32:20 +00006313ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6314 auto Imp = importSeq(
6315 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6316 E->getInitializer());
6317 if (!Imp)
6318 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006319
Balazs Keri3b30d652018-10-19 13:32:20 +00006320 SourceLocation ToLParenLoc;
6321 TypeSourceInfo *ToTypeSourceInfo;
6322 QualType ToType;
6323 Expr *ToInitializer;
6324 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006325
6326 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006327 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6328 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006329}
6330
Balazs Keri3b30d652018-10-19 13:32:20 +00006331ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6332 auto Imp = importSeq(
6333 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6334 if (!Imp)
6335 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006336
Balazs Keri3b30d652018-10-19 13:32:20 +00006337 SourceLocation ToBuiltinLoc, ToRParenLoc;
6338 QualType ToType;
6339 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6340
6341 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6342 if (Error Err = ImportArrayChecked(
6343 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6344 ToExprs.begin()))
6345 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006346
6347 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006348 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006349}
6350
Balazs Keri3b30d652018-10-19 13:32:20 +00006351ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6352 auto Imp = importSeq(
6353 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6354 if (!Imp)
6355 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006356
Balazs Keri3b30d652018-10-19 13:32:20 +00006357 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6358 LabelDecl *ToLabel;
6359 QualType ToType;
6360 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006361
6362 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006363 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006364}
6365
Balazs Keri3b30d652018-10-19 13:32:20 +00006366ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6367 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6368 if (!Imp)
6369 return Imp.takeError();
6370
6371 SourceLocation ToLParen, ToRParen;
6372 Expr *ToSubExpr;
6373 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006374
Fangrui Song6907ce22018-07-30 19:24:48 +00006375 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006376 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006377}
6378
Balazs Keri3b30d652018-10-19 13:32:20 +00006379ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6380 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6381 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6382 return std::move(Err);
6383
6384 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6385 if (!ToLParenLocOrErr)
6386 return ToLParenLocOrErr.takeError();
6387
6388 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6389 if (!ToRParenLocOrErr)
6390 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006391
6392 return new (Importer.getToContext()) ParenListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006393 Importer.getToContext(), *ToLParenLocOrErr, ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006394}
6395
Balazs Keri3b30d652018-10-19 13:32:20 +00006396ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6397 auto Imp = importSeq(
6398 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6399 if (!Imp)
6400 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006401
Balazs Keri3b30d652018-10-19 13:32:20 +00006402 CompoundStmt *ToSubStmt;
6403 QualType ToType;
6404 SourceLocation ToLParenLoc, ToRParenLoc;
6405 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006406
Balazs Keri3b30d652018-10-19 13:32:20 +00006407 return new (Importer.getToContext()) StmtExpr(
6408 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006409}
6410
Balazs Keri3b30d652018-10-19 13:32:20 +00006411ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6412 auto Imp = importSeq(
6413 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6414 if (!Imp)
6415 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006416
Balazs Keri3b30d652018-10-19 13:32:20 +00006417 Expr *ToSubExpr;
6418 QualType ToType;
6419 SourceLocation ToOperatorLoc;
6420 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006421
Aaron Ballmana5038552018-01-09 13:07:03 +00006422 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006423 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6424 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006425}
6426
Balazs Keri3b30d652018-10-19 13:32:20 +00006427ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006428ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006429 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6430 if (!Imp)
6431 return Imp.takeError();
6432
6433 QualType ToType;
6434 SourceLocation ToOperatorLoc, ToRParenLoc;
6435 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006436
Douglas Gregord8552cd2010-02-19 01:24:23 +00006437 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006438 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6439 import(E->getArgumentTypeInfo());
6440 if (!ToArgumentTypeInfoOrErr)
6441 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006442
Balazs Keri3b30d652018-10-19 13:32:20 +00006443 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6444 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6445 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006446 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006447
Balazs Keri3b30d652018-10-19 13:32:20 +00006448 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6449 if (!ToArgumentExprOrErr)
6450 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006451
Balazs Keri3b30d652018-10-19 13:32:20 +00006452 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6453 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006454}
6455
Balazs Keri3b30d652018-10-19 13:32:20 +00006456ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6457 auto Imp = importSeq(
6458 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6459 if (!Imp)
6460 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006461
Balazs Keri3b30d652018-10-19 13:32:20 +00006462 Expr *ToLHS, *ToRHS;
6463 QualType ToType;
6464 SourceLocation ToOperatorLoc;
6465 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006466
Balazs Keri3b30d652018-10-19 13:32:20 +00006467 return new (Importer.getToContext()) BinaryOperator(
6468 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6469 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006470}
6471
Balazs Keri3b30d652018-10-19 13:32:20 +00006472ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6473 auto Imp = importSeq(
6474 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6475 E->getRHS(), E->getType());
6476 if (!Imp)
6477 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006478
Balazs Keri3b30d652018-10-19 13:32:20 +00006479 Expr *ToCond, *ToLHS, *ToRHS;
6480 SourceLocation ToQuestionLoc, ToColonLoc;
6481 QualType ToType;
6482 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006483
6484 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006485 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6486 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006487}
6488
Balazs Keri3b30d652018-10-19 13:32:20 +00006489ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006490 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006491 auto Imp = importSeq(
6492 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6493 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6494 if (!Imp)
6495 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006496
Balazs Keri3b30d652018-10-19 13:32:20 +00006497 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6498 OpaqueValueExpr *ToOpaqueValue;
6499 SourceLocation ToQuestionLoc, ToColonLoc;
6500 QualType ToType;
6501 std::tie(
6502 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6503 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006504
6505 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006506 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6507 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6508 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006509}
6510
Balazs Keri3b30d652018-10-19 13:32:20 +00006511ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6512 auto Imp = importSeq(
6513 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6514 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6515 if (!Imp)
6516 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006517
Balazs Keri3b30d652018-10-19 13:32:20 +00006518 SourceLocation ToBeginLoc, ToEndLoc;
6519 TypeSourceInfo *ToQueriedTypeSourceInfo;
6520 Expr *ToDimensionExpression;
6521 QualType ToType;
6522 std::tie(
6523 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6524 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006525
6526 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006527 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6528 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006529}
6530
Balazs Keri3b30d652018-10-19 13:32:20 +00006531ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6532 auto Imp = importSeq(
6533 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6534 if (!Imp)
6535 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006536
Balazs Keri3b30d652018-10-19 13:32:20 +00006537 SourceLocation ToBeginLoc, ToEndLoc;
6538 Expr *ToQueriedExpression;
6539 QualType ToType;
6540 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006541
6542 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006543 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6544 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006545}
6546
Balazs Keri3b30d652018-10-19 13:32:20 +00006547ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6548 auto Imp = importSeq(
6549 E->getLocation(), E->getType(), E->getSourceExpr());
6550 if (!Imp)
6551 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006552
Balazs Keri3b30d652018-10-19 13:32:20 +00006553 SourceLocation ToLocation;
6554 QualType ToType;
6555 Expr *ToSourceExpr;
6556 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006557
6558 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006559 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006560}
6561
Balazs Keri3b30d652018-10-19 13:32:20 +00006562ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6563 auto Imp = importSeq(
6564 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6565 if (!Imp)
6566 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006567
Balazs Keri3b30d652018-10-19 13:32:20 +00006568 Expr *ToLHS, *ToRHS;
6569 SourceLocation ToRBracketLoc;
6570 QualType ToType;
6571 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006572
6573 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006574 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6575 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006576}
6577
Balazs Keri3b30d652018-10-19 13:32:20 +00006578ExpectedStmt
6579ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6580 auto Imp = importSeq(
6581 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6582 E->getComputationResultType(), E->getOperatorLoc());
6583 if (!Imp)
6584 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006585
Balazs Keri3b30d652018-10-19 13:32:20 +00006586 Expr *ToLHS, *ToRHS;
6587 QualType ToType, ToComputationLHSType, ToComputationResultType;
6588 SourceLocation ToOperatorLoc;
6589 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6590 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006591
Balazs Keri3b30d652018-10-19 13:32:20 +00006592 return new (Importer.getToContext()) CompoundAssignOperator(
6593 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6594 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6595 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006596}
6597
Balazs Keri3b30d652018-10-19 13:32:20 +00006598Expected<CXXCastPath>
6599ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6600 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006601 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006602 if (auto SpecOrErr = import(*I))
6603 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006604 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006605 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006606 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006607 return Path;
John McCallcf142162010-08-07 06:22:56 +00006608}
6609
Balazs Keri3b30d652018-10-19 13:32:20 +00006610ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6611 ExpectedType ToTypeOrErr = import(E->getType());
6612 if (!ToTypeOrErr)
6613 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006614
Balazs Keri3b30d652018-10-19 13:32:20 +00006615 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6616 if (!ToSubExprOrErr)
6617 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006618
Balazs Keri3b30d652018-10-19 13:32:20 +00006619 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6620 if (!ToBasePathOrErr)
6621 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006622
Balazs Keri3b30d652018-10-19 13:32:20 +00006623 return ImplicitCastExpr::Create(
6624 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6625 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006626}
6627
Balazs Keri3b30d652018-10-19 13:32:20 +00006628ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6629 auto Imp1 = importSeq(
6630 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6631 if (!Imp1)
6632 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006633
Balazs Keri3b30d652018-10-19 13:32:20 +00006634 QualType ToType;
6635 Expr *ToSubExpr;
6636 TypeSourceInfo *ToTypeInfoAsWritten;
6637 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006638
Balazs Keri3b30d652018-10-19 13:32:20 +00006639 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6640 if (!ToBasePathOrErr)
6641 return ToBasePathOrErr.takeError();
6642 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006643
Aleksei Sidorina693b372016-09-28 10:16:56 +00006644 switch (E->getStmtClass()) {
6645 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006646 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006647 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6648 if (!ToLParenLocOrErr)
6649 return ToLParenLocOrErr.takeError();
6650 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6651 if (!ToRParenLocOrErr)
6652 return ToRParenLocOrErr.takeError();
6653 return CStyleCastExpr::Create(
6654 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6655 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6656 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006657 }
6658
6659 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006660 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006661 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6662 if (!ToLParenLocOrErr)
6663 return ToLParenLocOrErr.takeError();
6664 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6665 if (!ToRParenLocOrErr)
6666 return ToRParenLocOrErr.takeError();
6667 return CXXFunctionalCastExpr::Create(
6668 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6669 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6670 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006671 }
6672
6673 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006674 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6675 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6676 if (!ToLParenLocOrErr)
6677 return ToLParenLocOrErr.takeError();
6678 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6679 if (!ToBridgeKeywordLocOrErr)
6680 return ToBridgeKeywordLocOrErr.takeError();
6681 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6682 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6683 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006684 }
6685 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006686 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006687 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006688 }
6689}
6690
Balazs Keri3b30d652018-10-19 13:32:20 +00006691ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6692 SmallVector<OffsetOfNode, 4> ToNodes;
6693 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6694 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006695
Balazs Keri3b30d652018-10-19 13:32:20 +00006696 SourceLocation ToBeginLoc, ToEndLoc;
6697 if (FromNode.getKind() != OffsetOfNode::Base) {
6698 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6699 if (!Imp)
6700 return Imp.takeError();
6701 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6702 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006703
Balazs Keri3b30d652018-10-19 13:32:20 +00006704 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006705 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006706 ToNodes.push_back(
6707 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006708 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006709 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006710 auto ToBSOrErr = import(FromNode.getBase());
6711 if (!ToBSOrErr)
6712 return ToBSOrErr.takeError();
6713 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006714 break;
6715 }
6716 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006717 auto ToFieldOrErr = import(FromNode.getField());
6718 if (!ToFieldOrErr)
6719 return ToFieldOrErr.takeError();
6720 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006721 break;
6722 }
6723 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006724 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6725 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006726 break;
6727 }
6728 }
6729 }
6730
Balazs Keri3b30d652018-10-19 13:32:20 +00006731 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6732 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6733 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6734 if (!ToIndexExprOrErr)
6735 return ToIndexExprOrErr.takeError();
6736 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006737 }
6738
Balazs Keri3b30d652018-10-19 13:32:20 +00006739 auto Imp = importSeq(
6740 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6741 E->getRParenLoc());
6742 if (!Imp)
6743 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006744
Balazs Keri3b30d652018-10-19 13:32:20 +00006745 QualType ToType;
6746 TypeSourceInfo *ToTypeSourceInfo;
6747 SourceLocation ToOperatorLoc, ToRParenLoc;
6748 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6749
6750 return OffsetOfExpr::Create(
6751 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6752 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006753}
6754
Balazs Keri3b30d652018-10-19 13:32:20 +00006755ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6756 auto Imp = importSeq(
6757 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6758 if (!Imp)
6759 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006760
Balazs Keri3b30d652018-10-19 13:32:20 +00006761 QualType ToType;
6762 Expr *ToOperand;
6763 SourceLocation ToBeginLoc, ToEndLoc;
6764 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006765
Balazs Keri3b30d652018-10-19 13:32:20 +00006766 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006767 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006768 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006769 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006770 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006771
Balazs Keri3b30d652018-10-19 13:32:20 +00006772 return new (Importer.getToContext()) CXXNoexceptExpr(
6773 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006774}
6775
Balazs Keri3b30d652018-10-19 13:32:20 +00006776ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6777 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6778 if (!Imp)
6779 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006780
Balazs Keri3b30d652018-10-19 13:32:20 +00006781 Expr *ToSubExpr;
6782 QualType ToType;
6783 SourceLocation ToThrowLoc;
6784 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006785
6786 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006787 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006788}
6789
Balazs Keri3b30d652018-10-19 13:32:20 +00006790ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6791 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6792 if (!ToUsedLocOrErr)
6793 return ToUsedLocOrErr.takeError();
6794
6795 auto ToParamOrErr = import(E->getParam());
6796 if (!ToParamOrErr)
6797 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006798
6799 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006800 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006801}
6802
Balazs Keri3b30d652018-10-19 13:32:20 +00006803ExpectedStmt
6804ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6805 auto Imp = importSeq(
6806 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6807 if (!Imp)
6808 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006809
Balazs Keri3b30d652018-10-19 13:32:20 +00006810 QualType ToType;
6811 TypeSourceInfo *ToTypeSourceInfo;
6812 SourceLocation ToRParenLoc;
6813 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006814
6815 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006816 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006817}
6818
Balazs Keri3b30d652018-10-19 13:32:20 +00006819ExpectedStmt
6820ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6821 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6822 if (!ToSubExprOrErr)
6823 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006824
Balazs Keri3b30d652018-10-19 13:32:20 +00006825 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6826 if (!ToDtorOrErr)
6827 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006828
6829 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006830 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6831 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006832}
6833
Balazs Keri3b30d652018-10-19 13:32:20 +00006834ExpectedStmt
6835ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6836 auto Imp = importSeq(
6837 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6838 E->getParenOrBraceRange());
6839 if (!Imp)
6840 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006841
Balazs Keri3b30d652018-10-19 13:32:20 +00006842 CXXConstructorDecl *ToConstructor;
6843 QualType ToType;
6844 TypeSourceInfo *ToTypeSourceInfo;
6845 SourceRange ToParenOrBraceRange;
6846 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006847
Balazs Keri3b30d652018-10-19 13:32:20 +00006848 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6849 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6850 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006851
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006852 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006853 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6854 ToParenOrBraceRange, E->hadMultipleCandidates(),
6855 E->isListInitialization(), E->isStdInitListInitialization(),
6856 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006857}
6858
Balazs Keri3b30d652018-10-19 13:32:20 +00006859ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006860ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006861 auto Imp = importSeq(
6862 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6863 if (!Imp)
6864 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006865
Balazs Keri3b30d652018-10-19 13:32:20 +00006866 QualType ToType;
6867 Expr *ToTemporaryExpr;
6868 const ValueDecl *ToExtendingDecl;
6869 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006870
6871 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006872 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006873
6874 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006875 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006876 return ToMTE;
6877}
6878
Balazs Keri3b30d652018-10-19 13:32:20 +00006879ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6880 auto Imp = importSeq(
6881 E->getType(), E->getPattern(), E->getEllipsisLoc());
6882 if (!Imp)
6883 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006884
Balazs Keri3b30d652018-10-19 13:32:20 +00006885 QualType ToType;
6886 Expr *ToPattern;
6887 SourceLocation ToEllipsisLoc;
6888 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006889
6890 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006891 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006892}
6893
Balazs Keri3b30d652018-10-19 13:32:20 +00006894ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6895 auto Imp = importSeq(
6896 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6897 if (!Imp)
6898 return Imp.takeError();
6899
6900 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6901 NamedDecl *ToPack;
6902 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006903
6904 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006905 if (!E->isValueDependent())
6906 Length = E->getPackLength();
6907
Balazs Keri3b30d652018-10-19 13:32:20 +00006908 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006909 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006910 if (Error Err = ImportTemplateArguments(
6911 E->getPartialArguments().data(),
6912 E->getPartialArguments().size(),
6913 ToPartialArguments))
6914 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006915 }
6916
6917 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006918 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6919 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006920}
6921
Aleksei Sidorina693b372016-09-28 10:16:56 +00006922
Balazs Keri3b30d652018-10-19 13:32:20 +00006923ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6924 auto Imp = importSeq(
6925 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6926 E->getArraySize(), E->getInitializer(), E->getType(),
6927 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6928 E->getDirectInitRange());
6929 if (!Imp)
6930 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006931
Balazs Keri3b30d652018-10-19 13:32:20 +00006932 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6933 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6934 Expr *ToArraySize, *ToInitializer;
6935 QualType ToType;
6936 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6937 std::tie(
6938 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6939 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006940
Balazs Keri3b30d652018-10-19 13:32:20 +00006941 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6942 if (Error Err =
6943 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6944 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006945
6946 return new (Importer.getToContext()) CXXNewExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006947 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6948 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6949 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6950 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6951 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006952}
6953
Balazs Keri3b30d652018-10-19 13:32:20 +00006954ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6955 auto Imp = importSeq(
6956 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6957 if (!Imp)
6958 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006959
Balazs Keri3b30d652018-10-19 13:32:20 +00006960 QualType ToType;
6961 FunctionDecl *ToOperatorDelete;
6962 Expr *ToArgument;
6963 SourceLocation ToBeginLoc;
6964 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006965
6966 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006967 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6968 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6969 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006970}
6971
Balazs Keri3b30d652018-10-19 13:32:20 +00006972ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6973 auto Imp = importSeq(
6974 E->getType(), E->getLocation(), E->getConstructor(),
6975 E->getParenOrBraceRange());
6976 if (!Imp)
6977 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006978
Balazs Keri3b30d652018-10-19 13:32:20 +00006979 QualType ToType;
6980 SourceLocation ToLocation;
6981 CXXConstructorDecl *ToConstructor;
6982 SourceRange ToParenOrBraceRange;
6983 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006984
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006985 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006986 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6987 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006988
Balazs Keri3b30d652018-10-19 13:32:20 +00006989 return CXXConstructExpr::Create(
6990 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6991 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6992 E->isListInitialization(), E->isStdInitListInitialization(),
6993 E->requiresZeroInitialization(), E->getConstructionKind(),
6994 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006995}
6996
Balazs Keri3b30d652018-10-19 13:32:20 +00006997ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
6998 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6999 if (!ToSubExprOrErr)
7000 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007001
Balazs Keri3b30d652018-10-19 13:32:20 +00007002 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7003 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7004 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007005
Balazs Keri3b30d652018-10-19 13:32:20 +00007006 return ExprWithCleanups::Create(
7007 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7008 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007009}
7010
Balazs Keri3b30d652018-10-19 13:32:20 +00007011ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7012 auto Imp = importSeq(
7013 E->getCallee(), E->getType(), E->getRParenLoc());
7014 if (!Imp)
7015 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007016
Balazs Keri3b30d652018-10-19 13:32:20 +00007017 Expr *ToCallee;
7018 QualType ToType;
7019 SourceLocation ToRParenLoc;
7020 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007021
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007022 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007023 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7024 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007025
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007026 return new (Importer.getToContext()) CXXMemberCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007027 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7028 ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007029}
7030
Balazs Keri3b30d652018-10-19 13:32:20 +00007031ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7032 ExpectedType ToTypeOrErr = import(E->getType());
7033 if (!ToTypeOrErr)
7034 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007035
Balazs Keri3b30d652018-10-19 13:32:20 +00007036 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7037 if (!ToLocationOrErr)
7038 return ToLocationOrErr.takeError();
7039
7040 return new (Importer.getToContext()) CXXThisExpr(
7041 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007042}
7043
Balazs Keri3b30d652018-10-19 13:32:20 +00007044ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7045 ExpectedType ToTypeOrErr = import(E->getType());
7046 if (!ToTypeOrErr)
7047 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007048
Balazs Keri3b30d652018-10-19 13:32:20 +00007049 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7050 if (!ToLocationOrErr)
7051 return ToLocationOrErr.takeError();
7052
7053 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7054 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007055}
7056
Balazs Keri3b30d652018-10-19 13:32:20 +00007057ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7058 auto Imp1 = importSeq(
7059 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7060 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7061 if (!Imp1)
7062 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007063
Balazs Keri3b30d652018-10-19 13:32:20 +00007064 Expr *ToBase;
7065 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7066 NestedNameSpecifierLoc ToQualifierLoc;
7067 ValueDecl *ToMemberDecl;
7068 QualType ToType;
7069 std::tie(
7070 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7071 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007072
Balazs Keri3b30d652018-10-19 13:32:20 +00007073 auto Imp2 = importSeq(
7074 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7075 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7076 if (!Imp2)
7077 return Imp2.takeError();
7078 NamedDecl *ToDecl;
7079 DeclarationName ToName;
7080 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7081 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007082
7083 DeclAccessPair ToFoundDecl =
7084 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007085
Balazs Keri3b30d652018-10-19 13:32:20 +00007086 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007087
7088 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007089 // FIXME: handle template arguments
7090 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Sean Callanan59721b32015-04-28 18:41:46 +00007091 }
7092
Balazs Keri3b30d652018-10-19 13:32:20 +00007093 return MemberExpr::Create(
7094 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7095 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
7096 ToMemberNameInfo, nullptr, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007097}
7098
Balazs Keri3b30d652018-10-19 13:32:20 +00007099ExpectedStmt
7100ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7101 auto Imp = importSeq(
7102 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7103 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7104 if (!Imp)
7105 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007106
Balazs Keri3b30d652018-10-19 13:32:20 +00007107 Expr *ToBase;
7108 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7109 NestedNameSpecifierLoc ToQualifierLoc;
7110 TypeSourceInfo *ToScopeTypeInfo;
7111 std::tie(
7112 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7113 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007114
7115 PseudoDestructorTypeStorage Storage;
7116 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7117 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007118 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7119 if (!ToDestroyedTypeLocOrErr)
7120 return ToDestroyedTypeLocOrErr.takeError();
7121 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007122 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007123 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7124 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7125 else
7126 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007127 }
7128
7129 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007130 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7131 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007132}
7133
Balazs Keri3b30d652018-10-19 13:32:20 +00007134ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007135 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007136 auto Imp = importSeq(
7137 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7138 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7139 if (!Imp)
7140 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007141
Balazs Keri3b30d652018-10-19 13:32:20 +00007142 QualType ToType;
7143 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7144 NestedNameSpecifierLoc ToQualifierLoc;
7145 NamedDecl *ToFirstQualifierFoundInScope;
7146 std::tie(
7147 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7148 ToFirstQualifierFoundInScope) = *Imp;
7149
7150 Expr *ToBase = nullptr;
7151 if (!E->isImplicitAccess()) {
7152 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7153 ToBase = *ToBaseOrErr;
7154 else
7155 return ToBaseOrErr.takeError();
7156 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007157
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007158 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007159 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007160 if (Error Err = ImportTemplateArgumentListInfo(
7161 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7162 ToTAInfo))
7163 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007164 ResInfo = &ToTAInfo;
7165 }
7166
Balazs Keri3b30d652018-10-19 13:32:20 +00007167 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7168 if (!ToMemberNameInfoOrErr)
7169 return ToMemberNameInfoOrErr.takeError();
7170 DeclarationNameInfo ToMemberNameInfo(
7171 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007172 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007173 if (Error Err = ImportDeclarationNameLoc(
7174 E->getMemberNameInfo(), ToMemberNameInfo))
7175 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007176
7177 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007178 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7179 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7180 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007181}
7182
Balazs Keri3b30d652018-10-19 13:32:20 +00007183ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007184ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007185 auto Imp = importSeq(
7186 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7187 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7188 if (!Imp)
7189 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007190
Balazs Keri3b30d652018-10-19 13:32:20 +00007191 NestedNameSpecifierLoc ToQualifierLoc;
7192 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7193 DeclarationName ToDeclName;
7194 std::tie(
7195 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7196 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007197
Balazs Keri3b30d652018-10-19 13:32:20 +00007198 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7199 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7200 return std::move(Err);
7201
7202 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007203 TemplateArgumentListInfo *ResInfo = nullptr;
7204 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007205 if (Error Err =
7206 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7207 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007208 ResInfo = &ToTAInfo;
7209 }
7210
7211 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007212 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7213 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007214}
7215
Balazs Keri3b30d652018-10-19 13:32:20 +00007216ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7217 CXXUnresolvedConstructExpr *E) {
7218 auto Imp = importSeq(
7219 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7220 if (!Imp)
7221 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007222
Balazs Keri3b30d652018-10-19 13:32:20 +00007223 SourceLocation ToLParenLoc, ToRParenLoc;
7224 TypeSourceInfo *ToTypeSourceInfo;
7225 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7226
7227 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7228 if (Error Err =
7229 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7230 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007231
7232 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007233 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7234 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007235}
7236
Balazs Keri3b30d652018-10-19 13:32:20 +00007237ExpectedStmt
7238ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7239 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7240 if (!ToNamingClassOrErr)
7241 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007242
Balazs Keri3b30d652018-10-19 13:32:20 +00007243 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7244 if (!ToQualifierLocOrErr)
7245 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007246
Balazs Keri3b30d652018-10-19 13:32:20 +00007247 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7248 if (!ToNameInfoOrErr)
7249 return ToNameInfoOrErr.takeError();
7250 DeclarationNameInfo ToNameInfo(
7251 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007252 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007253 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7254 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007255
7256 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007257 for (auto *D : E->decls())
7258 if (auto ToDOrErr = import(D))
7259 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007260 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007261 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007262
Balazs Keri3b30d652018-10-19 13:32:20 +00007263 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7264 TemplateArgumentListInfo ToTAInfo;
7265 if (Error Err = ImportTemplateArgumentListInfo(
7266 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7267 ToTAInfo))
7268 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007269
Balazs Keri3b30d652018-10-19 13:32:20 +00007270 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7271 if (!ToTemplateKeywordLocOrErr)
7272 return ToTemplateKeywordLocOrErr.takeError();
7273
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007274 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007275 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7276 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7277 ToDecls.begin(), ToDecls.end());
7278 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007279
7280 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007281 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7282 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7283 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007284}
7285
Balazs Keri3b30d652018-10-19 13:32:20 +00007286ExpectedStmt
7287ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7288 auto Imp1 = importSeq(
7289 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7290 E->getTemplateKeywordLoc());
7291 if (!Imp1)
7292 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007293
Balazs Keri3b30d652018-10-19 13:32:20 +00007294 QualType ToType;
7295 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7296 NestedNameSpecifierLoc ToQualifierLoc;
7297 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7298
7299 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7300 if (!Imp2)
7301 return Imp2.takeError();
7302 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7303 // Import additional name location/type info.
7304 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7305 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007306
7307 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007308 for (Decl *D : E->decls())
7309 if (auto ToDOrErr = import(D))
7310 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007311 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007312 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007313
7314 TemplateArgumentListInfo ToTAInfo;
7315 TemplateArgumentListInfo *ResInfo = nullptr;
7316 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007317 if (Error Err =
7318 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7319 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007320 ResInfo = &ToTAInfo;
7321 }
7322
Balazs Keri3b30d652018-10-19 13:32:20 +00007323 Expr *ToBase = nullptr;
7324 if (!E->isImplicitAccess()) {
7325 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7326 ToBase = *ToBaseOrErr;
7327 else
7328 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007329 }
7330
7331 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007332 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7333 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7334 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007335}
7336
Balazs Keri3b30d652018-10-19 13:32:20 +00007337ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7338 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7339 if (!Imp)
7340 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007341
Balazs Keri3b30d652018-10-19 13:32:20 +00007342 Expr *ToCallee;
7343 QualType ToType;
7344 SourceLocation ToRParenLoc;
7345 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007346
7347 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007348 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7349 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7350 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007351
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007352 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
7353 return new (Importer.getToContext()) CXXOperatorCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007354 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
7355 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007356 }
7357
Balazs Keri3b30d652018-10-19 13:32:20 +00007358 return new (Importer.getToContext()) CallExpr(
7359 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7360 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007361}
7362
Balazs Keri3b30d652018-10-19 13:32:20 +00007363ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7364 CXXRecordDecl *FromClass = E->getLambdaClass();
7365 auto ToClassOrErr = import(FromClass);
7366 if (!ToClassOrErr)
7367 return ToClassOrErr.takeError();
7368 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007369
7370 // NOTE: lambda classes are created with BeingDefined flag set up.
7371 // It means that ImportDefinition doesn't work for them and we should fill it
7372 // manually.
7373 if (ToClass->isBeingDefined()) {
7374 for (auto FromField : FromClass->fields()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007375 auto ToFieldOrErr = import(FromField);
7376 if (!ToFieldOrErr)
7377 return ToFieldOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007378 }
7379 }
7380
Balazs Keri3b30d652018-10-19 13:32:20 +00007381 auto ToCallOpOrErr = import(E->getCallOperator());
7382 if (!ToCallOpOrErr)
7383 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007384
7385 ToClass->completeDefinition();
7386
Balazs Keri3b30d652018-10-19 13:32:20 +00007387 SmallVector<LambdaCapture, 8> ToCaptures;
7388 ToCaptures.reserve(E->capture_size());
7389 for (const auto &FromCapture : E->captures()) {
7390 if (auto ToCaptureOrErr = import(FromCapture))
7391 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007392 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007393 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007394 }
7395
Balazs Keri3b30d652018-10-19 13:32:20 +00007396 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7397 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7398 return std::move(Err);
7399
7400 auto Imp = importSeq(
7401 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7402 if (!Imp)
7403 return Imp.takeError();
7404
7405 SourceRange ToIntroducerRange;
7406 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7407 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007408
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007409 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007410 Importer.getToContext(), ToClass, ToIntroducerRange,
7411 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7412 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7413 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007414}
7415
Sean Callanan8bca9962016-03-28 21:43:01 +00007416
Balazs Keri3b30d652018-10-19 13:32:20 +00007417ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7418 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7419 if (!Imp)
7420 return Imp.takeError();
7421
7422 SourceLocation ToLBraceLoc, ToRBraceLoc;
7423 QualType ToType;
7424 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7425
7426 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7427 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7428 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007429
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007430 ASTContext &ToCtx = Importer.getToContext();
7431 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007432 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7433 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007434
Balazs Keri3b30d652018-10-19 13:32:20 +00007435 if (E->hasArrayFiller()) {
7436 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7437 To->setArrayFiller(*ToFillerOrErr);
7438 else
7439 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007440 }
7441
Balazs Keri3b30d652018-10-19 13:32:20 +00007442 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7443 if (auto ToFDOrErr = import(FromFD))
7444 To->setInitializedFieldInUnion(*ToFDOrErr);
7445 else
7446 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007447 }
7448
Balazs Keri3b30d652018-10-19 13:32:20 +00007449 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7450 if (auto ToSyntFormOrErr = import(SyntForm))
7451 To->setSyntacticForm(*ToSyntFormOrErr);
7452 else
7453 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007454 }
7455
Gabor Martona20ce602018-09-03 13:10:53 +00007456 // Copy InitListExprBitfields, which are not handled in the ctor of
7457 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007458 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007459
7460 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007461}
7462
Balazs Keri3b30d652018-10-19 13:32:20 +00007463ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007464 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007465 ExpectedType ToTypeOrErr = import(E->getType());
7466 if (!ToTypeOrErr)
7467 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007468
Balazs Keri3b30d652018-10-19 13:32:20 +00007469 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7470 if (!ToSubExprOrErr)
7471 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007472
Balazs Keri3b30d652018-10-19 13:32:20 +00007473 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7474 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007475}
7476
Balazs Keri3b30d652018-10-19 13:32:20 +00007477ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007478 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007479 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7480 if (!Imp)
7481 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007482
Balazs Keri3b30d652018-10-19 13:32:20 +00007483 SourceLocation ToLocation;
7484 QualType ToType;
7485 CXXConstructorDecl *ToConstructor;
7486 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007487
7488 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007489 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7490 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007491}
7492
Balazs Keri3b30d652018-10-19 13:32:20 +00007493ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7494 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7495 if (!Imp)
7496 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007497
Balazs Keri3b30d652018-10-19 13:32:20 +00007498 QualType ToType;
7499 Expr *ToCommonExpr, *ToSubExpr;
7500 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007501
Balazs Keri3b30d652018-10-19 13:32:20 +00007502 return new (Importer.getToContext()) ArrayInitLoopExpr(
7503 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007504}
7505
Balazs Keri3b30d652018-10-19 13:32:20 +00007506ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7507 ExpectedType ToTypeOrErr = import(E->getType());
7508 if (!ToTypeOrErr)
7509 return ToTypeOrErr.takeError();
7510 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007511}
7512
Balazs Keri3b30d652018-10-19 13:32:20 +00007513ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7514 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7515 if (!ToBeginLocOrErr)
7516 return ToBeginLocOrErr.takeError();
7517
7518 auto ToFieldOrErr = import(E->getField());
7519 if (!ToFieldOrErr)
7520 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007521
7522 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007523 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007524}
7525
Balazs Keri3b30d652018-10-19 13:32:20 +00007526ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7527 auto Imp = importSeq(
7528 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7529 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7530 if (!Imp)
7531 return Imp.takeError();
7532
7533 QualType ToType;
7534 Expr *ToSubExpr;
7535 TypeSourceInfo *ToTypeInfoAsWritten;
7536 SourceLocation ToOperatorLoc, ToRParenLoc;
7537 SourceRange ToAngleBrackets;
7538 std::tie(
7539 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7540 ToAngleBrackets) = *Imp;
7541
Sean Callanandd2c1742016-05-16 20:48:03 +00007542 ExprValueKind VK = E->getValueKind();
7543 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007544 auto ToBasePathOrErr = ImportCastPath(E);
7545 if (!ToBasePathOrErr)
7546 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007547
Sean Callanandd2c1742016-05-16 20:48:03 +00007548 if (isa<CXXStaticCastExpr>(E)) {
7549 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007550 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7551 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007552 } else if (isa<CXXDynamicCastExpr>(E)) {
7553 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007554 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7555 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007556 } else if (isa<CXXReinterpretCastExpr>(E)) {
7557 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007558 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7559 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007560 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007561 return CXXConstCastExpr::Create(
7562 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7563 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007564 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007565 llvm_unreachable("Unknown cast type");
7566 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007567 }
7568}
7569
Balazs Keri3b30d652018-10-19 13:32:20 +00007570ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007571 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007572 auto Imp = importSeq(
7573 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7574 if (!Imp)
7575 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007576
Balazs Keri3b30d652018-10-19 13:32:20 +00007577 QualType ToType;
7578 SourceLocation ToExprLoc;
7579 NonTypeTemplateParmDecl *ToParameter;
7580 Expr *ToReplacement;
7581 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007582
7583 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007584 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007585}
7586
Balazs Keri3b30d652018-10-19 13:32:20 +00007587ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7588 auto Imp = importSeq(
7589 E->getType(), E->getBeginLoc(), E->getEndLoc());
7590 if (!Imp)
7591 return Imp.takeError();
7592
7593 QualType ToType;
7594 SourceLocation ToBeginLoc, ToEndLoc;
7595 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007596
7597 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007598 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7599 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007600
7601 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7602 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007603 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007604
7605 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007606 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7607 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007608}
7609
Balazs Keri3b30d652018-10-19 13:32:20 +00007610ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7611 ExpectedType ToTypeOrErr = import(E->getType());
7612 if (!ToTypeOrErr)
7613 return ToTypeOrErr.takeError();
7614
7615 auto ToSourceRangeOrErr = import(E->getSourceRange());
7616 if (!ToSourceRangeOrErr)
7617 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007618
7619 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007620 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7621 return new (Importer.getToContext()) CXXTypeidExpr(
7622 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7623 else
7624 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007625 }
7626
Balazs Keri3b30d652018-10-19 13:32:20 +00007627 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7628 if (!ToExprOperandOrErr)
7629 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007630
Balazs Keri3b30d652018-10-19 13:32:20 +00007631 return new (Importer.getToContext()) CXXTypeidExpr(
7632 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007633}
7634
Lang Hames19e07e12017-06-20 21:06:00 +00007635void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7636 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007637 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7638 if (auto ImportedOrErr = import(FromOverriddenMethod))
7639 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7640 (*ImportedOrErr)->getCanonicalDecl()));
7641 else
7642 consumeError(ImportedOrErr.takeError());
7643 }
Lang Hames19e07e12017-06-20 21:06:00 +00007644}
7645
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007646ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007647 ASTContext &FromContext, FileManager &FromFileManager,
7648 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007649 : ToContext(ToContext), FromContext(FromContext),
7650 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7651 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007652 ImportedDecls[FromContext.getTranslationUnitDecl()]
7653 = ToContext.getTranslationUnitDecl();
7654}
7655
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007656ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007657
7658QualType ASTImporter::Import(QualType FromT) {
7659 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007660 return {};
John McCall424cec92011-01-19 06:33:43 +00007661
Balazs Keri3b30d652018-10-19 13:32:20 +00007662 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007663
7664 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007665 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007666 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007667 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007668 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007669
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007670 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007671 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007672 ExpectedType ToTOrErr = Importer.Visit(FromTy);
7673 if (!ToTOrErr) {
7674 llvm::consumeError(ToTOrErr.takeError());
7675 return {};
7676 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007677
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007678 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007679 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007680
Balazs Keri3b30d652018-10-19 13:32:20 +00007681 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007682}
7683
Douglas Gregor62d311f2010-02-09 19:21:46 +00007684TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007685 if (!FromTSI)
7686 return FromTSI;
7687
7688 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007689 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007690 QualType T = Import(FromTSI->getType());
7691 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007692 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007693
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007694 return ToContext.getTrivialTypeSourceInfo(
7695 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007696}
7697
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007698Attr *ASTImporter::Import(const Attr *FromAttr) {
7699 Attr *ToAttr = FromAttr->clone(ToContext);
7700 ToAttr->setRange(Import(FromAttr->getRange()));
7701 return ToAttr;
7702}
7703
Sean Callanan59721b32015-04-28 18:41:46 +00007704Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7705 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7706 if (Pos != ImportedDecls.end()) {
7707 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007708 // FIXME: move this call to ImportDeclParts().
Balazs Keri3b30d652018-10-19 13:32:20 +00007709 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD))
7710 llvm::consumeError(std::move(Err));
Sean Callanan59721b32015-04-28 18:41:46 +00007711 return ToD;
7712 } else {
7713 return nullptr;
7714 }
7715}
7716
Douglas Gregor62d311f2010-02-09 19:21:46 +00007717Decl *ASTImporter::Import(Decl *FromD) {
7718 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007719 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007720
Douglas Gregord451ea92011-07-29 23:31:30 +00007721 ASTNodeImporter Importer(*this);
7722
Gabor Marton26f72a92018-07-12 09:42:05 +00007723 // Check whether we've already imported this declaration.
7724 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7725 if (ToD) {
7726 // If FromD has some updated flags after last import, apply it
7727 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007728 return ToD;
7729 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007730
7731 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007732 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
7733 if (!ToDOrErr) {
7734 llvm::consumeError(ToDOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007735 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007736 }
7737 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007738
Gabor Marton26f72a92018-07-12 09:42:05 +00007739 // Notify subclasses.
7740 Imported(FromD, ToD);
7741
Gabor Martonac3a5d62018-09-17 12:04:52 +00007742 updateFlags(FromD, ToD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007743 return ToD;
7744}
7745
Balazs Keri3b30d652018-10-19 13:32:20 +00007746Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007747 if (!FromDC)
7748 return FromDC;
7749
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007750 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007751 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007752 return nullptr;
7753
Fangrui Song6907ce22018-07-30 19:24:48 +00007754 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007755 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007756 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7757 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007758 if (ToRecord->isCompleteDefinition()) {
7759 // Do nothing.
7760 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007761 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7762 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7763 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007764 } else {
7765 CompleteDecl(ToRecord);
7766 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007767 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7768 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007769 if (ToEnum->isCompleteDefinition()) {
7770 // Do nothing.
7771 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007772 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7773 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7774 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007775 } else {
7776 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007777 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007778 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7779 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007780 if (ToClass->getDefinition()) {
7781 // Do nothing.
7782 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007783 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7784 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7785 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007786 } else {
7787 CompleteDecl(ToClass);
7788 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007789 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7790 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007791 if (ToProto->getDefinition()) {
7792 // Do nothing.
7793 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007794 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7795 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7796 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007797 } else {
7798 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007799 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007800 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007801
Douglas Gregor95d82832012-01-24 18:36:04 +00007802 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007803}
7804
7805Expr *ASTImporter::Import(Expr *FromE) {
7806 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007807 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007808
7809 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7810}
7811
7812Stmt *ASTImporter::Import(Stmt *FromS) {
7813 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007814 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007815
Fangrui Song6907ce22018-07-30 19:24:48 +00007816 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007817 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7818 if (Pos != ImportedStmts.end())
7819 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007820
Balazs Keri3b30d652018-10-19 13:32:20 +00007821 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007822 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007823 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
7824 if (!ToSOrErr) {
7825 llvm::consumeError(ToSOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007826 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007827 }
Craig Topper36250ad2014-05-12 05:36:57 +00007828
Balazs Keri3b30d652018-10-19 13:32:20 +00007829 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007830 auto *FromE = cast<Expr>(FromS);
7831 // Copy ExprBitfields, which may not be handled in Expr subclasses
7832 // constructors.
7833 ToE->setValueKind(FromE->getValueKind());
7834 ToE->setObjectKind(FromE->getObjectKind());
7835 ToE->setTypeDependent(FromE->isTypeDependent());
7836 ToE->setValueDependent(FromE->isValueDependent());
7837 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7838 ToE->setContainsUnexpandedParameterPack(
7839 FromE->containsUnexpandedParameterPack());
7840 }
7841
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007842 // Record the imported declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007843 ImportedStmts[FromS] = *ToSOrErr;
7844 return *ToSOrErr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007845}
7846
7847NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7848 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007849 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007850
Douglas Gregor90ebf252011-04-27 16:48:40 +00007851 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7852
7853 switch (FromNNS->getKind()) {
7854 case NestedNameSpecifier::Identifier:
7855 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7856 return NestedNameSpecifier::Create(ToContext, prefix, II);
7857 }
Craig Topper36250ad2014-05-12 05:36:57 +00007858 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007859
7860 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007861 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007862 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007863 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7864 }
Craig Topper36250ad2014-05-12 05:36:57 +00007865 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007866
7867 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007868 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007869 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007870 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7871 }
Craig Topper36250ad2014-05-12 05:36:57 +00007872 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007873
7874 case NestedNameSpecifier::Global:
7875 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7876
Nikola Smiljanic67860242014-09-26 00:28:20 +00007877 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007878 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007879 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007880 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7881 }
7882 return nullptr;
7883
Douglas Gregor90ebf252011-04-27 16:48:40 +00007884 case NestedNameSpecifier::TypeSpec:
7885 case NestedNameSpecifier::TypeSpecWithTemplate: {
7886 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7887 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007888 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007889 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007890 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007891 bTemplate, T.getTypePtr());
7892 }
7893 }
Craig Topper36250ad2014-05-12 05:36:57 +00007894 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007895 }
7896
7897 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007898}
7899
Douglas Gregor14454802011-02-25 02:25:35 +00007900NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007901 // Copied from NestedNameSpecifier mostly.
7902 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7903 NestedNameSpecifierLoc NNS = FromNNS;
7904
7905 // Push each of the nested-name-specifiers's onto a stack for
7906 // serialization in reverse order.
7907 while (NNS) {
7908 NestedNames.push_back(NNS);
7909 NNS = NNS.getPrefix();
7910 }
7911
7912 NestedNameSpecifierLocBuilder Builder;
7913
7914 while (!NestedNames.empty()) {
7915 NNS = NestedNames.pop_back_val();
7916 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7917 if (!Spec)
7918 return NestedNameSpecifierLoc();
7919
7920 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7921 switch (Kind) {
7922 case NestedNameSpecifier::Identifier:
7923 Builder.Extend(getToContext(),
7924 Spec->getAsIdentifier(),
7925 Import(NNS.getLocalBeginLoc()),
7926 Import(NNS.getLocalEndLoc()));
7927 break;
7928
7929 case NestedNameSpecifier::Namespace:
7930 Builder.Extend(getToContext(),
7931 Spec->getAsNamespace(),
7932 Import(NNS.getLocalBeginLoc()),
7933 Import(NNS.getLocalEndLoc()));
7934 break;
7935
7936 case NestedNameSpecifier::NamespaceAlias:
7937 Builder.Extend(getToContext(),
7938 Spec->getAsNamespaceAlias(),
7939 Import(NNS.getLocalBeginLoc()),
7940 Import(NNS.getLocalEndLoc()));
7941 break;
7942
7943 case NestedNameSpecifier::TypeSpec:
7944 case NestedNameSpecifier::TypeSpecWithTemplate: {
7945 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7946 QualType(Spec->getAsType(), 0));
7947 Builder.Extend(getToContext(),
7948 Import(NNS.getLocalBeginLoc()),
7949 TSI->getTypeLoc(),
7950 Import(NNS.getLocalEndLoc()));
7951 break;
7952 }
7953
7954 case NestedNameSpecifier::Global:
7955 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7956 break;
7957
7958 case NestedNameSpecifier::Super: {
7959 SourceRange ToRange = Import(NNS.getSourceRange());
7960 Builder.MakeSuper(getToContext(),
7961 Spec->getAsRecordDecl(),
7962 ToRange.getBegin(),
7963 ToRange.getEnd());
7964 }
7965 }
7966 }
7967
7968 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007969}
7970
Douglas Gregore2e50d332010-12-01 01:36:18 +00007971TemplateName ASTImporter::Import(TemplateName From) {
7972 switch (From.getKind()) {
7973 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007974 if (auto *ToTemplate =
7975 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007976 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007977
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007978 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007979
Douglas Gregore2e50d332010-12-01 01:36:18 +00007980 case TemplateName::OverloadedTemplate: {
7981 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7982 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007983 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007984 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007985 ToTemplates.addDecl(To);
7986 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007987 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007988 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007989 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00007990 ToTemplates.end());
7991 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007992
Douglas Gregore2e50d332010-12-01 01:36:18 +00007993 case TemplateName::QualifiedTemplate: {
7994 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7995 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7996 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007997 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007998
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007999 if (auto *ToTemplate =
8000 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00008001 return ToContext.getQualifiedTemplateName(Qualifier,
8002 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008003 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008004
8005 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008006 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008007
Douglas Gregore2e50d332010-12-01 01:36:18 +00008008 case TemplateName::DependentTemplate: {
8009 DependentTemplateName *DTN = From.getAsDependentTemplateName();
8010 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
8011 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008012 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008013
Douglas Gregore2e50d332010-12-01 01:36:18 +00008014 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008015 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008016 Import(DTN->getIdentifier()));
8017 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008018
Douglas Gregore2e50d332010-12-01 01:36:18 +00008019 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
8020 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008021
8022 case TemplateName::SubstTemplateTemplateParm: {
8023 SubstTemplateTemplateParmStorage *subst
8024 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008025 auto *param =
8026 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00008027 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008028 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00008029
8030 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008031 if (replacement.isNull())
8032 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008033
John McCalld9dfe3a2011-06-30 08:33:18 +00008034 return ToContext.getSubstTemplateTemplateParm(param, replacement);
8035 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008036
Douglas Gregor5590be02011-01-15 06:45:20 +00008037 case TemplateName::SubstTemplateTemplateParmPack: {
8038 SubstTemplateTemplateParmPackStorage *SubstPack
8039 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008040 auto *Param =
8041 cast_or_null<TemplateTemplateParmDecl>(
8042 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00008043 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008044 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008045
Douglas Gregor5590be02011-01-15 06:45:20 +00008046 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008047 Expected<TemplateArgument> ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00008048 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
Balazs Keri3b30d652018-10-19 13:32:20 +00008049 if (!ArgPack) {
8050 llvm::consumeError(ArgPack.takeError());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008051 return {};
Balazs Keri3b30d652018-10-19 13:32:20 +00008052 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008053
Balazs Keri3b30d652018-10-19 13:32:20 +00008054 return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00008055 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008056 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008057
Douglas Gregore2e50d332010-12-01 01:36:18 +00008058 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008059}
8060
Douglas Gregor62d311f2010-02-09 19:21:46 +00008061SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
8062 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008063 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008064
Douglas Gregor811663e2010-02-10 00:15:17 +00008065 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008066
Douglas Gregor811663e2010-02-10 00:15:17 +00008067 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00008068 FileID ToFileID = Import(Decomposed.first);
8069 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008070 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008071 SourceManager &ToSM = ToContext.getSourceManager();
8072 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00008073}
8074
8075SourceRange ASTImporter::Import(SourceRange FromRange) {
8076 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
8077}
8078
Douglas Gregor811663e2010-02-10 00:15:17 +00008079FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008080 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008081 if (Pos != ImportedFileIDs.end())
8082 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008083
Douglas Gregor811663e2010-02-10 00:15:17 +00008084 SourceManager &FromSM = FromContext.getSourceManager();
8085 SourceManager &ToSM = ToContext.getSourceManager();
8086 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008087
8088 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008089 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008090 if (FromSLoc.isExpansion()) {
8091 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
8092 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
8093 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
8094 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8095 SourceLocation MLoc;
8096 if (FromEx.isMacroArgExpansion()) {
8097 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
8098 } else {
8099 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
8100 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
8101 FromEx.isExpansionTokenRange());
8102 }
8103 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008104 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008105 // Include location of this file.
8106 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
8107
8108 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
8109 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8110 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8111 // disk again
8112 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8113 // than mmap the files several times.
8114 const FileEntry *Entry =
8115 ToFileManager.getFile(Cache->OrigEntry->getName());
8116 if (!Entry)
8117 return {};
8118 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
8119 FromSLoc.getFile().getFileCharacteristic());
8120 } else {
8121 // FIXME: We want to re-use the existing MemoryBuffer!
8122 const llvm::MemoryBuffer *FromBuf =
8123 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
8124 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8125 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8126 FromBuf->getBufferIdentifier());
8127 ToID = ToSM.createFileID(std::move(ToBuf),
8128 FromSLoc.getFile().getFileCharacteristic());
8129 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008130 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008131
Sebastian Redl99219f12010-09-30 01:03:06 +00008132 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008133 return ToID;
8134}
8135
Sean Callanandd2c1742016-05-16 20:48:03 +00008136CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8137 Expr *ToExpr = Import(From->getInit());
8138 if (!ToExpr && From->getInit())
8139 return nullptr;
8140
8141 if (From->isBaseInitializer()) {
8142 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8143 if (!ToTInfo && From->getTypeSourceInfo())
8144 return nullptr;
8145
8146 return new (ToContext) CXXCtorInitializer(
8147 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
8148 ToExpr, Import(From->getRParenLoc()),
8149 From->isPackExpansion() ? Import(From->getEllipsisLoc())
8150 : SourceLocation());
8151 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008152 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008153 if (!ToField && From->getMember())
8154 return nullptr;
8155
8156 return new (ToContext) CXXCtorInitializer(
8157 ToContext, ToField, Import(From->getMemberLocation()),
8158 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8159 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008160 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00008161 Import(From->getIndirectMember()));
8162 if (!ToIField && From->getIndirectMember())
8163 return nullptr;
8164
8165 return new (ToContext) CXXCtorInitializer(
8166 ToContext, ToIField, Import(From->getMemberLocation()),
8167 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8168 } else if (From->isDelegatingInitializer()) {
8169 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8170 if (!ToTInfo && From->getTypeSourceInfo())
8171 return nullptr;
8172
8173 return new (ToContext)
8174 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
8175 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008176 } else {
8177 return nullptr;
8178 }
8179}
8180
Aleksei Sidorina693b372016-09-28 10:16:56 +00008181CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
8182 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8183 if (Pos != ImportedCXXBaseSpecifiers.end())
8184 return Pos->second;
8185
8186 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
8187 Import(BaseSpec->getSourceRange()),
8188 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8189 BaseSpec->getAccessSpecifierAsWritten(),
8190 Import(BaseSpec->getTypeSourceInfo()),
8191 Import(BaseSpec->getEllipsisLoc()));
8192 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8193 return Imported;
8194}
8195
Balazs Keri3b30d652018-10-19 13:32:20 +00008196Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008197 Decl *To = Import(From);
8198 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008199 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008200
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008201 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008202 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008203
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008204 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00008205 if (!ToRecord->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008206 return Importer.ImportDefinition(
8207 cast<RecordDecl>(FromDC), ToRecord,
8208 ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008209 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00008210 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008211
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008212 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00008213 if (!ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008214 return Importer.ImportDefinition(
8215 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008216 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008217 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008218
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008219 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008220 if (!ToIFace->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008221 return Importer.ImportDefinition(
8222 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8223 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008224 }
8225 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008226
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008227 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008228 if (!ToProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008229 return Importer.ImportDefinition(
8230 cast<ObjCProtocolDecl>(FromDC), ToProto,
8231 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008232 }
8233 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008234
Balazs Keri3b30d652018-10-19 13:32:20 +00008235 return Importer.ImportDeclContext(FromDC, true);
Douglas Gregor0a791672011-01-18 03:11:38 +00008236 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008237
8238 return Error::success();
8239}
8240
8241void ASTImporter::ImportDefinition(Decl *From) {
8242 Error Err = ImportDefinition_New(From);
8243 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008244}
8245
Douglas Gregor96e578d2010-02-05 17:54:41 +00008246DeclarationName ASTImporter::Import(DeclarationName FromName) {
8247 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008248 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008249
8250 switch (FromName.getNameKind()) {
8251 case DeclarationName::Identifier:
8252 return Import(FromName.getAsIdentifierInfo());
8253
8254 case DeclarationName::ObjCZeroArgSelector:
8255 case DeclarationName::ObjCOneArgSelector:
8256 case DeclarationName::ObjCMultiArgSelector:
8257 return Import(FromName.getObjCSelector());
8258
8259 case DeclarationName::CXXConstructorName: {
8260 QualType T = Import(FromName.getCXXNameType());
8261 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008262 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008263
8264 return ToContext.DeclarationNames.getCXXConstructorName(
8265 ToContext.getCanonicalType(T));
8266 }
8267
8268 case DeclarationName::CXXDestructorName: {
8269 QualType T = Import(FromName.getCXXNameType());
8270 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008271 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008272
8273 return ToContext.DeclarationNames.getCXXDestructorName(
8274 ToContext.getCanonicalType(T));
8275 }
8276
Richard Smith35845152017-02-07 01:37:30 +00008277 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008278 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00008279 Import(FromName.getCXXDeductionGuideTemplate()));
8280 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008281 return {};
Richard Smith35845152017-02-07 01:37:30 +00008282 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
8283 }
8284
Douglas Gregor96e578d2010-02-05 17:54:41 +00008285 case DeclarationName::CXXConversionFunctionName: {
8286 QualType T = Import(FromName.getCXXNameType());
8287 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008288 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008289
8290 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8291 ToContext.getCanonicalType(T));
8292 }
8293
8294 case DeclarationName::CXXOperatorName:
8295 return ToContext.DeclarationNames.getCXXOperatorName(
8296 FromName.getCXXOverloadedOperator());
8297
8298 case DeclarationName::CXXLiteralOperatorName:
8299 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
8300 Import(FromName.getCXXLiteralIdentifier()));
8301
8302 case DeclarationName::CXXUsingDirective:
8303 // FIXME: STATICS!
8304 return DeclarationName::getUsingDirectiveName();
8305 }
8306
David Blaikiee4d798f2012-01-20 21:50:17 +00008307 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008308}
8309
Douglas Gregore2e50d332010-12-01 01:36:18 +00008310IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008311 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008312 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008313
Sean Callananf94ef1d2016-05-14 06:11:19 +00008314 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8315
8316 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8317 ToId->setBuiltinID(FromId->getBuiltinID());
8318
8319 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008320}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008321
Douglas Gregor43f54792010-02-17 02:12:47 +00008322Selector ASTImporter::Import(Selector FromSel) {
8323 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008324 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00008325
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008326 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008327 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8328 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8329 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8330 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8331}
8332
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008333DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8334 DeclContext *DC,
8335 unsigned IDNS,
8336 NamedDecl **Decls,
8337 unsigned NumDecls) {
8338 return Name;
8339}
8340
8341DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008342 if (LastDiagFromFrom)
8343 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8344 FromContext.getDiagnostics());
8345 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008346 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008347}
8348
8349DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008350 if (!LastDiagFromFrom)
8351 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8352 ToContext.getDiagnostics());
8353 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008354 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008355}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008356
Douglas Gregor2e15c842012-02-01 21:00:38 +00008357void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008358 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008359 if (!ID->getDefinition())
8360 ID->startDefinition();
8361 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008362 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008363 if (!PD->getDefinition())
8364 PD->startDefinition();
8365 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008366 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008367 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8368 TD->startDefinition();
8369 TD->setCompleteDefinition(true);
8370 }
8371 }
8372 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008373 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008374 }
8375}
8376
Gabor Marton26f72a92018-07-12 09:42:05 +00008377Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8378 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8379 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8380 "Try to import an already imported Decl");
8381 if (Pos != ImportedDecls.end())
8382 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008383 ImportedDecls[From] = To;
8384 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008385}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008386
Douglas Gregordd6006f2012-07-17 21:16:27 +00008387bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8388 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00008389 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00008390 = ImportedTypes.find(From.getTypePtr());
8391 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8392 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008393
Douglas Gregordd6006f2012-07-17 21:16:27 +00008394 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008395 getStructuralEquivalenceKind(*this), false,
8396 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008397 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008398}