blob: 77a1a7f4990aa6833ad6c03f7314e750cb2bfc8e [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000014
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Balazs Keri3b30d652018-10-19 13:32:20 +000074 using llvm::make_error;
75 using llvm::Error;
76 using llvm::Expected;
77 using ExpectedType = llvm::Expected<QualType>;
78 using ExpectedStmt = llvm::Expected<Stmt *>;
79 using ExpectedExpr = llvm::Expected<Expr *>;
80 using ExpectedDecl = llvm::Expected<Decl *>;
81 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000082
Balazs Keri3b30d652018-10-19 13:32:20 +000083 std::string ImportError::toString() const {
84 // FIXME: Improve error texts.
85 switch (Error) {
86 case NameConflict:
87 return "NameConflict";
88 case UnsupportedConstruct:
89 return "UnsupportedConstruct";
90 case Unknown:
91 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000092 }
Balazs Keri2a13d662018-10-19 15:16:51 +000093 llvm_unreachable("Invalid error code.");
94 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
125 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000126 }
127
Gabor Marton26f72a92018-07-12 09:42:05 +0000128 void updateFlags(const Decl *From, Decl *To) {
129 // Check if some flags or attrs are new in 'From' and copy into 'To'.
130 // FIXME: Other flags or attrs?
131 if (From->isUsed(false) && !To->isUsed(false))
132 To->setIsUsed();
133 }
134
Balazs Keri3b30d652018-10-19 13:32:20 +0000135 Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
136 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
137 "Try to get field index for non-field.");
138
139 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
140 if (!Owner)
141 return None;
142
143 unsigned Index = 0;
144 for (const auto *D : Owner->decls()) {
145 if (D == F)
146 return Index;
147
148 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
149 ++Index;
150 }
151
152 llvm_unreachable("Field was not found in its parent context.");
153
154 return None;
155 }
156
157 // FIXME: Temporary until every import returns Expected.
158 template <>
159 LLVM_NODISCARD Error
160 ASTImporter::importInto(SourceLocation &To, const SourceLocation &From) {
161 To = Import(From);
162 if (From.isValid() && To.isInvalid())
163 return llvm::make_error<ImportError>();
164 return Error::success();
165 }
166 // FIXME: Temporary until every import returns Expected.
167 template <>
168 LLVM_NODISCARD Error
169 ASTImporter::importInto(QualType &To, const QualType &From) {
170 To = Import(From);
171 if (!From.isNull() && To.isNull())
172 return llvm::make_error<ImportError>();
173 return Error::success();
174 }
175
176 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
177 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
178 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000180
Balazs Keri3b30d652018-10-19 13:32:20 +0000181 // Use this instead of Importer.importInto .
182 template <typename ImportT>
183 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
184 return Importer.importInto(To, From);
185 }
186
187 // Use this to import pointers of specific type.
188 template <typename ImportT>
189 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
190 auto ToI = Importer.Import(From);
191 if (!ToI && From)
192 return make_error<ImportError>();
193 To = cast_or_null<ImportT>(ToI);
194 return Error::success();
195 // FIXME: This should be the final code.
196 //auto ToOrErr = Importer.Import(From);
197 //if (ToOrErr) {
198 // To = cast_or_null<ImportT>(*ToOrErr);
199 //}
200 //return ToOrErr.takeError();
201 }
202
203 // Call the import function of ASTImporter for a baseclass of type `T` and
204 // cast the return value to `T`.
205 template <typename T>
206 Expected<T *> import(T *From) {
207 auto *To = Importer.Import(From);
208 if (!To && From)
209 return make_error<ImportError>();
210 return cast_or_null<T>(To);
211 // FIXME: This should be the final code.
212 //auto ToOrErr = Importer.Import(From);
213 //if (!ToOrErr)
214 // return ToOrErr.takeError();
215 //return cast_or_null<T>(*ToOrErr);
216 }
217
218 template <typename T>
219 Expected<T *> import(const T *From) {
220 return import(const_cast<T *>(From));
221 }
222
223 // Call the import function of ASTImporter for type `T`.
224 template <typename T>
225 Expected<T> import(const T &From) {
226 T To = Importer.Import(From);
227 T DefaultT;
228 if (To == DefaultT && !(From == DefaultT))
229 return make_error<ImportError>();
230 return To;
231 // FIXME: This should be the final code.
232 //return Importer.Import(From);
233 }
234
235 template <class T>
236 Expected<std::tuple<T>>
237 importSeq(const T &From) {
238 Expected<T> ToOrErr = import(From);
239 if (!ToOrErr)
240 return ToOrErr.takeError();
241 return std::make_tuple<T>(std::move(*ToOrErr));
242 }
243
244 // Import multiple objects with a single function call.
245 // This should work for every type for which a variant of `import` exists.
246 // The arguments are processed from left to right and import is stopped on
247 // first error.
248 template <class THead, class... TTail>
249 Expected<std::tuple<THead, TTail...>>
250 importSeq(const THead &FromHead, const TTail &...FromTail) {
251 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
252 if (!ToHeadOrErr)
253 return ToHeadOrErr.takeError();
254 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
255 if (!ToTailOrErr)
256 return ToTailOrErr.takeError();
257 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
258 }
259
260// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 template <typename ToDeclT> struct CallOverloadedCreateFun {
262 template <typename... Args>
263 auto operator()(Args &&... args)
264 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
265 return ToDeclT::Create(std::forward<Args>(args)...);
266 }
267 };
268
269 // Always use these functions to create a Decl during import. There are
270 // certain tasks which must be done after the Decl was created, e.g. we
271 // must immediately register that as an imported Decl. The parameter `ToD`
272 // will be set to the newly created Decl or if had been imported before
273 // then to the already imported Decl. Returns a bool value set to true if
274 // the `FromD` had been imported before.
275 template <typename ToDeclT, typename FromDeclT, typename... Args>
276 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
277 Args &&... args) {
278 // There may be several overloads of ToDeclT::Create. We must make sure
279 // to call the one which would be chosen by the arguments, thus we use a
280 // wrapper for the overload set.
281 CallOverloadedCreateFun<ToDeclT> OC;
282 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
283 std::forward<Args>(args)...);
284 }
285 // Use this overload if a special Type is needed to be created. E.g if we
286 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
287 // then:
288 // TypedefNameDecl *ToTypedef;
289 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
290 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
291 typename... Args>
292 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
293 Args &&... args) {
294 CallOverloadedCreateFun<NewDeclT> OC;
295 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
296 std::forward<Args>(args)...);
297 }
298 // Use this version if a special create function must be
299 // used, e.g. CXXRecordDecl::CreateLambda .
300 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
301 typename... Args>
302 LLVM_NODISCARD bool
303 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
304 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000305 // FIXME: This code is needed later.
306 //if (Importer.getImportDeclErrorIfAny(FromD)) {
307 // ToD = nullptr;
308 // return true; // Already imported but with error.
309 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000310 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
311 if (ToD)
312 return true; // Already imported.
313 ToD = CreateFun(std::forward<Args>(args)...);
314 InitializeImportedDecl(FromD, ToD);
315 return false; // A new Decl is created.
316 }
317
318 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
319 Importer.MapImported(FromD, ToD);
320 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
321 if (FromD->hasAttrs())
322 for (const Attr *FromAttr : FromD->getAttrs())
323 ToD->addAttr(Importer.Import(FromAttr));
324 if (FromD->isUsed())
325 ToD->setIsUsed();
326 if (FromD->isImplicit())
327 ToD->setImplicit();
328 }
329
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000331 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000332
Balazs Keri3b30d652018-10-19 13:32:20 +0000333 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
334 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
335 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336
337 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000338 ExpectedType VisitType(const Type *T);
339 ExpectedType VisitAtomicType(const AtomicType *T);
340 ExpectedType VisitBuiltinType(const BuiltinType *T);
341 ExpectedType VisitDecayedType(const DecayedType *T);
342 ExpectedType VisitComplexType(const ComplexType *T);
343 ExpectedType VisitPointerType(const PointerType *T);
344 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
345 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
346 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
347 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
348 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
349 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
350 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
351 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000352 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000353 ExpectedType VisitVectorType(const VectorType *T);
354 ExpectedType VisitExtVectorType(const ExtVectorType *T);
355 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
356 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
357 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
358 ExpectedType VisitParenType(const ParenType *T);
359 ExpectedType VisitTypedefType(const TypedefType *T);
360 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000361 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000362 ExpectedType VisitTypeOfType(const TypeOfType *T);
363 ExpectedType VisitDecltypeType(const DecltypeType *T);
364 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
365 ExpectedType VisitAutoType(const AutoType *T);
366 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000368 ExpectedType VisitRecordType(const RecordType *T);
369 ExpectedType VisitEnumType(const EnumType *T);
370 ExpectedType VisitAttributedType(const AttributedType *T);
371 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
372 ExpectedType VisitSubstTemplateTypeParmType(
373 const SubstTemplateTypeParmType *T);
374 ExpectedType VisitTemplateSpecializationType(
375 const TemplateSpecializationType *T);
376 ExpectedType VisitElaboratedType(const ElaboratedType *T);
377 ExpectedType VisitDependentNameType(const DependentNameType *T);
378 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
379 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000380 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
382 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
383 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000384
385 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000386 Error ImportDeclParts(
387 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
388 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
389 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
390 Error ImportDeclarationNameLoc(
391 const DeclarationNameInfo &From, DeclarationNameInfo &To);
392 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
393 Error ImportDeclContext(
394 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
395 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000396
Balazs Keri3b30d652018-10-19 13:32:20 +0000397 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000398
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000399 using Designator = DesignatedInitExpr::Designator;
400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000401 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000402 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000403 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000404 /// nothing (if minimal import is set) or might be everything (if minimal
405 /// import is not set).
406 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000407 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000408 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000410 /// DeclContext.
411 IDK_Basic
412 };
413
Douglas Gregor2e15c842012-02-01 21:00:38 +0000414 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
415 return IDK == IDK_Everything ||
416 (IDK == IDK_Default && !Importer.isMinimalImport());
417 }
418
Balazs Keri3b30d652018-10-19 13:32:20 +0000419 Error ImportInitializer(VarDecl *From, VarDecl *To);
420 Error ImportDefinition(
421 RecordDecl *From, RecordDecl *To,
422 ImportDefinitionKind Kind = IDK_Default);
423 Error ImportDefinition(
424 EnumDecl *From, EnumDecl *To,
425 ImportDefinitionKind Kind = IDK_Default);
426 Error ImportDefinition(
427 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
428 ImportDefinitionKind Kind = IDK_Default);
429 Error ImportDefinition(
430 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
431 ImportDefinitionKind Kind = IDK_Default);
432 Expected<TemplateParameterList *> ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000433 TemplateParameterList *Params);
Balazs Keri3b30d652018-10-19 13:32:20 +0000434 Error ImportTemplateArguments(
435 const TemplateArgument *FromArgs, unsigned NumFromArgs,
436 SmallVectorImpl<TemplateArgument> &ToArgs);
437 Expected<TemplateArgument>
438 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000439
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000440 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000441 Error ImportTemplateArgumentListInfo(
442 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000443
444 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000445 Error ImportTemplateArgumentListInfo(
446 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
447 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000448
Gabor Marton5254e642018-06-27 13:32:50 +0000449 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000450 using FunctionTemplateAndArgsTy =
451 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
452 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000453 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
454 FunctionDecl *FromFD);
455
Balazs Keri3b30d652018-10-19 13:32:20 +0000456 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000457
Gabor Marton950fb572018-07-17 12:39:27 +0000458 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000459 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
460 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000461 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
462 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000463 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000464 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000465 bool IsStructuralMatch(FunctionTemplateDecl *From,
466 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000467 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000468 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000469 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000470 ExpectedDecl VisitDecl(Decl *D);
471 ExpectedDecl VisitImportDecl(ImportDecl *D);
472 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
473 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
474 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
475 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
476 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
477 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
479 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
480 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
481 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
482 ExpectedDecl VisitLabelDecl(LabelDecl *D);
483 ExpectedDecl VisitEnumDecl(EnumDecl *D);
484 ExpectedDecl VisitRecordDecl(RecordDecl *D);
485 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
486 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
487 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
488 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
489 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
490 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
491 ExpectedDecl VisitFieldDecl(FieldDecl *D);
492 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
493 ExpectedDecl VisitFriendDecl(FriendDecl *D);
494 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
495 ExpectedDecl VisitVarDecl(VarDecl *D);
496 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
497 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
498 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
499 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
500 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
501 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
502 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
503 ExpectedDecl VisitUsingDecl(UsingDecl *D);
504 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
505 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
506 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
507 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000508
Balazs Keri3b30d652018-10-19 13:32:20 +0000509 Expected<ObjCTypeParamList *>
510 ImportObjCTypeParamList(ObjCTypeParamList *list);
511
512 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
513 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
514 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
515 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
516 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
517 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
518 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
519 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
520 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
521 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000522 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000523 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
524 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
525 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000526
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000527 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000528 ExpectedStmt VisitStmt(Stmt *S);
529 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
530 ExpectedStmt VisitDeclStmt(DeclStmt *S);
531 ExpectedStmt VisitNullStmt(NullStmt *S);
532 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
533 ExpectedStmt VisitCaseStmt(CaseStmt *S);
534 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
535 ExpectedStmt VisitLabelStmt(LabelStmt *S);
536 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
537 ExpectedStmt VisitIfStmt(IfStmt *S);
538 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
539 ExpectedStmt VisitWhileStmt(WhileStmt *S);
540 ExpectedStmt VisitDoStmt(DoStmt *S);
541 ExpectedStmt VisitForStmt(ForStmt *S);
542 ExpectedStmt VisitGotoStmt(GotoStmt *S);
543 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
544 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
545 ExpectedStmt VisitBreakStmt(BreakStmt *S);
546 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000547 // FIXME: MSAsmStmt
548 // FIXME: SEHExceptStmt
549 // FIXME: SEHFinallyStmt
550 // FIXME: SEHTryStmt
551 // FIXME: SEHLeaveStmt
552 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000553 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
554 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
555 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000556 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000557 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
558 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
559 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
560 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
561 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
562 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
563 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000564
565 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000566 ExpectedStmt VisitExpr(Expr *E);
567 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
568 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
569 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
570 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
571 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
572 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
573 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
574 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
575 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
576 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
577 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
578 ExpectedStmt VisitStringLiteral(StringLiteral *E);
579 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
580 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
581 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000582 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000583 ExpectedStmt VisitParenExpr(ParenExpr *E);
584 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
585 ExpectedStmt VisitStmtExpr(StmtExpr *E);
586 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
587 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
588 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
589 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
590 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
591 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
592 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
593 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
594 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
595 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
596 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
597 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
598 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
599 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
600 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
601 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
602 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
603 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
604 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
605 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
606 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
607 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
608 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
609 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
610 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
611 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
612 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
613 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
614 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
615 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
616 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
617 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
618 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
619 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
620 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
621 ExpectedStmt VisitMemberExpr(MemberExpr *E);
622 ExpectedStmt VisitCallExpr(CallExpr *E);
623 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
624 ExpectedStmt VisitInitListExpr(InitListExpr *E);
625 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
626 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
627 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
628 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
629 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
630 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
631 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
632 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
633 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000634
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000635 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000636 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000637 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000638 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
639 Expected<ItemT> ToOrErr = import(*Ibegin);
640 if (!ToOrErr)
641 return ToOrErr.takeError();
642 *Obegin = *ToOrErr;
643 }
644 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000645 }
646
Balazs Keri3b30d652018-10-19 13:32:20 +0000647 // Import every item from a container structure into an output container.
648 // If error occurs, stops at first error and returns the error.
649 // The output container should have space for all needed elements (it is not
650 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000651 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000652 Error ImportContainerChecked(
653 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
654 return ImportArrayChecked(
655 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000656 }
657
658 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000659 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000660 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
661 }
Lang Hames19e07e12017-06-20 21:06:00 +0000662
Lang Hames19e07e12017-06-20 21:06:00 +0000663 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000664
Balazs Keri3b30d652018-10-19 13:32:20 +0000665 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
666 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000668
Balazs Keri3b30d652018-10-19 13:32:20 +0000669// FIXME: Temporary until every import returns Expected.
670template <>
671Expected<TemplateName> ASTNodeImporter::import(const TemplateName &From) {
672 TemplateName To = Importer.Import(From);
673 if (To.isNull() && !From.isNull())
674 return make_error<ImportError>();
675 return To;
676}
677
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000678template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000679Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000680 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
681 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000682 auto ToLAngleLocOrErr = import(FromLAngleLoc);
683 if (!ToLAngleLocOrErr)
684 return ToLAngleLocOrErr.takeError();
685 auto ToRAngleLocOrErr = import(FromRAngleLoc);
686 if (!ToRAngleLocOrErr)
687 return ToRAngleLocOrErr.takeError();
688
689 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
690 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
691 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000692 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000693 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000694}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000695
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000696template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000697Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000698 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
699 return ImportTemplateArgumentListInfo(
700 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
701}
702
703template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000704Error ASTNodeImporter::ImportTemplateArgumentListInfo<
705 ASTTemplateArgumentListInfo>(
706 const ASTTemplateArgumentListInfo &From,
707 TemplateArgumentListInfo &Result) {
708 return ImportTemplateArgumentListInfo(
709 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000710}
711
Balazs Keri3b30d652018-10-19 13:32:20 +0000712Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000713ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
714 FunctionDecl *FromFD) {
715 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000716 FunctionDecl::TK_FunctionTemplateSpecialization);
717
718 FunctionTemplateAndArgsTy Result;
719
Gabor Marton5254e642018-06-27 13:32:50 +0000720 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000721 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
722 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000723
724 // Import template arguments.
725 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000726 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
727 std::get<1>(Result)))
728 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000729
Balazs Keri3b30d652018-10-19 13:32:20 +0000730 return Result;
731}
732
733template <>
734Expected<TemplateParameterList *>
735ASTNodeImporter::import(TemplateParameterList *From) {
736 SmallVector<NamedDecl *, 4> To(From->size());
737 if (Error Err = ImportContainerChecked(*From, To))
738 return std::move(Err);
739
740 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
741 if (!ToRequiresClause)
742 return ToRequiresClause.takeError();
743
744 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
745 if (!ToTemplateLocOrErr)
746 return ToTemplateLocOrErr.takeError();
747 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
748 if (!ToLAngleLocOrErr)
749 return ToLAngleLocOrErr.takeError();
750 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
751 if (!ToRAngleLocOrErr)
752 return ToRAngleLocOrErr.takeError();
753
754 return TemplateParameterList::Create(
755 Importer.getToContext(),
756 *ToTemplateLocOrErr,
757 *ToLAngleLocOrErr,
758 To,
759 *ToRAngleLocOrErr,
760 *ToRequiresClause);
761}
762
763template <>
764Expected<TemplateArgument>
765ASTNodeImporter::import(const TemplateArgument &From) {
766 switch (From.getKind()) {
767 case TemplateArgument::Null:
768 return TemplateArgument();
769
770 case TemplateArgument::Type: {
771 ExpectedType ToTypeOrErr = import(From.getAsType());
772 if (!ToTypeOrErr)
773 return ToTypeOrErr.takeError();
774 return TemplateArgument(*ToTypeOrErr);
775 }
776
777 case TemplateArgument::Integral: {
778 ExpectedType ToTypeOrErr = import(From.getIntegralType());
779 if (!ToTypeOrErr)
780 return ToTypeOrErr.takeError();
781 return TemplateArgument(From, *ToTypeOrErr);
782 }
783
784 case TemplateArgument::Declaration: {
785 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
786 if (!ToOrErr)
787 return ToOrErr.takeError();
788 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
789 if (!ToTypeOrErr)
790 return ToTypeOrErr.takeError();
791 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
792 }
793
794 case TemplateArgument::NullPtr: {
795 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
796 if (!ToTypeOrErr)
797 return ToTypeOrErr.takeError();
798 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
799 }
800
801 case TemplateArgument::Template: {
802 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
803 if (!ToTemplateOrErr)
804 return ToTemplateOrErr.takeError();
805
806 return TemplateArgument(*ToTemplateOrErr);
807 }
808
809 case TemplateArgument::TemplateExpansion: {
810 Expected<TemplateName> ToTemplateOrErr =
811 import(From.getAsTemplateOrTemplatePattern());
812 if (!ToTemplateOrErr)
813 return ToTemplateOrErr.takeError();
814
815 return TemplateArgument(
816 *ToTemplateOrErr, From.getNumTemplateExpansions());
817 }
818
819 case TemplateArgument::Expression:
820 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
821 return TemplateArgument(*ToExpr);
822 else
823 return ToExpr.takeError();
824
825 case TemplateArgument::Pack: {
826 SmallVector<TemplateArgument, 2> ToPack;
827 ToPack.reserve(From.pack_size());
828 if (Error Err = ImportTemplateArguments(
829 From.pack_begin(), From.pack_size(), ToPack))
830 return std::move(Err);
831
832 return TemplateArgument(
833 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
834 }
835 }
836
837 llvm_unreachable("Invalid template argument kind");
838}
839
840template <>
841Expected<TemplateArgumentLoc>
842ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
843 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
844 if (!ArgOrErr)
845 return ArgOrErr.takeError();
846 TemplateArgument Arg = *ArgOrErr;
847
848 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
849
850 TemplateArgumentLocInfo ToInfo;
851 if (Arg.getKind() == TemplateArgument::Expression) {
852 ExpectedExpr E = import(FromInfo.getAsExpr());
853 if (!E)
854 return E.takeError();
855 ToInfo = TemplateArgumentLocInfo(*E);
856 } else if (Arg.getKind() == TemplateArgument::Type) {
857 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
858 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
859 else
860 return TSIOrErr.takeError();
861 } else {
862 auto ToTemplateQualifierLocOrErr =
863 import(FromInfo.getTemplateQualifierLoc());
864 if (!ToTemplateQualifierLocOrErr)
865 return ToTemplateQualifierLocOrErr.takeError();
866 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
867 if (!ToTemplateNameLocOrErr)
868 return ToTemplateNameLocOrErr.takeError();
869 auto ToTemplateEllipsisLocOrErr =
870 import(FromInfo.getTemplateEllipsisLoc());
871 if (!ToTemplateEllipsisLocOrErr)
872 return ToTemplateEllipsisLocOrErr.takeError();
873
874 ToInfo = TemplateArgumentLocInfo(
875 *ToTemplateQualifierLocOrErr,
876 *ToTemplateNameLocOrErr,
877 *ToTemplateEllipsisLocOrErr);
878 }
879
880 return TemplateArgumentLoc(Arg, ToInfo);
881}
882
883template <>
884Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
885 if (DG.isNull())
886 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
887 size_t NumDecls = DG.end() - DG.begin();
888 SmallVector<Decl *, 1> ToDecls;
889 ToDecls.reserve(NumDecls);
890 for (Decl *FromD : DG) {
891 if (auto ToDOrErr = import(FromD))
892 ToDecls.push_back(*ToDOrErr);
893 else
894 return ToDOrErr.takeError();
895 }
896 return DeclGroupRef::Create(Importer.getToContext(),
897 ToDecls.begin(),
898 NumDecls);
899}
900
901template <>
902Expected<ASTNodeImporter::Designator>
903ASTNodeImporter::import(const Designator &D) {
904 if (D.isFieldDesignator()) {
905 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
906
907 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
908 if (!ToDotLocOrErr)
909 return ToDotLocOrErr.takeError();
910
911 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
912 if (!ToFieldLocOrErr)
913 return ToFieldLocOrErr.takeError();
914
915 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
916 }
917
918 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
919 if (!ToLBracketLocOrErr)
920 return ToLBracketLocOrErr.takeError();
921
922 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
923 if (!ToRBracketLocOrErr)
924 return ToRBracketLocOrErr.takeError();
925
926 if (D.isArrayDesignator())
927 return Designator(D.getFirstExprIndex(),
928 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
929
930 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
931 if (!ToEllipsisLocOrErr)
932 return ToEllipsisLocOrErr.takeError();
933
934 assert(D.isArrayRangeDesignator());
935 return Designator(
936 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
937 *ToRBracketLocOrErr);
938}
939
940template <>
941Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
942 VarDecl *Var = nullptr;
943 if (From.capturesVariable()) {
944 if (auto VarOrErr = import(From.getCapturedVar()))
945 Var = *VarOrErr;
946 else
947 return VarOrErr.takeError();
948 }
949
950 auto LocationOrErr = import(From.getLocation());
951 if (!LocationOrErr)
952 return LocationOrErr.takeError();
953
954 SourceLocation EllipsisLoc;
955 if (From.isPackExpansion())
956 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
957 return std::move(Err);
958
959 return LambdaCapture(
960 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
961 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000962}
963
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000964} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000965
Douglas Gregor3996e242010-02-15 22:01:00 +0000966//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000967// Import Types
968//----------------------------------------------------------------------------
969
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000970using namespace clang;
971
Balazs Keri3b30d652018-10-19 13:32:20 +0000972ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000973 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
974 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000975 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000976}
977
Balazs Keri3b30d652018-10-19 13:32:20 +0000978ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
979 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
980 if (!UnderlyingTypeOrErr)
981 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000982
Balazs Keri3b30d652018-10-19 13:32:20 +0000983 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000984}
985
Balazs Keri3b30d652018-10-19 13:32:20 +0000986ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000988#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
989 case BuiltinType::Id: \
990 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000991#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000992#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
993 case BuiltinType::Id: \
994 return Importer.getToContext().Id##Ty;
995#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000996#define SHARED_SINGLETON_TYPE(Expansion)
997#define BUILTIN_TYPE(Id, SingletonId) \
998 case BuiltinType::Id: return Importer.getToContext().SingletonId;
999#include "clang/AST/BuiltinTypes.def"
1000
1001 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1002 // context supports C++.
1003
1004 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1005 // context supports ObjC.
1006
Douglas Gregor96e578d2010-02-05 17:54:41 +00001007 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +00001008 // The context we're importing from has an unsigned 'char'. If we're
1009 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001010 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001011 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001012 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001013
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014 return Importer.getToContext().CharTy;
1015
Douglas Gregor96e578d2010-02-05 17:54:41 +00001016 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +00001017 // The context we're importing from has an unsigned 'char'. If we're
1018 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001019 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001020 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001021 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001022
Douglas Gregor96e578d2010-02-05 17:54:41 +00001023 return Importer.getToContext().CharTy;
1024
Chris Lattnerad3467e2010-12-25 23:25:43 +00001025 case BuiltinType::WChar_S:
1026 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001027 // FIXME: If not in C++, shall we translate to the C equivalent of
1028 // wchar_t?
1029 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001030 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001031
1032 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001033}
1034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1036 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1037 if (!ToOriginalTypeOrErr)
1038 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001039
Balazs Keri3b30d652018-10-19 13:32:20 +00001040 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001041}
1042
Balazs Keri3b30d652018-10-19 13:32:20 +00001043ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1044 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1045 if (!ToElementTypeOrErr)
1046 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001047
Balazs Keri3b30d652018-10-19 13:32:20 +00001048 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001049}
1050
Balazs Keri3b30d652018-10-19 13:32:20 +00001051ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1052 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1053 if (!ToPointeeTypeOrErr)
1054 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001055
Balazs Keri3b30d652018-10-19 13:32:20 +00001056 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001057}
1058
Balazs Keri3b30d652018-10-19 13:32:20 +00001059ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001060 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001061 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1062 if (!ToPointeeTypeOrErr)
1063 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001064
Balazs Keri3b30d652018-10-19 13:32:20 +00001065 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001066}
1067
Balazs Keri3b30d652018-10-19 13:32:20 +00001068ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001069ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001070 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001071 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1072 if (!ToPointeeTypeOrErr)
1073 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Balazs Keri3b30d652018-10-19 13:32:20 +00001075 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001076}
1077
Balazs Keri3b30d652018-10-19 13:32:20 +00001078ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001079ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001080 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001081 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1082 if (!ToPointeeTypeOrErr)
1083 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001084
Balazs Keri3b30d652018-10-19 13:32:20 +00001085 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001086}
1087
Balazs Keri3b30d652018-10-19 13:32:20 +00001088ExpectedType
1089ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001090 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001091 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1092 if (!ToPointeeTypeOrErr)
1093 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001094
Balazs Keri3b30d652018-10-19 13:32:20 +00001095 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1096 if (!ClassTypeOrErr)
1097 return ClassTypeOrErr.takeError();
1098
1099 return Importer.getToContext().getMemberPointerType(
1100 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001101}
1102
Balazs Keri3b30d652018-10-19 13:32:20 +00001103ExpectedType
1104ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1105 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1106 if (!ToElementTypeOrErr)
1107 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001108
Balazs Keri3b30d652018-10-19 13:32:20 +00001109 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001110 T->getSize(),
1111 T->getSizeModifier(),
1112 T->getIndexTypeCVRQualifiers());
1113}
1114
Balazs Keri3b30d652018-10-19 13:32:20 +00001115ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001116ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001117 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1118 if (!ToElementTypeOrErr)
1119 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001120
Balazs Keri3b30d652018-10-19 13:32:20 +00001121 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001122 T->getSizeModifier(),
1123 T->getIndexTypeCVRQualifiers());
1124}
1125
Balazs Keri3b30d652018-10-19 13:32:20 +00001126ExpectedType
1127ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1128 QualType ToElementType;
1129 Expr *ToSizeExpr;
1130 SourceRange ToBracketsRange;
1131 if (auto Imp = importSeq(
1132 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1133 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1134 else
1135 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001136
Balazs Keri3b30d652018-10-19 13:32:20 +00001137 return Importer.getToContext().getVariableArrayType(
1138 ToElementType, ToSizeExpr, T->getSizeModifier(),
1139 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001140}
1141
Balazs Keri3b30d652018-10-19 13:32:20 +00001142ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001143 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001144 QualType ToElementType;
1145 Expr *ToSizeExpr;
1146 SourceRange ToBracketsRange;
1147 if (auto Imp = importSeq(
1148 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1149 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1150 else
1151 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001152 // SizeExpr may be null if size is not specified directly.
1153 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001154
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001155 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001156 ToElementType, ToSizeExpr, T->getSizeModifier(),
1157 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001158}
1159
Balazs Keri3b30d652018-10-19 13:32:20 +00001160ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1161 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1162 if (!ToElementTypeOrErr)
1163 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001164
Balazs Keri3b30d652018-10-19 13:32:20 +00001165 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001166 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001167 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001168}
1169
Balazs Keri3b30d652018-10-19 13:32:20 +00001170ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1171 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1172 if (!ToElementTypeOrErr)
1173 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001174
Balazs Keri3b30d652018-10-19 13:32:20 +00001175 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001176 T->getNumElements());
1177}
1178
Balazs Keri3b30d652018-10-19 13:32:20 +00001179ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001180ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001181 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001182 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001183 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1184 if (!ToReturnTypeOrErr)
1185 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001186
Balazs Keri3b30d652018-10-19 13:32:20 +00001187 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001188 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001189}
1190
Balazs Keri3b30d652018-10-19 13:32:20 +00001191ExpectedType
1192ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1193 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1194 if (!ToReturnTypeOrErr)
1195 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001196
Douglas Gregor96e578d2010-02-05 17:54:41 +00001197 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001198 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001199 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001200 ExpectedType TyOrErr = import(A);
1201 if (!TyOrErr)
1202 return TyOrErr.takeError();
1203 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001204 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001205
Douglas Gregor96e578d2010-02-05 17:54:41 +00001206 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001207 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001208 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001209 ExpectedType TyOrErr = import(E);
1210 if (!TyOrErr)
1211 return TyOrErr.takeError();
1212 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001213 }
John McCalldb40c7f2010-12-14 08:05:40 +00001214
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001215 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1216 FunctionProtoType::ExtProtoInfo ToEPI;
1217
Balazs Keri3b30d652018-10-19 13:32:20 +00001218 auto Imp = importSeq(
1219 FromEPI.ExceptionSpec.NoexceptExpr,
1220 FromEPI.ExceptionSpec.SourceDecl,
1221 FromEPI.ExceptionSpec.SourceTemplate);
1222 if (!Imp)
1223 return Imp.takeError();
1224
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001225 ToEPI.ExtInfo = FromEPI.ExtInfo;
1226 ToEPI.Variadic = FromEPI.Variadic;
1227 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1228 ToEPI.TypeQuals = FromEPI.TypeQuals;
1229 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001230 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1231 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 std::tie(
1233 ToEPI.ExceptionSpec.NoexceptExpr,
1234 ToEPI.ExceptionSpec.SourceDecl,
1235 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001236
Balazs Keri3b30d652018-10-19 13:32:20 +00001237 return Importer.getToContext().getFunctionType(
1238 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001239}
1240
Balazs Keri3b30d652018-10-19 13:32:20 +00001241ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001242 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001243 UnresolvedUsingTypenameDecl *ToD;
1244 Decl *ToPrevD;
1245 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1246 std::tie(ToD, ToPrevD) = *Imp;
1247 else
1248 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001249
Balazs Keri3b30d652018-10-19 13:32:20 +00001250 return Importer.getToContext().getTypeDeclType(
1251 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001252}
1253
Balazs Keri3b30d652018-10-19 13:32:20 +00001254ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1255 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1256 if (!ToInnerTypeOrErr)
1257 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001258
Balazs Keri3b30d652018-10-19 13:32:20 +00001259 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001260}
1261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1263 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1264 if (!ToDeclOrErr)
1265 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001266
Balazs Keri3b30d652018-10-19 13:32:20 +00001267 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001268}
1269
Balazs Keri3b30d652018-10-19 13:32:20 +00001270ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1271 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1272 if (!ToExprOrErr)
1273 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001274
Balazs Keri3b30d652018-10-19 13:32:20 +00001275 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001276}
1277
Balazs Keri3b30d652018-10-19 13:32:20 +00001278ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1279 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1280 if (!ToUnderlyingTypeOrErr)
1281 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001282
Balazs Keri3b30d652018-10-19 13:32:20 +00001283 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001284}
1285
Balazs Keri3b30d652018-10-19 13:32:20 +00001286ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001287 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001288 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1289 if (!ToExprOrErr)
1290 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001291
Balazs Keri3b30d652018-10-19 13:32:20 +00001292 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1293 if (!ToUnderlyingTypeOrErr)
1294 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001295
Balazs Keri3b30d652018-10-19 13:32:20 +00001296 return Importer.getToContext().getDecltypeType(
1297 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001298}
1299
Balazs Keri3b30d652018-10-19 13:32:20 +00001300ExpectedType
1301ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1302 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1303 if (!ToBaseTypeOrErr)
1304 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001305
Balazs Keri3b30d652018-10-19 13:32:20 +00001306 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1307 if (!ToUnderlyingTypeOrErr)
1308 return ToUnderlyingTypeOrErr.takeError();
1309
1310 return Importer.getToContext().getUnaryTransformType(
1311 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001312}
1313
Balazs Keri3b30d652018-10-19 13:32:20 +00001314ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001315 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001316 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1317 if (!ToDeducedTypeOrErr)
1318 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001319
Balazs Keri3b30d652018-10-19 13:32:20 +00001320 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1321 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001322 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001323}
1324
Balazs Keri3b30d652018-10-19 13:32:20 +00001325ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001326 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001327 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1328 if (!ToDeclOrErr)
1329 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001330
Balazs Keri3b30d652018-10-19 13:32:20 +00001331 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1332 if (!ToInjTypeOrErr)
1333 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001334
1335 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1336 // See comments in InjectedClassNameType definition for details
1337 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1338 enum {
1339 TypeAlignmentInBits = 4,
1340 TypeAlignment = 1 << TypeAlignmentInBits
1341 };
1342
1343 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001344 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001345}
1346
Balazs Keri3b30d652018-10-19 13:32:20 +00001347ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1348 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1349 if (!ToDeclOrErr)
1350 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001351
Balazs Keri3b30d652018-10-19 13:32:20 +00001352 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001353}
1354
Balazs Keri3b30d652018-10-19 13:32:20 +00001355ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1356 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1357 if (!ToDeclOrErr)
1358 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001359
Balazs Keri3b30d652018-10-19 13:32:20 +00001360 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001361}
1362
Balazs Keri3b30d652018-10-19 13:32:20 +00001363ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1364 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1365 if (!ToModifiedTypeOrErr)
1366 return ToModifiedTypeOrErr.takeError();
1367 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1368 if (!ToEquivalentTypeOrErr)
1369 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001370
1371 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001372 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001373}
1374
Balazs Keri3b30d652018-10-19 13:32:20 +00001375ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001376 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001377 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1378 if (!ToDeclOrErr)
1379 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001380
1381 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001382 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001383}
1384
Balazs Keri3b30d652018-10-19 13:32:20 +00001385ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001386 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001387 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1388 if (!ReplacedOrErr)
1389 return ReplacedOrErr.takeError();
1390 const TemplateTypeParmType *Replaced =
1391 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001392
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1394 if (!ToReplacementTypeOrErr)
1395 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001396
1397 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001399}
1400
Balazs Keri3b30d652018-10-19 13:32:20 +00001401ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001402 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001403 auto ToTemplateOrErr = import(T->getTemplateName());
1404 if (!ToTemplateOrErr)
1405 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001407 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001408 if (Error Err = ImportTemplateArguments(
1409 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1410 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001411
Douglas Gregore2e50d332010-12-01 01:36:18 +00001412 QualType ToCanonType;
1413 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001414 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001415 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001416 if (ExpectedType TyOrErr = import(FromCanonType))
1417 ToCanonType = *TyOrErr;
1418 else
1419 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001420 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001421 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001422 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001423 ToCanonType);
1424}
1425
Balazs Keri3b30d652018-10-19 13:32:20 +00001426ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001427 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 auto ToQualifierOrErr = import(T->getQualifier());
1429 if (!ToQualifierOrErr)
1430 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001431
Balazs Keri3b30d652018-10-19 13:32:20 +00001432 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1433 if (!ToNamedTypeOrErr)
1434 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001435
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1437 if (!ToOwnedTagDeclOrErr)
1438 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001439
Abramo Bagnara6150c882010-05-11 21:36:43 +00001440 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001441 *ToQualifierOrErr,
1442 *ToNamedTypeOrErr,
1443 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001444}
1445
Balazs Keri3b30d652018-10-19 13:32:20 +00001446ExpectedType
1447ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1448 ExpectedType ToPatternOrErr = import(T->getPattern());
1449 if (!ToPatternOrErr)
1450 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001451
Balazs Keri3b30d652018-10-19 13:32:20 +00001452 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001453 T->getNumExpansions());
1454}
1455
Balazs Keri3b30d652018-10-19 13:32:20 +00001456ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001457 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001458 auto ToQualifierOrErr = import(T->getQualifier());
1459 if (!ToQualifierOrErr)
1460 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001461
Balazs Keri3b30d652018-10-19 13:32:20 +00001462 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001463
1464 SmallVector<TemplateArgument, 2> ToPack;
1465 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001466 if (Error Err = ImportTemplateArguments(
1467 T->getArgs(), T->getNumArgs(), ToPack))
1468 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001469
1470 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001471 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001472}
1473
Balazs Keri3b30d652018-10-19 13:32:20 +00001474ExpectedType
1475ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1476 auto ToQualifierOrErr = import(T->getQualifier());
1477 if (!ToQualifierOrErr)
1478 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001479
1480 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001481
Balazs Keri3b30d652018-10-19 13:32:20 +00001482 QualType Canon;
1483 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1484 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1485 Canon = (*TyOrErr).getCanonicalType();
1486 else
1487 return TyOrErr.takeError();
1488 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001489
Balazs Keri3b30d652018-10-19 13:32:20 +00001490 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1491 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001492 Name, Canon);
1493}
1494
Balazs Keri3b30d652018-10-19 13:32:20 +00001495ExpectedType
1496ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1497 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1498 if (!ToDeclOrErr)
1499 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500
Balazs Keri3b30d652018-10-19 13:32:20 +00001501 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001502}
1503
Balazs Keri3b30d652018-10-19 13:32:20 +00001504ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1505 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1506 if (!ToBaseTypeOrErr)
1507 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001508
Douglas Gregore9d95f12015-07-07 03:57:35 +00001509 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001510 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001511 if (ExpectedType TyOrErr = import(TypeArg))
1512 TypeArgs.push_back(*TyOrErr);
1513 else
1514 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001515 }
1516
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001517 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001518 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001519 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1520 Protocols.push_back(*ProtocolOrErr);
1521 else
1522 return ProtocolOrErr.takeError();
1523
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524 }
1525
Balazs Keri3b30d652018-10-19 13:32:20 +00001526 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001527 Protocols,
1528 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001529}
1530
Balazs Keri3b30d652018-10-19 13:32:20 +00001531ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001532ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001533 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1534 if (!ToPointeeTypeOrErr)
1535 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001536
Balazs Keri3b30d652018-10-19 13:32:20 +00001537 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001538}
1539
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001540//----------------------------------------------------------------------------
1541// Import Declarations
1542//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001543Error ASTNodeImporter::ImportDeclParts(
1544 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1545 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001546 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1547 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1548 DeclContext *OrigDC = D->getDeclContext();
1549 FunctionDecl *FunDecl;
1550 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1551 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001552 auto getLeafPointeeType = [](const Type *T) {
1553 while (T->isPointerType() || T->isArrayType()) {
1554 T = T->getPointeeOrArrayElementType();
1555 }
1556 return T;
1557 };
1558 for (const ParmVarDecl *P : FunDecl->parameters()) {
1559 const Type *LeafT =
1560 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1561 auto *RT = dyn_cast<RecordType>(LeafT);
1562 if (RT && RT->getDecl() == D) {
1563 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1564 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001565 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001566 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001567 }
1568 }
1569
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001570 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001571 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1572 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001573
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001574 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001575 if (Error Err = importInto(Name, D->getDeclName()))
1576 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001577
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001578 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001579 if (Error Err = importInto(Loc, D->getLocation()))
1580 return Err;
1581
Sean Callanan59721b32015-04-28 18:41:46 +00001582 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Balazs Keri3b30d652018-10-19 13:32:20 +00001583
1584 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001585}
1586
Balazs Keri3b30d652018-10-19 13:32:20 +00001587Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001588 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001589 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001590
Balazs Keri3b30d652018-10-19 13:32:20 +00001591 if (!ToD)
1592 if (Error Err = importInto(ToD, FromD))
1593 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001594
Balazs Keri3b30d652018-10-19 13:32:20 +00001595 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1596 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1597 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1598 !ToRecord->getDefinition()) {
1599 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1600 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001601 }
1602 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001603 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001604 }
1605
Balazs Keri3b30d652018-10-19 13:32:20 +00001606 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1607 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001608 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001609 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1610 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001611 }
1612 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001613 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001614 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001615
1616 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001617}
1618
Balazs Keri3b30d652018-10-19 13:32:20 +00001619Error
1620ASTNodeImporter::ImportDeclarationNameLoc(
1621 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001622 // NOTE: To.Name and To.Loc are already imported.
1623 // We only have to import To.LocInfo.
1624 switch (To.getName().getNameKind()) {
1625 case DeclarationName::Identifier:
1626 case DeclarationName::ObjCZeroArgSelector:
1627 case DeclarationName::ObjCOneArgSelector:
1628 case DeclarationName::ObjCMultiArgSelector:
1629 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001630 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001631 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001632
1633 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001634 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1635 To.setCXXOperatorNameRange(*ToRangeOrErr);
1636 else
1637 return ToRangeOrErr.takeError();
1638 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001639 }
1640 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001641 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1642 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1643 else
1644 return LocOrErr.takeError();
1645 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001646 }
1647 case DeclarationName::CXXConstructorName:
1648 case DeclarationName::CXXDestructorName:
1649 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001650 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1651 To.setNamedTypeInfo(*ToTInfoOrErr);
1652 else
1653 return ToTInfoOrErr.takeError();
1654 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001655 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001656 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001657 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001658}
1659
Balazs Keri3b30d652018-10-19 13:32:20 +00001660Error
1661ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001662 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001663 auto ToDCOrErr = Importer.ImportContext(FromDC);
1664 return ToDCOrErr.takeError();
1665 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001666 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001667 for (auto *From : FromDC->decls()) {
1668 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001669 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001670 // Ignore the error, continue with next Decl.
1671 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001672 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001673 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001674
Balazs Keri3b30d652018-10-19 13:32:20 +00001675 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001676}
1677
Balazs Keri3b30d652018-10-19 13:32:20 +00001678Error ASTNodeImporter::ImportDeclContext(
1679 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1680 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1681 if (!ToDCOrErr)
1682 return ToDCOrErr.takeError();
1683 ToDC = *ToDCOrErr;
1684
1685 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1686 auto ToLexicalDCOrErr = Importer.ImportContext(
1687 FromD->getLexicalDeclContext());
1688 if (!ToLexicalDCOrErr)
1689 return ToLexicalDCOrErr.takeError();
1690 ToLexicalDC = *ToLexicalDCOrErr;
1691 } else
1692 ToLexicalDC = ToDC;
1693
1694 return Error::success();
1695}
1696
1697Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001698 const CXXRecordDecl *From, CXXRecordDecl *To) {
1699 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1700 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001701
Balazs Keri1d20cc22018-07-16 12:16:39 +00001702 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001703 if (FromM->isImplicit()) {
1704 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1705 if (!ToMOrErr)
1706 return ToMOrErr.takeError();
1707 }
1708
1709 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001710}
1711
Balazs Keri3b30d652018-10-19 13:32:20 +00001712static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1713 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001714 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001715 Decl *ToTypedef = Importer.Import(FromTypedef);
1716 if (!ToTypedef)
1717 return make_error<ImportError>();
1718 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToTypedef));
1719 // FIXME: This should be the final code.
1720 //if (Expected<Decl *> ToTypedefOrErr = Importer.Import(FromTypedef))
1721 // To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1722 //else
1723 // return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001724 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001725 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001726}
1727
Balazs Keri3b30d652018-10-19 13:32:20 +00001728Error ASTNodeImporter::ImportDefinition(
1729 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001730 if (To->getDefinition() || To->isBeingDefined()) {
1731 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001732 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001733
Balazs Keri3b30d652018-10-19 13:32:20 +00001734 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001735 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001736
Douglas Gregore2e50d332010-12-01 01:36:18 +00001737 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001738
Balazs Keri3b30d652018-10-19 13:32:20 +00001739 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1740 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001741
Douglas Gregore2e50d332010-12-01 01:36:18 +00001742 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001743 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1744 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1745 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001746
1747 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1748 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1749 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001750 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001751 ToData.Aggregate = FromData.Aggregate;
1752 ToData.PlainOldData = FromData.PlainOldData;
1753 ToData.Empty = FromData.Empty;
1754 ToData.Polymorphic = FromData.Polymorphic;
1755 ToData.Abstract = FromData.Abstract;
1756 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001757 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1758 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1759 ToData.HasBasesWithNonStaticDataMembers =
1760 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001761 ToData.HasPrivateFields = FromData.HasPrivateFields;
1762 ToData.HasProtectedFields = FromData.HasProtectedFields;
1763 ToData.HasPublicFields = FromData.HasPublicFields;
1764 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001765 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001766 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001767 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001768 ToData.HasUninitializedReferenceMember
1769 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001770 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001771 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1772 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001773 ToData.NeedOverloadResolutionForCopyConstructor
1774 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001775 ToData.NeedOverloadResolutionForMoveConstructor
1776 = FromData.NeedOverloadResolutionForMoveConstructor;
1777 ToData.NeedOverloadResolutionForMoveAssignment
1778 = FromData.NeedOverloadResolutionForMoveAssignment;
1779 ToData.NeedOverloadResolutionForDestructor
1780 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001781 ToData.DefaultedCopyConstructorIsDeleted
1782 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001783 ToData.DefaultedMoveConstructorIsDeleted
1784 = FromData.DefaultedMoveConstructorIsDeleted;
1785 ToData.DefaultedMoveAssignmentIsDeleted
1786 = FromData.DefaultedMoveAssignmentIsDeleted;
1787 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001788 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1789 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001790 ToData.HasConstexprNonCopyMoveConstructor
1791 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001792 ToData.HasDefaultedDefaultConstructor
1793 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001794 ToData.DefaultedDefaultConstructorIsConstexpr
1795 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001796 ToData.HasConstexprDefaultConstructor
1797 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001798 ToData.HasNonLiteralTypeFieldsOrBases
1799 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001800 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001801 ToData.UserProvidedDefaultConstructor
1802 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001803 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001804 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1805 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1806 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1807 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001808 ToData.ImplicitCopyAssignmentHasConstParam
1809 = FromData.ImplicitCopyAssignmentHasConstParam;
1810 ToData.HasDeclaredCopyConstructorWithConstParam
1811 = FromData.HasDeclaredCopyConstructorWithConstParam;
1812 ToData.HasDeclaredCopyAssignmentWithConstParam
1813 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001814
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001815 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001816 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001817 ExpectedType TyOrErr = import(Base1.getType());
1818 if (!TyOrErr)
1819 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001820
1821 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001822 if (Base1.isPackExpansion()) {
1823 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1824 EllipsisLoc = *LocOrErr;
1825 else
1826 return LocOrErr.takeError();
1827 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001828
1829 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001830 if (Error Err =
1831 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1832 return Err;
1833
1834 auto RangeOrErr = import(Base1.getSourceRange());
1835 if (!RangeOrErr)
1836 return RangeOrErr.takeError();
1837
1838 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1839 if (!TSIOrErr)
1840 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001841
Douglas Gregore2e50d332010-12-01 01:36:18 +00001842 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001843 new (Importer.getToContext()) CXXBaseSpecifier(
1844 *RangeOrErr,
1845 Base1.isVirtual(),
1846 Base1.isBaseOfClass(),
1847 Base1.getAccessSpecifierAsWritten(),
1848 *TSIOrErr,
1849 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001850 }
1851 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001852 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001853 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001854
Douglas Gregor2e15c842012-02-01 21:00:38 +00001855 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001856 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1857 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001858
Douglas Gregore2e50d332010-12-01 01:36:18 +00001859 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001860 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001861}
1862
Balazs Keri3b30d652018-10-19 13:32:20 +00001863Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001864 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001865 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001866
Gabor Martonac3a5d62018-09-17 12:04:52 +00001867 Expr *FromInit = From->getInit();
1868 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001869 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001870
Balazs Keri3b30d652018-10-19 13:32:20 +00001871 ExpectedExpr ToInitOrErr = import(FromInit);
1872 if (!ToInitOrErr)
1873 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001874
Balazs Keri3b30d652018-10-19 13:32:20 +00001875 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001876 if (From->isInitKnownICE()) {
1877 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1878 Eval->CheckedICE = true;
1879 Eval->IsICE = From->isInitICE();
1880 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001881
1882 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001883 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001884}
1885
Balazs Keri3b30d652018-10-19 13:32:20 +00001886Error ASTNodeImporter::ImportDefinition(
1887 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001888 if (To->getDefinition() || To->isBeingDefined()) {
1889 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001890 return ImportDeclContext(From, /*ForceImport=*/true);
1891 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001892 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001893
Douglas Gregord451ea92011-07-29 23:31:30 +00001894 To->startDefinition();
1895
Balazs Keri3b30d652018-10-19 13:32:20 +00001896 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1897 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001898
Balazs Keri3b30d652018-10-19 13:32:20 +00001899 ExpectedType ToTypeOrErr =
1900 import(Importer.getFromContext().getTypeDeclType(From));
1901 if (!ToTypeOrErr)
1902 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001903
Balazs Keri3b30d652018-10-19 13:32:20 +00001904 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1905 if (!ToPromotionTypeOrErr)
1906 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001907
1908 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001909 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1910 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001911
Douglas Gregord451ea92011-07-29 23:31:30 +00001912 // FIXME: we might need to merge the number of positive or negative bits
1913 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001914 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001915 From->getNumPositiveBits(),
1916 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001917 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001918}
1919
Balazs Keri3b30d652018-10-19 13:32:20 +00001920// FIXME: Remove this, use `import` instead.
1921Expected<TemplateParameterList *> ASTNodeImporter::ImportTemplateParameterList(
1922 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001923 SmallVector<NamedDecl *, 4> ToParams(Params->size());
Balazs Keri3b30d652018-10-19 13:32:20 +00001924 if (Error Err = ImportContainerChecked(*Params, ToParams))
1925 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001926
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001927 Expr *ToRequiresClause;
1928 if (Expr *const R = Params->getRequiresClause()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001929 if (Error Err = importInto(ToRequiresClause, R))
1930 return std::move(Err);
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001931 } else {
1932 ToRequiresClause = nullptr;
1933 }
1934
Balazs Keri3b30d652018-10-19 13:32:20 +00001935 auto ToTemplateLocOrErr = import(Params->getTemplateLoc());
1936 if (!ToTemplateLocOrErr)
1937 return ToTemplateLocOrErr.takeError();
1938 auto ToLAngleLocOrErr = import(Params->getLAngleLoc());
1939 if (!ToLAngleLocOrErr)
1940 return ToLAngleLocOrErr.takeError();
1941 auto ToRAngleLocOrErr = import(Params->getRAngleLoc());
1942 if (!ToRAngleLocOrErr)
1943 return ToRAngleLocOrErr.takeError();
1944
1945 return TemplateParameterList::Create(
1946 Importer.getToContext(),
1947 *ToTemplateLocOrErr,
1948 *ToLAngleLocOrErr,
1949 ToParams,
1950 *ToRAngleLocOrErr,
1951 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001952}
1953
Balazs Keri3b30d652018-10-19 13:32:20 +00001954Error ASTNodeImporter::ImportTemplateArguments(
1955 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1956 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001957 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001958 if (auto ToOrErr = import(FromArgs[I]))
1959 ToArgs.push_back(*ToOrErr);
1960 else
1961 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001962 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001963
Balazs Keri3b30d652018-10-19 13:32:20 +00001964 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001965}
1966
Balazs Keri3b30d652018-10-19 13:32:20 +00001967// FIXME: Do not forget to remove this and use only 'import'.
1968Expected<TemplateArgument>
1969ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1970 return import(From);
1971}
1972
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001973template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001974Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001975 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1976 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001977 if (auto ToLocOrErr = import(FromLoc))
1978 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001979 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001980 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001981 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001982 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001983}
1984
Gabor Marton26f72a92018-07-12 09:42:05 +00001985static StructuralEquivalenceKind
1986getStructuralEquivalenceKind(const ASTImporter &Importer) {
1987 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1988 : StructuralEquivalenceKind::Default;
1989}
1990
Gabor Marton950fb572018-07-17 12:39:27 +00001991bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1992 StructuralEquivalenceContext Ctx(
1993 Importer.getFromContext(), Importer.getToContext(),
1994 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1995 false, Complain);
1996 return Ctx.IsEquivalent(From, To);
1997}
1998
1999bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002000 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002001 // Eliminate a potential failure point where we attempt to re-import
2002 // something we're trying to import while completing ToRecord.
2003 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2004 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002005 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002006 if (ToOriginRecord)
2007 ToRecord = ToOriginRecord;
2008 }
2009
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002010 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002011 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002012 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002013 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002014 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002015 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002016}
2017
Larisse Voufo39a1e502013-08-06 01:03:05 +00002018bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2019 bool Complain) {
2020 StructuralEquivalenceContext Ctx(
2021 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002022 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2023 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002024 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002025}
2026
Douglas Gregor98c10182010-02-12 22:17:39 +00002027bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002028 StructuralEquivalenceContext Ctx(
2029 Importer.getFromContext(), Importer.getToContext(),
2030 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002031 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002032}
2033
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002034bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2035 FunctionTemplateDecl *To) {
2036 StructuralEquivalenceContext Ctx(
2037 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002038 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2039 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002040 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002041}
2042
Balazs Keric7797c42018-07-11 09:37:24 +00002043bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2044 StructuralEquivalenceContext Ctx(
2045 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002046 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2047 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002048 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002049}
2050
Douglas Gregor91155082012-11-14 22:29:20 +00002051bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002052 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002053 const llvm::APSInt &FromVal = FromEC->getInitVal();
2054 const llvm::APSInt &ToVal = ToEC->getInitVal();
2055
2056 return FromVal.isSigned() == ToVal.isSigned() &&
2057 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2058 FromVal == ToVal;
2059}
2060
2061bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002062 ClassTemplateDecl *To) {
2063 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2064 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002065 Importer.getNonEquivalentDecls(),
2066 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002067 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002068}
2069
Larisse Voufo39a1e502013-08-06 01:03:05 +00002070bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2071 VarTemplateDecl *To) {
2072 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2073 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002074 Importer.getNonEquivalentDecls(),
2075 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002076 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002077}
2078
Balazs Keri3b30d652018-10-19 13:32:20 +00002079ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002080 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002081 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002082 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002083}
2084
Balazs Keri3b30d652018-10-19 13:32:20 +00002085ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2086 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2087 << D->getDeclKindName();
2088 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2089}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002090
Balazs Keri3b30d652018-10-19 13:32:20 +00002091ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2092 // Import the context of this declaration.
2093 DeclContext *DC, *LexicalDC;
2094 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2095 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002096
2097 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002098 ExpectedSLoc LocOrErr = import(D->getLocation());
2099 if (!LocOrErr)
2100 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002101
Gabor Marton26f72a92018-07-12 09:42:05 +00002102 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002103 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002104 return ToD;
2105
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002106 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002107 LexicalDC->addDeclInternal(ToD);
2108 return ToD;
2109}
2110
Balazs Keri3b30d652018-10-19 13:32:20 +00002111ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002112 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002113 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002114
Gabor Marton26f72a92018-07-12 09:42:05 +00002115 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002116
Sean Callanan65198272011-11-17 23:20:56 +00002117 return ToD;
2118}
2119
Balazs Keri3b30d652018-10-19 13:32:20 +00002120ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2121 ExpectedSLoc LocOrErr = import(D->getLocation());
2122 if (!LocOrErr)
2123 return LocOrErr.takeError();
2124 auto ColonLocOrErr = import(D->getColonLoc());
2125 if (!ColonLocOrErr)
2126 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002127
2128 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002129 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2130 if (!DCOrErr)
2131 return DCOrErr.takeError();
2132 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002133
Gabor Marton26f72a92018-07-12 09:42:05 +00002134 AccessSpecDecl *ToD;
2135 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002136 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002137 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002138
2139 // Lexical DeclContext and Semantic DeclContext
2140 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002141 ToD->setLexicalDeclContext(DC);
2142 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002143
Gabor Marton26f72a92018-07-12 09:42:05 +00002144 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002145}
2146
Balazs Keri3b30d652018-10-19 13:32:20 +00002147ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2148 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2149 if (!DCOrErr)
2150 return DCOrErr.takeError();
2151 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002152 DeclContext *LexicalDC = DC;
2153
Balazs Keri3b30d652018-10-19 13:32:20 +00002154 SourceLocation ToLocation, ToRParenLoc;
2155 Expr *ToAssertExpr;
2156 StringLiteral *ToMessage;
2157 if (auto Imp = importSeq(
2158 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2159 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2160 else
2161 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002162
Gabor Marton26f72a92018-07-12 09:42:05 +00002163 StaticAssertDecl *ToD;
2164 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002165 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2166 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002167 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002168
2169 ToD->setLexicalDeclContext(LexicalDC);
2170 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002171 return ToD;
2172}
2173
Balazs Keri3b30d652018-10-19 13:32:20 +00002174ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002175 // Import the major distinguishing characteristics of this namespace.
2176 DeclContext *DC, *LexicalDC;
2177 DeclarationName Name;
2178 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002179 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002180 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2181 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002182 if (ToD)
2183 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002184
2185 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002186 if (!Name) {
2187 // This is an anonymous namespace. Adopt an existing anonymous
2188 // namespace if we can.
2189 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002190 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002191 MergeWithNamespace = TU->getAnonymousNamespace();
2192 else
2193 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2194 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002195 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002196 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002197 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002198 for (auto *FoundDecl : FoundDecls) {
2199 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002200 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002201
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002202 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002203 MergeWithNamespace = FoundNS;
2204 ConflictingDecls.clear();
2205 break;
2206 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002207
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002208 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002209 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002210
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002211 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002212 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002213 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002214 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002215 if (!Name)
2216 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002217 }
2218 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002219
Balazs Keri3b30d652018-10-19 13:32:20 +00002220 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2221 if (!BeginLocOrErr)
2222 return BeginLocOrErr.takeError();
2223
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002224 // Create the "to" namespace, if needed.
2225 NamespaceDecl *ToNamespace = MergeWithNamespace;
2226 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002227 if (GetImportedOrCreateDecl(
2228 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002229 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002230 /*PrevDecl=*/nullptr))
2231 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002232 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002233 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002234
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002235 // If this is an anonymous namespace, register it as the anonymous
2236 // namespace within its context.
2237 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002238 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002239 TU->setAnonymousNamespace(ToNamespace);
2240 else
2241 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2242 }
2243 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002244 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002245
Balazs Keri3b30d652018-10-19 13:32:20 +00002246 if (Error Err = ImportDeclContext(D))
2247 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002248
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002249 return ToNamespace;
2250}
2251
Balazs Keri3b30d652018-10-19 13:32:20 +00002252ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002253 // Import the major distinguishing characteristics of this namespace.
2254 DeclContext *DC, *LexicalDC;
2255 DeclarationName Name;
2256 SourceLocation Loc;
2257 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002258 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2259 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002260 if (LookupD)
2261 return LookupD;
2262
2263 // NOTE: No conflict resolution is done for namespace aliases now.
2264
Balazs Keri3b30d652018-10-19 13:32:20 +00002265 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2266 NestedNameSpecifierLoc ToQualifierLoc;
2267 NamespaceDecl *ToNamespace;
2268 if (auto Imp = importSeq(
2269 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2270 D->getTargetNameLoc(), D->getNamespace()))
2271 std::tie(
2272 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2273 ToNamespace) = *Imp;
2274 else
2275 return Imp.takeError();
2276 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002277
Gabor Marton26f72a92018-07-12 09:42:05 +00002278 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002279 if (GetImportedOrCreateDecl(
2280 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2281 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002282 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002283
2284 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002285 LexicalDC->addDeclInternal(ToD);
2286
2287 return ToD;
2288}
2289
Balazs Keri3b30d652018-10-19 13:32:20 +00002290ExpectedDecl
2291ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002292 // Import the major distinguishing characteristics of this typedef.
2293 DeclContext *DC, *LexicalDC;
2294 DeclarationName Name;
2295 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002296 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002297 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2298 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002299 if (ToD)
2300 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002301
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002302 // If this typedef is not in block scope, determine whether we've
2303 // seen a typedef with the same name (that we can merge with) or any
2304 // other entity by that name (which name lookup could conflict with).
2305 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002306 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002307 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002308 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002309 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002310 for (auto *FoundDecl : FoundDecls) {
2311 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002312 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002313 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002314 if (Importer.IsStructurallyEquivalent(
2315 D->getUnderlyingType(), FoundTypedef->getUnderlyingType()))
2316 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002317 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002318
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002319 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002320 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002321
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002322 if (!ConflictingDecls.empty()) {
2323 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002324 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002325 ConflictingDecls.size());
2326 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002327 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002328 }
2329 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002330
Balazs Keri3b30d652018-10-19 13:32:20 +00002331 QualType ToUnderlyingType;
2332 TypeSourceInfo *ToTypeSourceInfo;
2333 SourceLocation ToBeginLoc;
2334 if (auto Imp = importSeq(
2335 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2336 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2337 else
2338 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002339
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002340 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002341 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002342 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002343 if (IsAlias) {
2344 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002345 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2346 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002347 return ToTypedef;
2348 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002349 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2350 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002351 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002352
Douglas Gregordd483172010-02-22 17:42:47 +00002353 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002354 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002355
2356 // Templated declarations should not appear in DeclContext.
2357 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2358 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2359 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002360
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002361 return ToTypedef;
2362}
2363
Balazs Keri3b30d652018-10-19 13:32:20 +00002364ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002365 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2366}
2367
Balazs Keri3b30d652018-10-19 13:32:20 +00002368ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002369 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2370}
2371
Balazs Keri3b30d652018-10-19 13:32:20 +00002372ExpectedDecl
2373ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002374 // Import the major distinguishing characteristics of this typedef.
2375 DeclContext *DC, *LexicalDC;
2376 DeclarationName Name;
2377 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002378 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002379 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2380 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002381 if (FoundD)
2382 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002383
2384 // If this typedef is not in block scope, determine whether we've
2385 // seen a typedef with the same name (that we can merge with) or any
2386 // other entity by that name (which name lookup could conflict with).
2387 if (!DC->isFunctionOrMethod()) {
2388 SmallVector<NamedDecl *, 4> ConflictingDecls;
2389 unsigned IDNS = Decl::IDNS_Ordinary;
2390 SmallVector<NamedDecl *, 2> FoundDecls;
2391 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002392 for (auto *FoundDecl : FoundDecls) {
2393 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002394 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002395 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002396 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002397 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002398 }
2399
2400 if (!ConflictingDecls.empty()) {
2401 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2402 ConflictingDecls.data(),
2403 ConflictingDecls.size());
2404 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002405 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002406 }
2407 }
2408
Balazs Keri3b30d652018-10-19 13:32:20 +00002409 TemplateParameterList *ToTemplateParameters;
2410 TypeAliasDecl *ToTemplatedDecl;
2411 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2412 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2413 else
2414 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002415
Gabor Marton26f72a92018-07-12 09:42:05 +00002416 TypeAliasTemplateDecl *ToAlias;
2417 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002418 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002419 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002420
Balazs Keri3b30d652018-10-19 13:32:20 +00002421 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002422
Gabor Horvath7a91c082017-11-14 11:30:38 +00002423 ToAlias->setAccess(D->getAccess());
2424 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002425 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002426 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002427}
2428
Balazs Keri3b30d652018-10-19 13:32:20 +00002429ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002430 // Import the major distinguishing characteristics of this label.
2431 DeclContext *DC, *LexicalDC;
2432 DeclarationName Name;
2433 SourceLocation Loc;
2434 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002435 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2436 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002437 if (ToD)
2438 return ToD;
2439
2440 assert(LexicalDC->isFunctionOrMethod());
2441
Gabor Marton26f72a92018-07-12 09:42:05 +00002442 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002443 if (D->isGnuLocal()) {
2444 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2445 if (!BeginLocOrErr)
2446 return BeginLocOrErr.takeError();
2447 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2448 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2449 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002450
Balazs Keri3b30d652018-10-19 13:32:20 +00002451 } else {
2452 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2453 Name.getAsIdentifierInfo()))
2454 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002455
Balazs Keri3b30d652018-10-19 13:32:20 +00002456 }
2457
2458 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2459 if (!ToStmtOrErr)
2460 return ToStmtOrErr.takeError();
2461
2462 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002463 ToLabel->setLexicalDeclContext(LexicalDC);
2464 LexicalDC->addDeclInternal(ToLabel);
2465 return ToLabel;
2466}
2467
Balazs Keri3b30d652018-10-19 13:32:20 +00002468ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002469 // Import the major distinguishing characteristics of this enum.
2470 DeclContext *DC, *LexicalDC;
2471 DeclarationName Name;
2472 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002473 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002474 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2475 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002476 if (ToD)
2477 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002478
Douglas Gregor98c10182010-02-12 22:17:39 +00002479 // Figure out what enum name we're looking for.
2480 unsigned IDNS = Decl::IDNS_Tag;
2481 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002482 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002483 if (Error Err = importInto(
2484 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2485 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002486 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002487 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002488 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002489
Douglas Gregor98c10182010-02-12 22:17:39 +00002490 // We may already have an enum of the same name; try to find and match it.
2491 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002492 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002493 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002494 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002495 for (auto *FoundDecl : FoundDecls) {
2496 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002497 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002498
Balazs Keri3b30d652018-10-19 13:32:20 +00002499 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002500 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002501 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002502 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002503
Balazs Keri3b30d652018-10-19 13:32:20 +00002504 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002505 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002506 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002507 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002508
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002509 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002510 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002511
Douglas Gregor98c10182010-02-12 22:17:39 +00002512 if (!ConflictingDecls.empty()) {
2513 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002514 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002515 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002516 if (!Name)
2517 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002518 }
2519 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002520
Balazs Keri3b30d652018-10-19 13:32:20 +00002521 SourceLocation ToBeginLoc;
2522 NestedNameSpecifierLoc ToQualifierLoc;
2523 QualType ToIntegerType;
2524 if (auto Imp = importSeq(
2525 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2526 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2527 else
2528 return Imp.takeError();
2529
Douglas Gregor98c10182010-02-12 22:17:39 +00002530 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002531 EnumDecl *D2;
2532 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002533 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002534 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2535 D->isScopedUsingClassTag(), D->isFixed()))
2536 return D2;
2537
Balazs Keri3b30d652018-10-19 13:32:20 +00002538 D2->setQualifierInfo(ToQualifierLoc);
2539 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002540 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002541 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002542 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002543
Douglas Gregor98c10182010-02-12 22:17:39 +00002544 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002545 if (D->isCompleteDefinition())
2546 if (Error Err = ImportDefinition(D, D2))
2547 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002548
Douglas Gregor3996e242010-02-15 22:01:00 +00002549 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002550}
2551
Balazs Keri3b30d652018-10-19 13:32:20 +00002552ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002553 bool IsFriendTemplate = false;
2554 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2555 IsFriendTemplate =
2556 DCXX->getDescribedClassTemplate() &&
2557 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2558 Decl::FOK_None;
2559 }
2560
Douglas Gregor5c73e912010-02-11 00:48:18 +00002561 // If this record has a definition in the translation unit we're coming from,
2562 // but this particular declaration is not that definition, import the
2563 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002564 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002565 if (Definition && Definition != D &&
Balazs Keri0c23dc52018-08-13 13:08:37 +00002566 // Friend template declaration must be imported on its own.
2567 !IsFriendTemplate &&
Gabor Martona3af5672018-05-23 14:24:02 +00002568 // In contrast to a normal CXXRecordDecl, the implicit
2569 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2570 // The definition of the implicit CXXRecordDecl in this case is the
2571 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2572 // condition in order to be able to import the implict Decl.
2573 !D->isImplicit()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002574 ExpectedDecl ImportedDefOrErr = import(Definition);
2575 if (!ImportedDefOrErr)
2576 return ImportedDefOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002577
Balazs Keri3b30d652018-10-19 13:32:20 +00002578 return Importer.MapImported(D, *ImportedDefOrErr);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002579 }
Gabor Martona3af5672018-05-23 14:24:02 +00002580
Douglas Gregor5c73e912010-02-11 00:48:18 +00002581 // Import the major distinguishing characteristics of this record.
2582 DeclContext *DC, *LexicalDC;
2583 DeclarationName Name;
2584 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002585 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002586 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2587 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002588 if (ToD)
2589 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002590
Douglas Gregor5c73e912010-02-11 00:48:18 +00002591 // Figure out what structure name we're looking for.
2592 unsigned IDNS = Decl::IDNS_Tag;
2593 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002594 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002595 if (Error Err = importInto(
2596 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2597 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002598 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002599 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002600 IDNS |= Decl::IDNS_Ordinary;
2601
2602 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002603 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002604 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002605 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002606 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002607 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002608 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002609
2610 if (!FoundDecls.empty()) {
2611 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2612 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2613 D->getASTContext().getExternalSource()->CompleteType(D);
2614 }
2615
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002616 for (auto *FoundDecl : FoundDecls) {
2617 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002618 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002619
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002620 Decl *Found = FoundDecl;
2621 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2622 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002623 Found = Tag->getDecl();
2624 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002625
2626 if (D->getDescribedTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002627 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Martona0df7a92018-05-30 09:19:26 +00002628 Found = Template->getTemplatedDecl();
Balazs Keri3b30d652018-10-19 13:32:20 +00002629 } else {
2630 ConflictingDecls.push_back(FoundDecl);
Gabor Martona0df7a92018-05-30 09:19:26 +00002631 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002632 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002633 }
2634
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002635 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002636 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002637 if (!IsStructuralMatch(D, FoundRecord, false))
2638 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002639 } else {
2640 if (!IsStructuralMatch(D, FoundRecord)) {
2641 ConflictingDecls.push_back(FoundDecl);
2642 continue;
2643 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002644 }
2645
Sean Callanan9092d472017-05-13 00:46:33 +00002646 PrevDecl = FoundRecord;
2647
Douglas Gregor25791052010-02-12 00:09:27 +00002648 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002649 if ((SearchName && !D->isCompleteDefinition() && !IsFriendTemplate)
Douglas Gregordd6006f2012-07-17 21:16:27 +00002650 || (D->isCompleteDefinition() &&
2651 D->isAnonymousStructOrUnion()
Balazs Keri3b30d652018-10-19 13:32:20 +00002652 == FoundDef->isAnonymousStructOrUnion())) {
Douglas Gregor25791052010-02-12 00:09:27 +00002653 // The record types structurally match, or the "from" translation
2654 // unit only had a forward declaration anyway; call it the same
2655 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002656 // FIXME: Structural equivalence check should check for same
2657 // user-defined methods.
2658 Importer.MapImported(D, FoundDef);
2659 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2660 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2661 assert(FoundCXX && "Record type mismatch");
2662
2663 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2664 // FoundDef may not have every implicit method that D has
2665 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002666 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2667 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002668 }
2669 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002670 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002671 if (IsFriendTemplate)
2672 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002673 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002674 // We have a forward declaration of this type, so adopt that forward
2675 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002676
Sean Callananc94711c2014-03-04 18:11:50 +00002677 // If one or both can be completed from external storage then try one
2678 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002679
Sean Callananc94711c2014-03-04 18:11:50 +00002680 if (FoundRecord->hasExternalLexicalStorage() &&
2681 !FoundRecord->isCompleteDefinition())
2682 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2683 if (D->hasExternalLexicalStorage())
2684 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002685
Sean Callananc94711c2014-03-04 18:11:50 +00002686 if (FoundRecord->isCompleteDefinition() &&
2687 D->isCompleteDefinition() &&
Balazs Keri3b30d652018-10-19 13:32:20 +00002688 !IsStructuralMatch(D, FoundRecord)) {
2689 ConflictingDecls.push_back(FoundDecl);
Sean Callananc94711c2014-03-04 18:11:50 +00002690 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002691 }
Balazs Keri0c23dc52018-08-13 13:08:37 +00002692
Douglas Gregor25791052010-02-12 00:09:27 +00002693 AdoptDecl = FoundRecord;
2694 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002695 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002696
2697 continue;
2698 } else if (isa<ValueDecl>(Found))
2699 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002700
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002701 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002702 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002703
Douglas Gregordd6006f2012-07-17 21:16:27 +00002704 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002705 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002706 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002707 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002708 if (!Name)
2709 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002710 }
2711 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002712
Balazs Keri3b30d652018-10-19 13:32:20 +00002713 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2714 if (!BeginLocOrErr)
2715 return BeginLocOrErr.takeError();
2716
Douglas Gregor5c73e912010-02-11 00:48:18 +00002717 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002718 RecordDecl *D2 = AdoptDecl;
2719 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002720 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002721 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002722 if (DCXX->isLambda()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002723 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2724 if (!TInfoOrErr)
2725 return TInfoOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00002726 if (GetImportedOrCreateSpecialDecl(
2727 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002728 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002729 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2730 return D2CXX;
Balazs Keri3b30d652018-10-19 13:32:20 +00002731 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2732 if (!CDeclOrErr)
2733 return CDeclOrErr.takeError();
2734 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002735 } else if (DCXX->isInjectedClassName()) {
2736 // We have to be careful to do a similar dance to the one in
2737 // Sema::ActOnStartCXXMemberDeclarations
2738 CXXRecordDecl *const PrevDecl = nullptr;
2739 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002740 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002741 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002742 Name.getAsIdentifierInfo(), PrevDecl,
2743 DelayTypeCreation))
2744 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002745 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002746 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002747 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002748 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002749 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002750 Name.getAsIdentifierInfo(),
2751 cast_or_null<CXXRecordDecl>(PrevDecl)))
2752 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002753 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002754
Douglas Gregor3996e242010-02-15 22:01:00 +00002755 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002756 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002757 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002758 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002759 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002760
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002761 if (ClassTemplateDecl *FromDescribed =
2762 DCXX->getDescribedClassTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002763 ClassTemplateDecl *ToDescribed;
2764 if (Error Err = importInto(ToDescribed, FromDescribed))
2765 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002766 D2CXX->setDescribedClassTemplate(ToDescribed);
Balazs Keri0c23dc52018-08-13 13:08:37 +00002767 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
Gabor Marton5915777e2018-06-26 13:44:24 +00002768 // In a record describing a template the type should be an
2769 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2770 // previously set type to the correct value here (ToDescribed is not
2771 // available at record create).
2772 // FIXME: The previous type is cleared but not removed from
2773 // ASTContext's internal storage.
2774 CXXRecordDecl *Injected = nullptr;
2775 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2776 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2777 if (Record && Record->isInjectedClassName()) {
2778 Injected = Record;
2779 break;
2780 }
2781 }
2782 D2CXX->setTypeForDecl(nullptr);
2783 Importer.getToContext().getInjectedClassNameType(D2CXX,
2784 ToDescribed->getInjectedClassNameSpecialization());
2785 if (Injected) {
2786 Injected->setTypeForDecl(nullptr);
2787 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2788 }
2789 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002790 } else if (MemberSpecializationInfo *MemberInfo =
2791 DCXX->getMemberSpecializationInfo()) {
2792 TemplateSpecializationKind SK =
2793 MemberInfo->getTemplateSpecializationKind();
2794 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002795
2796 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2797 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2798 else
2799 return ToInstOrErr.takeError();
2800
2801 if (ExpectedSLoc POIOrErr =
2802 import(MemberInfo->getPointOfInstantiation()))
2803 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2804 *POIOrErr);
2805 else
2806 return POIOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002807 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002808
Douglas Gregor25791052010-02-12 00:09:27 +00002809 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002810 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002811 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002812 Name.getAsIdentifierInfo(), PrevDecl))
2813 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002814 D2->setLexicalDeclContext(LexicalDC);
2815 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002816 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002817
Balazs Keri3b30d652018-10-19 13:32:20 +00002818 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2819 D2->setQualifierInfo(*QualifierLocOrErr);
2820 else
2821 return QualifierLocOrErr.takeError();
2822
Douglas Gregordd6006f2012-07-17 21:16:27 +00002823 if (D->isAnonymousStructOrUnion())
2824 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002825 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002826
2827 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002828
Balazs Keri3b30d652018-10-19 13:32:20 +00002829 if (D->isCompleteDefinition())
2830 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2831 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002832
Douglas Gregor3996e242010-02-15 22:01:00 +00002833 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002834}
2835
Balazs Keri3b30d652018-10-19 13:32:20 +00002836ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002837 // Import the major distinguishing characteristics of this enumerator.
2838 DeclContext *DC, *LexicalDC;
2839 DeclarationName Name;
2840 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002841 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002842 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2843 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002844 if (ToD)
2845 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002846
Fangrui Song6907ce22018-07-30 19:24:48 +00002847 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002848 // in the same context.
2849 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002850 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002851 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002852 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002853 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002854 for (auto *FoundDecl : FoundDecls) {
2855 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002856 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002857
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002858 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002859 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002860 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002861 }
2862
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002863 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002864 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002865
Douglas Gregor98c10182010-02-12 22:17:39 +00002866 if (!ConflictingDecls.empty()) {
2867 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002868 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002869 ConflictingDecls.size());
2870 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002871 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002872 }
2873 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002874
Balazs Keri3b30d652018-10-19 13:32:20 +00002875 ExpectedType TypeOrErr = import(D->getType());
2876 if (!TypeOrErr)
2877 return TypeOrErr.takeError();
2878
2879 ExpectedExpr InitOrErr = import(D->getInitExpr());
2880 if (!InitOrErr)
2881 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002882
Gabor Marton26f72a92018-07-12 09:42:05 +00002883 EnumConstantDecl *ToEnumerator;
2884 if (GetImportedOrCreateDecl(
2885 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002886 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002887 return ToEnumerator;
2888
Douglas Gregordd483172010-02-22 17:42:47 +00002889 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002890 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002891 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002892 return ToEnumerator;
2893}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002894
Balazs Keri3b30d652018-10-19 13:32:20 +00002895Error ASTNodeImporter::ImportTemplateInformation(
2896 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002897 switch (FromFD->getTemplatedKind()) {
2898 case FunctionDecl::TK_NonTemplate:
2899 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002900 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002901
2902 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002903 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002904
2905 if (Expected<FunctionDecl *> InstFDOrErr =
2906 import(FromFD->getInstantiatedFromMemberFunction()))
2907 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2908 else
2909 return InstFDOrErr.takeError();
2910
2911 if (ExpectedSLoc POIOrErr = import(
2912 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2913 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2914 else
2915 return POIOrErr.takeError();
2916
2917 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002918 }
2919
2920 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002921 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002922 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002923 if (!FunctionAndArgsOrErr)
2924 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002925
2926 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002927 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002928
Gabor Marton5254e642018-06-27 13:32:50 +00002929 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002930 TemplateArgumentListInfo ToTAInfo;
2931 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002932 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002933 if (Error Err = ImportTemplateArgumentListInfo(
2934 *FromTAArgsAsWritten, ToTAInfo))
2935 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002936
Balazs Keri3b30d652018-10-19 13:32:20 +00002937 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2938 if (!POIOrErr)
2939 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002940
Gabor Marton5254e642018-06-27 13:32:50 +00002941 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002942 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002943 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2944 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2945 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002946 }
2947
2948 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2949 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2950 UnresolvedSet<8> TemplDecls;
2951 unsigned NumTemplates = FromInfo->getNumTemplates();
2952 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002953 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2954 import(FromInfo->getTemplate(I)))
2955 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002956 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002957 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002958 }
2959
2960 // Import TemplateArgumentListInfo.
2961 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002962 if (Error Err = ImportTemplateArgumentListInfo(
2963 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2964 llvm::makeArrayRef(
2965 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2966 ToTAInfo))
2967 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002968
2969 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2970 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002971 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002972 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002973 }
Sam McCallfdc32072018-01-26 12:06:44 +00002974 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002975}
2976
Balazs Keri3b30d652018-10-19 13:32:20 +00002977Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002978ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002979 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002980 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002981 if (!FunctionAndArgsOrErr)
2982 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002983
Balazs Keri3b30d652018-10-19 13:32:20 +00002984 FunctionTemplateDecl *Template;
2985 TemplateArgsTy ToTemplArgs;
2986 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002987 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002988 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002989 return FoundSpec;
2990}
2991
Balazs Keri3b30d652018-10-19 13:32:20 +00002992ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002993
Balazs Keri3b30d652018-10-19 13:32:20 +00002994 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002995 auto RedeclIt = Redecls.begin();
2996 // Import the first part of the decl chain. I.e. import all previous
2997 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002998 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2999 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3000 if (!ToRedeclOrErr)
3001 return ToRedeclOrErr.takeError();
3002 }
Gabor Marton5254e642018-06-27 13:32:50 +00003003 assert(*RedeclIt == D);
3004
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003005 // Import the major distinguishing characteristics of this function.
3006 DeclContext *DC, *LexicalDC;
3007 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003008 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003009 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003010 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3011 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003012 if (ToD)
3013 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003014
Gabor Marton5254e642018-06-27 13:32:50 +00003015 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003016 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003017
Gabor Marton5254e642018-06-27 13:32:50 +00003018 // If this is a function template specialization, then try to find the same
3019 // existing specialization in the "to" context. The localUncachedLookup
3020 // below will not find any specialization, but would find the primary
3021 // template; thus, we have to skip normal lookup in case of specializations.
3022 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3023 if (D->getTemplatedKind() ==
3024 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003025 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3026 if (!FoundFunctionOrErr)
3027 return FoundFunctionOrErr.takeError();
3028 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
3029 if (D->doesThisDeclarationHaveABody() && FoundFunction->hasBody())
3030 return Importer.MapImported(D, FoundFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003031 FoundByLookup = FoundFunction;
3032 }
3033 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003034 // Try to find a function in our own ("to") context with the same name, same
3035 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003036 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003037 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003038 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003039 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003040 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003041 for (auto *FoundDecl : FoundDecls) {
3042 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003043 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003044
Balazs Keria35798d2018-07-17 09:52:41 +00003045 // If template was found, look at the templated function.
3046 if (FromFT) {
3047 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
3048 FoundDecl = Template->getTemplatedDecl();
3049 else
3050 continue;
3051 }
3052
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003053 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003054 if (FoundFunction->hasExternalFormalLinkage() &&
3055 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00003056 if (IsStructuralMatch(D, FoundFunction)) {
3057 const FunctionDecl *Definition = nullptr;
3058 if (D->doesThisDeclarationHaveABody() &&
3059 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003060 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00003061 D, const_cast<FunctionDecl *>(Definition));
3062 }
3063 FoundByLookup = FoundFunction;
3064 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003065 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003066
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003067 // FIXME: Check for overloading more carefully, e.g., by boosting
3068 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003069
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003070 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003071 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003072 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003073
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003074 // Complain about inconsistent function types.
3075 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003076 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003077 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003078 diag::note_odr_value_here)
3079 << FoundFunction->getType();
3080 }
3081 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003082
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003083 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003084 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003085
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003086 if (!ConflictingDecls.empty()) {
3087 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003088 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003089 ConflictingDecls.size());
3090 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003091 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003092 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003093 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003094
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003095 DeclarationNameInfo NameInfo(Name, Loc);
3096 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003097 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3098 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003099
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003100 QualType FromTy = D->getType();
3101 bool usedDifferentExceptionSpec = false;
3102
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003103 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003104 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3105 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3106 // FunctionDecl that we are importing the FunctionProtoType for.
3107 // To avoid an infinite recursion when importing, create the FunctionDecl
3108 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003109 if (FromEPI.ExceptionSpec.SourceDecl ||
3110 FromEPI.ExceptionSpec.SourceTemplate ||
3111 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003112 FunctionProtoType::ExtProtoInfo DefaultEPI;
3113 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003114 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003115 usedDifferentExceptionSpec = true;
3116 }
3117 }
3118
Balazs Keri3b30d652018-10-19 13:32:20 +00003119 QualType T;
3120 TypeSourceInfo *TInfo;
3121 SourceLocation ToInnerLocStart, ToEndLoc;
3122 NestedNameSpecifierLoc ToQualifierLoc;
3123 if (auto Imp = importSeq(
3124 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3125 D->getQualifierLoc(), D->getEndLoc()))
3126 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3127 else
3128 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003129
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003130 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003131 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003132 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003133 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3134 Parameters.push_back(*ToPOrErr);
3135 else
3136 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003137 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003138
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003139 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003140 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003141 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003142 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003143 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3144 ToInnerLocStart, NameInfo, T, TInfo,
3145 FromConstructor->isExplicit(),
3146 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003147 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003148 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003149 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003150 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3151 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3152 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003153 return ToFunction;
3154 } else if (CXXConversionDecl *FromConversion =
3155 dyn_cast<CXXConversionDecl>(D)) {
3156 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3157 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003158 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003159 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3160 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003161 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003162 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3163 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003164 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003165 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3166 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003167 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003168 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003169 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003170 D->getStorageClass(), D->isInlineSpecified(),
3171 D->hasWrittenPrototype(), D->isConstexpr()))
3172 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003173 }
John McCall3e11ebe2010-03-15 10:12:16 +00003174
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003175 // Connect the redecl chain.
3176 if (FoundByLookup) {
3177 auto *Recent = const_cast<FunctionDecl *>(
3178 FoundByLookup->getMostRecentDecl());
3179 ToFunction->setPreviousDecl(Recent);
3180 }
3181
3182 // Import Ctor initializers.
3183 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3184 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3185 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3186 // Import first, then allocate memory and copy if there was no error.
3187 if (Error Err = ImportContainerChecked(
3188 FromConstructor->inits(), CtorInitializers))
3189 return std::move(Err);
3190 auto **Memory =
3191 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3192 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3193 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3194 ToCtor->setCtorInitializers(Memory);
3195 ToCtor->setNumCtorInitializers(NumInitializers);
3196 }
3197 }
3198
Balazs Keri3b30d652018-10-19 13:32:20 +00003199 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003200 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003201 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003202 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3203 ToFunction->setTrivial(D->isTrivial());
3204 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003205 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003206
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003207 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003208 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003209 Param->setOwningFunction(ToFunction);
3210 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003211 }
David Blaikie9c70e042011-09-21 18:16:56 +00003212 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003213
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003214 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3215 // params it refers to.
3216 if (TInfo) {
3217 if (auto ProtoLoc =
3218 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3219 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3220 ProtoLoc.setParam(I, Parameters[I]);
3221 }
3222 }
3223
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003224 if (usedDifferentExceptionSpec) {
3225 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003226 if (ExpectedType TyOrErr = import(D->getType()))
3227 ToFunction->setType(*TyOrErr);
3228 else
3229 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003230 }
3231
Balazs Keria35798d2018-07-17 09:52:41 +00003232 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003233 if (FromFT) {
3234 auto ToFTOrErr = import(FromFT);
3235 if (!ToFTOrErr)
3236 return ToFTOrErr.takeError();
3237 }
Balazs Keria35798d2018-07-17 09:52:41 +00003238
Gabor Marton5254e642018-06-27 13:32:50 +00003239 if (D->doesThisDeclarationHaveABody()) {
3240 if (Stmt *FromBody = D->getBody()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003241 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3242 ToFunction->setBody(*ToBodyOrErr);
3243 else
3244 return ToBodyOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00003245 }
3246 }
3247
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003248 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003249
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003250 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003251 if (Error Err = ImportTemplateInformation(D, ToFunction))
3252 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003253
Gabor Marton5254e642018-06-27 13:32:50 +00003254 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3255
3256 // TODO Can we generalize this approach to other AST nodes as well?
3257 if (D->getDeclContext()->containsDeclAndLoad(D))
3258 DC->addDeclInternal(ToFunction);
3259 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003260 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003261
Gabor Marton5254e642018-06-27 13:32:50 +00003262 // Friend declaration's lexical context is the befriending class, but the
3263 // semantic context is the enclosing scope of the befriending class.
3264 // We want the friend functions to be found in the semantic context by lookup.
3265 // FIXME should we handle this generically in VisitFriendDecl?
3266 // In Other cases when LexicalDC != DC we don't want it to be added,
3267 // e.g out-of-class definitions like void B::f() {} .
3268 if (LexicalDC != DC && IsFriend) {
3269 DC->makeDeclVisibleInContext(ToFunction);
3270 }
3271
Gabor Marton7a0841e2018-10-29 10:18:28 +00003272 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3273 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3274
Gabor Marton5254e642018-06-27 13:32:50 +00003275 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003276 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3277 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3278 if (!ToRedeclOrErr)
3279 return ToRedeclOrErr.takeError();
3280 }
Gabor Marton5254e642018-06-27 13:32:50 +00003281
Douglas Gregor43f54792010-02-17 02:12:47 +00003282 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003283}
3284
Balazs Keri3b30d652018-10-19 13:32:20 +00003285ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003286 return VisitFunctionDecl(D);
3287}
3288
Balazs Keri3b30d652018-10-19 13:32:20 +00003289ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003290 return VisitCXXMethodDecl(D);
3291}
3292
Balazs Keri3b30d652018-10-19 13:32:20 +00003293ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003294 return VisitCXXMethodDecl(D);
3295}
3296
Balazs Keri3b30d652018-10-19 13:32:20 +00003297ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003298 return VisitCXXMethodDecl(D);
3299}
3300
Balazs Keri3b30d652018-10-19 13:32:20 +00003301ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003302 // Import the major distinguishing characteristics of a variable.
3303 DeclContext *DC, *LexicalDC;
3304 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003305 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003306 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003307 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3308 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003309 if (ToD)
3310 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003311
Fangrui Song6907ce22018-07-30 19:24:48 +00003312 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003313 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003314 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003315 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003316 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003317 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003318 if (!Name &&
3319 ASTImporter::getFieldIndex(D) !=
3320 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003321 continue;
3322
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003323 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003324 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003325 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003326 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3327 // initializer of a FieldDecl might not had been instantiated in the
3328 // "To" context. However, the "From" context might instantiated that,
3329 // thus we have to merge that.
3330 if (Expr *FromInitializer = D->getInClassInitializer()) {
3331 // We don't have yet the initializer set.
3332 if (FoundField->hasInClassInitializer() &&
3333 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003334 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3335 FoundField->setInClassInitializer(*ToInitializerOrErr);
3336 else {
3337 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003338 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003339 // FIXME: warning message?
3340 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003341 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003342 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003343 }
3344 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003345 return FoundField;
3346 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003347
Balazs Keri3b30d652018-10-19 13:32:20 +00003348 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003349 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3350 << Name << D->getType() << FoundField->getType();
3351 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3352 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003353
3354 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003355 }
3356 }
3357
Balazs Keri3b30d652018-10-19 13:32:20 +00003358 QualType ToType;
3359 TypeSourceInfo *ToTInfo;
3360 Expr *ToBitWidth;
3361 SourceLocation ToInnerLocStart;
3362 Expr *ToInitializer;
3363 if (auto Imp = importSeq(
3364 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3365 D->getInnerLocStart(), D->getInClassInitializer()))
3366 std::tie(
3367 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3368 else
3369 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003370
Gabor Marton26f72a92018-07-12 09:42:05 +00003371 FieldDecl *ToField;
3372 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003373 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3374 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3375 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003376 return ToField;
3377
Douglas Gregordd483172010-02-22 17:42:47 +00003378 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003379 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003380 if (ToInitializer)
3381 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003382 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003383 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003384 return ToField;
3385}
3386
Balazs Keri3b30d652018-10-19 13:32:20 +00003387ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003388 // Import the major distinguishing characteristics of a variable.
3389 DeclContext *DC, *LexicalDC;
3390 DeclarationName Name;
3391 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003392 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003393 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3394 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003395 if (ToD)
3396 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003397
Fangrui Song6907ce22018-07-30 19:24:48 +00003398 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003399 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003400 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003401 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003402 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003403 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003404 if (!Name &&
3405 ASTImporter::getFieldIndex(D) !=
3406 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003407 continue;
3408
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003409 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003410 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003411 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003412 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003413 return FoundField;
3414 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003415
3416 // If there are more anonymous fields to check, continue.
3417 if (!Name && I < N-1)
3418 continue;
3419
Balazs Keri3b30d652018-10-19 13:32:20 +00003420 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003421 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3422 << Name << D->getType() << FoundField->getType();
3423 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3424 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003425
3426 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003427 }
3428 }
3429
Francois Pichet783dd6e2010-11-21 06:08:52 +00003430 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003431 auto TypeOrErr = import(D->getType());
3432 if (!TypeOrErr)
3433 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003434
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003435 auto **NamedChain =
3436 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003437
3438 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003439 for (auto *PI : D->chain())
3440 if (Expected<NamedDecl *> ToD = import(PI))
3441 NamedChain[i++] = *ToD;
3442 else
3443 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003444
Gabor Marton26f72a92018-07-12 09:42:05 +00003445 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3446 IndirectFieldDecl *ToIndirectField;
3447 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003448 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003449 // FIXME here we leak `NamedChain` which is allocated before
3450 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003451
Balazs Keri3b30d652018-10-19 13:32:20 +00003452 for (const auto *Attr : D->attrs())
3453 ToIndirectField->addAttr(Importer.Import(Attr));
Aaron Ballman260995b2014-10-15 16:58:18 +00003454
Francois Pichet783dd6e2010-11-21 06:08:52 +00003455 ToIndirectField->setAccess(D->getAccess());
3456 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003457 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003458 return ToIndirectField;
3459}
3460
Balazs Keri3b30d652018-10-19 13:32:20 +00003461ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003462 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003463 DeclContext *DC, *LexicalDC;
3464 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3465 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003466
3467 // Determine whether we've already imported this decl.
3468 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3469 auto *RD = cast<CXXRecordDecl>(DC);
3470 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003471
3472 while (ImportedFriend) {
3473 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003474 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3475 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003476 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003477
3478 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3479 if (Importer.IsStructurallyEquivalent(
3480 D->getFriendType()->getType(),
3481 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003482 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003483 }
3484 ImportedFriend = ImportedFriend->getNextFriend();
3485 }
3486
3487 // Not found. Create it.
3488 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003489 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003490 NamedDecl *ToFriendD;
3491 if (Error Err = importInto(ToFriendD, FriendD))
3492 return std::move(Err);
3493
3494 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003495 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3496 ToFriendD->setObjectOfFriendDecl(false);
3497
3498 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003499 } else { // The friend is a type, not a decl.
3500 if (auto TSIOrErr = import(D->getFriendType()))
3501 ToFU = *TSIOrErr;
3502 else
3503 return TSIOrErr.takeError();
3504 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003505
3506 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003507 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003508 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003509 if (auto ListOrErr = ImportTemplateParameterList(FromTPLists[I]))
3510 ToTPLists[I] = *ListOrErr;
3511 else
3512 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003513 }
3514
Balazs Keri3b30d652018-10-19 13:32:20 +00003515 auto LocationOrErr = import(D->getLocation());
3516 if (!LocationOrErr)
3517 return LocationOrErr.takeError();
3518 auto FriendLocOrErr = import(D->getFriendLoc());
3519 if (!FriendLocOrErr)
3520 return FriendLocOrErr.takeError();
3521
Gabor Marton26f72a92018-07-12 09:42:05 +00003522 FriendDecl *FrD;
3523 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003524 *LocationOrErr, ToFU,
3525 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003526 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003527
3528 FrD->setAccess(D->getAccess());
3529 FrD->setLexicalDeclContext(LexicalDC);
3530 LexicalDC->addDeclInternal(FrD);
3531 return FrD;
3532}
3533
Balazs Keri3b30d652018-10-19 13:32:20 +00003534ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003535 // Import the major distinguishing characteristics of an ivar.
3536 DeclContext *DC, *LexicalDC;
3537 DeclarationName Name;
3538 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003539 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003540 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3541 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003542 if (ToD)
3543 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003544
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003545 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003546 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003547 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003548 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003549 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003550 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003551 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003552 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003553 return FoundIvar;
3554 }
3555
3556 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3557 << Name << D->getType() << FoundIvar->getType();
3558 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3559 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003560
3561 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003562 }
3563 }
3564
Balazs Keri3b30d652018-10-19 13:32:20 +00003565 QualType ToType;
3566 TypeSourceInfo *ToTypeSourceInfo;
3567 Expr *ToBitWidth;
3568 SourceLocation ToInnerLocStart;
3569 if (auto Imp = importSeq(
3570 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3571 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3572 else
3573 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003574
Gabor Marton26f72a92018-07-12 09:42:05 +00003575 ObjCIvarDecl *ToIvar;
3576 if (GetImportedOrCreateDecl(
3577 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003578 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3579 ToType, ToTypeSourceInfo,
3580 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003581 return ToIvar;
3582
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003583 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003584 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003585 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003586}
3587
Balazs Keri3b30d652018-10-19 13:32:20 +00003588ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003589
3590 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3591 auto RedeclIt = Redecls.begin();
3592 // Import the first part of the decl chain. I.e. import all previous
3593 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003594 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3595 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3596 if (!RedeclOrErr)
3597 return RedeclOrErr.takeError();
3598 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003599 assert(*RedeclIt == D);
3600
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003601 // Import the major distinguishing characteristics of a variable.
3602 DeclContext *DC, *LexicalDC;
3603 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003604 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003605 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003606 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3607 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003608 if (ToD)
3609 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003610
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003611 // Try to find a variable in our own ("to") context with the same name and
3612 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003613 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003614 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003615 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003616 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003617 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003618 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003619 for (auto *FoundDecl : FoundDecls) {
3620 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003621 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003622
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003623 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003624 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003625 if (FoundVar->hasExternalFormalLinkage() &&
3626 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003627 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003628 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003629
3630 // The VarDecl in the "From" context has a definition, but in the
3631 // "To" context we already have a definition.
3632 VarDecl *FoundDef = FoundVar->getDefinition();
3633 if (D->isThisDeclarationADefinition() && FoundDef)
3634 // FIXME Check for ODR error if the two definitions have
3635 // different initializers?
3636 return Importer.MapImported(D, FoundDef);
3637
3638 // The VarDecl in the "From" context has an initializer, but in the
3639 // "To" context we already have an initializer.
3640 const VarDecl *FoundDInit = nullptr;
3641 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3642 // FIXME Diagnose ODR error if the two initializers are different?
3643 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3644
3645 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003646 break;
3647 }
3648
Douglas Gregor56521c52010-02-12 17:23:39 +00003649 const ArrayType *FoundArray
3650 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3651 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003652 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003653 if (FoundArray && TArray) {
3654 if (isa<IncompleteArrayType>(FoundArray) &&
3655 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003656 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003657 if (auto TyOrErr = import(D->getType()))
3658 FoundVar->setType(*TyOrErr);
3659 else
3660 return TyOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003661
Gabor Martonac3a5d62018-09-17 12:04:52 +00003662 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003663 break;
3664 } else if (isa<IncompleteArrayType>(TArray) &&
3665 isa<ConstantArrayType>(FoundArray)) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003666 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003667 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003668 }
3669 }
3670
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003671 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003672 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003673 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3674 << FoundVar->getType();
3675 }
3676 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003677
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003678 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003679 }
3680
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003681 if (!ConflictingDecls.empty()) {
3682 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003683 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003684 ConflictingDecls.size());
3685 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003686 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003687 }
3688 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003689
Balazs Keri3b30d652018-10-19 13:32:20 +00003690 QualType ToType;
3691 TypeSourceInfo *ToTypeSourceInfo;
3692 SourceLocation ToInnerLocStart;
3693 NestedNameSpecifierLoc ToQualifierLoc;
3694 if (auto Imp = importSeq(
3695 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3696 D->getQualifierLoc()))
3697 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3698 else
3699 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003700
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003701 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003702 VarDecl *ToVar;
3703 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003704 ToInnerLocStart, Loc,
3705 Name.getAsIdentifierInfo(),
3706 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003707 D->getStorageClass()))
3708 return ToVar;
3709
Balazs Keri3b30d652018-10-19 13:32:20 +00003710 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003711 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003712 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003713
Gabor Martonac3a5d62018-09-17 12:04:52 +00003714 if (FoundByLookup) {
3715 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3716 ToVar->setPreviousDecl(Recent);
3717 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003718
Balazs Keri3b30d652018-10-19 13:32:20 +00003719 if (Error Err = ImportInitializer(D, ToVar))
3720 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003721
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003722 if (D->isConstexpr())
3723 ToVar->setConstexpr(true);
3724
Gabor Martonac3a5d62018-09-17 12:04:52 +00003725 if (D->getDeclContext()->containsDeclAndLoad(D))
3726 DC->addDeclInternal(ToVar);
3727 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3728 LexicalDC->addDeclInternal(ToVar);
3729
3730 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003731 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3732 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3733 if (!RedeclOrErr)
3734 return RedeclOrErr.takeError();
3735 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003736
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003737 return ToVar;
3738}
3739
Balazs Keri3b30d652018-10-19 13:32:20 +00003740ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003741 // Parameters are created in the translation unit's context, then moved
3742 // into the function declaration's context afterward.
3743 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003744
Balazs Keri3b30d652018-10-19 13:32:20 +00003745 DeclarationName ToDeclName;
3746 SourceLocation ToLocation;
3747 QualType ToType;
3748 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3749 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3750 else
3751 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003752
Douglas Gregor8b228d72010-02-17 21:22:52 +00003753 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003754 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003755 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3756 ToLocation, ToDeclName.getAsIdentifierInfo(),
3757 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003758 return ToParm;
3759 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003760}
3761
Balazs Keri3b30d652018-10-19 13:32:20 +00003762ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003763 // Parameters are created in the translation unit's context, then moved
3764 // into the function declaration's context afterward.
3765 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003766
Balazs Keri3b30d652018-10-19 13:32:20 +00003767 DeclarationName ToDeclName;
3768 SourceLocation ToLocation, ToInnerLocStart;
3769 QualType ToType;
3770 TypeSourceInfo *ToTypeSourceInfo;
3771 if (auto Imp = importSeq(
3772 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3773 D->getTypeSourceInfo()))
3774 std::tie(
3775 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3776 ToTypeSourceInfo) = *Imp;
3777 else
3778 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003779
Gabor Marton26f72a92018-07-12 09:42:05 +00003780 ParmVarDecl *ToParm;
3781 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003782 ToInnerLocStart, ToLocation,
3783 ToDeclName.getAsIdentifierInfo(), ToType,
3784 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003785 /*DefaultArg*/ nullptr))
3786 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003787
3788 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003789 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003790 ToParm->setKNRPromoted(D->isKNRPromoted());
3791
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003792 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003793 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3794 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3795 else
3796 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003797 } else if (D->hasUnparsedDefaultArg()) {
3798 ToParm->setUnparsedDefaultArg();
3799 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003800 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3801 ToParm->setDefaultArg(*ToDefArgOrErr);
3802 else
3803 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003804 }
Sean Callanan59721b32015-04-28 18:41:46 +00003805
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003806 if (D->isObjCMethodParameter()) {
3807 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3808 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3809 } else {
3810 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3811 D->getFunctionScopeIndex());
3812 }
3813
Gabor Marton26f72a92018-07-12 09:42:05 +00003814 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003815}
3816
Balazs Keri3b30d652018-10-19 13:32:20 +00003817ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003818 // Import the major distinguishing characteristics of a method.
3819 DeclContext *DC, *LexicalDC;
3820 DeclarationName Name;
3821 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003822 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003823 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3824 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003825 if (ToD)
3826 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003827
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003828 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003829 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003830 for (auto *FoundDecl : FoundDecls) {
3831 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003832 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3833 continue;
3834
3835 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003836 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3837 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003838 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003839 << D->isInstanceMethod() << Name << D->getReturnType()
3840 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003841 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003842 diag::note_odr_objc_method_here)
3843 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003844
3845 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003846 }
3847
3848 // Check the number of parameters.
3849 if (D->param_size() != FoundMethod->param_size()) {
3850 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3851 << D->isInstanceMethod() << Name
3852 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003853 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003854 diag::note_odr_objc_method_here)
3855 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003856
3857 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003858 }
3859
3860 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003861 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003862 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3863 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003864 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003865 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003866 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003867 diag::err_odr_objc_method_param_type_inconsistent)
3868 << D->isInstanceMethod() << Name
3869 << (*P)->getType() << (*FoundP)->getType();
3870 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3871 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003872
3873 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003874 }
3875 }
3876
3877 // Check variadic/non-variadic.
3878 // Check the number of parameters.
3879 if (D->isVariadic() != FoundMethod->isVariadic()) {
3880 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3881 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003882 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003883 diag::note_odr_objc_method_here)
3884 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003885
3886 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003887 }
3888
3889 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003890 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003891 }
3892 }
3893
Balazs Keri3b30d652018-10-19 13:32:20 +00003894 SourceLocation ToEndLoc;
3895 QualType ToReturnType;
3896 TypeSourceInfo *ToReturnTypeSourceInfo;
3897 if (auto Imp = importSeq(
3898 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3899 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3900 else
3901 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003902
Gabor Marton26f72a92018-07-12 09:42:05 +00003903 ObjCMethodDecl *ToMethod;
3904 if (GetImportedOrCreateDecl(
3905 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003906 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3907 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003908 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3909 D->getImplementationControl(), D->hasRelatedResultType()))
3910 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003911
3912 // FIXME: When we decide to merge method definitions, we'll need to
3913 // deal with implicit parameters.
3914
3915 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003916 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003917 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003918 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3919 ToParams.push_back(*ToPOrErr);
3920 else
3921 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003922 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003923
Douglas Gregor43f54792010-02-17 02:12:47 +00003924 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003925 for (auto *ToParam : ToParams) {
3926 ToParam->setOwningFunction(ToMethod);
3927 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003928 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003929
Balazs Keri3b30d652018-10-19 13:32:20 +00003930 SmallVector<SourceLocation, 12> FromSelLocs;
3931 D->getSelectorLocs(FromSelLocs);
3932 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3933 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3934 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003935
Balazs Keri3b30d652018-10-19 13:32:20 +00003936 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003937
3938 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003939 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003940 return ToMethod;
3941}
3942
Balazs Keri3b30d652018-10-19 13:32:20 +00003943ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003944 // Import the major distinguishing characteristics of a category.
3945 DeclContext *DC, *LexicalDC;
3946 DeclarationName Name;
3947 SourceLocation Loc;
3948 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003949 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3950 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003951 if (ToD)
3952 return ToD;
3953
Balazs Keri3b30d652018-10-19 13:32:20 +00003954 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3955 TypeSourceInfo *ToTypeSourceInfo;
3956 if (auto Imp = importSeq(
3957 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3958 D->getTypeSourceInfo()))
3959 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3960 else
3961 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003962
Gabor Marton26f72a92018-07-12 09:42:05 +00003963 ObjCTypeParamDecl *Result;
3964 if (GetImportedOrCreateDecl(
3965 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003966 ToVarianceLoc, D->getIndex(),
3967 ToLocation, Name.getAsIdentifierInfo(),
3968 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003969 return Result;
3970
Douglas Gregor85f3f952015-07-07 03:57:15 +00003971 Result->setLexicalDeclContext(LexicalDC);
3972 return Result;
3973}
3974
Balazs Keri3b30d652018-10-19 13:32:20 +00003975ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003976 // Import the major distinguishing characteristics of a category.
3977 DeclContext *DC, *LexicalDC;
3978 DeclarationName Name;
3979 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003980 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003981 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3982 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003983 if (ToD)
3984 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003985
Balazs Keri3b30d652018-10-19 13:32:20 +00003986 ObjCInterfaceDecl *ToInterface;
3987 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3988 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003989
Douglas Gregor84c51c32010-02-18 01:47:50 +00003990 // Determine if we've already encountered this category.
3991 ObjCCategoryDecl *MergeWithCategory
3992 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3993 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3994 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003995 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3996 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3997 if (auto Imp = importSeq(
3998 D->getAtStartLoc(), D->getCategoryNameLoc(),
3999 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4000 std::tie(
4001 ToAtStartLoc, ToCategoryNameLoc,
4002 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4003 else
4004 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004005
4006 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004007 ToAtStartLoc, Loc,
4008 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004009 Name.getAsIdentifierInfo(), ToInterface,
4010 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004011 ToIvarLBraceLoc,
4012 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004013 return ToCategory;
4014
Douglas Gregor84c51c32010-02-18 01:47:50 +00004015 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004016 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004017 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004018 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004019 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4020 ToCategory->setTypeParamList(*PListOrErr);
4021 else
4022 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004023
Douglas Gregor84c51c32010-02-18 01:47:50 +00004024 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004025 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4026 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004027 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4028 = D->protocol_loc_begin();
4029 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4030 FromProtoEnd = D->protocol_end();
4031 FromProto != FromProtoEnd;
4032 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004033 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4034 Protocols.push_back(*ToProtoOrErr);
4035 else
4036 return ToProtoOrErr.takeError();
4037
4038 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4039 ProtocolLocs.push_back(*ToProtoLocOrErr);
4040 else
4041 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004042 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004043
Douglas Gregor84c51c32010-02-18 01:47:50 +00004044 // FIXME: If we're merging, make sure that the protocol list is the same.
4045 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4046 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004047
Douglas Gregor84c51c32010-02-18 01:47:50 +00004048 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004049 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004050 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004051
Douglas Gregor84c51c32010-02-18 01:47:50 +00004052 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004053 if (Error Err = ImportDeclContext(D))
4054 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004055
Douglas Gregor84c51c32010-02-18 01:47:50 +00004056 // If we have an implementation, import it as well.
4057 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004058 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4059 import(D->getImplementation()))
4060 ToCategory->setImplementation(*ToImplOrErr);
4061 else
4062 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004063 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004064
Douglas Gregor84c51c32010-02-18 01:47:50 +00004065 return ToCategory;
4066}
4067
Balazs Keri3b30d652018-10-19 13:32:20 +00004068Error ASTNodeImporter::ImportDefinition(
4069 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004070 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004071 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004072 if (Error Err = ImportDeclContext(From))
4073 return Err;
4074 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004075 }
4076
4077 // Start the protocol definition
4078 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004079
Douglas Gregor2aa53772012-01-24 17:42:07 +00004080 // Import protocols
4081 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4082 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004083 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4084 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004085 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4086 FromProtoEnd = From->protocol_end();
4087 FromProto != FromProtoEnd;
4088 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004089 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4090 Protocols.push_back(*ToProtoOrErr);
4091 else
4092 return ToProtoOrErr.takeError();
4093
4094 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4095 ProtocolLocs.push_back(*ToProtoLocOrErr);
4096 else
4097 return ToProtoLocOrErr.takeError();
4098
Douglas Gregor2aa53772012-01-24 17:42:07 +00004099 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004100
Douglas Gregor2aa53772012-01-24 17:42:07 +00004101 // FIXME: If we're merging, make sure that the protocol list is the same.
4102 To->setProtocolList(Protocols.data(), Protocols.size(),
4103 ProtocolLocs.data(), Importer.getToContext());
4104
Douglas Gregor2e15c842012-02-01 21:00:38 +00004105 if (shouldForceImportDeclContext(Kind)) {
4106 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004107 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4108 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004109 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004110 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004111}
4112
Balazs Keri3b30d652018-10-19 13:32:20 +00004113ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004114 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004115 // from, but this particular declaration is not that definition, import the
4116 // definition and map to that.
4117 ObjCProtocolDecl *Definition = D->getDefinition();
4118 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004119 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4120 return Importer.MapImported(D, *ImportedDefOrErr);
4121 else
4122 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004123 }
4124
Douglas Gregor84c51c32010-02-18 01:47:50 +00004125 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004126 DeclContext *DC, *LexicalDC;
4127 DeclarationName Name;
4128 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004129 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004130 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4131 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004132 if (ToD)
4133 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004134
Craig Topper36250ad2014-05-12 05:36:57 +00004135 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004136 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004137 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004138 for (auto *FoundDecl : FoundDecls) {
4139 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004140 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004141
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004142 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004143 break;
4144 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004145
Douglas Gregor98d156a2010-02-17 16:12:00 +00004146 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004147 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004148 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4149 if (!ToAtBeginLocOrErr)
4150 return ToAtBeginLocOrErr.takeError();
4151
Gabor Marton26f72a92018-07-12 09:42:05 +00004152 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4153 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004154 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004155 /*PrevDecl=*/nullptr))
4156 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004157 ToProto->setLexicalDeclContext(LexicalDC);
4158 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004159 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004160
4161 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004162
Balazs Keri3b30d652018-10-19 13:32:20 +00004163 if (D->isThisDeclarationADefinition())
4164 if (Error Err = ImportDefinition(D, ToProto))
4165 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004166
Douglas Gregor98d156a2010-02-17 16:12:00 +00004167 return ToProto;
4168}
4169
Balazs Keri3b30d652018-10-19 13:32:20 +00004170ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4171 DeclContext *DC, *LexicalDC;
4172 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4173 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004174
Balazs Keri3b30d652018-10-19 13:32:20 +00004175 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4176 if (!ExternLocOrErr)
4177 return ExternLocOrErr.takeError();
4178
4179 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4180 if (!LangLocOrErr)
4181 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004182
4183 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004184
4185 LinkageSpecDecl *ToLinkageSpec;
4186 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004187 *ExternLocOrErr, *LangLocOrErr,
4188 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004189 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004190
4191 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004192 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4193 if (!RBraceLocOrErr)
4194 return RBraceLocOrErr.takeError();
4195 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004196 }
4197
4198 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4199 LexicalDC->addDeclInternal(ToLinkageSpec);
4200
Sean Callanan0aae0412014-12-10 00:00:37 +00004201 return ToLinkageSpec;
4202}
4203
Balazs Keri3b30d652018-10-19 13:32:20 +00004204ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004205 DeclContext *DC, *LexicalDC;
4206 DeclarationName Name;
4207 SourceLocation Loc;
4208 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004209 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4210 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004211 if (ToD)
4212 return ToD;
4213
Balazs Keri3b30d652018-10-19 13:32:20 +00004214 SourceLocation ToLoc, ToUsingLoc;
4215 NestedNameSpecifierLoc ToQualifierLoc;
4216 if (auto Imp = importSeq(
4217 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4218 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4219 else
4220 return Imp.takeError();
4221
4222 DeclarationNameInfo NameInfo(Name, ToLoc);
4223 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4224 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004225
Gabor Marton26f72a92018-07-12 09:42:05 +00004226 UsingDecl *ToUsing;
4227 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004228 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004229 D->hasTypename()))
4230 return ToUsing;
4231
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004232 ToUsing->setLexicalDeclContext(LexicalDC);
4233 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004234
4235 if (NamedDecl *FromPattern =
4236 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004237 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4238 Importer.getToContext().setInstantiatedFromUsingDecl(
4239 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004240 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004241 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004242 }
4243
Balazs Keri3b30d652018-10-19 13:32:20 +00004244 for (UsingShadowDecl *FromShadow : D->shadows()) {
4245 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4246 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004247 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004248 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004249 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004250 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004251 }
4252 return ToUsing;
4253}
4254
Balazs Keri3b30d652018-10-19 13:32:20 +00004255ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004256 DeclContext *DC, *LexicalDC;
4257 DeclarationName Name;
4258 SourceLocation Loc;
4259 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004260 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4261 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004262 if (ToD)
4263 return ToD;
4264
Balazs Keri3b30d652018-10-19 13:32:20 +00004265 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4266 if (!ToUsingOrErr)
4267 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004268
Balazs Keri3b30d652018-10-19 13:32:20 +00004269 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4270 if (!ToTargetOrErr)
4271 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004272
Gabor Marton26f72a92018-07-12 09:42:05 +00004273 UsingShadowDecl *ToShadow;
4274 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004275 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004276 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004277
4278 ToShadow->setLexicalDeclContext(LexicalDC);
4279 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004280
4281 if (UsingShadowDecl *FromPattern =
4282 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004283 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4284 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4285 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004286 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004287 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004288 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004289 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004290 }
4291
4292 LexicalDC->addDeclInternal(ToShadow);
4293
4294 return ToShadow;
4295}
4296
Balazs Keri3b30d652018-10-19 13:32:20 +00004297ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004298 DeclContext *DC, *LexicalDC;
4299 DeclarationName Name;
4300 SourceLocation Loc;
4301 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004302 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4303 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004304 if (ToD)
4305 return ToD;
4306
Balazs Keri3b30d652018-10-19 13:32:20 +00004307 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4308 if (!ToComAncestorOrErr)
4309 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004310
Balazs Keri3b30d652018-10-19 13:32:20 +00004311 NamespaceDecl *ToNominatedNamespace;
4312 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4313 NestedNameSpecifierLoc ToQualifierLoc;
4314 if (auto Imp = importSeq(
4315 D->getNominatedNamespace(), D->getUsingLoc(),
4316 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4317 D->getIdentLocation()))
4318 std::tie(
4319 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4320 ToQualifierLoc, ToIdentLocation) = *Imp;
4321 else
4322 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004323
Gabor Marton26f72a92018-07-12 09:42:05 +00004324 UsingDirectiveDecl *ToUsingDir;
4325 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004326 ToUsingLoc,
4327 ToNamespaceKeyLocation,
4328 ToQualifierLoc,
4329 ToIdentLocation,
4330 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004331 return ToUsingDir;
4332
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004333 ToUsingDir->setLexicalDeclContext(LexicalDC);
4334 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004335
4336 return ToUsingDir;
4337}
4338
Balazs Keri3b30d652018-10-19 13:32:20 +00004339ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004340 UnresolvedUsingValueDecl *D) {
4341 DeclContext *DC, *LexicalDC;
4342 DeclarationName Name;
4343 SourceLocation Loc;
4344 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004345 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4346 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004347 if (ToD)
4348 return ToD;
4349
Balazs Keri3b30d652018-10-19 13:32:20 +00004350 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4351 NestedNameSpecifierLoc ToQualifierLoc;
4352 if (auto Imp = importSeq(
4353 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4354 D->getEllipsisLoc()))
4355 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4356 else
4357 return Imp.takeError();
4358
4359 DeclarationNameInfo NameInfo(Name, ToLoc);
4360 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4361 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004362
Gabor Marton26f72a92018-07-12 09:42:05 +00004363 UnresolvedUsingValueDecl *ToUsingValue;
4364 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004365 ToUsingLoc, ToQualifierLoc, NameInfo,
4366 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004367 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004368
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004369 ToUsingValue->setAccess(D->getAccess());
4370 ToUsingValue->setLexicalDeclContext(LexicalDC);
4371 LexicalDC->addDeclInternal(ToUsingValue);
4372
4373 return ToUsingValue;
4374}
4375
Balazs Keri3b30d652018-10-19 13:32:20 +00004376ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004377 UnresolvedUsingTypenameDecl *D) {
4378 DeclContext *DC, *LexicalDC;
4379 DeclarationName Name;
4380 SourceLocation Loc;
4381 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004382 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4383 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004384 if (ToD)
4385 return ToD;
4386
Balazs Keri3b30d652018-10-19 13:32:20 +00004387 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4388 NestedNameSpecifierLoc ToQualifierLoc;
4389 if (auto Imp = importSeq(
4390 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4391 D->getEllipsisLoc()))
4392 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4393 else
4394 return Imp.takeError();
4395
Gabor Marton26f72a92018-07-12 09:42:05 +00004396 UnresolvedUsingTypenameDecl *ToUsing;
4397 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004398 ToUsingLoc, ToTypenameLoc,
4399 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004400 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004401
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004402 ToUsing->setAccess(D->getAccess());
4403 ToUsing->setLexicalDeclContext(LexicalDC);
4404 LexicalDC->addDeclInternal(ToUsing);
4405
4406 return ToUsing;
4407}
4408
Balazs Keri3b30d652018-10-19 13:32:20 +00004409
4410Error ASTNodeImporter::ImportDefinition(
4411 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004412 if (To->getDefinition()) {
4413 // Check consistency of superclass.
4414 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4415 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004416 if (auto FromSuperOrErr = import(FromSuper))
4417 FromSuper = *FromSuperOrErr;
4418 else
4419 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004420 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004421
4422 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004423 if ((bool)FromSuper != (bool)ToSuper ||
4424 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004425 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004426 diag::err_odr_objc_superclass_inconsistent)
4427 << To->getDeclName();
4428 if (ToSuper)
4429 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4430 << To->getSuperClass()->getDeclName();
4431 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004432 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004433 diag::note_odr_objc_missing_superclass);
4434 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004435 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004436 diag::note_odr_objc_superclass)
4437 << From->getSuperClass()->getDeclName();
4438 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004439 Importer.FromDiag(From->getLocation(),
4440 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004441 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004442
Douglas Gregor2e15c842012-02-01 21:00:38 +00004443 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004444 if (Error Err = ImportDeclContext(From))
4445 return Err;
4446 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004447 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004448
Douglas Gregor2aa53772012-01-24 17:42:07 +00004449 // Start the definition.
4450 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004451
Douglas Gregor2aa53772012-01-24 17:42:07 +00004452 // If this class has a superclass, import it.
4453 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004454 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4455 To->setSuperClass(*SuperTInfoOrErr);
4456 else
4457 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004458 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004459
Douglas Gregor2aa53772012-01-24 17:42:07 +00004460 // Import protocols
4461 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4462 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004463 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4464 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004465
Douglas Gregor2aa53772012-01-24 17:42:07 +00004466 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4467 FromProtoEnd = From->protocol_end();
4468 FromProto != FromProtoEnd;
4469 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004470 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4471 Protocols.push_back(*ToProtoOrErr);
4472 else
4473 return ToProtoOrErr.takeError();
4474
4475 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4476 ProtocolLocs.push_back(*ToProtoLocOrErr);
4477 else
4478 return ToProtoLocOrErr.takeError();
4479
Douglas Gregor2aa53772012-01-24 17:42:07 +00004480 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004481
Douglas Gregor2aa53772012-01-24 17:42:07 +00004482 // FIXME: If we're merging, make sure that the protocol list is the same.
4483 To->setProtocolList(Protocols.data(), Protocols.size(),
4484 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004485
Douglas Gregor2aa53772012-01-24 17:42:07 +00004486 // Import categories. When the categories themselves are imported, they'll
4487 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004488 for (auto *Cat : From->known_categories()) {
4489 auto ToCatOrErr = import(Cat);
4490 if (!ToCatOrErr)
4491 return ToCatOrErr.takeError();
4492 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004493
Douglas Gregor2aa53772012-01-24 17:42:07 +00004494 // If we have an @implementation, import it as well.
4495 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004496 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4497 import(From->getImplementation()))
4498 To->setImplementation(*ToImplOrErr);
4499 else
4500 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004501 }
4502
Douglas Gregor2e15c842012-02-01 21:00:38 +00004503 if (shouldForceImportDeclContext(Kind)) {
4504 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004505 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4506 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004507 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004508 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004509}
4510
Balazs Keri3b30d652018-10-19 13:32:20 +00004511Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004512ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4513 if (!list)
4514 return nullptr;
4515
4516 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004517 for (auto *fromTypeParam : *list) {
4518 if (auto toTypeParamOrErr = import(fromTypeParam))
4519 toTypeParams.push_back(*toTypeParamOrErr);
4520 else
4521 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004522 }
4523
Balazs Keri3b30d652018-10-19 13:32:20 +00004524 auto LAngleLocOrErr = import(list->getLAngleLoc());
4525 if (!LAngleLocOrErr)
4526 return LAngleLocOrErr.takeError();
4527
4528 auto RAngleLocOrErr = import(list->getRAngleLoc());
4529 if (!RAngleLocOrErr)
4530 return RAngleLocOrErr.takeError();
4531
Douglas Gregor85f3f952015-07-07 03:57:15 +00004532 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004533 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004534 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004535 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004536}
4537
Balazs Keri3b30d652018-10-19 13:32:20 +00004538ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004539 // If this class has a definition in the translation unit we're coming from,
4540 // but this particular declaration is not that definition, import the
4541 // definition and map to that.
4542 ObjCInterfaceDecl *Definition = D->getDefinition();
4543 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004544 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4545 return Importer.MapImported(D, *ImportedDefOrErr);
4546 else
4547 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004548 }
4549
Douglas Gregor45635322010-02-16 01:20:57 +00004550 // Import the major distinguishing characteristics of an @interface.
4551 DeclContext *DC, *LexicalDC;
4552 DeclarationName Name;
4553 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004554 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004555 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4556 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004557 if (ToD)
4558 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004559
Douglas Gregor2aa53772012-01-24 17:42:07 +00004560 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004561 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004562 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004563 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004564 for (auto *FoundDecl : FoundDecls) {
4565 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004566 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004567
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004568 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004569 break;
4570 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004571
Douglas Gregor2aa53772012-01-24 17:42:07 +00004572 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004573 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004574 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004575 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4576 if (!AtBeginLocOrErr)
4577 return AtBeginLocOrErr.takeError();
4578
Gabor Marton26f72a92018-07-12 09:42:05 +00004579 if (GetImportedOrCreateDecl(
4580 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004581 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004582 /*TypeParamList=*/nullptr,
4583 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4584 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004585 ToIface->setLexicalDeclContext(LexicalDC);
4586 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004587 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004588 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004589 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004590 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004591 if (auto ToPListOrErr =
4592 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4593 ToIface->setTypeParamList(*ToPListOrErr);
4594 else
4595 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004596
Balazs Keri3b30d652018-10-19 13:32:20 +00004597 if (D->isThisDeclarationADefinition())
4598 if (Error Err = ImportDefinition(D, ToIface))
4599 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004600
Douglas Gregor98d156a2010-02-17 16:12:00 +00004601 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004602}
4603
Balazs Keri3b30d652018-10-19 13:32:20 +00004604ExpectedDecl
4605ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4606 ObjCCategoryDecl *Category;
4607 if (Error Err = importInto(Category, D->getCategoryDecl()))
4608 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004609
Douglas Gregor4da9d682010-12-07 15:32:12 +00004610 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4611 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004612 DeclContext *DC, *LexicalDC;
4613 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4614 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004615
Balazs Keri3b30d652018-10-19 13:32:20 +00004616 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4617 if (auto Imp = importSeq(
4618 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4619 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4620 else
4621 return Imp.takeError();
4622
Gabor Marton26f72a92018-07-12 09:42:05 +00004623 if (GetImportedOrCreateDecl(
4624 ToImpl, D, Importer.getToContext(), DC,
4625 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004626 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004627 return ToImpl;
4628
Balazs Keri3b30d652018-10-19 13:32:20 +00004629 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004630 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004631 Category->setImplementation(ToImpl);
4632 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004633
Gabor Marton26f72a92018-07-12 09:42:05 +00004634 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004635 if (Error Err = ImportDeclContext(D))
4636 return std::move(Err);
4637
Douglas Gregor4da9d682010-12-07 15:32:12 +00004638 return ToImpl;
4639}
4640
Balazs Keri3b30d652018-10-19 13:32:20 +00004641ExpectedDecl
4642ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004643 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004644 ObjCInterfaceDecl *Iface;
4645 if (Error Err = importInto(Iface, D->getClassInterface()))
4646 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004647
4648 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004649 ObjCInterfaceDecl *Super;
4650 if (Error Err = importInto(Super, D->getSuperClass()))
4651 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004652
4653 ObjCImplementationDecl *Impl = Iface->getImplementation();
4654 if (!Impl) {
4655 // We haven't imported an implementation yet. Create a new @implementation
4656 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004657 DeclContext *DC, *LexicalDC;
4658 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4659 return std::move(Err);
4660
4661 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4662 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4663 if (auto Imp = importSeq(
4664 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4665 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4666 std::tie(
4667 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4668 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4669 else
4670 return Imp.takeError();
4671
Gabor Marton26f72a92018-07-12 09:42:05 +00004672 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004673 DC, Iface, Super,
4674 ToLocation,
4675 ToAtStartLoc,
4676 ToSuperClassLoc,
4677 ToIvarLBraceLoc,
4678 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004679 return Impl;
4680
Balazs Keri3b30d652018-10-19 13:32:20 +00004681 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004682
Douglas Gregorda8025c2010-12-07 01:26:03 +00004683 // Associate the implementation with the class it implements.
4684 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004685 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004686 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004687 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004688
4689 // Verify that the existing @implementation has the same superclass.
4690 if ((Super && !Impl->getSuperClass()) ||
4691 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004692 (Super && Impl->getSuperClass() &&
4693 !declaresSameEntity(Super->getCanonicalDecl(),
4694 Impl->getSuperClass()))) {
4695 Importer.ToDiag(Impl->getLocation(),
4696 diag::err_odr_objc_superclass_inconsistent)
4697 << Iface->getDeclName();
4698 // FIXME: It would be nice to have the location of the superclass
4699 // below.
4700 if (Impl->getSuperClass())
4701 Importer.ToDiag(Impl->getLocation(),
4702 diag::note_odr_objc_superclass)
4703 << Impl->getSuperClass()->getDeclName();
4704 else
4705 Importer.ToDiag(Impl->getLocation(),
4706 diag::note_odr_objc_missing_superclass);
4707 if (D->getSuperClass())
4708 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004709 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004710 << D->getSuperClass()->getDeclName();
4711 else
4712 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004713 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004714
4715 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004716 }
4717 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004718
Douglas Gregorda8025c2010-12-07 01:26:03 +00004719 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004720 if (Error Err = ImportDeclContext(D))
4721 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004722
4723 return Impl;
4724}
4725
Balazs Keri3b30d652018-10-19 13:32:20 +00004726ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004727 // Import the major distinguishing characteristics of an @property.
4728 DeclContext *DC, *LexicalDC;
4729 DeclarationName Name;
4730 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004731 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004732 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4733 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004734 if (ToD)
4735 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004736
4737 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004738 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004739 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004740 for (auto *FoundDecl : FoundDecls) {
4741 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004742 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004743 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004744 FoundProp->getType())) {
4745 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4746 << Name << D->getType() << FoundProp->getType();
4747 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4748 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004749
4750 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004751 }
4752
4753 // FIXME: Check property attributes, getters, setters, etc.?
4754
4755 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004756 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004757 return FoundProp;
4758 }
4759 }
4760
Balazs Keri3b30d652018-10-19 13:32:20 +00004761 QualType ToType;
4762 TypeSourceInfo *ToTypeSourceInfo;
4763 SourceLocation ToAtLoc, ToLParenLoc;
4764 if (auto Imp = importSeq(
4765 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4766 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4767 else
4768 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004769
4770 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004771 ObjCPropertyDecl *ToProperty;
4772 if (GetImportedOrCreateDecl(
4773 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004774 Name.getAsIdentifierInfo(), ToAtLoc,
4775 ToLParenLoc, ToType,
4776 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004777 return ToProperty;
4778
Balazs Keri3b30d652018-10-19 13:32:20 +00004779 Selector ToGetterName, ToSetterName;
4780 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4781 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4782 ObjCIvarDecl *ToPropertyIvarDecl;
4783 if (auto Imp = importSeq(
4784 D->getGetterName(), D->getSetterName(),
4785 D->getGetterNameLoc(), D->getSetterNameLoc(),
4786 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4787 D->getPropertyIvarDecl()))
4788 std::tie(
4789 ToGetterName, ToSetterName,
4790 ToGetterNameLoc, ToSetterNameLoc,
4791 ToGetterMethodDecl, ToSetterMethodDecl,
4792 ToPropertyIvarDecl) = *Imp;
4793 else
4794 return Imp.takeError();
4795
Douglas Gregora11c4582010-02-17 18:02:10 +00004796 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004797 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004798
4799 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004800 ToProperty->setPropertyAttributesAsWritten(
4801 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004802 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4803 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4804 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4805 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4806 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004807 return ToProperty;
4808}
4809
Balazs Keri3b30d652018-10-19 13:32:20 +00004810ExpectedDecl
4811ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4812 ObjCPropertyDecl *Property;
4813 if (Error Err = importInto(Property, D->getPropertyDecl()))
4814 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004815
Balazs Keri3b30d652018-10-19 13:32:20 +00004816 DeclContext *DC, *LexicalDC;
4817 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4818 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004819
Balazs Keri3b30d652018-10-19 13:32:20 +00004820 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004821
4822 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004823 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004824 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4825 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004826
4827 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004828 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4829 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004830 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004831 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4832 if (auto Imp = importSeq(
4833 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4834 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4835 else
4836 return Imp.takeError();
4837
Gabor Marton26f72a92018-07-12 09:42:05 +00004838 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004839 ToBeginLoc,
4840 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004841 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004842 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004843 return ToImpl;
4844
Douglas Gregor14a49e22010-12-07 18:32:03 +00004845 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004846 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004847 } else {
4848 // Check that we have the same kind of property implementation (@synthesize
4849 // vs. @dynamic).
4850 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004851 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004852 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004853 << Property->getDeclName()
4854 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004855 == ObjCPropertyImplDecl::Dynamic);
4856 Importer.FromDiag(D->getLocation(),
4857 diag::note_odr_objc_property_impl_kind)
4858 << D->getPropertyDecl()->getDeclName()
4859 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004860
4861 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004862 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004863
4864 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004865 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4866 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004867 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004868 diag::err_odr_objc_synthesize_ivar_inconsistent)
4869 << Property->getDeclName()
4870 << ToImpl->getPropertyIvarDecl()->getDeclName()
4871 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004872 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004873 diag::note_odr_objc_synthesize_ivar_here)
4874 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004875
4876 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004877 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004878
Douglas Gregor14a49e22010-12-07 18:32:03 +00004879 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004880 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004881 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004882
Douglas Gregor14a49e22010-12-07 18:32:03 +00004883 return ToImpl;
4884}
4885
Balazs Keri3b30d652018-10-19 13:32:20 +00004886ExpectedDecl
4887ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004888 // For template arguments, we adopt the translation unit as our declaration
4889 // context. This context will be fixed when the actual template declaration
4890 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004891
Douglas Gregora082a492010-11-30 19:14:50 +00004892 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004893
4894 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4895 if (!BeginLocOrErr)
4896 return BeginLocOrErr.takeError();
4897
4898 ExpectedSLoc LocationOrErr = import(D->getLocation());
4899 if (!LocationOrErr)
4900 return LocationOrErr.takeError();
4901
Gabor Marton26f72a92018-07-12 09:42:05 +00004902 TemplateTypeParmDecl *ToD = nullptr;
4903 (void)GetImportedOrCreateDecl(
4904 ToD, D, Importer.getToContext(),
4905 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004906 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004907 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4908 D->wasDeclaredWithTypename(), D->isParameterPack());
4909 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004910}
4911
Balazs Keri3b30d652018-10-19 13:32:20 +00004912ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004913ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004914 DeclarationName ToDeclName;
4915 SourceLocation ToLocation, ToInnerLocStart;
4916 QualType ToType;
4917 TypeSourceInfo *ToTypeSourceInfo;
4918 if (auto Imp = importSeq(
4919 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4920 D->getInnerLocStart()))
4921 std::tie(
4922 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4923 ToInnerLocStart) = *Imp;
4924 else
4925 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004926
Douglas Gregora082a492010-11-30 19:14:50 +00004927 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004928
4929 NonTypeTemplateParmDecl *ToD = nullptr;
4930 (void)GetImportedOrCreateDecl(
4931 ToD, D, Importer.getToContext(),
4932 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004933 ToInnerLocStart, ToLocation, D->getDepth(),
4934 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4935 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004936 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004937}
4938
Balazs Keri3b30d652018-10-19 13:32:20 +00004939ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004940ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4941 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004942 auto NameOrErr = import(D->getDeclName());
4943 if (!NameOrErr)
4944 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004945
Douglas Gregora082a492010-11-30 19:14:50 +00004946 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004947 ExpectedSLoc LocationOrErr = import(D->getLocation());
4948 if (!LocationOrErr)
4949 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004950
Douglas Gregora082a492010-11-30 19:14:50 +00004951 // Import template parameters.
Balazs Keri3b30d652018-10-19 13:32:20 +00004952 auto TemplateParamsOrErr = ImportTemplateParameterList(
4953 D->getTemplateParameters());
4954 if (!TemplateParamsOrErr)
4955 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004956
Douglas Gregora082a492010-11-30 19:14:50 +00004957 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004958
4959 TemplateTemplateParmDecl *ToD = nullptr;
4960 (void)GetImportedOrCreateDecl(
4961 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004962 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4963 D->getDepth(), D->getPosition(), D->isParameterPack(),
4964 (*NameOrErr).getAsIdentifierInfo(),
4965 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004966 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004967}
4968
Gabor Marton9581c332018-05-23 13:53:36 +00004969// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4970// it has any definition in the redecl chain.
4971static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4972 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4973 if (!ToTemplatedDef)
4974 return nullptr;
4975 ClassTemplateDecl *TemplateWithDef =
4976 ToTemplatedDef->getDescribedClassTemplate();
4977 return TemplateWithDef;
4978}
4979
Balazs Keri3b30d652018-10-19 13:32:20 +00004980ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004981 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4982
Douglas Gregora082a492010-11-30 19:14:50 +00004983 // If this record has a definition in the translation unit we're coming from,
4984 // but this particular declaration is not that definition, import the
4985 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004986 auto *Definition =
4987 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Balazs Keri0c23dc52018-08-13 13:08:37 +00004988 if (Definition && Definition != D->getTemplatedDecl() && !IsFriend) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004989 if (ExpectedDecl ImportedDefOrErr = import(
4990 Definition->getDescribedClassTemplate()))
4991 return Importer.MapImported(D, *ImportedDefOrErr);
4992 else
4993 return ImportedDefOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00004994 }
Gabor Marton9581c332018-05-23 13:53:36 +00004995
Douglas Gregora082a492010-11-30 19:14:50 +00004996 // Import the major distinguishing characteristics of this class template.
4997 DeclContext *DC, *LexicalDC;
4998 DeclarationName Name;
4999 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005000 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005001 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5002 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005003 if (ToD)
5004 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00005005
Douglas Gregora082a492010-11-30 19:14:50 +00005006 // We may already have a template of the same name; try to find and match it.
5007 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005008 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005009 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005010 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005011 for (auto *FoundDecl : FoundDecls) {
5012 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00005013 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005014
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005015 Decl *Found = FoundDecl;
5016 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00005017
5018 // The class to be imported is a definition.
5019 if (D->isThisDeclarationADefinition()) {
5020 // Lookup will find the fwd decl only if that is more recent than the
5021 // definition. So, try to get the definition if that is available in
5022 // the redecl chain.
5023 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005024 if (TemplateWithDef)
5025 FoundTemplate = TemplateWithDef;
5026 else
Gabor Marton9581c332018-05-23 13:53:36 +00005027 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005028 }
5029
Douglas Gregora082a492010-11-30 19:14:50 +00005030 if (IsStructuralMatch(D, FoundTemplate)) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005031 if (!IsFriend) {
5032 Importer.MapImported(D->getTemplatedDecl(),
5033 FoundTemplate->getTemplatedDecl());
5034 return Importer.MapImported(D, FoundTemplate);
5035 }
Aleksei Sidorin761c2242018-05-15 11:09:07 +00005036
Balazs Keri0c23dc52018-08-13 13:08:37 +00005037 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005038 }
Douglas Gregora082a492010-11-30 19:14:50 +00005039 }
Gabor Marton9581c332018-05-23 13:53:36 +00005040
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005041 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005042 }
Gabor Marton9581c332018-05-23 13:53:36 +00005043
Douglas Gregora082a492010-11-30 19:14:50 +00005044 if (!ConflictingDecls.empty()) {
5045 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005046 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005047 ConflictingDecls.size());
5048 }
Gabor Marton9581c332018-05-23 13:53:36 +00005049
Douglas Gregora082a492010-11-30 19:14:50 +00005050 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005051 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005052 }
5053
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005054 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5055
Douglas Gregora082a492010-11-30 19:14:50 +00005056 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005057 CXXRecordDecl *ToTemplated;
5058 if (Error Err = importInto(ToTemplated, FromTemplated))
5059 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005060
Douglas Gregora082a492010-11-30 19:14:50 +00005061 // Create the class template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005062 auto TemplateParamsOrErr = ImportTemplateParameterList(
5063 D->getTemplateParameters());
5064 if (!TemplateParamsOrErr)
5065 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005066
Gabor Marton26f72a92018-07-12 09:42:05 +00005067 ClassTemplateDecl *D2;
5068 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005069 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005070 return D2;
5071
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005072 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005073
Balazs Keri0c23dc52018-08-13 13:08:37 +00005074 if (ToTemplated->getPreviousDecl()) {
5075 assert(
5076 ToTemplated->getPreviousDecl()->getDescribedClassTemplate() &&
5077 "Missing described template");
5078 D2->setPreviousDecl(
5079 ToTemplated->getPreviousDecl()->getDescribedClassTemplate());
5080 }
Douglas Gregora082a492010-11-30 19:14:50 +00005081 D2->setAccess(D->getAccess());
5082 D2->setLexicalDeclContext(LexicalDC);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005083 if (!IsFriend)
5084 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005085
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005086 if (FromTemplated->isCompleteDefinition() &&
5087 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005088 // FIXME: Import definition!
5089 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005090
Douglas Gregora082a492010-11-30 19:14:50 +00005091 return D2;
5092}
5093
Balazs Keri3b30d652018-10-19 13:32:20 +00005094ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005095 ClassTemplateSpecializationDecl *D) {
5096 // If this record has a definition in the translation unit we're coming from,
5097 // but this particular declaration is not that definition, import the
5098 // definition and map to that.
5099 TagDecl *Definition = D->getDefinition();
5100 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005101 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5102 return Importer.MapImported(D, *ImportedDefOrErr);
5103 else
5104 return ImportedDefOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005105 }
5106
Balazs Keri3b30d652018-10-19 13:32:20 +00005107 ClassTemplateDecl *ClassTemplate;
5108 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5109 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005110
Douglas Gregore2e50d332010-12-01 01:36:18 +00005111 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005112 DeclContext *DC, *LexicalDC;
5113 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5114 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005115
5116 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005117 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005118 if (Error Err = ImportTemplateArguments(
5119 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5120 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005121
Douglas Gregore2e50d332010-12-01 01:36:18 +00005122 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005123 void *InsertPos = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005124 ClassTemplateSpecializationDecl *D2 = nullptr;
5125 ClassTemplatePartialSpecializationDecl *PartialSpec =
5126 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5127 if (PartialSpec)
5128 D2 = ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
5129 else
5130 D2 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
5131 ClassTemplateSpecializationDecl * const PrevDecl = D2;
5132 RecordDecl *FoundDef = D2 ? D2->getDefinition() : nullptr;
5133 if (FoundDef) {
5134 if (!D->isCompleteDefinition()) {
5135 // The "From" translation unit only had a forward declaration; call it
5136 // the same declaration.
5137 // TODO Handle the redecl chain properly!
5138 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005139 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005140
5141 if (IsStructuralMatch(D, FoundDef)) {
5142
5143 Importer.MapImported(D, FoundDef);
5144
5145 // Import those those default field initializers which have been
5146 // instantiated in the "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005147 for (auto *FromField : D->fields()) {
5148 auto ToOrErr = import(FromField);
5149 if (!ToOrErr)
5150 // FIXME: return the error?
5151 consumeError(ToOrErr.takeError());
5152 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005153
5154 // Import those methods which have been instantiated in the
5155 // "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005156 for (CXXMethodDecl *FromM : D->methods()) {
5157 auto ToOrErr = import(FromM);
5158 if (!ToOrErr)
5159 // FIXME: return the error?
5160 consumeError(ToOrErr.takeError());
5161 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005162
5163 // TODO Import instantiated default arguments.
5164 // TODO Import instantiated exception specifications.
5165 //
5166 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint what
5167 // else could be fused during an AST merge.
5168
5169 return FoundDef;
5170 }
5171 } else { // We either couldn't find any previous specialization in the "To"
5172 // context, or we found one but without definition. Let's create a
5173 // new specialization and register that at the class template.
Balazs Keri3b30d652018-10-19 13:32:20 +00005174
5175 // Import the location of this declaration.
5176 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5177 if (!BeginLocOrErr)
5178 return BeginLocOrErr.takeError();
5179 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5180 if (!IdLocOrErr)
5181 return IdLocOrErr.takeError();
5182
Gabor Marton42e15de2018-08-22 11:52:14 +00005183 if (PartialSpec) {
5184 // Import TemplateArgumentListInfo.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005185 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005186 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Balazs Keri3b30d652018-10-19 13:32:20 +00005187 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5188 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005189
Balazs Keri3b30d652018-10-19 13:32:20 +00005190 QualType CanonInjType;
5191 if (Error Err = importInto(
5192 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5193 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005194 CanonInjType = CanonInjType.getCanonicalType();
5195
Balazs Keri3b30d652018-10-19 13:32:20 +00005196 auto ToTPListOrErr = ImportTemplateParameterList(
5197 PartialSpec->getTemplateParameters());
5198 if (!ToTPListOrErr)
5199 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005200
Gabor Marton26f72a92018-07-12 09:42:05 +00005201 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00005202 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5203 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
Gabor Marton26f72a92018-07-12 09:42:05 +00005204 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
Gabor Marton42e15de2018-08-22 11:52:14 +00005205 ToTAInfo, CanonInjType,
5206 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
Gabor Marton26f72a92018-07-12 09:42:05 +00005207 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005208
Gabor Marton42e15de2018-08-22 11:52:14 +00005209 // Update InsertPos, because preceding import calls may have invalidated
5210 // it by adding new specializations.
5211 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5212 // Add this partial specialization to the class template.
5213 ClassTemplate->AddPartialSpecialization(
5214 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
5215
5216 } else { // Not a partial specialization.
Gabor Marton26f72a92018-07-12 09:42:05 +00005217 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00005218 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5219 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5220 PrevDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00005221 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005222
5223 // Update InsertPos, because preceding import calls may have invalidated
5224 // it by adding new specializations.
5225 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5226 // Add this specialization to the class template.
5227 ClassTemplate->AddSpecialization(D2, InsertPos);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005228 }
5229
Douglas Gregore2e50d332010-12-01 01:36:18 +00005230 D2->setSpecializationKind(D->getSpecializationKind());
5231
Douglas Gregore2e50d332010-12-01 01:36:18 +00005232 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005233 if (auto LocOrErr = import(D->getQualifierLoc()))
5234 D2->setQualifierInfo(*LocOrErr);
5235 else
5236 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005237
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005238 if (auto *TSI = D->getTypeAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005239 if (auto TInfoOrErr = import(TSI))
5240 D2->setTypeAsWritten(*TInfoOrErr);
5241 else
5242 return TInfoOrErr.takeError();
5243
5244 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5245 D2->setTemplateKeywordLoc(*LocOrErr);
5246 else
5247 return LocOrErr.takeError();
5248
5249 if (auto LocOrErr = import(D->getExternLoc()))
5250 D2->setExternLoc(*LocOrErr);
5251 else
5252 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005253 }
5254
Balazs Keri3b30d652018-10-19 13:32:20 +00005255 if (D->getPointOfInstantiation().isValid()) {
5256 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5257 D2->setPointOfInstantiation(*POIOrErr);
5258 else
5259 return POIOrErr.takeError();
5260 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005261
5262 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5263
Gabor Martonb14056b2018-05-25 11:21:24 +00005264 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00005265 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00005266
5267 // Add to the DC only if it was an explicit specialization/instantiation.
5268 if (D2->isExplicitInstantiationOrSpecialization()) {
5269 LexicalDC->addDeclInternal(D2);
5270 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005271 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005272 if (D->isCompleteDefinition())
5273 if (Error Err = ImportDefinition(D, D2))
5274 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005275
Douglas Gregore2e50d332010-12-01 01:36:18 +00005276 return D2;
5277}
5278
Balazs Keri3b30d652018-10-19 13:32:20 +00005279ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005280 // If this variable has a definition in the translation unit we're coming
5281 // from,
5282 // but this particular declaration is not that definition, import the
5283 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005284 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005285 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5286 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005287 if (ExpectedDecl ImportedDefOrErr = import(
5288 Definition->getDescribedVarTemplate()))
5289 return Importer.MapImported(D, *ImportedDefOrErr);
5290 else
5291 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005292 }
5293
5294 // Import the major distinguishing characteristics of this variable template.
5295 DeclContext *DC, *LexicalDC;
5296 DeclarationName Name;
5297 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005298 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005299 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5300 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005301 if (ToD)
5302 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005303
5304 // We may already have a template of the same name; try to find and match it.
5305 assert(!DC->isFunctionOrMethod() &&
5306 "Variable templates cannot be declared at function scope");
5307 SmallVector<NamedDecl *, 4> ConflictingDecls;
5308 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005309 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005310 for (auto *FoundDecl : FoundDecls) {
5311 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005312 continue;
5313
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005314 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005315 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005316 if (IsStructuralMatch(D, FoundTemplate)) {
5317 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005318 Importer.MapImported(D->getTemplatedDecl(),
5319 FoundTemplate->getTemplatedDecl());
5320 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005321 }
5322 }
5323
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005324 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005325 }
5326
5327 if (!ConflictingDecls.empty()) {
5328 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5329 ConflictingDecls.data(),
5330 ConflictingDecls.size());
5331 }
5332
5333 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005334 // FIXME: Is it possible to get other error than name conflict?
5335 // (Put this `if` into the previous `if`?)
5336 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005337
5338 VarDecl *DTemplated = D->getTemplatedDecl();
5339
5340 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005341 // FIXME: Value not used?
5342 ExpectedType TypeOrErr = import(DTemplated->getType());
5343 if (!TypeOrErr)
5344 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005345
5346 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005347 VarDecl *ToTemplated;
5348 if (Error Err = importInto(ToTemplated, DTemplated))
5349 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005350
5351 // Create the variable template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005352 auto TemplateParamsOrErr = ImportTemplateParameterList(
5353 D->getTemplateParameters());
5354 if (!TemplateParamsOrErr)
5355 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005356
Gabor Marton26f72a92018-07-12 09:42:05 +00005357 VarTemplateDecl *ToVarTD;
5358 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005359 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005360 return ToVarTD;
5361
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005362 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005363
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005364 ToVarTD->setAccess(D->getAccess());
5365 ToVarTD->setLexicalDeclContext(LexicalDC);
5366 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005367
Larisse Voufo39a1e502013-08-06 01:03:05 +00005368 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005369 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005370 // FIXME: Import definition!
5371 }
5372
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005373 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005374}
5375
Balazs Keri3b30d652018-10-19 13:32:20 +00005376ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005377 VarTemplateSpecializationDecl *D) {
5378 // If this record has a definition in the translation unit we're coming from,
5379 // but this particular declaration is not that definition, import the
5380 // definition and map to that.
5381 VarDecl *Definition = D->getDefinition();
5382 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005383 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5384 return Importer.MapImported(D, *ImportedDefOrErr);
5385 else
5386 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005387 }
5388
Balazs Keri3b30d652018-10-19 13:32:20 +00005389 VarTemplateDecl *VarTemplate;
5390 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5391 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005392
5393 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005394 DeclContext *DC, *LexicalDC;
5395 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5396 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005397
5398 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005399 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5400 if (!BeginLocOrErr)
5401 return BeginLocOrErr.takeError();
5402
5403 auto IdLocOrErr = import(D->getLocation());
5404 if (!IdLocOrErr)
5405 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005406
5407 // Import template arguments.
5408 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005409 if (Error Err = ImportTemplateArguments(
5410 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5411 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005412
5413 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005414 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005415 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005416 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005417 if (D2) {
5418 // We already have a variable template specialization with these template
5419 // arguments.
5420
5421 // FIXME: Check for specialization vs. instantiation errors.
5422
5423 if (VarDecl *FoundDef = D2->getDefinition()) {
5424 if (!D->isThisDeclarationADefinition() ||
5425 IsStructuralMatch(D, FoundDef)) {
5426 // The record types structurally match, or the "from" translation
5427 // unit only had a forward declaration anyway; call it the same
5428 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005429 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005430 }
5431 }
5432 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005433 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005434 QualType T;
5435 if (Error Err = importInto(T, D->getType()))
5436 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005437
Balazs Keri3b30d652018-10-19 13:32:20 +00005438 auto TInfoOrErr = import(D->getTypeSourceInfo());
5439 if (!TInfoOrErr)
5440 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005441
5442 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005443 if (Error Err = ImportTemplateArgumentListInfo(
5444 D->getTemplateArgsInfo(), ToTAInfo))
5445 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005446
5447 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005448 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005449 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5450 // Import TemplateArgumentListInfo
5451 TemplateArgumentListInfo ArgInfos;
5452 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5453 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005454 if (Error Err = ImportTemplateArgumentListInfo(
5455 *FromTAArgsAsWritten, ArgInfos))
5456 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005457
Balazs Keri3b30d652018-10-19 13:32:20 +00005458 auto ToTPListOrErr = ImportTemplateParameterList(
5459 FromPartial->getTemplateParameters());
5460 if (!ToTPListOrErr)
5461 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005462
Gabor Marton26f72a92018-07-12 09:42:05 +00005463 PartVarSpecDecl *ToPartial;
5464 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005465 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5466 VarTemplate, T, *TInfoOrErr,
5467 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005468 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005469
Balazs Keri3b30d652018-10-19 13:32:20 +00005470 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5471 FromPartial->getInstantiatedFromMember()))
5472 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5473 else
5474 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005475
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005476 if (FromPartial->isMemberSpecialization())
5477 ToPartial->setMemberSpecialization();
5478
5479 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005480
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005481 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005482 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5483 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5484 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005485 D->getStorageClass(), TemplateArgs))
5486 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005487 }
5488
Balazs Keri3b30d652018-10-19 13:32:20 +00005489 if (D->getPointOfInstantiation().isValid()) {
5490 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5491 D2->setPointOfInstantiation(*POIOrErr);
5492 else
5493 return POIOrErr.takeError();
5494 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005495
Larisse Voufo39a1e502013-08-06 01:03:05 +00005496 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005497 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005498
5499 // Add this specialization to the class template.
5500 VarTemplate->AddSpecialization(D2, InsertPos);
5501
5502 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005503 if (auto LocOrErr = import(D->getQualifierLoc()))
5504 D2->setQualifierInfo(*LocOrErr);
5505 else
5506 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005507
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005508 if (D->isConstexpr())
5509 D2->setConstexpr(true);
5510
Larisse Voufo39a1e502013-08-06 01:03:05 +00005511 // Add the specialization to this context.
5512 D2->setLexicalDeclContext(LexicalDC);
5513 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005514
5515 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005516 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005517
Balazs Keri3b30d652018-10-19 13:32:20 +00005518 if (Error Err = ImportInitializer(D, D2))
5519 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005520
5521 return D2;
5522}
5523
Balazs Keri3b30d652018-10-19 13:32:20 +00005524ExpectedDecl
5525ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005526 DeclContext *DC, *LexicalDC;
5527 DeclarationName Name;
5528 SourceLocation Loc;
5529 NamedDecl *ToD;
5530
Balazs Keri3b30d652018-10-19 13:32:20 +00005531 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5532 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005533
5534 if (ToD)
5535 return ToD;
5536
5537 // Try to find a function in our own ("to") context with the same name, same
5538 // type, and in the same context as the function we're importing.
5539 if (!LexicalDC->isFunctionOrMethod()) {
5540 unsigned IDNS = Decl::IDNS_Ordinary;
5541 SmallVector<NamedDecl *, 2> FoundDecls;
5542 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005543 for (auto *FoundDecl : FoundDecls) {
5544 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005545 continue;
5546
Balazs Keri3b30d652018-10-19 13:32:20 +00005547 if (auto *FoundFunction =
5548 dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005549 if (FoundFunction->hasExternalFormalLinkage() &&
5550 D->hasExternalFormalLinkage()) {
5551 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00005552 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005553 // FIXME: Actually try to merge the body and other attributes.
5554 return FoundFunction;
5555 }
5556 }
5557 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005558 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005559 }
5560 }
5561
Balazs Keri3b30d652018-10-19 13:32:20 +00005562 auto ParamsOrErr = ImportTemplateParameterList(
5563 D->getTemplateParameters());
5564 if (!ParamsOrErr)
5565 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005566
Balazs Keri3b30d652018-10-19 13:32:20 +00005567 FunctionDecl *TemplatedFD;
5568 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5569 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005570
Gabor Marton26f72a92018-07-12 09:42:05 +00005571 FunctionTemplateDecl *ToFunc;
5572 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005573 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005574 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005575
5576 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
5577 ToFunc->setAccess(D->getAccess());
5578 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005579
5580 LexicalDC->addDeclInternal(ToFunc);
5581 return ToFunc;
5582}
5583
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005584//----------------------------------------------------------------------------
5585// Import Statements
5586//----------------------------------------------------------------------------
5587
Balazs Keri3b30d652018-10-19 13:32:20 +00005588ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005589 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5590 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005591 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005592}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005593
Balazs Keri3b30d652018-10-19 13:32:20 +00005594
5595ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005596 SmallVector<IdentifierInfo *, 4> Names;
5597 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5598 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005599 // ToII is nullptr when no symbolic name is given for output operand
5600 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005601 Names.push_back(ToII);
5602 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005603
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005604 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5605 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005606 // ToII is nullptr when no symbolic name is given for input operand
5607 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005608 Names.push_back(ToII);
5609 }
5610
5611 SmallVector<StringLiteral *, 4> Clobbers;
5612 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005613 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5614 Clobbers.push_back(*ClobberOrErr);
5615 else
5616 return ClobberOrErr.takeError();
5617
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005618 }
5619
5620 SmallVector<StringLiteral *, 4> Constraints;
5621 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005622 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5623 Constraints.push_back(*OutputOrErr);
5624 else
5625 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005626 }
5627
5628 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005629 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5630 Constraints.push_back(*InputOrErr);
5631 else
5632 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005633 }
5634
5635 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005636 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5637 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005638
Balazs Keri3b30d652018-10-19 13:32:20 +00005639 if (Error Err = ImportArrayChecked(
5640 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5641 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005642
Balazs Keri3b30d652018-10-19 13:32:20 +00005643 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5644 if (!AsmLocOrErr)
5645 return AsmLocOrErr.takeError();
5646 auto AsmStrOrErr = import(S->getAsmString());
5647 if (!AsmStrOrErr)
5648 return AsmStrOrErr.takeError();
5649 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5650 if (!RParenLocOrErr)
5651 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005652
5653 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005654 Importer.getToContext(),
5655 *AsmLocOrErr,
5656 S->isSimple(),
5657 S->isVolatile(),
5658 S->getNumOutputs(),
5659 S->getNumInputs(),
5660 Names.data(),
5661 Constraints.data(),
5662 Exprs.data(),
5663 *AsmStrOrErr,
5664 S->getNumClobbers(),
5665 Clobbers.data(),
5666 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005667}
5668
Balazs Keri3b30d652018-10-19 13:32:20 +00005669ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5670 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5671 if (!Imp)
5672 return Imp.takeError();
5673
5674 DeclGroupRef ToDG;
5675 SourceLocation ToBeginLoc, ToEndLoc;
5676 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5677
5678 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005679}
5680
Balazs Keri3b30d652018-10-19 13:32:20 +00005681ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5682 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5683 if (!ToSemiLocOrErr)
5684 return ToSemiLocOrErr.takeError();
5685 return new (Importer.getToContext()) NullStmt(
5686 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005687}
5688
Balazs Keri3b30d652018-10-19 13:32:20 +00005689ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005690 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005691
Balazs Keri3b30d652018-10-19 13:32:20 +00005692 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5693 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005694
Balazs Keri3b30d652018-10-19 13:32:20 +00005695 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5696 if (!ToLBracLocOrErr)
5697 return ToLBracLocOrErr.takeError();
5698
5699 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5700 if (!ToRBracLocOrErr)
5701 return ToRBracLocOrErr.takeError();
5702
5703 return CompoundStmt::Create(
5704 Importer.getToContext(), ToStmts,
5705 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005706}
5707
Balazs Keri3b30d652018-10-19 13:32:20 +00005708ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5709 auto Imp = importSeq(
5710 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5711 S->getEllipsisLoc(), S->getColonLoc());
5712 if (!Imp)
5713 return Imp.takeError();
5714
5715 Expr *ToLHS, *ToRHS;
5716 Stmt *ToSubStmt;
5717 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5718 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5719 *Imp;
5720
Bruno Ricci5b30571752018-10-28 12:30:53 +00005721 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5722 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005723 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005724
Gabor Horvath480892b2017-10-18 09:25:18 +00005725 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005726}
5727
Balazs Keri3b30d652018-10-19 13:32:20 +00005728ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5729 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5730 if (!Imp)
5731 return Imp.takeError();
5732
5733 SourceLocation ToDefaultLoc, ToColonLoc;
5734 Stmt *ToSubStmt;
5735 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5736
5737 return new (Importer.getToContext()) DefaultStmt(
5738 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005739}
5740
Balazs Keri3b30d652018-10-19 13:32:20 +00005741ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5742 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5743 if (!Imp)
5744 return Imp.takeError();
5745
5746 SourceLocation ToIdentLoc;
5747 LabelDecl *ToLabelDecl;
5748 Stmt *ToSubStmt;
5749 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5750
5751 return new (Importer.getToContext()) LabelStmt(
5752 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005753}
5754
Balazs Keri3b30d652018-10-19 13:32:20 +00005755ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5756 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5757 if (!ToAttrLocOrErr)
5758 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005759 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5760 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005761 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5762 return std::move(Err);
5763 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5764 if (!ToSubStmtOrErr)
5765 return ToSubStmtOrErr.takeError();
5766
5767 return AttributedStmt::Create(
5768 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005769}
5770
Balazs Keri3b30d652018-10-19 13:32:20 +00005771ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5772 auto Imp = importSeq(
5773 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5774 S->getThen(), S->getElseLoc(), S->getElse());
5775 if (!Imp)
5776 return Imp.takeError();
5777
5778 SourceLocation ToIfLoc, ToElseLoc;
5779 Stmt *ToInit, *ToThen, *ToElse;
5780 VarDecl *ToConditionVariable;
5781 Expr *ToCond;
5782 std::tie(
5783 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5784 *Imp;
5785
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005786 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5787 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5788 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005789}
5790
Balazs Keri3b30d652018-10-19 13:32:20 +00005791ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5792 auto Imp = importSeq(
5793 S->getInit(), S->getConditionVariable(), S->getCond(),
5794 S->getBody(), S->getSwitchLoc());
5795 if (!Imp)
5796 return Imp.takeError();
5797
5798 Stmt *ToInit, *ToBody;
5799 VarDecl *ToConditionVariable;
5800 Expr *ToCond;
5801 SourceLocation ToSwitchLoc;
5802 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5803
Bruno Riccie2806f82018-10-29 16:12:37 +00005804 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5805 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005806 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005807 ToStmt->setSwitchLoc(ToSwitchLoc);
5808
Sean Callanan59721b32015-04-28 18:41:46 +00005809 // Now we have to re-chain the cases.
5810 SwitchCase *LastChainedSwitchCase = nullptr;
5811 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5812 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005813 Expected<SwitchCase *> ToSCOrErr = import(SC);
5814 if (!ToSCOrErr)
5815 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005816 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005817 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005818 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005819 ToStmt->setSwitchCaseList(*ToSCOrErr);
5820 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005821 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005822
Sean Callanan59721b32015-04-28 18:41:46 +00005823 return ToStmt;
5824}
5825
Balazs Keri3b30d652018-10-19 13:32:20 +00005826ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5827 auto Imp = importSeq(
5828 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5829 if (!Imp)
5830 return Imp.takeError();
5831
5832 VarDecl *ToConditionVariable;
5833 Expr *ToCond;
5834 Stmt *ToBody;
5835 SourceLocation ToWhileLoc;
5836 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5837
Bruno Riccibacf7512018-10-30 13:42:41 +00005838 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5839 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005840}
5841
Balazs Keri3b30d652018-10-19 13:32:20 +00005842ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5843 auto Imp = importSeq(
5844 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5845 S->getRParenLoc());
5846 if (!Imp)
5847 return Imp.takeError();
5848
5849 Stmt *ToBody;
5850 Expr *ToCond;
5851 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5852 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5853
5854 return new (Importer.getToContext()) DoStmt(
5855 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005856}
5857
Balazs Keri3b30d652018-10-19 13:32:20 +00005858ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5859 auto Imp = importSeq(
5860 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5861 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5862 if (!Imp)
5863 return Imp.takeError();
5864
5865 Stmt *ToInit;
5866 Expr *ToCond, *ToInc;
5867 VarDecl *ToConditionVariable;
5868 Stmt *ToBody;
5869 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5870 std::tie(
5871 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5872 ToLParenLoc, ToRParenLoc) = *Imp;
5873
5874 return new (Importer.getToContext()) ForStmt(
5875 Importer.getToContext(),
5876 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5877 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005878}
5879
Balazs Keri3b30d652018-10-19 13:32:20 +00005880ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5881 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5882 if (!Imp)
5883 return Imp.takeError();
5884
5885 LabelDecl *ToLabel;
5886 SourceLocation ToGotoLoc, ToLabelLoc;
5887 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5888
5889 return new (Importer.getToContext()) GotoStmt(
5890 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005891}
5892
Balazs Keri3b30d652018-10-19 13:32:20 +00005893ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5894 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5895 if (!Imp)
5896 return Imp.takeError();
5897
5898 SourceLocation ToGotoLoc, ToStarLoc;
5899 Expr *ToTarget;
5900 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5901
5902 return new (Importer.getToContext()) IndirectGotoStmt(
5903 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005904}
5905
Balazs Keri3b30d652018-10-19 13:32:20 +00005906ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5907 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5908 if (!ToContinueLocOrErr)
5909 return ToContinueLocOrErr.takeError();
5910 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005911}
5912
Balazs Keri3b30d652018-10-19 13:32:20 +00005913ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5914 auto ToBreakLocOrErr = import(S->getBreakLoc());
5915 if (!ToBreakLocOrErr)
5916 return ToBreakLocOrErr.takeError();
5917 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005918}
5919
Balazs Keri3b30d652018-10-19 13:32:20 +00005920ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5921 auto Imp = importSeq(
5922 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5923 if (!Imp)
5924 return Imp.takeError();
5925
5926 SourceLocation ToReturnLoc;
5927 Expr *ToRetValue;
5928 const VarDecl *ToNRVOCandidate;
5929 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5930
Bruno Ricci023b1d12018-10-30 14:40:49 +00005931 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5932 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005933}
5934
Balazs Keri3b30d652018-10-19 13:32:20 +00005935ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5936 auto Imp = importSeq(
5937 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5938 if (!Imp)
5939 return Imp.takeError();
5940
5941 SourceLocation ToCatchLoc;
5942 VarDecl *ToExceptionDecl;
5943 Stmt *ToHandlerBlock;
5944 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5945
5946 return new (Importer.getToContext()) CXXCatchStmt (
5947 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005948}
5949
Balazs Keri3b30d652018-10-19 13:32:20 +00005950ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5951 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5952 if (!ToTryLocOrErr)
5953 return ToTryLocOrErr.takeError();
5954
5955 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5956 if (!ToTryBlockOrErr)
5957 return ToTryBlockOrErr.takeError();
5958
Sean Callanan59721b32015-04-28 18:41:46 +00005959 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5960 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5961 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005962 if (auto ToHandlerOrErr = import(FromHandler))
5963 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005964 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005965 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005966 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005967
5968 return CXXTryStmt::Create(
5969 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005970}
5971
Balazs Keri3b30d652018-10-19 13:32:20 +00005972ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5973 auto Imp1 = importSeq(
5974 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5975 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5976 if (!Imp1)
5977 return Imp1.takeError();
5978 auto Imp2 = importSeq(
5979 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5980 if (!Imp2)
5981 return Imp2.takeError();
5982
5983 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5984 Expr *ToCond, *ToInc;
5985 Stmt *ToInit, *ToBody;
5986 std::tie(
5987 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5988 ToBody) = *Imp1;
5989 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5990 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5991
5992 return new (Importer.getToContext()) CXXForRangeStmt(
5993 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5994 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005995}
5996
Balazs Keri3b30d652018-10-19 13:32:20 +00005997ExpectedStmt
5998ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5999 auto Imp = importSeq(
6000 S->getElement(), S->getCollection(), S->getBody(),
6001 S->getForLoc(), S->getRParenLoc());
6002 if (!Imp)
6003 return Imp.takeError();
6004
6005 Stmt *ToElement, *ToBody;
6006 Expr *ToCollection;
6007 SourceLocation ToForLoc, ToRParenLoc;
6008 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6009
6010 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6011 ToCollection,
6012 ToBody,
6013 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006014 ToRParenLoc);
6015}
6016
Balazs Keri3b30d652018-10-19 13:32:20 +00006017ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6018 auto Imp = importSeq(
6019 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6020 S->getCatchBody());
6021 if (!Imp)
6022 return Imp.takeError();
6023
6024 SourceLocation ToAtCatchLoc, ToRParenLoc;
6025 VarDecl *ToCatchParamDecl;
6026 Stmt *ToCatchBody;
6027 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6028
6029 return new (Importer.getToContext()) ObjCAtCatchStmt (
6030 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006031}
6032
Balazs Keri3b30d652018-10-19 13:32:20 +00006033ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6034 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6035 if (!ToAtFinallyLocOrErr)
6036 return ToAtFinallyLocOrErr.takeError();
6037 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6038 if (!ToAtFinallyStmtOrErr)
6039 return ToAtFinallyStmtOrErr.takeError();
6040 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6041 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006042}
6043
Balazs Keri3b30d652018-10-19 13:32:20 +00006044ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6045 auto Imp = importSeq(
6046 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6047 if (!Imp)
6048 return Imp.takeError();
6049
6050 SourceLocation ToAtTryLoc;
6051 Stmt *ToTryBody, *ToFinallyStmt;
6052 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6053
Sean Callanan59721b32015-04-28 18:41:46 +00006054 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6055 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6056 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006057 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6058 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006059 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006060 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006061 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006062
Sean Callanan59721b32015-04-28 18:41:46 +00006063 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006064 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006065 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006066 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006067}
6068
Balazs Keri3b30d652018-10-19 13:32:20 +00006069ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006070 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006071 auto Imp = importSeq(
6072 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6073 if (!Imp)
6074 return Imp.takeError();
6075
6076 SourceLocation ToAtSynchronizedLoc;
6077 Expr *ToSynchExpr;
6078 Stmt *ToSynchBody;
6079 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6080
Sean Callanan59721b32015-04-28 18:41:46 +00006081 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6082 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6083}
6084
Balazs Keri3b30d652018-10-19 13:32:20 +00006085ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6086 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6087 if (!ToThrowLocOrErr)
6088 return ToThrowLocOrErr.takeError();
6089 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6090 if (!ToThrowExprOrErr)
6091 return ToThrowExprOrErr.takeError();
6092 return new (Importer.getToContext()) ObjCAtThrowStmt(
6093 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006094}
6095
Balazs Keri3b30d652018-10-19 13:32:20 +00006096ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6097 ObjCAutoreleasePoolStmt *S) {
6098 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6099 if (!ToAtLocOrErr)
6100 return ToAtLocOrErr.takeError();
6101 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6102 if (!ToSubStmtOrErr)
6103 return ToSubStmtOrErr.takeError();
6104 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6105 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006106}
6107
6108//----------------------------------------------------------------------------
6109// Import Expressions
6110//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006111ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006112 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6113 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006114 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006115}
6116
Balazs Keri3b30d652018-10-19 13:32:20 +00006117ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6118 auto Imp = importSeq(
6119 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6120 E->getRParenLoc(), E->getType());
6121 if (!Imp)
6122 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006123
Balazs Keri3b30d652018-10-19 13:32:20 +00006124 SourceLocation ToBuiltinLoc, ToRParenLoc;
6125 Expr *ToSubExpr;
6126 TypeSourceInfo *ToWrittenTypeInfo;
6127 QualType ToType;
6128 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6129 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006130
6131 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006132 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6133 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006134}
6135
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006136
Balazs Keri3b30d652018-10-19 13:32:20 +00006137ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6138 ExpectedType TypeOrErr = import(E->getType());
6139 if (!TypeOrErr)
6140 return TypeOrErr.takeError();
6141
6142 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6143 if (!BeginLocOrErr)
6144 return BeginLocOrErr.takeError();
6145
6146 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006147}
6148
Balazs Keri3b30d652018-10-19 13:32:20 +00006149ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6150 auto Imp = importSeq(
6151 E->getBeginLoc(), E->getType(), E->getFunctionName());
6152 if (!Imp)
6153 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006154
Balazs Keri3b30d652018-10-19 13:32:20 +00006155 SourceLocation ToBeginLoc;
6156 QualType ToType;
6157 StringLiteral *ToFunctionName;
6158 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006159
Bruno Ricci17ff0262018-10-27 19:21:19 +00006160 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6161 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006162}
6163
Balazs Keri3b30d652018-10-19 13:32:20 +00006164ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6165 auto Imp = importSeq(
6166 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6167 E->getLocation(), E->getType());
6168 if (!Imp)
6169 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006170
Balazs Keri3b30d652018-10-19 13:32:20 +00006171 NestedNameSpecifierLoc ToQualifierLoc;
6172 SourceLocation ToTemplateKeywordLoc, ToLocation;
6173 ValueDecl *ToDecl;
6174 QualType ToType;
6175 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6176 *Imp;
6177
6178 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006179 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006180 auto FoundDOrErr = import(E->getFoundDecl());
6181 if (!FoundDOrErr)
6182 return FoundDOrErr.takeError();
6183 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006184 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006185
Aleksei Sidorina693b372016-09-28 10:16:56 +00006186 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006187 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006188 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006189 if (Error Err =
6190 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6191 return std::move(Err);
6192 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006193 }
6194
Balazs Keri3b30d652018-10-19 13:32:20 +00006195 auto *ToE = DeclRefExpr::Create(
6196 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6197 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6198 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006199 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006200 ToE->setHadMultipleCandidates(true);
6201 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006202}
6203
Balazs Keri3b30d652018-10-19 13:32:20 +00006204ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6205 ExpectedType TypeOrErr = import(E->getType());
6206 if (!TypeOrErr)
6207 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006208
Balazs Keri3b30d652018-10-19 13:32:20 +00006209 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006210}
6211
Balazs Keri3b30d652018-10-19 13:32:20 +00006212ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6213 ExpectedExpr ToInitOrErr = import(E->getInit());
6214 if (!ToInitOrErr)
6215 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006216
Balazs Keri3b30d652018-10-19 13:32:20 +00006217 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6218 if (!ToEqualOrColonLocOrErr)
6219 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006220
Balazs Keri3b30d652018-10-19 13:32:20 +00006221 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006222 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006223 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6224 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6225 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006226 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006227 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006228 }
6229
Balazs Keri3b30d652018-10-19 13:32:20 +00006230 SmallVector<Designator, 4> ToDesignators(E->size());
6231 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6232 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006233
6234 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006235 Importer.getToContext(), ToDesignators,
6236 ToIndexExprs, *ToEqualOrColonLocOrErr,
6237 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006238}
6239
Balazs Keri3b30d652018-10-19 13:32:20 +00006240ExpectedStmt
6241ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6242 ExpectedType ToTypeOrErr = import(E->getType());
6243 if (!ToTypeOrErr)
6244 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006245
Balazs Keri3b30d652018-10-19 13:32:20 +00006246 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6247 if (!ToLocationOrErr)
6248 return ToLocationOrErr.takeError();
6249
6250 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6251 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006252}
6253
Balazs Keri3b30d652018-10-19 13:32:20 +00006254ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6255 ExpectedType ToTypeOrErr = import(E->getType());
6256 if (!ToTypeOrErr)
6257 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006258
Balazs Keri3b30d652018-10-19 13:32:20 +00006259 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6260 if (!ToLocationOrErr)
6261 return ToLocationOrErr.takeError();
6262
6263 return IntegerLiteral::Create(
6264 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006265}
6266
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006267
Balazs Keri3b30d652018-10-19 13:32:20 +00006268ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6269 ExpectedType ToTypeOrErr = import(E->getType());
6270 if (!ToTypeOrErr)
6271 return ToTypeOrErr.takeError();
6272
6273 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6274 if (!ToLocationOrErr)
6275 return ToLocationOrErr.takeError();
6276
6277 return FloatingLiteral::Create(
6278 Importer.getToContext(), E->getValue(), E->isExact(),
6279 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006280}
6281
Balazs Keri3b30d652018-10-19 13:32:20 +00006282ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6283 auto ToTypeOrErr = import(E->getType());
6284 if (!ToTypeOrErr)
6285 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006286
Balazs Keri3b30d652018-10-19 13:32:20 +00006287 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6288 if (!ToSubExprOrErr)
6289 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006290
Balazs Keri3b30d652018-10-19 13:32:20 +00006291 return new (Importer.getToContext()) ImaginaryLiteral(
6292 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006293}
6294
Balazs Keri3b30d652018-10-19 13:32:20 +00006295ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6296 ExpectedType ToTypeOrErr = import(E->getType());
6297 if (!ToTypeOrErr)
6298 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006299
Balazs Keri3b30d652018-10-19 13:32:20 +00006300 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6301 if (!ToLocationOrErr)
6302 return ToLocationOrErr.takeError();
6303
6304 return new (Importer.getToContext()) CharacterLiteral(
6305 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006306}
6307
Balazs Keri3b30d652018-10-19 13:32:20 +00006308ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6309 ExpectedType ToTypeOrErr = import(E->getType());
6310 if (!ToTypeOrErr)
6311 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006312
Balazs Keri3b30d652018-10-19 13:32:20 +00006313 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6314 if (Error Err = ImportArrayChecked(
6315 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6316 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006317
Balazs Keri3b30d652018-10-19 13:32:20 +00006318 return StringLiteral::Create(
6319 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6320 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006321}
6322
Balazs Keri3b30d652018-10-19 13:32:20 +00006323ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6324 auto Imp = importSeq(
6325 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6326 E->getInitializer());
6327 if (!Imp)
6328 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006329
Balazs Keri3b30d652018-10-19 13:32:20 +00006330 SourceLocation ToLParenLoc;
6331 TypeSourceInfo *ToTypeSourceInfo;
6332 QualType ToType;
6333 Expr *ToInitializer;
6334 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006335
6336 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006337 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6338 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006339}
6340
Balazs Keri3b30d652018-10-19 13:32:20 +00006341ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6342 auto Imp = importSeq(
6343 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6344 if (!Imp)
6345 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006346
Balazs Keri3b30d652018-10-19 13:32:20 +00006347 SourceLocation ToBuiltinLoc, ToRParenLoc;
6348 QualType ToType;
6349 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6350
6351 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6352 if (Error Err = ImportArrayChecked(
6353 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6354 ToExprs.begin()))
6355 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006356
6357 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006358 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006359}
6360
Balazs Keri3b30d652018-10-19 13:32:20 +00006361ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6362 auto Imp = importSeq(
6363 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6364 if (!Imp)
6365 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006366
Balazs Keri3b30d652018-10-19 13:32:20 +00006367 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6368 LabelDecl *ToLabel;
6369 QualType ToType;
6370 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006371
6372 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006373 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006374}
6375
Bill Wendling8003edc2018-11-09 00:41:36 +00006376ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6377 auto Imp = importSeq(E->getSubExpr());
6378 if (!Imp)
6379 return Imp.takeError();
6380
6381 Expr *ToSubExpr;
6382 std::tie(ToSubExpr) = *Imp;
6383
Bill Wendling6ff17512018-11-21 20:44:18 +00006384 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006385}
6386
Balazs Keri3b30d652018-10-19 13:32:20 +00006387ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6388 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6389 if (!Imp)
6390 return Imp.takeError();
6391
6392 SourceLocation ToLParen, ToRParen;
6393 Expr *ToSubExpr;
6394 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006395
Fangrui Song6907ce22018-07-30 19:24:48 +00006396 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006397 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006398}
6399
Balazs Keri3b30d652018-10-19 13:32:20 +00006400ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6401 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6402 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6403 return std::move(Err);
6404
6405 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6406 if (!ToLParenLocOrErr)
6407 return ToLParenLocOrErr.takeError();
6408
6409 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6410 if (!ToRParenLocOrErr)
6411 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006412
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006413 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6414 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006415}
6416
Balazs Keri3b30d652018-10-19 13:32:20 +00006417ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6418 auto Imp = importSeq(
6419 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6420 if (!Imp)
6421 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006422
Balazs Keri3b30d652018-10-19 13:32:20 +00006423 CompoundStmt *ToSubStmt;
6424 QualType ToType;
6425 SourceLocation ToLParenLoc, ToRParenLoc;
6426 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006427
Balazs Keri3b30d652018-10-19 13:32:20 +00006428 return new (Importer.getToContext()) StmtExpr(
6429 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006430}
6431
Balazs Keri3b30d652018-10-19 13:32:20 +00006432ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6433 auto Imp = importSeq(
6434 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6435 if (!Imp)
6436 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006437
Balazs Keri3b30d652018-10-19 13:32:20 +00006438 Expr *ToSubExpr;
6439 QualType ToType;
6440 SourceLocation ToOperatorLoc;
6441 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006442
Aaron Ballmana5038552018-01-09 13:07:03 +00006443 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006444 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6445 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006446}
6447
Balazs Keri3b30d652018-10-19 13:32:20 +00006448ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006449ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006450 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6451 if (!Imp)
6452 return Imp.takeError();
6453
6454 QualType ToType;
6455 SourceLocation ToOperatorLoc, ToRParenLoc;
6456 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006457
Douglas Gregord8552cd2010-02-19 01:24:23 +00006458 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006459 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6460 import(E->getArgumentTypeInfo());
6461 if (!ToArgumentTypeInfoOrErr)
6462 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006463
Balazs Keri3b30d652018-10-19 13:32:20 +00006464 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6465 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6466 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006467 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006468
Balazs Keri3b30d652018-10-19 13:32:20 +00006469 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6470 if (!ToArgumentExprOrErr)
6471 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006472
Balazs Keri3b30d652018-10-19 13:32:20 +00006473 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6474 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006475}
6476
Balazs Keri3b30d652018-10-19 13:32:20 +00006477ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6478 auto Imp = importSeq(
6479 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6480 if (!Imp)
6481 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006482
Balazs Keri3b30d652018-10-19 13:32:20 +00006483 Expr *ToLHS, *ToRHS;
6484 QualType ToType;
6485 SourceLocation ToOperatorLoc;
6486 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006487
Balazs Keri3b30d652018-10-19 13:32:20 +00006488 return new (Importer.getToContext()) BinaryOperator(
6489 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6490 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006491}
6492
Balazs Keri3b30d652018-10-19 13:32:20 +00006493ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6494 auto Imp = importSeq(
6495 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6496 E->getRHS(), E->getType());
6497 if (!Imp)
6498 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006499
Balazs Keri3b30d652018-10-19 13:32:20 +00006500 Expr *ToCond, *ToLHS, *ToRHS;
6501 SourceLocation ToQuestionLoc, ToColonLoc;
6502 QualType ToType;
6503 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006504
6505 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006506 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6507 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006508}
6509
Balazs Keri3b30d652018-10-19 13:32:20 +00006510ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006511 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006512 auto Imp = importSeq(
6513 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6514 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6515 if (!Imp)
6516 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006517
Balazs Keri3b30d652018-10-19 13:32:20 +00006518 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6519 OpaqueValueExpr *ToOpaqueValue;
6520 SourceLocation ToQuestionLoc, ToColonLoc;
6521 QualType ToType;
6522 std::tie(
6523 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6524 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006525
6526 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006527 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6528 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6529 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006530}
6531
Balazs Keri3b30d652018-10-19 13:32:20 +00006532ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6533 auto Imp = importSeq(
6534 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6535 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6536 if (!Imp)
6537 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006538
Balazs Keri3b30d652018-10-19 13:32:20 +00006539 SourceLocation ToBeginLoc, ToEndLoc;
6540 TypeSourceInfo *ToQueriedTypeSourceInfo;
6541 Expr *ToDimensionExpression;
6542 QualType ToType;
6543 std::tie(
6544 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6545 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006546
6547 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006548 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6549 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006550}
6551
Balazs Keri3b30d652018-10-19 13:32:20 +00006552ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6553 auto Imp = importSeq(
6554 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6555 if (!Imp)
6556 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006557
Balazs Keri3b30d652018-10-19 13:32:20 +00006558 SourceLocation ToBeginLoc, ToEndLoc;
6559 Expr *ToQueriedExpression;
6560 QualType ToType;
6561 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006562
6563 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006564 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6565 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006566}
6567
Balazs Keri3b30d652018-10-19 13:32:20 +00006568ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6569 auto Imp = importSeq(
6570 E->getLocation(), E->getType(), E->getSourceExpr());
6571 if (!Imp)
6572 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006573
Balazs Keri3b30d652018-10-19 13:32:20 +00006574 SourceLocation ToLocation;
6575 QualType ToType;
6576 Expr *ToSourceExpr;
6577 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006578
6579 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006580 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006581}
6582
Balazs Keri3b30d652018-10-19 13:32:20 +00006583ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6584 auto Imp = importSeq(
6585 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6586 if (!Imp)
6587 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006588
Balazs Keri3b30d652018-10-19 13:32:20 +00006589 Expr *ToLHS, *ToRHS;
6590 SourceLocation ToRBracketLoc;
6591 QualType ToType;
6592 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006593
6594 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006595 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6596 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006597}
6598
Balazs Keri3b30d652018-10-19 13:32:20 +00006599ExpectedStmt
6600ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6601 auto Imp = importSeq(
6602 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6603 E->getComputationResultType(), E->getOperatorLoc());
6604 if (!Imp)
6605 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006606
Balazs Keri3b30d652018-10-19 13:32:20 +00006607 Expr *ToLHS, *ToRHS;
6608 QualType ToType, ToComputationLHSType, ToComputationResultType;
6609 SourceLocation ToOperatorLoc;
6610 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6611 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006612
Balazs Keri3b30d652018-10-19 13:32:20 +00006613 return new (Importer.getToContext()) CompoundAssignOperator(
6614 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6615 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6616 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006617}
6618
Balazs Keri3b30d652018-10-19 13:32:20 +00006619Expected<CXXCastPath>
6620ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6621 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006622 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006623 if (auto SpecOrErr = import(*I))
6624 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006625 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006626 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006627 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006628 return Path;
John McCallcf142162010-08-07 06:22:56 +00006629}
6630
Balazs Keri3b30d652018-10-19 13:32:20 +00006631ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6632 ExpectedType ToTypeOrErr = import(E->getType());
6633 if (!ToTypeOrErr)
6634 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006635
Balazs Keri3b30d652018-10-19 13:32:20 +00006636 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6637 if (!ToSubExprOrErr)
6638 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006639
Balazs Keri3b30d652018-10-19 13:32:20 +00006640 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6641 if (!ToBasePathOrErr)
6642 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006643
Balazs Keri3b30d652018-10-19 13:32:20 +00006644 return ImplicitCastExpr::Create(
6645 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6646 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006647}
6648
Balazs Keri3b30d652018-10-19 13:32:20 +00006649ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6650 auto Imp1 = importSeq(
6651 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6652 if (!Imp1)
6653 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006654
Balazs Keri3b30d652018-10-19 13:32:20 +00006655 QualType ToType;
6656 Expr *ToSubExpr;
6657 TypeSourceInfo *ToTypeInfoAsWritten;
6658 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006659
Balazs Keri3b30d652018-10-19 13:32:20 +00006660 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6661 if (!ToBasePathOrErr)
6662 return ToBasePathOrErr.takeError();
6663 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006664
Aleksei Sidorina693b372016-09-28 10:16:56 +00006665 switch (E->getStmtClass()) {
6666 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006667 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006668 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6669 if (!ToLParenLocOrErr)
6670 return ToLParenLocOrErr.takeError();
6671 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6672 if (!ToRParenLocOrErr)
6673 return ToRParenLocOrErr.takeError();
6674 return CStyleCastExpr::Create(
6675 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6676 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6677 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006678 }
6679
6680 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006681 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006682 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6683 if (!ToLParenLocOrErr)
6684 return ToLParenLocOrErr.takeError();
6685 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6686 if (!ToRParenLocOrErr)
6687 return ToRParenLocOrErr.takeError();
6688 return CXXFunctionalCastExpr::Create(
6689 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6690 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6691 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006692 }
6693
6694 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006695 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6696 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6697 if (!ToLParenLocOrErr)
6698 return ToLParenLocOrErr.takeError();
6699 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6700 if (!ToBridgeKeywordLocOrErr)
6701 return ToBridgeKeywordLocOrErr.takeError();
6702 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6703 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6704 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006705 }
6706 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006707 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006708 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006709 }
6710}
6711
Balazs Keri3b30d652018-10-19 13:32:20 +00006712ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6713 SmallVector<OffsetOfNode, 4> ToNodes;
6714 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6715 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006716
Balazs Keri3b30d652018-10-19 13:32:20 +00006717 SourceLocation ToBeginLoc, ToEndLoc;
6718 if (FromNode.getKind() != OffsetOfNode::Base) {
6719 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6720 if (!Imp)
6721 return Imp.takeError();
6722 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6723 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006724
Balazs Keri3b30d652018-10-19 13:32:20 +00006725 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006726 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006727 ToNodes.push_back(
6728 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006730 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006731 auto ToBSOrErr = import(FromNode.getBase());
6732 if (!ToBSOrErr)
6733 return ToBSOrErr.takeError();
6734 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006735 break;
6736 }
6737 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006738 auto ToFieldOrErr = import(FromNode.getField());
6739 if (!ToFieldOrErr)
6740 return ToFieldOrErr.takeError();
6741 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006742 break;
6743 }
6744 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006745 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6746 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006747 break;
6748 }
6749 }
6750 }
6751
Balazs Keri3b30d652018-10-19 13:32:20 +00006752 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6753 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6754 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6755 if (!ToIndexExprOrErr)
6756 return ToIndexExprOrErr.takeError();
6757 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006758 }
6759
Balazs Keri3b30d652018-10-19 13:32:20 +00006760 auto Imp = importSeq(
6761 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6762 E->getRParenLoc());
6763 if (!Imp)
6764 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006765
Balazs Keri3b30d652018-10-19 13:32:20 +00006766 QualType ToType;
6767 TypeSourceInfo *ToTypeSourceInfo;
6768 SourceLocation ToOperatorLoc, ToRParenLoc;
6769 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6770
6771 return OffsetOfExpr::Create(
6772 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6773 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006774}
6775
Balazs Keri3b30d652018-10-19 13:32:20 +00006776ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6777 auto Imp = importSeq(
6778 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6779 if (!Imp)
6780 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006781
Balazs Keri3b30d652018-10-19 13:32:20 +00006782 QualType ToType;
6783 Expr *ToOperand;
6784 SourceLocation ToBeginLoc, ToEndLoc;
6785 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006786
Balazs Keri3b30d652018-10-19 13:32:20 +00006787 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006788 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006789 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006790 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006791 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006792
Balazs Keri3b30d652018-10-19 13:32:20 +00006793 return new (Importer.getToContext()) CXXNoexceptExpr(
6794 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006795}
6796
Balazs Keri3b30d652018-10-19 13:32:20 +00006797ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6798 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6799 if (!Imp)
6800 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006801
Balazs Keri3b30d652018-10-19 13:32:20 +00006802 Expr *ToSubExpr;
6803 QualType ToType;
6804 SourceLocation ToThrowLoc;
6805 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006806
6807 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006808 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006809}
6810
Balazs Keri3b30d652018-10-19 13:32:20 +00006811ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6812 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6813 if (!ToUsedLocOrErr)
6814 return ToUsedLocOrErr.takeError();
6815
6816 auto ToParamOrErr = import(E->getParam());
6817 if (!ToParamOrErr)
6818 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006819
6820 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006821 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006822}
6823
Balazs Keri3b30d652018-10-19 13:32:20 +00006824ExpectedStmt
6825ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6826 auto Imp = importSeq(
6827 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6828 if (!Imp)
6829 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006830
Balazs Keri3b30d652018-10-19 13:32:20 +00006831 QualType ToType;
6832 TypeSourceInfo *ToTypeSourceInfo;
6833 SourceLocation ToRParenLoc;
6834 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006835
6836 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006837 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006838}
6839
Balazs Keri3b30d652018-10-19 13:32:20 +00006840ExpectedStmt
6841ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6842 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6843 if (!ToSubExprOrErr)
6844 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006845
Balazs Keri3b30d652018-10-19 13:32:20 +00006846 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6847 if (!ToDtorOrErr)
6848 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006849
6850 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006851 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6852 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006853}
6854
Balazs Keri3b30d652018-10-19 13:32:20 +00006855ExpectedStmt
6856ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6857 auto Imp = importSeq(
6858 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6859 E->getParenOrBraceRange());
6860 if (!Imp)
6861 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006862
Balazs Keri3b30d652018-10-19 13:32:20 +00006863 CXXConstructorDecl *ToConstructor;
6864 QualType ToType;
6865 TypeSourceInfo *ToTypeSourceInfo;
6866 SourceRange ToParenOrBraceRange;
6867 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006868
Balazs Keri3b30d652018-10-19 13:32:20 +00006869 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6870 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6871 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006872
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006873 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006874 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6875 ToParenOrBraceRange, E->hadMultipleCandidates(),
6876 E->isListInitialization(), E->isStdInitListInitialization(),
6877 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006878}
6879
Balazs Keri3b30d652018-10-19 13:32:20 +00006880ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006881ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006882 auto Imp = importSeq(
6883 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6884 if (!Imp)
6885 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006886
Balazs Keri3b30d652018-10-19 13:32:20 +00006887 QualType ToType;
6888 Expr *ToTemporaryExpr;
6889 const ValueDecl *ToExtendingDecl;
6890 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006891
6892 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006893 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006894
6895 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006896 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006897 return ToMTE;
6898}
6899
Balazs Keri3b30d652018-10-19 13:32:20 +00006900ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6901 auto Imp = importSeq(
6902 E->getType(), E->getPattern(), E->getEllipsisLoc());
6903 if (!Imp)
6904 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006905
Balazs Keri3b30d652018-10-19 13:32:20 +00006906 QualType ToType;
6907 Expr *ToPattern;
6908 SourceLocation ToEllipsisLoc;
6909 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006910
6911 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006912 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006913}
6914
Balazs Keri3b30d652018-10-19 13:32:20 +00006915ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6916 auto Imp = importSeq(
6917 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6918 if (!Imp)
6919 return Imp.takeError();
6920
6921 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6922 NamedDecl *ToPack;
6923 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006924
6925 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006926 if (!E->isValueDependent())
6927 Length = E->getPackLength();
6928
Balazs Keri3b30d652018-10-19 13:32:20 +00006929 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006930 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006931 if (Error Err = ImportTemplateArguments(
6932 E->getPartialArguments().data(),
6933 E->getPartialArguments().size(),
6934 ToPartialArguments))
6935 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006936 }
6937
6938 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006939 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6940 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006941}
6942
Aleksei Sidorina693b372016-09-28 10:16:56 +00006943
Balazs Keri3b30d652018-10-19 13:32:20 +00006944ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6945 auto Imp = importSeq(
6946 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6947 E->getArraySize(), E->getInitializer(), E->getType(),
6948 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6949 E->getDirectInitRange());
6950 if (!Imp)
6951 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006952
Balazs Keri3b30d652018-10-19 13:32:20 +00006953 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6954 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6955 Expr *ToArraySize, *ToInitializer;
6956 QualType ToType;
6957 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6958 std::tie(
6959 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6960 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006961
Balazs Keri3b30d652018-10-19 13:32:20 +00006962 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6963 if (Error Err =
6964 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6965 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006966
6967 return new (Importer.getToContext()) CXXNewExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006968 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6969 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6970 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6971 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6972 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006973}
6974
Balazs Keri3b30d652018-10-19 13:32:20 +00006975ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6976 auto Imp = importSeq(
6977 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6978 if (!Imp)
6979 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006980
Balazs Keri3b30d652018-10-19 13:32:20 +00006981 QualType ToType;
6982 FunctionDecl *ToOperatorDelete;
6983 Expr *ToArgument;
6984 SourceLocation ToBeginLoc;
6985 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006986
6987 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006988 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6989 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6990 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006991}
6992
Balazs Keri3b30d652018-10-19 13:32:20 +00006993ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6994 auto Imp = importSeq(
6995 E->getType(), E->getLocation(), E->getConstructor(),
6996 E->getParenOrBraceRange());
6997 if (!Imp)
6998 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006999
Balazs Keri3b30d652018-10-19 13:32:20 +00007000 QualType ToType;
7001 SourceLocation ToLocation;
7002 CXXConstructorDecl *ToConstructor;
7003 SourceRange ToParenOrBraceRange;
7004 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007005
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007006 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007007 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7008 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007009
Balazs Keri3b30d652018-10-19 13:32:20 +00007010 return CXXConstructExpr::Create(
7011 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7012 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7013 E->isListInitialization(), E->isStdInitListInitialization(),
7014 E->requiresZeroInitialization(), E->getConstructionKind(),
7015 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007016}
7017
Balazs Keri3b30d652018-10-19 13:32:20 +00007018ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7019 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7020 if (!ToSubExprOrErr)
7021 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007022
Balazs Keri3b30d652018-10-19 13:32:20 +00007023 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7024 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7025 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007026
Balazs Keri3b30d652018-10-19 13:32:20 +00007027 return ExprWithCleanups::Create(
7028 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7029 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007030}
7031
Balazs Keri3b30d652018-10-19 13:32:20 +00007032ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7033 auto Imp = importSeq(
7034 E->getCallee(), E->getType(), E->getRParenLoc());
7035 if (!Imp)
7036 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007037
Balazs Keri3b30d652018-10-19 13:32:20 +00007038 Expr *ToCallee;
7039 QualType ToType;
7040 SourceLocation ToRParenLoc;
7041 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007042
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007043 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007044 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7045 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007046
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007047 return new (Importer.getToContext()) CXXMemberCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007048 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7049 ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007050}
7051
Balazs Keri3b30d652018-10-19 13:32:20 +00007052ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7053 ExpectedType ToTypeOrErr = import(E->getType());
7054 if (!ToTypeOrErr)
7055 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007056
Balazs Keri3b30d652018-10-19 13:32:20 +00007057 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7058 if (!ToLocationOrErr)
7059 return ToLocationOrErr.takeError();
7060
7061 return new (Importer.getToContext()) CXXThisExpr(
7062 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007063}
7064
Balazs Keri3b30d652018-10-19 13:32:20 +00007065ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7066 ExpectedType ToTypeOrErr = import(E->getType());
7067 if (!ToTypeOrErr)
7068 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007069
Balazs Keri3b30d652018-10-19 13:32:20 +00007070 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7071 if (!ToLocationOrErr)
7072 return ToLocationOrErr.takeError();
7073
7074 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7075 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007076}
7077
Balazs Keri3b30d652018-10-19 13:32:20 +00007078ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7079 auto Imp1 = importSeq(
7080 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7081 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7082 if (!Imp1)
7083 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007084
Balazs Keri3b30d652018-10-19 13:32:20 +00007085 Expr *ToBase;
7086 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7087 NestedNameSpecifierLoc ToQualifierLoc;
7088 ValueDecl *ToMemberDecl;
7089 QualType ToType;
7090 std::tie(
7091 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7092 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007093
Balazs Keri3b30d652018-10-19 13:32:20 +00007094 auto Imp2 = importSeq(
7095 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7096 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7097 if (!Imp2)
7098 return Imp2.takeError();
7099 NamedDecl *ToDecl;
7100 DeclarationName ToName;
7101 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7102 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007103
7104 DeclAccessPair ToFoundDecl =
7105 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007106
Balazs Keri3b30d652018-10-19 13:32:20 +00007107 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007108
7109 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007110 // FIXME: handle template arguments
7111 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Sean Callanan59721b32015-04-28 18:41:46 +00007112 }
7113
Balazs Keri3b30d652018-10-19 13:32:20 +00007114 return MemberExpr::Create(
7115 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7116 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
7117 ToMemberNameInfo, nullptr, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007118}
7119
Balazs Keri3b30d652018-10-19 13:32:20 +00007120ExpectedStmt
7121ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7122 auto Imp = importSeq(
7123 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7124 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7125 if (!Imp)
7126 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007127
Balazs Keri3b30d652018-10-19 13:32:20 +00007128 Expr *ToBase;
7129 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7130 NestedNameSpecifierLoc ToQualifierLoc;
7131 TypeSourceInfo *ToScopeTypeInfo;
7132 std::tie(
7133 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7134 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007135
7136 PseudoDestructorTypeStorage Storage;
7137 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7138 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007139 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7140 if (!ToDestroyedTypeLocOrErr)
7141 return ToDestroyedTypeLocOrErr.takeError();
7142 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007143 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007144 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7145 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7146 else
7147 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007148 }
7149
7150 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007151 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7152 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007153}
7154
Balazs Keri3b30d652018-10-19 13:32:20 +00007155ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007156 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007157 auto Imp = importSeq(
7158 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7159 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7160 if (!Imp)
7161 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007162
Balazs Keri3b30d652018-10-19 13:32:20 +00007163 QualType ToType;
7164 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7165 NestedNameSpecifierLoc ToQualifierLoc;
7166 NamedDecl *ToFirstQualifierFoundInScope;
7167 std::tie(
7168 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7169 ToFirstQualifierFoundInScope) = *Imp;
7170
7171 Expr *ToBase = nullptr;
7172 if (!E->isImplicitAccess()) {
7173 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7174 ToBase = *ToBaseOrErr;
7175 else
7176 return ToBaseOrErr.takeError();
7177 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007178
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007179 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007180 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007181 if (Error Err = ImportTemplateArgumentListInfo(
7182 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7183 ToTAInfo))
7184 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007185 ResInfo = &ToTAInfo;
7186 }
7187
Balazs Keri3b30d652018-10-19 13:32:20 +00007188 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7189 if (!ToMemberNameInfoOrErr)
7190 return ToMemberNameInfoOrErr.takeError();
7191 DeclarationNameInfo ToMemberNameInfo(
7192 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007193 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007194 if (Error Err = ImportDeclarationNameLoc(
7195 E->getMemberNameInfo(), ToMemberNameInfo))
7196 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007197
7198 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007199 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7200 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7201 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007202}
7203
Balazs Keri3b30d652018-10-19 13:32:20 +00007204ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007205ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007206 auto Imp = importSeq(
7207 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7208 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7209 if (!Imp)
7210 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007211
Balazs Keri3b30d652018-10-19 13:32:20 +00007212 NestedNameSpecifierLoc ToQualifierLoc;
7213 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7214 DeclarationName ToDeclName;
7215 std::tie(
7216 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7217 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007218
Balazs Keri3b30d652018-10-19 13:32:20 +00007219 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7220 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7221 return std::move(Err);
7222
7223 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007224 TemplateArgumentListInfo *ResInfo = nullptr;
7225 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007226 if (Error Err =
7227 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7228 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007229 ResInfo = &ToTAInfo;
7230 }
7231
7232 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007233 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7234 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007235}
7236
Balazs Keri3b30d652018-10-19 13:32:20 +00007237ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7238 CXXUnresolvedConstructExpr *E) {
7239 auto Imp = importSeq(
7240 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7241 if (!Imp)
7242 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007243
Balazs Keri3b30d652018-10-19 13:32:20 +00007244 SourceLocation ToLParenLoc, ToRParenLoc;
7245 TypeSourceInfo *ToTypeSourceInfo;
7246 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7247
7248 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7249 if (Error Err =
7250 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7251 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007252
7253 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007254 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7255 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007256}
7257
Balazs Keri3b30d652018-10-19 13:32:20 +00007258ExpectedStmt
7259ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7260 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7261 if (!ToNamingClassOrErr)
7262 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007263
Balazs Keri3b30d652018-10-19 13:32:20 +00007264 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7265 if (!ToQualifierLocOrErr)
7266 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007267
Balazs Keri3b30d652018-10-19 13:32:20 +00007268 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7269 if (!ToNameInfoOrErr)
7270 return ToNameInfoOrErr.takeError();
7271 DeclarationNameInfo ToNameInfo(
7272 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007273 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007274 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7275 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007276
7277 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007278 for (auto *D : E->decls())
7279 if (auto ToDOrErr = import(D))
7280 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007281 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007282 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007283
Balazs Keri3b30d652018-10-19 13:32:20 +00007284 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7285 TemplateArgumentListInfo ToTAInfo;
7286 if (Error Err = ImportTemplateArgumentListInfo(
7287 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7288 ToTAInfo))
7289 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007290
Balazs Keri3b30d652018-10-19 13:32:20 +00007291 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7292 if (!ToTemplateKeywordLocOrErr)
7293 return ToTemplateKeywordLocOrErr.takeError();
7294
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007295 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007296 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7297 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7298 ToDecls.begin(), ToDecls.end());
7299 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007300
7301 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007302 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7303 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7304 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007305}
7306
Balazs Keri3b30d652018-10-19 13:32:20 +00007307ExpectedStmt
7308ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7309 auto Imp1 = importSeq(
7310 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7311 E->getTemplateKeywordLoc());
7312 if (!Imp1)
7313 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007314
Balazs Keri3b30d652018-10-19 13:32:20 +00007315 QualType ToType;
7316 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7317 NestedNameSpecifierLoc ToQualifierLoc;
7318 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7319
7320 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7321 if (!Imp2)
7322 return Imp2.takeError();
7323 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7324 // Import additional name location/type info.
7325 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7326 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007327
7328 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007329 for (Decl *D : E->decls())
7330 if (auto ToDOrErr = import(D))
7331 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007332 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007333 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007334
7335 TemplateArgumentListInfo ToTAInfo;
7336 TemplateArgumentListInfo *ResInfo = nullptr;
7337 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007338 if (Error Err =
7339 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7340 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007341 ResInfo = &ToTAInfo;
7342 }
7343
Balazs Keri3b30d652018-10-19 13:32:20 +00007344 Expr *ToBase = nullptr;
7345 if (!E->isImplicitAccess()) {
7346 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7347 ToBase = *ToBaseOrErr;
7348 else
7349 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007350 }
7351
7352 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007353 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7354 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7355 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007356}
7357
Balazs Keri3b30d652018-10-19 13:32:20 +00007358ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7359 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7360 if (!Imp)
7361 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007362
Balazs Keri3b30d652018-10-19 13:32:20 +00007363 Expr *ToCallee;
7364 QualType ToType;
7365 SourceLocation ToRParenLoc;
7366 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007367
7368 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007369 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7370 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7371 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007372
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007373 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
7374 return new (Importer.getToContext()) CXXOperatorCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007375 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
7376 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007377 }
7378
Balazs Keri3b30d652018-10-19 13:32:20 +00007379 return new (Importer.getToContext()) CallExpr(
7380 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7381 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007382}
7383
Balazs Keri3b30d652018-10-19 13:32:20 +00007384ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7385 CXXRecordDecl *FromClass = E->getLambdaClass();
7386 auto ToClassOrErr = import(FromClass);
7387 if (!ToClassOrErr)
7388 return ToClassOrErr.takeError();
7389 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007390
7391 // NOTE: lambda classes are created with BeingDefined flag set up.
7392 // It means that ImportDefinition doesn't work for them and we should fill it
7393 // manually.
7394 if (ToClass->isBeingDefined()) {
7395 for (auto FromField : FromClass->fields()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007396 auto ToFieldOrErr = import(FromField);
7397 if (!ToFieldOrErr)
7398 return ToFieldOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007399 }
7400 }
7401
Balazs Keri3b30d652018-10-19 13:32:20 +00007402 auto ToCallOpOrErr = import(E->getCallOperator());
7403 if (!ToCallOpOrErr)
7404 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007405
7406 ToClass->completeDefinition();
7407
Balazs Keri3b30d652018-10-19 13:32:20 +00007408 SmallVector<LambdaCapture, 8> ToCaptures;
7409 ToCaptures.reserve(E->capture_size());
7410 for (const auto &FromCapture : E->captures()) {
7411 if (auto ToCaptureOrErr = import(FromCapture))
7412 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007413 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007414 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007415 }
7416
Balazs Keri3b30d652018-10-19 13:32:20 +00007417 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7418 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7419 return std::move(Err);
7420
7421 auto Imp = importSeq(
7422 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7423 if (!Imp)
7424 return Imp.takeError();
7425
7426 SourceRange ToIntroducerRange;
7427 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7428 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007429
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007430 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007431 Importer.getToContext(), ToClass, ToIntroducerRange,
7432 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7433 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7434 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007435}
7436
Sean Callanan8bca9962016-03-28 21:43:01 +00007437
Balazs Keri3b30d652018-10-19 13:32:20 +00007438ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7439 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7440 if (!Imp)
7441 return Imp.takeError();
7442
7443 SourceLocation ToLBraceLoc, ToRBraceLoc;
7444 QualType ToType;
7445 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7446
7447 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7448 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7449 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007450
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007451 ASTContext &ToCtx = Importer.getToContext();
7452 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007453 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7454 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007455
Balazs Keri3b30d652018-10-19 13:32:20 +00007456 if (E->hasArrayFiller()) {
7457 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7458 To->setArrayFiller(*ToFillerOrErr);
7459 else
7460 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007461 }
7462
Balazs Keri3b30d652018-10-19 13:32:20 +00007463 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7464 if (auto ToFDOrErr = import(FromFD))
7465 To->setInitializedFieldInUnion(*ToFDOrErr);
7466 else
7467 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007468 }
7469
Balazs Keri3b30d652018-10-19 13:32:20 +00007470 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7471 if (auto ToSyntFormOrErr = import(SyntForm))
7472 To->setSyntacticForm(*ToSyntFormOrErr);
7473 else
7474 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007475 }
7476
Gabor Martona20ce602018-09-03 13:10:53 +00007477 // Copy InitListExprBitfields, which are not handled in the ctor of
7478 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007479 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007480
7481 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007482}
7483
Balazs Keri3b30d652018-10-19 13:32:20 +00007484ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007485 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007486 ExpectedType ToTypeOrErr = import(E->getType());
7487 if (!ToTypeOrErr)
7488 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007489
Balazs Keri3b30d652018-10-19 13:32:20 +00007490 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7491 if (!ToSubExprOrErr)
7492 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007493
Balazs Keri3b30d652018-10-19 13:32:20 +00007494 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7495 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007496}
7497
Balazs Keri3b30d652018-10-19 13:32:20 +00007498ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007499 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007500 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7501 if (!Imp)
7502 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007503
Balazs Keri3b30d652018-10-19 13:32:20 +00007504 SourceLocation ToLocation;
7505 QualType ToType;
7506 CXXConstructorDecl *ToConstructor;
7507 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007508
7509 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007510 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7511 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007512}
7513
Balazs Keri3b30d652018-10-19 13:32:20 +00007514ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7515 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7516 if (!Imp)
7517 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007518
Balazs Keri3b30d652018-10-19 13:32:20 +00007519 QualType ToType;
7520 Expr *ToCommonExpr, *ToSubExpr;
7521 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007522
Balazs Keri3b30d652018-10-19 13:32:20 +00007523 return new (Importer.getToContext()) ArrayInitLoopExpr(
7524 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007525}
7526
Balazs Keri3b30d652018-10-19 13:32:20 +00007527ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7528 ExpectedType ToTypeOrErr = import(E->getType());
7529 if (!ToTypeOrErr)
7530 return ToTypeOrErr.takeError();
7531 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007532}
7533
Balazs Keri3b30d652018-10-19 13:32:20 +00007534ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7535 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7536 if (!ToBeginLocOrErr)
7537 return ToBeginLocOrErr.takeError();
7538
7539 auto ToFieldOrErr = import(E->getField());
7540 if (!ToFieldOrErr)
7541 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007542
7543 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007544 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007545}
7546
Balazs Keri3b30d652018-10-19 13:32:20 +00007547ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7548 auto Imp = importSeq(
7549 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7550 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7551 if (!Imp)
7552 return Imp.takeError();
7553
7554 QualType ToType;
7555 Expr *ToSubExpr;
7556 TypeSourceInfo *ToTypeInfoAsWritten;
7557 SourceLocation ToOperatorLoc, ToRParenLoc;
7558 SourceRange ToAngleBrackets;
7559 std::tie(
7560 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7561 ToAngleBrackets) = *Imp;
7562
Sean Callanandd2c1742016-05-16 20:48:03 +00007563 ExprValueKind VK = E->getValueKind();
7564 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007565 auto ToBasePathOrErr = ImportCastPath(E);
7566 if (!ToBasePathOrErr)
7567 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007568
Sean Callanandd2c1742016-05-16 20:48:03 +00007569 if (isa<CXXStaticCastExpr>(E)) {
7570 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007571 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7572 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007573 } else if (isa<CXXDynamicCastExpr>(E)) {
7574 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007575 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7576 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007577 } else if (isa<CXXReinterpretCastExpr>(E)) {
7578 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007579 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7580 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007581 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007582 return CXXConstCastExpr::Create(
7583 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7584 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007585 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007586 llvm_unreachable("Unknown cast type");
7587 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007588 }
7589}
7590
Balazs Keri3b30d652018-10-19 13:32:20 +00007591ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007592 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007593 auto Imp = importSeq(
7594 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7595 if (!Imp)
7596 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007597
Balazs Keri3b30d652018-10-19 13:32:20 +00007598 QualType ToType;
7599 SourceLocation ToExprLoc;
7600 NonTypeTemplateParmDecl *ToParameter;
7601 Expr *ToReplacement;
7602 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007603
7604 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007605 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007606}
7607
Balazs Keri3b30d652018-10-19 13:32:20 +00007608ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7609 auto Imp = importSeq(
7610 E->getType(), E->getBeginLoc(), E->getEndLoc());
7611 if (!Imp)
7612 return Imp.takeError();
7613
7614 QualType ToType;
7615 SourceLocation ToBeginLoc, ToEndLoc;
7616 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007617
7618 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007619 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7620 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007621
7622 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7623 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007624 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007625
7626 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007627 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7628 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007629}
7630
Balazs Keri3b30d652018-10-19 13:32:20 +00007631ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7632 ExpectedType ToTypeOrErr = import(E->getType());
7633 if (!ToTypeOrErr)
7634 return ToTypeOrErr.takeError();
7635
7636 auto ToSourceRangeOrErr = import(E->getSourceRange());
7637 if (!ToSourceRangeOrErr)
7638 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007639
7640 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007641 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7642 return new (Importer.getToContext()) CXXTypeidExpr(
7643 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7644 else
7645 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007646 }
7647
Balazs Keri3b30d652018-10-19 13:32:20 +00007648 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7649 if (!ToExprOperandOrErr)
7650 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007651
Balazs Keri3b30d652018-10-19 13:32:20 +00007652 return new (Importer.getToContext()) CXXTypeidExpr(
7653 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007654}
7655
Lang Hames19e07e12017-06-20 21:06:00 +00007656void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7657 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007658 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7659 if (auto ImportedOrErr = import(FromOverriddenMethod))
7660 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7661 (*ImportedOrErr)->getCanonicalDecl()));
7662 else
7663 consumeError(ImportedOrErr.takeError());
7664 }
Lang Hames19e07e12017-06-20 21:06:00 +00007665}
7666
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007667ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007668 ASTContext &FromContext, FileManager &FromFileManager,
7669 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007670 : ToContext(ToContext), FromContext(FromContext),
7671 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7672 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007673 ImportedDecls[FromContext.getTranslationUnitDecl()]
7674 = ToContext.getTranslationUnitDecl();
7675}
7676
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007677ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007678
7679QualType ASTImporter::Import(QualType FromT) {
7680 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007681 return {};
John McCall424cec92011-01-19 06:33:43 +00007682
Balazs Keri3b30d652018-10-19 13:32:20 +00007683 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007684
7685 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007686 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007687 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007688 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007689 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007690
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007691 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007692 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007693 ExpectedType ToTOrErr = Importer.Visit(FromTy);
7694 if (!ToTOrErr) {
7695 llvm::consumeError(ToTOrErr.takeError());
7696 return {};
7697 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007698
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007699 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007700 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007701
Balazs Keri3b30d652018-10-19 13:32:20 +00007702 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007703}
7704
Douglas Gregor62d311f2010-02-09 19:21:46 +00007705TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007706 if (!FromTSI)
7707 return FromTSI;
7708
7709 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007710 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007711 QualType T = Import(FromTSI->getType());
7712 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007713 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007714
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007715 return ToContext.getTrivialTypeSourceInfo(
7716 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007717}
7718
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007719Attr *ASTImporter::Import(const Attr *FromAttr) {
7720 Attr *ToAttr = FromAttr->clone(ToContext);
7721 ToAttr->setRange(Import(FromAttr->getRange()));
7722 return ToAttr;
7723}
7724
Sean Callanan59721b32015-04-28 18:41:46 +00007725Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7726 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7727 if (Pos != ImportedDecls.end()) {
7728 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007729 // FIXME: move this call to ImportDeclParts().
Balazs Keri3b30d652018-10-19 13:32:20 +00007730 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD))
7731 llvm::consumeError(std::move(Err));
Sean Callanan59721b32015-04-28 18:41:46 +00007732 return ToD;
7733 } else {
7734 return nullptr;
7735 }
7736}
7737
Douglas Gregor62d311f2010-02-09 19:21:46 +00007738Decl *ASTImporter::Import(Decl *FromD) {
7739 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007740 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007741
Douglas Gregord451ea92011-07-29 23:31:30 +00007742 ASTNodeImporter Importer(*this);
7743
Gabor Marton26f72a92018-07-12 09:42:05 +00007744 // Check whether we've already imported this declaration.
7745 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7746 if (ToD) {
7747 // If FromD has some updated flags after last import, apply it
7748 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007749 return ToD;
7750 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007751
7752 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007753 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
7754 if (!ToDOrErr) {
7755 llvm::consumeError(ToDOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007756 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007757 }
7758 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007759
Gabor Marton26f72a92018-07-12 09:42:05 +00007760 // Notify subclasses.
7761 Imported(FromD, ToD);
7762
Gabor Martonac3a5d62018-09-17 12:04:52 +00007763 updateFlags(FromD, ToD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007764 return ToD;
7765}
7766
Balazs Keri3b30d652018-10-19 13:32:20 +00007767Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007768 if (!FromDC)
7769 return FromDC;
7770
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007771 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007772 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007773 return nullptr;
7774
Fangrui Song6907ce22018-07-30 19:24:48 +00007775 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007776 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007777 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7778 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007779 if (ToRecord->isCompleteDefinition()) {
7780 // Do nothing.
7781 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007782 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7783 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7784 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007785 } else {
7786 CompleteDecl(ToRecord);
7787 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007788 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7789 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007790 if (ToEnum->isCompleteDefinition()) {
7791 // Do nothing.
7792 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007793 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7794 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7795 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007796 } else {
7797 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007798 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007799 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7800 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007801 if (ToClass->getDefinition()) {
7802 // Do nothing.
7803 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007804 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7805 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7806 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007807 } else {
7808 CompleteDecl(ToClass);
7809 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007810 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7811 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007812 if (ToProto->getDefinition()) {
7813 // Do nothing.
7814 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007815 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7816 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7817 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007818 } else {
7819 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007820 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007821 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007822
Douglas Gregor95d82832012-01-24 18:36:04 +00007823 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007824}
7825
7826Expr *ASTImporter::Import(Expr *FromE) {
7827 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007828 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007829
7830 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7831}
7832
7833Stmt *ASTImporter::Import(Stmt *FromS) {
7834 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007835 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007836
Fangrui Song6907ce22018-07-30 19:24:48 +00007837 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007838 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7839 if (Pos != ImportedStmts.end())
7840 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007841
Balazs Keri3b30d652018-10-19 13:32:20 +00007842 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007843 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007844 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
7845 if (!ToSOrErr) {
7846 llvm::consumeError(ToSOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007847 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007848 }
Craig Topper36250ad2014-05-12 05:36:57 +00007849
Balazs Keri3b30d652018-10-19 13:32:20 +00007850 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007851 auto *FromE = cast<Expr>(FromS);
7852 // Copy ExprBitfields, which may not be handled in Expr subclasses
7853 // constructors.
7854 ToE->setValueKind(FromE->getValueKind());
7855 ToE->setObjectKind(FromE->getObjectKind());
7856 ToE->setTypeDependent(FromE->isTypeDependent());
7857 ToE->setValueDependent(FromE->isValueDependent());
7858 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7859 ToE->setContainsUnexpandedParameterPack(
7860 FromE->containsUnexpandedParameterPack());
7861 }
7862
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007863 // Record the imported declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007864 ImportedStmts[FromS] = *ToSOrErr;
7865 return *ToSOrErr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007866}
7867
7868NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7869 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007870 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007871
Douglas Gregor90ebf252011-04-27 16:48:40 +00007872 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7873
7874 switch (FromNNS->getKind()) {
7875 case NestedNameSpecifier::Identifier:
7876 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7877 return NestedNameSpecifier::Create(ToContext, prefix, II);
7878 }
Craig Topper36250ad2014-05-12 05:36:57 +00007879 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007880
7881 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007882 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007883 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007884 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7885 }
Craig Topper36250ad2014-05-12 05:36:57 +00007886 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007887
7888 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007889 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007890 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007891 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7892 }
Craig Topper36250ad2014-05-12 05:36:57 +00007893 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007894
7895 case NestedNameSpecifier::Global:
7896 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7897
Nikola Smiljanic67860242014-09-26 00:28:20 +00007898 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007899 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007900 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007901 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7902 }
7903 return nullptr;
7904
Douglas Gregor90ebf252011-04-27 16:48:40 +00007905 case NestedNameSpecifier::TypeSpec:
7906 case NestedNameSpecifier::TypeSpecWithTemplate: {
7907 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7908 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007909 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007910 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007911 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007912 bTemplate, T.getTypePtr());
7913 }
7914 }
Craig Topper36250ad2014-05-12 05:36:57 +00007915 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007916 }
7917
7918 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007919}
7920
Douglas Gregor14454802011-02-25 02:25:35 +00007921NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007922 // Copied from NestedNameSpecifier mostly.
7923 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7924 NestedNameSpecifierLoc NNS = FromNNS;
7925
7926 // Push each of the nested-name-specifiers's onto a stack for
7927 // serialization in reverse order.
7928 while (NNS) {
7929 NestedNames.push_back(NNS);
7930 NNS = NNS.getPrefix();
7931 }
7932
7933 NestedNameSpecifierLocBuilder Builder;
7934
7935 while (!NestedNames.empty()) {
7936 NNS = NestedNames.pop_back_val();
7937 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7938 if (!Spec)
7939 return NestedNameSpecifierLoc();
7940
7941 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7942 switch (Kind) {
7943 case NestedNameSpecifier::Identifier:
7944 Builder.Extend(getToContext(),
7945 Spec->getAsIdentifier(),
7946 Import(NNS.getLocalBeginLoc()),
7947 Import(NNS.getLocalEndLoc()));
7948 break;
7949
7950 case NestedNameSpecifier::Namespace:
7951 Builder.Extend(getToContext(),
7952 Spec->getAsNamespace(),
7953 Import(NNS.getLocalBeginLoc()),
7954 Import(NNS.getLocalEndLoc()));
7955 break;
7956
7957 case NestedNameSpecifier::NamespaceAlias:
7958 Builder.Extend(getToContext(),
7959 Spec->getAsNamespaceAlias(),
7960 Import(NNS.getLocalBeginLoc()),
7961 Import(NNS.getLocalEndLoc()));
7962 break;
7963
7964 case NestedNameSpecifier::TypeSpec:
7965 case NestedNameSpecifier::TypeSpecWithTemplate: {
7966 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7967 QualType(Spec->getAsType(), 0));
7968 Builder.Extend(getToContext(),
7969 Import(NNS.getLocalBeginLoc()),
7970 TSI->getTypeLoc(),
7971 Import(NNS.getLocalEndLoc()));
7972 break;
7973 }
7974
7975 case NestedNameSpecifier::Global:
7976 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7977 break;
7978
7979 case NestedNameSpecifier::Super: {
7980 SourceRange ToRange = Import(NNS.getSourceRange());
7981 Builder.MakeSuper(getToContext(),
7982 Spec->getAsRecordDecl(),
7983 ToRange.getBegin(),
7984 ToRange.getEnd());
7985 }
7986 }
7987 }
7988
7989 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007990}
7991
Douglas Gregore2e50d332010-12-01 01:36:18 +00007992TemplateName ASTImporter::Import(TemplateName From) {
7993 switch (From.getKind()) {
7994 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007995 if (auto *ToTemplate =
7996 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007997 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007998
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007999 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008000
Douglas Gregore2e50d332010-12-01 01:36:18 +00008001 case TemplateName::OverloadedTemplate: {
8002 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8003 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008004 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008005 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00008006 ToTemplates.addDecl(To);
8007 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008008 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008009 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008010 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008011 ToTemplates.end());
8012 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008013
Douglas Gregore2e50d332010-12-01 01:36:18 +00008014 case TemplateName::QualifiedTemplate: {
8015 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
8016 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
8017 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008018 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008019
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008020 if (auto *ToTemplate =
8021 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00008022 return ToContext.getQualifiedTemplateName(Qualifier,
8023 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008024 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008025
8026 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008027 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008028
Douglas Gregore2e50d332010-12-01 01:36:18 +00008029 case TemplateName::DependentTemplate: {
8030 DependentTemplateName *DTN = From.getAsDependentTemplateName();
8031 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
8032 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008033 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008034
Douglas Gregore2e50d332010-12-01 01:36:18 +00008035 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008036 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008037 Import(DTN->getIdentifier()));
8038 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008039
Douglas Gregore2e50d332010-12-01 01:36:18 +00008040 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
8041 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008042
8043 case TemplateName::SubstTemplateTemplateParm: {
8044 SubstTemplateTemplateParmStorage *subst
8045 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008046 auto *param =
8047 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00008048 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008049 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00008050
8051 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008052 if (replacement.isNull())
8053 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008054
John McCalld9dfe3a2011-06-30 08:33:18 +00008055 return ToContext.getSubstTemplateTemplateParm(param, replacement);
8056 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008057
Douglas Gregor5590be02011-01-15 06:45:20 +00008058 case TemplateName::SubstTemplateTemplateParmPack: {
8059 SubstTemplateTemplateParmPackStorage *SubstPack
8060 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008061 auto *Param =
8062 cast_or_null<TemplateTemplateParmDecl>(
8063 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00008064 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008065 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008066
Douglas Gregor5590be02011-01-15 06:45:20 +00008067 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008068 Expected<TemplateArgument> ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00008069 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
Balazs Keri3b30d652018-10-19 13:32:20 +00008070 if (!ArgPack) {
8071 llvm::consumeError(ArgPack.takeError());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008072 return {};
Balazs Keri3b30d652018-10-19 13:32:20 +00008073 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008074
Balazs Keri3b30d652018-10-19 13:32:20 +00008075 return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00008076 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008077 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008078
Douglas Gregore2e50d332010-12-01 01:36:18 +00008079 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008080}
8081
Douglas Gregor62d311f2010-02-09 19:21:46 +00008082SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
8083 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008084 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008085
Douglas Gregor811663e2010-02-10 00:15:17 +00008086 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008087
Douglas Gregor811663e2010-02-10 00:15:17 +00008088 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00008089 FileID ToFileID = Import(Decomposed.first);
8090 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008091 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008092 SourceManager &ToSM = ToContext.getSourceManager();
8093 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00008094}
8095
8096SourceRange ASTImporter::Import(SourceRange FromRange) {
8097 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
8098}
8099
Douglas Gregor811663e2010-02-10 00:15:17 +00008100FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008101 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008102 if (Pos != ImportedFileIDs.end())
8103 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008104
Douglas Gregor811663e2010-02-10 00:15:17 +00008105 SourceManager &FromSM = FromContext.getSourceManager();
8106 SourceManager &ToSM = ToContext.getSourceManager();
8107 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008108
8109 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008110 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008111 if (FromSLoc.isExpansion()) {
8112 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
8113 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
8114 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
8115 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8116 SourceLocation MLoc;
8117 if (FromEx.isMacroArgExpansion()) {
8118 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
8119 } else {
8120 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
8121 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
8122 FromEx.isExpansionTokenRange());
8123 }
8124 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008125 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008126 // Include location of this file.
8127 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
8128
8129 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
8130 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8131 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8132 // disk again
8133 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8134 // than mmap the files several times.
8135 const FileEntry *Entry =
8136 ToFileManager.getFile(Cache->OrigEntry->getName());
8137 if (!Entry)
8138 return {};
8139 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
8140 FromSLoc.getFile().getFileCharacteristic());
8141 } else {
8142 // FIXME: We want to re-use the existing MemoryBuffer!
8143 const llvm::MemoryBuffer *FromBuf =
8144 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
8145 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8146 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8147 FromBuf->getBufferIdentifier());
8148 ToID = ToSM.createFileID(std::move(ToBuf),
8149 FromSLoc.getFile().getFileCharacteristic());
8150 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008151 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008152
Sebastian Redl99219f12010-09-30 01:03:06 +00008153 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008154 return ToID;
8155}
8156
Sean Callanandd2c1742016-05-16 20:48:03 +00008157CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8158 Expr *ToExpr = Import(From->getInit());
8159 if (!ToExpr && From->getInit())
8160 return nullptr;
8161
8162 if (From->isBaseInitializer()) {
8163 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8164 if (!ToTInfo && From->getTypeSourceInfo())
8165 return nullptr;
8166
8167 return new (ToContext) CXXCtorInitializer(
8168 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
8169 ToExpr, Import(From->getRParenLoc()),
8170 From->isPackExpansion() ? Import(From->getEllipsisLoc())
8171 : SourceLocation());
8172 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008173 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008174 if (!ToField && From->getMember())
8175 return nullptr;
8176
8177 return new (ToContext) CXXCtorInitializer(
8178 ToContext, ToField, Import(From->getMemberLocation()),
8179 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8180 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008181 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00008182 Import(From->getIndirectMember()));
8183 if (!ToIField && From->getIndirectMember())
8184 return nullptr;
8185
8186 return new (ToContext) CXXCtorInitializer(
8187 ToContext, ToIField, Import(From->getMemberLocation()),
8188 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8189 } else if (From->isDelegatingInitializer()) {
8190 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8191 if (!ToTInfo && From->getTypeSourceInfo())
8192 return nullptr;
8193
8194 return new (ToContext)
8195 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
8196 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008197 } else {
8198 return nullptr;
8199 }
8200}
8201
Aleksei Sidorina693b372016-09-28 10:16:56 +00008202CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
8203 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8204 if (Pos != ImportedCXXBaseSpecifiers.end())
8205 return Pos->second;
8206
8207 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
8208 Import(BaseSpec->getSourceRange()),
8209 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8210 BaseSpec->getAccessSpecifierAsWritten(),
8211 Import(BaseSpec->getTypeSourceInfo()),
8212 Import(BaseSpec->getEllipsisLoc()));
8213 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8214 return Imported;
8215}
8216
Balazs Keri3b30d652018-10-19 13:32:20 +00008217Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008218 Decl *To = Import(From);
8219 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008220 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008221
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008222 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008223 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008224
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008225 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00008226 if (!ToRecord->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008227 return Importer.ImportDefinition(
8228 cast<RecordDecl>(FromDC), ToRecord,
8229 ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008230 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00008231 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008232
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008233 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00008234 if (!ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008235 return Importer.ImportDefinition(
8236 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008237 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008238 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008239
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008240 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008241 if (!ToIFace->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008242 return Importer.ImportDefinition(
8243 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8244 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008245 }
8246 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008247
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008248 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008249 if (!ToProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008250 return Importer.ImportDefinition(
8251 cast<ObjCProtocolDecl>(FromDC), ToProto,
8252 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008253 }
8254 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008255
Balazs Keri3b30d652018-10-19 13:32:20 +00008256 return Importer.ImportDeclContext(FromDC, true);
Douglas Gregor0a791672011-01-18 03:11:38 +00008257 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008258
8259 return Error::success();
8260}
8261
8262void ASTImporter::ImportDefinition(Decl *From) {
8263 Error Err = ImportDefinition_New(From);
8264 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008265}
8266
Douglas Gregor96e578d2010-02-05 17:54:41 +00008267DeclarationName ASTImporter::Import(DeclarationName FromName) {
8268 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008269 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008270
8271 switch (FromName.getNameKind()) {
8272 case DeclarationName::Identifier:
8273 return Import(FromName.getAsIdentifierInfo());
8274
8275 case DeclarationName::ObjCZeroArgSelector:
8276 case DeclarationName::ObjCOneArgSelector:
8277 case DeclarationName::ObjCMultiArgSelector:
8278 return Import(FromName.getObjCSelector());
8279
8280 case DeclarationName::CXXConstructorName: {
8281 QualType T = Import(FromName.getCXXNameType());
8282 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008283 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008284
8285 return ToContext.DeclarationNames.getCXXConstructorName(
8286 ToContext.getCanonicalType(T));
8287 }
8288
8289 case DeclarationName::CXXDestructorName: {
8290 QualType T = Import(FromName.getCXXNameType());
8291 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008292 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008293
8294 return ToContext.DeclarationNames.getCXXDestructorName(
8295 ToContext.getCanonicalType(T));
8296 }
8297
Richard Smith35845152017-02-07 01:37:30 +00008298 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008299 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00008300 Import(FromName.getCXXDeductionGuideTemplate()));
8301 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008302 return {};
Richard Smith35845152017-02-07 01:37:30 +00008303 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
8304 }
8305
Douglas Gregor96e578d2010-02-05 17:54:41 +00008306 case DeclarationName::CXXConversionFunctionName: {
8307 QualType T = Import(FromName.getCXXNameType());
8308 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008309 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008310
8311 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8312 ToContext.getCanonicalType(T));
8313 }
8314
8315 case DeclarationName::CXXOperatorName:
8316 return ToContext.DeclarationNames.getCXXOperatorName(
8317 FromName.getCXXOverloadedOperator());
8318
8319 case DeclarationName::CXXLiteralOperatorName:
8320 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
8321 Import(FromName.getCXXLiteralIdentifier()));
8322
8323 case DeclarationName::CXXUsingDirective:
8324 // FIXME: STATICS!
8325 return DeclarationName::getUsingDirectiveName();
8326 }
8327
David Blaikiee4d798f2012-01-20 21:50:17 +00008328 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008329}
8330
Douglas Gregore2e50d332010-12-01 01:36:18 +00008331IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008332 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008333 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008334
Sean Callananf94ef1d2016-05-14 06:11:19 +00008335 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8336
8337 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8338 ToId->setBuiltinID(FromId->getBuiltinID());
8339
8340 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008341}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008342
Douglas Gregor43f54792010-02-17 02:12:47 +00008343Selector ASTImporter::Import(Selector FromSel) {
8344 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008345 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00008346
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008347 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008348 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8349 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8350 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8351 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8352}
8353
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008354DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8355 DeclContext *DC,
8356 unsigned IDNS,
8357 NamedDecl **Decls,
8358 unsigned NumDecls) {
8359 return Name;
8360}
8361
8362DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008363 if (LastDiagFromFrom)
8364 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8365 FromContext.getDiagnostics());
8366 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008367 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008368}
8369
8370DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008371 if (!LastDiagFromFrom)
8372 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8373 ToContext.getDiagnostics());
8374 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008375 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008376}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008377
Douglas Gregor2e15c842012-02-01 21:00:38 +00008378void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008379 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008380 if (!ID->getDefinition())
8381 ID->startDefinition();
8382 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008383 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008384 if (!PD->getDefinition())
8385 PD->startDefinition();
8386 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008387 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008388 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8389 TD->startDefinition();
8390 TD->setCompleteDefinition(true);
8391 }
8392 }
8393 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008394 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008395 }
8396}
8397
Gabor Marton26f72a92018-07-12 09:42:05 +00008398Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8399 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8400 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8401 "Try to import an already imported Decl");
8402 if (Pos != ImportedDecls.end())
8403 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008404 ImportedDecls[From] = To;
8405 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008406}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008407
Douglas Gregordd6006f2012-07-17 21:16:27 +00008408bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8409 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00008410 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00008411 = ImportedTypes.find(From.getTypePtr());
8412 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8413 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008414
Douglas Gregordd6006f2012-07-17 21:16:27 +00008415 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008416 getStructuralEquivalenceKind(*this), false,
8417 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008418 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008419}