blob: f6e25dd8a2ae686c69da40f25ad00a78ec573da6 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor96e578d2010-02-05 17:54:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTImporter class which imports AST nodes from one
10// context into another context.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000013
Douglas Gregor96e578d2010-02-05 17:54:41 +000014#include "clang/AST/ASTImporter.h"
Gabor Marton54058b52018-12-17 13:53:12 +000015#include "clang/AST/ASTImporterLookupTable.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);
Gabor Marton7df342a2018-12-17 12:42:12 +0000125 if (auto *TD = dyn_cast<TagDecl>(D))
126 return getCanonicalForwardRedeclChain<TagDecl>(TD);
Gabor Martonac3a5d62018-09-17 12:04:52 +0000127 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000128 }
129
Gabor Marton26f72a92018-07-12 09:42:05 +0000130 void updateFlags(const Decl *From, Decl *To) {
131 // Check if some flags or attrs are new in 'From' and copy into 'To'.
132 // FIXME: Other flags or attrs?
133 if (From->isUsed(false) && !To->isUsed(false))
134 To->setIsUsed();
135 }
136
Balazs Keri3b30d652018-10-19 13:32:20 +0000137 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
138 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
139 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000140 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000141
Balazs Keri3b30d652018-10-19 13:32:20 +0000142 // Use this instead of Importer.importInto .
143 template <typename ImportT>
144 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
145 return Importer.importInto(To, From);
146 }
147
148 // Use this to import pointers of specific type.
149 template <typename ImportT>
150 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000151 auto ToOrErr = Importer.Import_New(From);
152 if (ToOrErr)
153 To = cast_or_null<ImportT>(*ToOrErr);
154 return ToOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +0000155 }
156
157 // Call the import function of ASTImporter for a baseclass of type `T` and
158 // cast the return value to `T`.
159 template <typename T>
160 Expected<T *> import(T *From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000161 auto ToOrErr = Importer.Import_New(From);
162 if (!ToOrErr)
163 return ToOrErr.takeError();
164 return cast_or_null<T>(*ToOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +0000165 }
166
167 template <typename T>
168 Expected<T *> import(const T *From) {
169 return import(const_cast<T *>(From));
170 }
171
172 // Call the import function of ASTImporter for type `T`.
173 template <typename T>
174 Expected<T> import(const T &From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000175 return Importer.Import_New(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000176 }
177
178 template <class T>
179 Expected<std::tuple<T>>
180 importSeq(const T &From) {
181 Expected<T> ToOrErr = import(From);
182 if (!ToOrErr)
183 return ToOrErr.takeError();
184 return std::make_tuple<T>(std::move(*ToOrErr));
185 }
186
187 // Import multiple objects with a single function call.
188 // This should work for every type for which a variant of `import` exists.
189 // The arguments are processed from left to right and import is stopped on
190 // first error.
191 template <class THead, class... TTail>
192 Expected<std::tuple<THead, TTail...>>
193 importSeq(const THead &FromHead, const TTail &...FromTail) {
194 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
195 if (!ToHeadOrErr)
196 return ToHeadOrErr.takeError();
197 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
198 if (!ToTailOrErr)
199 return ToTailOrErr.takeError();
200 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
201 }
202
203// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000204 template <typename ToDeclT> struct CallOverloadedCreateFun {
205 template <typename... Args>
206 auto operator()(Args &&... args)
207 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
208 return ToDeclT::Create(std::forward<Args>(args)...);
209 }
210 };
211
212 // Always use these functions to create a Decl during import. There are
213 // certain tasks which must be done after the Decl was created, e.g. we
214 // must immediately register that as an imported Decl. The parameter `ToD`
215 // will be set to the newly created Decl or if had been imported before
216 // then to the already imported Decl. Returns a bool value set to true if
217 // the `FromD` had been imported before.
218 template <typename ToDeclT, typename FromDeclT, typename... Args>
219 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
220 Args &&... args) {
221 // There may be several overloads of ToDeclT::Create. We must make sure
222 // to call the one which would be chosen by the arguments, thus we use a
223 // wrapper for the overload set.
224 CallOverloadedCreateFun<ToDeclT> OC;
225 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
226 std::forward<Args>(args)...);
227 }
228 // Use this overload if a special Type is needed to be created. E.g if we
229 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
230 // then:
231 // TypedefNameDecl *ToTypedef;
232 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
233 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
234 typename... Args>
235 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
236 Args &&... args) {
237 CallOverloadedCreateFun<NewDeclT> OC;
238 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
239 std::forward<Args>(args)...);
240 }
241 // Use this version if a special create function must be
242 // used, e.g. CXXRecordDecl::CreateLambda .
243 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
244 typename... Args>
245 LLVM_NODISCARD bool
246 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
247 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000248 // FIXME: This code is needed later.
249 //if (Importer.getImportDeclErrorIfAny(FromD)) {
250 // ToD = nullptr;
251 // return true; // Already imported but with error.
252 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000253 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
254 if (ToD)
255 return true; // Already imported.
256 ToD = CreateFun(std::forward<Args>(args)...);
Gabor Marton54058b52018-12-17 13:53:12 +0000257 // Keep track of imported Decls.
258 Importer.MapImported(FromD, ToD);
259 Importer.AddToLookupTable(ToD);
Gabor Marton26f72a92018-07-12 09:42:05 +0000260 InitializeImportedDecl(FromD, ToD);
261 return false; // A new Decl is created.
262 }
263
264 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
Gabor Marton26f72a92018-07-12 09:42:05 +0000265 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
266 if (FromD->hasAttrs())
Balazs Keri57949eb2019-03-25 09:16:39 +0000267 for (const Attr *FromAttr : FromD->getAttrs()) {
268 // FIXME: Return of the error here is not possible until store of
269 // import errors is implemented.
270 auto ToAttrOrErr = import(FromAttr);
271 if (ToAttrOrErr)
272 ToD->addAttr(*ToAttrOrErr);
273 else
274 llvm::consumeError(ToAttrOrErr.takeError());
275 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000276 if (FromD->isUsed())
277 ToD->setIsUsed();
278 if (FromD->isImplicit())
279 ToD->setImplicit();
280 }
281
Gabor Martondd59d272019-03-19 14:04:50 +0000282 // Check if we have found an existing definition. Returns with that
283 // definition if yes, otherwise returns null.
284 Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) {
285 const FunctionDecl *Definition = nullptr;
286 if (D->doesThisDeclarationHaveABody() &&
287 FoundFunction->hasBody(Definition))
288 return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition));
289 return nullptr;
290 }
291
Douglas Gregor96e578d2010-02-05 17:54:41 +0000292 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000293 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000294
Balazs Keri3b30d652018-10-19 13:32:20 +0000295 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
296 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
297 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000298
299 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000300 ExpectedType VisitType(const Type *T);
301 ExpectedType VisitAtomicType(const AtomicType *T);
302 ExpectedType VisitBuiltinType(const BuiltinType *T);
303 ExpectedType VisitDecayedType(const DecayedType *T);
304 ExpectedType VisitComplexType(const ComplexType *T);
305 ExpectedType VisitPointerType(const PointerType *T);
306 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
307 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
308 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
309 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
310 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
311 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
312 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
313 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000314 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000315 ExpectedType VisitVectorType(const VectorType *T);
316 ExpectedType VisitExtVectorType(const ExtVectorType *T);
317 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
318 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
319 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
320 ExpectedType VisitParenType(const ParenType *T);
321 ExpectedType VisitTypedefType(const TypedefType *T);
322 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000323 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000324 ExpectedType VisitTypeOfType(const TypeOfType *T);
325 ExpectedType VisitDecltypeType(const DecltypeType *T);
326 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
327 ExpectedType VisitAutoType(const AutoType *T);
328 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000329 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000330 ExpectedType VisitRecordType(const RecordType *T);
331 ExpectedType VisitEnumType(const EnumType *T);
332 ExpectedType VisitAttributedType(const AttributedType *T);
333 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
334 ExpectedType VisitSubstTemplateTypeParmType(
335 const SubstTemplateTypeParmType *T);
336 ExpectedType VisitTemplateSpecializationType(
337 const TemplateSpecializationType *T);
338 ExpectedType VisitElaboratedType(const ElaboratedType *T);
339 ExpectedType VisitDependentNameType(const DependentNameType *T);
340 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
341 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000342 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000343 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
344 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
345 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000346
347 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000348 Error ImportDeclParts(
349 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
350 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
351 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
352 Error ImportDeclarationNameLoc(
353 const DeclarationNameInfo &From, DeclarationNameInfo &To);
354 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
355 Error ImportDeclContext(
356 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
357 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000358
Balazs Keri3b30d652018-10-19 13:32:20 +0000359 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000360
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000361 using Designator = DesignatedInitExpr::Designator;
362
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000363 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000364 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000365 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000366 /// nothing (if minimal import is set) or might be everything (if minimal
367 /// import is not set).
368 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000369 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000370 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000371 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000372 /// DeclContext.
373 IDK_Basic
374 };
375
Douglas Gregor2e15c842012-02-01 21:00:38 +0000376 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
377 return IDK == IDK_Everything ||
378 (IDK == IDK_Default && !Importer.isMinimalImport());
379 }
380
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 Error ImportInitializer(VarDecl *From, VarDecl *To);
382 Error ImportDefinition(
383 RecordDecl *From, RecordDecl *To,
384 ImportDefinitionKind Kind = IDK_Default);
385 Error ImportDefinition(
386 EnumDecl *From, EnumDecl *To,
387 ImportDefinitionKind Kind = IDK_Default);
388 Error ImportDefinition(
389 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
390 ImportDefinitionKind Kind = IDK_Default);
391 Error ImportDefinition(
392 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
393 ImportDefinitionKind Kind = IDK_Default);
Balazs Keri3b30d652018-10-19 13:32:20 +0000394 Error ImportTemplateArguments(
395 const TemplateArgument *FromArgs, unsigned NumFromArgs,
396 SmallVectorImpl<TemplateArgument> &ToArgs);
397 Expected<TemplateArgument>
398 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000399
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000400 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000401 Error ImportTemplateArgumentListInfo(
402 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000403
404 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000405 Error ImportTemplateArgumentListInfo(
406 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
407 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000408
Gabor Marton5254e642018-06-27 13:32:50 +0000409 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000410 using FunctionTemplateAndArgsTy =
411 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
412 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000413 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
414 FunctionDecl *FromFD);
415
Balazs Keri3b30d652018-10-19 13:32:20 +0000416 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000417
Shafik Yaghmour96b3d202019-01-28 21:55:33 +0000418 Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD);
419
Gabor Marton458d1452019-02-14 13:07:03 +0000420 template <typename T>
421 bool hasSameVisibilityContext(T *Found, T *From);
422
Gabor Marton950fb572018-07-17 12:39:27 +0000423 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000424 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
425 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000426 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
427 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000428 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000429 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000430 bool IsStructuralMatch(FunctionTemplateDecl *From,
431 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000432 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000433 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000434 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000435 ExpectedDecl VisitDecl(Decl *D);
436 ExpectedDecl VisitImportDecl(ImportDecl *D);
437 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
438 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
439 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
440 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
441 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
442 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
443 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
444 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
445 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
446 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
447 ExpectedDecl VisitLabelDecl(LabelDecl *D);
448 ExpectedDecl VisitEnumDecl(EnumDecl *D);
449 ExpectedDecl VisitRecordDecl(RecordDecl *D);
450 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
451 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
452 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
453 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
454 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
455 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
456 ExpectedDecl VisitFieldDecl(FieldDecl *D);
457 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
458 ExpectedDecl VisitFriendDecl(FriendDecl *D);
459 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
460 ExpectedDecl VisitVarDecl(VarDecl *D);
461 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
462 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
463 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
464 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
465 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
466 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
467 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
468 ExpectedDecl VisitUsingDecl(UsingDecl *D);
469 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
470 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
471 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
472 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000473
Balazs Keri3b30d652018-10-19 13:32:20 +0000474 Expected<ObjCTypeParamList *>
475 ImportObjCTypeParamList(ObjCTypeParamList *list);
476
477 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
478 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
479 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
480 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
481 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
482 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
483 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
484 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
485 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
486 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000487 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000488 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
489 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
490 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000491
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000492 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000493 ExpectedStmt VisitStmt(Stmt *S);
494 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
495 ExpectedStmt VisitDeclStmt(DeclStmt *S);
496 ExpectedStmt VisitNullStmt(NullStmt *S);
497 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
498 ExpectedStmt VisitCaseStmt(CaseStmt *S);
499 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
500 ExpectedStmt VisitLabelStmt(LabelStmt *S);
501 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
502 ExpectedStmt VisitIfStmt(IfStmt *S);
503 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
504 ExpectedStmt VisitWhileStmt(WhileStmt *S);
505 ExpectedStmt VisitDoStmt(DoStmt *S);
506 ExpectedStmt VisitForStmt(ForStmt *S);
507 ExpectedStmt VisitGotoStmt(GotoStmt *S);
508 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
509 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
510 ExpectedStmt VisitBreakStmt(BreakStmt *S);
511 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000512 // FIXME: MSAsmStmt
513 // FIXME: SEHExceptStmt
514 // FIXME: SEHFinallyStmt
515 // FIXME: SEHTryStmt
516 // FIXME: SEHLeaveStmt
517 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000518 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
519 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
520 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000521 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000522 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
523 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
524 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
525 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
526 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
527 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
528 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000529
530 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000531 ExpectedStmt VisitExpr(Expr *E);
532 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
Tom Roeder521f0042019-02-26 19:26:41 +0000533 ExpectedStmt VisitChooseExpr(ChooseExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000534 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
535 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
536 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
537 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
538 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
539 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
540 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
541 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
542 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
543 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
544 ExpectedStmt VisitStringLiteral(StringLiteral *E);
545 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
546 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
547 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000548 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000549 ExpectedStmt VisitParenExpr(ParenExpr *E);
550 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
551 ExpectedStmt VisitStmtExpr(StmtExpr *E);
552 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
553 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
554 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
555 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
556 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
557 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
558 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
559 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
560 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
561 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
562 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
563 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
564 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
565 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
566 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
567 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
568 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
569 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
570 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
571 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
572 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
573 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
574 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
575 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
576 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
577 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
578 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
579 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
580 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
581 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
582 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
583 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
584 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
585 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
586 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
587 ExpectedStmt VisitMemberExpr(MemberExpr *E);
588 ExpectedStmt VisitCallExpr(CallExpr *E);
589 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
590 ExpectedStmt VisitInitListExpr(InitListExpr *E);
591 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
592 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
593 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
594 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
595 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
596 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
597 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
598 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
599 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000600
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000601 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000602 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000603 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000604 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
605 Expected<ItemT> ToOrErr = import(*Ibegin);
606 if (!ToOrErr)
607 return ToOrErr.takeError();
608 *Obegin = *ToOrErr;
609 }
610 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000611 }
612
Balazs Keri3b30d652018-10-19 13:32:20 +0000613 // Import every item from a container structure into an output container.
614 // If error occurs, stops at first error and returns the error.
615 // The output container should have space for all needed elements (it is not
616 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000617 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000618 Error ImportContainerChecked(
619 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
620 return ImportArrayChecked(
621 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000622 }
623
624 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000625 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000626 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
627 }
Lang Hames19e07e12017-06-20 21:06:00 +0000628
Lang Hames19e07e12017-06-20 21:06:00 +0000629 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000630
Balazs Keri3b30d652018-10-19 13:32:20 +0000631 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
632 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000633 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000634
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000635template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000636Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000637 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
638 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000639 auto ToLAngleLocOrErr = import(FromLAngleLoc);
640 if (!ToLAngleLocOrErr)
641 return ToLAngleLocOrErr.takeError();
642 auto ToRAngleLocOrErr = import(FromRAngleLoc);
643 if (!ToRAngleLocOrErr)
644 return ToRAngleLocOrErr.takeError();
645
646 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
647 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
648 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000649 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000650 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000651}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000652
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000653template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000654Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000655 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
656 return ImportTemplateArgumentListInfo(
657 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
658}
659
660template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000661Error ASTNodeImporter::ImportTemplateArgumentListInfo<
662 ASTTemplateArgumentListInfo>(
663 const ASTTemplateArgumentListInfo &From,
664 TemplateArgumentListInfo &Result) {
665 return ImportTemplateArgumentListInfo(
666 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000667}
668
Balazs Keri3b30d652018-10-19 13:32:20 +0000669Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000670ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
671 FunctionDecl *FromFD) {
672 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000673 FunctionDecl::TK_FunctionTemplateSpecialization);
674
675 FunctionTemplateAndArgsTy Result;
676
Gabor Marton5254e642018-06-27 13:32:50 +0000677 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000678 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
679 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000680
681 // Import template arguments.
682 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000683 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
684 std::get<1>(Result)))
685 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000686
Balazs Keri3b30d652018-10-19 13:32:20 +0000687 return Result;
688}
689
690template <>
691Expected<TemplateParameterList *>
692ASTNodeImporter::import(TemplateParameterList *From) {
693 SmallVector<NamedDecl *, 4> To(From->size());
694 if (Error Err = ImportContainerChecked(*From, To))
695 return std::move(Err);
696
697 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
698 if (!ToRequiresClause)
699 return ToRequiresClause.takeError();
700
701 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
702 if (!ToTemplateLocOrErr)
703 return ToTemplateLocOrErr.takeError();
704 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
705 if (!ToLAngleLocOrErr)
706 return ToLAngleLocOrErr.takeError();
707 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
708 if (!ToRAngleLocOrErr)
709 return ToRAngleLocOrErr.takeError();
710
711 return TemplateParameterList::Create(
712 Importer.getToContext(),
713 *ToTemplateLocOrErr,
714 *ToLAngleLocOrErr,
715 To,
716 *ToRAngleLocOrErr,
717 *ToRequiresClause);
718}
719
720template <>
721Expected<TemplateArgument>
722ASTNodeImporter::import(const TemplateArgument &From) {
723 switch (From.getKind()) {
724 case TemplateArgument::Null:
725 return TemplateArgument();
726
727 case TemplateArgument::Type: {
728 ExpectedType ToTypeOrErr = import(From.getAsType());
729 if (!ToTypeOrErr)
730 return ToTypeOrErr.takeError();
731 return TemplateArgument(*ToTypeOrErr);
732 }
733
734 case TemplateArgument::Integral: {
735 ExpectedType ToTypeOrErr = import(From.getIntegralType());
736 if (!ToTypeOrErr)
737 return ToTypeOrErr.takeError();
738 return TemplateArgument(From, *ToTypeOrErr);
739 }
740
741 case TemplateArgument::Declaration: {
742 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
743 if (!ToOrErr)
744 return ToOrErr.takeError();
745 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
746 if (!ToTypeOrErr)
747 return ToTypeOrErr.takeError();
748 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
749 }
750
751 case TemplateArgument::NullPtr: {
752 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
753 if (!ToTypeOrErr)
754 return ToTypeOrErr.takeError();
755 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
756 }
757
758 case TemplateArgument::Template: {
759 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
760 if (!ToTemplateOrErr)
761 return ToTemplateOrErr.takeError();
762
763 return TemplateArgument(*ToTemplateOrErr);
764 }
765
766 case TemplateArgument::TemplateExpansion: {
767 Expected<TemplateName> ToTemplateOrErr =
768 import(From.getAsTemplateOrTemplatePattern());
769 if (!ToTemplateOrErr)
770 return ToTemplateOrErr.takeError();
771
772 return TemplateArgument(
773 *ToTemplateOrErr, From.getNumTemplateExpansions());
774 }
775
776 case TemplateArgument::Expression:
777 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
778 return TemplateArgument(*ToExpr);
779 else
780 return ToExpr.takeError();
781
782 case TemplateArgument::Pack: {
783 SmallVector<TemplateArgument, 2> ToPack;
784 ToPack.reserve(From.pack_size());
785 if (Error Err = ImportTemplateArguments(
786 From.pack_begin(), From.pack_size(), ToPack))
787 return std::move(Err);
788
789 return TemplateArgument(
790 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
791 }
792 }
793
794 llvm_unreachable("Invalid template argument kind");
795}
796
797template <>
798Expected<TemplateArgumentLoc>
799ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
800 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
801 if (!ArgOrErr)
802 return ArgOrErr.takeError();
803 TemplateArgument Arg = *ArgOrErr;
804
805 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
806
807 TemplateArgumentLocInfo ToInfo;
808 if (Arg.getKind() == TemplateArgument::Expression) {
809 ExpectedExpr E = import(FromInfo.getAsExpr());
810 if (!E)
811 return E.takeError();
812 ToInfo = TemplateArgumentLocInfo(*E);
813 } else if (Arg.getKind() == TemplateArgument::Type) {
814 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
815 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
816 else
817 return TSIOrErr.takeError();
818 } else {
819 auto ToTemplateQualifierLocOrErr =
820 import(FromInfo.getTemplateQualifierLoc());
821 if (!ToTemplateQualifierLocOrErr)
822 return ToTemplateQualifierLocOrErr.takeError();
823 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
824 if (!ToTemplateNameLocOrErr)
825 return ToTemplateNameLocOrErr.takeError();
826 auto ToTemplateEllipsisLocOrErr =
827 import(FromInfo.getTemplateEllipsisLoc());
828 if (!ToTemplateEllipsisLocOrErr)
829 return ToTemplateEllipsisLocOrErr.takeError();
830
831 ToInfo = TemplateArgumentLocInfo(
832 *ToTemplateQualifierLocOrErr,
833 *ToTemplateNameLocOrErr,
834 *ToTemplateEllipsisLocOrErr);
835 }
836
837 return TemplateArgumentLoc(Arg, ToInfo);
838}
839
840template <>
841Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
842 if (DG.isNull())
843 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
844 size_t NumDecls = DG.end() - DG.begin();
845 SmallVector<Decl *, 1> ToDecls;
846 ToDecls.reserve(NumDecls);
847 for (Decl *FromD : DG) {
848 if (auto ToDOrErr = import(FromD))
849 ToDecls.push_back(*ToDOrErr);
850 else
851 return ToDOrErr.takeError();
852 }
853 return DeclGroupRef::Create(Importer.getToContext(),
854 ToDecls.begin(),
855 NumDecls);
856}
857
858template <>
859Expected<ASTNodeImporter::Designator>
860ASTNodeImporter::import(const Designator &D) {
861 if (D.isFieldDesignator()) {
862 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
863
864 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
865 if (!ToDotLocOrErr)
866 return ToDotLocOrErr.takeError();
867
868 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
869 if (!ToFieldLocOrErr)
870 return ToFieldLocOrErr.takeError();
871
872 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
873 }
874
875 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
876 if (!ToLBracketLocOrErr)
877 return ToLBracketLocOrErr.takeError();
878
879 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
880 if (!ToRBracketLocOrErr)
881 return ToRBracketLocOrErr.takeError();
882
883 if (D.isArrayDesignator())
884 return Designator(D.getFirstExprIndex(),
885 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
886
887 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
888 if (!ToEllipsisLocOrErr)
889 return ToEllipsisLocOrErr.takeError();
890
891 assert(D.isArrayRangeDesignator());
892 return Designator(
893 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
894 *ToRBracketLocOrErr);
895}
896
897template <>
898Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
899 VarDecl *Var = nullptr;
900 if (From.capturesVariable()) {
901 if (auto VarOrErr = import(From.getCapturedVar()))
902 Var = *VarOrErr;
903 else
904 return VarOrErr.takeError();
905 }
906
907 auto LocationOrErr = import(From.getLocation());
908 if (!LocationOrErr)
909 return LocationOrErr.takeError();
910
911 SourceLocation EllipsisLoc;
912 if (From.isPackExpansion())
913 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
914 return std::move(Err);
915
916 return LambdaCapture(
917 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
918 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000919}
920
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000921} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000922
Douglas Gregor3996e242010-02-15 22:01:00 +0000923//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000924// Import Types
925//----------------------------------------------------------------------------
926
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000927using namespace clang;
928
Balazs Keri3b30d652018-10-19 13:32:20 +0000929ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000930 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
931 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000932 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000933}
934
Balazs Keri3b30d652018-10-19 13:32:20 +0000935ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
936 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
937 if (!UnderlyingTypeOrErr)
938 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000939
Balazs Keri3b30d652018-10-19 13:32:20 +0000940 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000941}
942
Balazs Keri3b30d652018-10-19 13:32:20 +0000943ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000944 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000945#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
946 case BuiltinType::Id: \
947 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000948#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000949#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
950 case BuiltinType::Id: \
951 return Importer.getToContext().Id##Ty;
952#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000953#define SHARED_SINGLETON_TYPE(Expansion)
954#define BUILTIN_TYPE(Id, SingletonId) \
955 case BuiltinType::Id: return Importer.getToContext().SingletonId;
956#include "clang/AST/BuiltinTypes.def"
957
958 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
959 // context supports C++.
960
961 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
962 // context supports ObjC.
963
Douglas Gregor96e578d2010-02-05 17:54:41 +0000964 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000965 // The context we're importing from has an unsigned 'char'. If we're
966 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000967 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000968 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000969 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000970
Douglas Gregor96e578d2010-02-05 17:54:41 +0000971 return Importer.getToContext().CharTy;
972
Douglas Gregor96e578d2010-02-05 17:54:41 +0000973 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000974 // The context we're importing from has an unsigned 'char'. If we're
975 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000976 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000977 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000978 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000979
Douglas Gregor96e578d2010-02-05 17:54:41 +0000980 return Importer.getToContext().CharTy;
981
Chris Lattnerad3467e2010-12-25 23:25:43 +0000982 case BuiltinType::WChar_S:
983 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000984 // FIXME: If not in C++, shall we translate to the C equivalent of
985 // wchar_t?
986 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000988
989 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000990}
991
Balazs Keri3b30d652018-10-19 13:32:20 +0000992ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
993 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
994 if (!ToOriginalTypeOrErr)
995 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +0000996
Balazs Keri3b30d652018-10-19 13:32:20 +0000997 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000998}
999
Balazs Keri3b30d652018-10-19 13:32:20 +00001000ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1001 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1002 if (!ToElementTypeOrErr)
1003 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001004
Balazs Keri3b30d652018-10-19 13:32:20 +00001005 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001006}
1007
Balazs Keri3b30d652018-10-19 13:32:20 +00001008ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1009 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1010 if (!ToPointeeTypeOrErr)
1011 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001012
Balazs Keri3b30d652018-10-19 13:32:20 +00001013 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014}
1015
Balazs Keri3b30d652018-10-19 13:32:20 +00001016ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001017 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001018 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1019 if (!ToPointeeTypeOrErr)
1020 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001021
Balazs Keri3b30d652018-10-19 13:32:20 +00001022 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001023}
1024
Balazs Keri3b30d652018-10-19 13:32:20 +00001025ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001026ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001027 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001028 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1029 if (!ToPointeeTypeOrErr)
1030 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001031
Balazs Keri3b30d652018-10-19 13:32:20 +00001032 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001033}
1034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001036ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001037 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001038 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1039 if (!ToPointeeTypeOrErr)
1040 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001041
Balazs Keri3b30d652018-10-19 13:32:20 +00001042 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001043}
1044
Balazs Keri3b30d652018-10-19 13:32:20 +00001045ExpectedType
1046ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001047 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001048 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1049 if (!ToPointeeTypeOrErr)
1050 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001051
Balazs Keri3b30d652018-10-19 13:32:20 +00001052 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1053 if (!ClassTypeOrErr)
1054 return ClassTypeOrErr.takeError();
1055
1056 return Importer.getToContext().getMemberPointerType(
1057 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001058}
1059
Balazs Keri3b30d652018-10-19 13:32:20 +00001060ExpectedType
1061ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1062 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1063 if (!ToElementTypeOrErr)
1064 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001065
Balazs Keri3b30d652018-10-19 13:32:20 +00001066 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001067 T->getSize(),
1068 T->getSizeModifier(),
1069 T->getIndexTypeCVRQualifiers());
1070}
1071
Balazs Keri3b30d652018-10-19 13:32:20 +00001072ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001073ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001074 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1075 if (!ToElementTypeOrErr)
1076 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001077
Balazs Keri3b30d652018-10-19 13:32:20 +00001078 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001079 T->getSizeModifier(),
1080 T->getIndexTypeCVRQualifiers());
1081}
1082
Balazs Keri3b30d652018-10-19 13:32:20 +00001083ExpectedType
1084ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1085 QualType ToElementType;
1086 Expr *ToSizeExpr;
1087 SourceRange ToBracketsRange;
1088 if (auto Imp = importSeq(
1089 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1090 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1091 else
1092 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001093
Balazs Keri3b30d652018-10-19 13:32:20 +00001094 return Importer.getToContext().getVariableArrayType(
1095 ToElementType, ToSizeExpr, T->getSizeModifier(),
1096 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001097}
1098
Balazs Keri3b30d652018-10-19 13:32:20 +00001099ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001100 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001101 QualType ToElementType;
1102 Expr *ToSizeExpr;
1103 SourceRange ToBracketsRange;
1104 if (auto Imp = importSeq(
1105 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1106 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1107 else
1108 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001109 // SizeExpr may be null if size is not specified directly.
1110 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001111
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001112 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001113 ToElementType, ToSizeExpr, T->getSizeModifier(),
1114 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001115}
1116
Balazs Keri3b30d652018-10-19 13:32:20 +00001117ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1118 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1119 if (!ToElementTypeOrErr)
1120 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001121
Balazs Keri3b30d652018-10-19 13:32:20 +00001122 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001123 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001124 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001125}
1126
Balazs Keri3b30d652018-10-19 13:32:20 +00001127ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1128 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1129 if (!ToElementTypeOrErr)
1130 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001131
Balazs Keri3b30d652018-10-19 13:32:20 +00001132 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001133 T->getNumElements());
1134}
1135
Balazs Keri3b30d652018-10-19 13:32:20 +00001136ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001137ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001138 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001139 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001140 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1141 if (!ToReturnTypeOrErr)
1142 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001143
Balazs Keri3b30d652018-10-19 13:32:20 +00001144 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001145 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001146}
1147
Balazs Keri3b30d652018-10-19 13:32:20 +00001148ExpectedType
1149ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1150 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1151 if (!ToReturnTypeOrErr)
1152 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001153
Douglas Gregor96e578d2010-02-05 17:54:41 +00001154 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001155 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001156 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001157 ExpectedType TyOrErr = import(A);
1158 if (!TyOrErr)
1159 return TyOrErr.takeError();
1160 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001161 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001162
Douglas Gregor96e578d2010-02-05 17:54:41 +00001163 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001164 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001165 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001166 ExpectedType TyOrErr = import(E);
1167 if (!TyOrErr)
1168 return TyOrErr.takeError();
1169 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001170 }
John McCalldb40c7f2010-12-14 08:05:40 +00001171
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001172 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1173 FunctionProtoType::ExtProtoInfo ToEPI;
1174
Balazs Keri3b30d652018-10-19 13:32:20 +00001175 auto Imp = importSeq(
1176 FromEPI.ExceptionSpec.NoexceptExpr,
1177 FromEPI.ExceptionSpec.SourceDecl,
1178 FromEPI.ExceptionSpec.SourceTemplate);
1179 if (!Imp)
1180 return Imp.takeError();
1181
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001182 ToEPI.ExtInfo = FromEPI.ExtInfo;
1183 ToEPI.Variadic = FromEPI.Variadic;
1184 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1185 ToEPI.TypeQuals = FromEPI.TypeQuals;
1186 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001187 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1188 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001189 std::tie(
1190 ToEPI.ExceptionSpec.NoexceptExpr,
1191 ToEPI.ExceptionSpec.SourceDecl,
1192 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001193
Balazs Keri3b30d652018-10-19 13:32:20 +00001194 return Importer.getToContext().getFunctionType(
1195 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001196}
1197
Balazs Keri3b30d652018-10-19 13:32:20 +00001198ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001199 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001200 UnresolvedUsingTypenameDecl *ToD;
1201 Decl *ToPrevD;
1202 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1203 std::tie(ToD, ToPrevD) = *Imp;
1204 else
1205 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001206
Balazs Keri3b30d652018-10-19 13:32:20 +00001207 return Importer.getToContext().getTypeDeclType(
1208 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001209}
1210
Balazs Keri3b30d652018-10-19 13:32:20 +00001211ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1212 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1213 if (!ToInnerTypeOrErr)
1214 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001215
Balazs Keri3b30d652018-10-19 13:32:20 +00001216 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001217}
1218
Balazs Keri3b30d652018-10-19 13:32:20 +00001219ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1220 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1221 if (!ToDeclOrErr)
1222 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001223
Balazs Keri3b30d652018-10-19 13:32:20 +00001224 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001225}
1226
Balazs Keri3b30d652018-10-19 13:32:20 +00001227ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1228 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1229 if (!ToExprOrErr)
1230 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001231
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001233}
1234
Balazs Keri3b30d652018-10-19 13:32:20 +00001235ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1236 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1237 if (!ToUnderlyingTypeOrErr)
1238 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001239
Balazs Keri3b30d652018-10-19 13:32:20 +00001240 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001241}
1242
Balazs Keri3b30d652018-10-19 13:32:20 +00001243ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001244 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001245 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1246 if (!ToExprOrErr)
1247 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001248
Balazs Keri3b30d652018-10-19 13:32:20 +00001249 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1250 if (!ToUnderlyingTypeOrErr)
1251 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001252
Balazs Keri3b30d652018-10-19 13:32:20 +00001253 return Importer.getToContext().getDecltypeType(
1254 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001255}
1256
Balazs Keri3b30d652018-10-19 13:32:20 +00001257ExpectedType
1258ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1259 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1260 if (!ToBaseTypeOrErr)
1261 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001262
Balazs Keri3b30d652018-10-19 13:32:20 +00001263 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1264 if (!ToUnderlyingTypeOrErr)
1265 return ToUnderlyingTypeOrErr.takeError();
1266
1267 return Importer.getToContext().getUnaryTransformType(
1268 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001269}
1270
Balazs Keri3b30d652018-10-19 13:32:20 +00001271ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001272 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001273 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1274 if (!ToDeducedTypeOrErr)
1275 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001276
Balazs Keri3b30d652018-10-19 13:32:20 +00001277 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1278 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001279 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001280}
1281
Balazs Keri3b30d652018-10-19 13:32:20 +00001282ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001283 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001284 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1285 if (!ToDeclOrErr)
1286 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001287
Balazs Keri3b30d652018-10-19 13:32:20 +00001288 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1289 if (!ToInjTypeOrErr)
1290 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001291
1292 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1293 // See comments in InjectedClassNameType definition for details
1294 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1295 enum {
1296 TypeAlignmentInBits = 4,
1297 TypeAlignment = 1 << TypeAlignmentInBits
1298 };
1299
1300 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001301 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001302}
1303
Balazs Keri3b30d652018-10-19 13:32:20 +00001304ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1305 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1306 if (!ToDeclOrErr)
1307 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001308
Balazs Keri3b30d652018-10-19 13:32:20 +00001309 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001310}
1311
Balazs Keri3b30d652018-10-19 13:32:20 +00001312ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1313 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1314 if (!ToDeclOrErr)
1315 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001316
Balazs Keri3b30d652018-10-19 13:32:20 +00001317 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001318}
1319
Balazs Keri3b30d652018-10-19 13:32:20 +00001320ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1321 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1322 if (!ToModifiedTypeOrErr)
1323 return ToModifiedTypeOrErr.takeError();
1324 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1325 if (!ToEquivalentTypeOrErr)
1326 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001327
1328 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001329 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001330}
1331
Balazs Keri3b30d652018-10-19 13:32:20 +00001332ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001333 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001334 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1335 if (!ToDeclOrErr)
1336 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001337
1338 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001339 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001340}
1341
Balazs Keri3b30d652018-10-19 13:32:20 +00001342ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001343 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001344 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1345 if (!ReplacedOrErr)
1346 return ReplacedOrErr.takeError();
1347 const TemplateTypeParmType *Replaced =
1348 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001349
Balazs Keri3b30d652018-10-19 13:32:20 +00001350 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1351 if (!ToReplacementTypeOrErr)
1352 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001353
1354 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001355 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001356}
1357
Balazs Keri3b30d652018-10-19 13:32:20 +00001358ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001359 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001360 auto ToTemplateOrErr = import(T->getTemplateName());
1361 if (!ToTemplateOrErr)
1362 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001363
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001364 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001365 if (Error Err = ImportTemplateArguments(
1366 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1367 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001368
Douglas Gregore2e50d332010-12-01 01:36:18 +00001369 QualType ToCanonType;
1370 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001371 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001372 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001373 if (ExpectedType TyOrErr = import(FromCanonType))
1374 ToCanonType = *TyOrErr;
1375 else
1376 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001377 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001378 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001379 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001380 ToCanonType);
1381}
1382
Balazs Keri3b30d652018-10-19 13:32:20 +00001383ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001384 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001385 auto ToQualifierOrErr = import(T->getQualifier());
1386 if (!ToQualifierOrErr)
1387 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001388
Balazs Keri3b30d652018-10-19 13:32:20 +00001389 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1390 if (!ToNamedTypeOrErr)
1391 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001392
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1394 if (!ToOwnedTagDeclOrErr)
1395 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001396
Abramo Bagnara6150c882010-05-11 21:36:43 +00001397 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 *ToQualifierOrErr,
1399 *ToNamedTypeOrErr,
1400 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001401}
1402
Balazs Keri3b30d652018-10-19 13:32:20 +00001403ExpectedType
1404ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1405 ExpectedType ToPatternOrErr = import(T->getPattern());
1406 if (!ToPatternOrErr)
1407 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001408
Balazs Keri3b30d652018-10-19 13:32:20 +00001409 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001410 T->getNumExpansions());
1411}
1412
Balazs Keri3b30d652018-10-19 13:32:20 +00001413ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001414 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001415 auto ToQualifierOrErr = import(T->getQualifier());
1416 if (!ToQualifierOrErr)
1417 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001418
Balazs Keri3b30d652018-10-19 13:32:20 +00001419 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001420
1421 SmallVector<TemplateArgument, 2> ToPack;
1422 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001423 if (Error Err = ImportTemplateArguments(
1424 T->getArgs(), T->getNumArgs(), ToPack))
1425 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001426
1427 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001429}
1430
Balazs Keri3b30d652018-10-19 13:32:20 +00001431ExpectedType
1432ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1433 auto ToQualifierOrErr = import(T->getQualifier());
1434 if (!ToQualifierOrErr)
1435 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001436
1437 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001438
Balazs Keri3b30d652018-10-19 13:32:20 +00001439 QualType Canon;
1440 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1441 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1442 Canon = (*TyOrErr).getCanonicalType();
1443 else
1444 return TyOrErr.takeError();
1445 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001446
Balazs Keri3b30d652018-10-19 13:32:20 +00001447 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1448 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001449 Name, Canon);
1450}
1451
Balazs Keri3b30d652018-10-19 13:32:20 +00001452ExpectedType
1453ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1454 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1455 if (!ToDeclOrErr)
1456 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001457
Balazs Keri3b30d652018-10-19 13:32:20 +00001458 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001459}
1460
Balazs Keri3b30d652018-10-19 13:32:20 +00001461ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1462 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1463 if (!ToBaseTypeOrErr)
1464 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001465
Douglas Gregore9d95f12015-07-07 03:57:35 +00001466 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001467 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001468 if (ExpectedType TyOrErr = import(TypeArg))
1469 TypeArgs.push_back(*TyOrErr);
1470 else
1471 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001472 }
1473
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001474 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001475 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001476 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1477 Protocols.push_back(*ProtocolOrErr);
1478 else
1479 return ProtocolOrErr.takeError();
1480
Douglas Gregor96e578d2010-02-05 17:54:41 +00001481 }
1482
Balazs Keri3b30d652018-10-19 13:32:20 +00001483 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001484 Protocols,
1485 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001486}
1487
Balazs Keri3b30d652018-10-19 13:32:20 +00001488ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001489ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001490 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1491 if (!ToPointeeTypeOrErr)
1492 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001493
Balazs Keri3b30d652018-10-19 13:32:20 +00001494 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495}
1496
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001497//----------------------------------------------------------------------------
1498// Import Declarations
1499//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001500Error ASTNodeImporter::ImportDeclParts(
1501 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1502 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001503 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1504 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1505 DeclContext *OrigDC = D->getDeclContext();
1506 FunctionDecl *FunDecl;
1507 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1508 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001509 auto getLeafPointeeType = [](const Type *T) {
1510 while (T->isPointerType() || T->isArrayType()) {
1511 T = T->getPointeeOrArrayElementType();
1512 }
1513 return T;
1514 };
1515 for (const ParmVarDecl *P : FunDecl->parameters()) {
1516 const Type *LeafT =
1517 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1518 auto *RT = dyn_cast<RecordType>(LeafT);
1519 if (RT && RT->getDecl() == D) {
1520 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1521 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001522 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001523 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001524 }
1525 }
1526
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001527 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001528 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1529 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001530
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001531 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001532 if (Error Err = importInto(Name, D->getDeclName()))
1533 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001534
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001535 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001536 if (Error Err = importInto(Loc, D->getLocation()))
1537 return Err;
1538
Sean Callanan59721b32015-04-28 18:41:46 +00001539 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001540 if (ToD)
1541 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1542 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001543
1544 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001545}
1546
Balazs Keri3b30d652018-10-19 13:32:20 +00001547Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001548 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001549 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001550
Balazs Keri3b30d652018-10-19 13:32:20 +00001551 if (!ToD)
1552 if (Error Err = importInto(ToD, FromD))
1553 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001554
Balazs Keri3b30d652018-10-19 13:32:20 +00001555 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1556 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1557 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1558 !ToRecord->getDefinition()) {
1559 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1560 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001561 }
1562 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001563 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001564 }
1565
Balazs Keri3b30d652018-10-19 13:32:20 +00001566 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1567 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001568 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001569 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1570 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001571 }
1572 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001573 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001574 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001575
1576 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001577}
1578
Balazs Keri3b30d652018-10-19 13:32:20 +00001579Error
1580ASTNodeImporter::ImportDeclarationNameLoc(
1581 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001582 // NOTE: To.Name and To.Loc are already imported.
1583 // We only have to import To.LocInfo.
1584 switch (To.getName().getNameKind()) {
1585 case DeclarationName::Identifier:
1586 case DeclarationName::ObjCZeroArgSelector:
1587 case DeclarationName::ObjCOneArgSelector:
1588 case DeclarationName::ObjCMultiArgSelector:
1589 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001590 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001591 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001592
1593 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001594 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1595 To.setCXXOperatorNameRange(*ToRangeOrErr);
1596 else
1597 return ToRangeOrErr.takeError();
1598 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001599 }
1600 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001601 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1602 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1603 else
1604 return LocOrErr.takeError();
1605 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001606 }
1607 case DeclarationName::CXXConstructorName:
1608 case DeclarationName::CXXDestructorName:
1609 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001610 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1611 To.setNamedTypeInfo(*ToTInfoOrErr);
1612 else
1613 return ToTInfoOrErr.takeError();
1614 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001615 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001616 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001617 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001618}
1619
Balazs Keri3b30d652018-10-19 13:32:20 +00001620Error
1621ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001622 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001623 auto ToDCOrErr = Importer.ImportContext(FromDC);
1624 return ToDCOrErr.takeError();
1625 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001626 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001627 for (auto *From : FromDC->decls()) {
1628 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001629 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001630 // Ignore the error, continue with next Decl.
1631 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001632 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001633 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001634
Balazs Keri3b30d652018-10-19 13:32:20 +00001635 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001636}
1637
Balazs Keri3b30d652018-10-19 13:32:20 +00001638Error ASTNodeImporter::ImportDeclContext(
1639 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1640 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1641 if (!ToDCOrErr)
1642 return ToDCOrErr.takeError();
1643 ToDC = *ToDCOrErr;
1644
1645 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1646 auto ToLexicalDCOrErr = Importer.ImportContext(
1647 FromD->getLexicalDeclContext());
1648 if (!ToLexicalDCOrErr)
1649 return ToLexicalDCOrErr.takeError();
1650 ToLexicalDC = *ToLexicalDCOrErr;
1651 } else
1652 ToLexicalDC = ToDC;
1653
1654 return Error::success();
1655}
1656
1657Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001658 const CXXRecordDecl *From, CXXRecordDecl *To) {
1659 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1660 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001661
Balazs Keri1d20cc22018-07-16 12:16:39 +00001662 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001663 if (FromM->isImplicit()) {
1664 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1665 if (!ToMOrErr)
1666 return ToMOrErr.takeError();
1667 }
1668
1669 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001670}
1671
Balazs Keri3b30d652018-10-19 13:32:20 +00001672static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1673 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001674 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri57949eb2019-03-25 09:16:39 +00001675 if (ExpectedDecl ToTypedefOrErr = Importer.Import_New(FromTypedef))
1676 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1677 else
1678 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001679 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001680 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001681}
1682
Balazs Keri3b30d652018-10-19 13:32:20 +00001683Error ASTNodeImporter::ImportDefinition(
1684 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001685 if (To->getDefinition() || To->isBeingDefined()) {
1686 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001687 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001688
Balazs Keri3b30d652018-10-19 13:32:20 +00001689 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001690 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001691
Douglas Gregore2e50d332010-12-01 01:36:18 +00001692 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001693
Balazs Keri3b30d652018-10-19 13:32:20 +00001694 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1695 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001696
Douglas Gregore2e50d332010-12-01 01:36:18 +00001697 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001698 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1699 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1700 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001701
1702 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1703 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1704 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001705 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001706 ToData.Aggregate = FromData.Aggregate;
1707 ToData.PlainOldData = FromData.PlainOldData;
1708 ToData.Empty = FromData.Empty;
1709 ToData.Polymorphic = FromData.Polymorphic;
1710 ToData.Abstract = FromData.Abstract;
1711 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001712 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1713 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1714 ToData.HasBasesWithNonStaticDataMembers =
1715 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001716 ToData.HasPrivateFields = FromData.HasPrivateFields;
1717 ToData.HasProtectedFields = FromData.HasProtectedFields;
1718 ToData.HasPublicFields = FromData.HasPublicFields;
1719 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001720 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001721 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001722 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001723 ToData.HasUninitializedReferenceMember
1724 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001725 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001726 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1727 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001728 ToData.NeedOverloadResolutionForCopyConstructor
1729 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001730 ToData.NeedOverloadResolutionForMoveConstructor
1731 = FromData.NeedOverloadResolutionForMoveConstructor;
1732 ToData.NeedOverloadResolutionForMoveAssignment
1733 = FromData.NeedOverloadResolutionForMoveAssignment;
1734 ToData.NeedOverloadResolutionForDestructor
1735 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001736 ToData.DefaultedCopyConstructorIsDeleted
1737 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001738 ToData.DefaultedMoveConstructorIsDeleted
1739 = FromData.DefaultedMoveConstructorIsDeleted;
1740 ToData.DefaultedMoveAssignmentIsDeleted
1741 = FromData.DefaultedMoveAssignmentIsDeleted;
1742 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001743 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1744 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001745 ToData.HasConstexprNonCopyMoveConstructor
1746 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001747 ToData.HasDefaultedDefaultConstructor
1748 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001749 ToData.DefaultedDefaultConstructorIsConstexpr
1750 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001751 ToData.HasConstexprDefaultConstructor
1752 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001753 ToData.HasNonLiteralTypeFieldsOrBases
1754 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001755 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001756 ToData.UserProvidedDefaultConstructor
1757 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001758 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001759 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1760 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1761 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1762 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001763 ToData.ImplicitCopyAssignmentHasConstParam
1764 = FromData.ImplicitCopyAssignmentHasConstParam;
1765 ToData.HasDeclaredCopyConstructorWithConstParam
1766 = FromData.HasDeclaredCopyConstructorWithConstParam;
1767 ToData.HasDeclaredCopyAssignmentWithConstParam
1768 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001769
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001770 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001771 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001772 ExpectedType TyOrErr = import(Base1.getType());
1773 if (!TyOrErr)
1774 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001775
1776 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001777 if (Base1.isPackExpansion()) {
1778 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1779 EllipsisLoc = *LocOrErr;
1780 else
1781 return LocOrErr.takeError();
1782 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001783
1784 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001785 if (Error Err =
1786 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1787 return Err;
1788
1789 auto RangeOrErr = import(Base1.getSourceRange());
1790 if (!RangeOrErr)
1791 return RangeOrErr.takeError();
1792
1793 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1794 if (!TSIOrErr)
1795 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001796
Douglas Gregore2e50d332010-12-01 01:36:18 +00001797 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001798 new (Importer.getToContext()) CXXBaseSpecifier(
1799 *RangeOrErr,
1800 Base1.isVirtual(),
1801 Base1.isBaseOfClass(),
1802 Base1.getAccessSpecifierAsWritten(),
1803 *TSIOrErr,
1804 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001805 }
1806 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001807 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001808 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001809
Douglas Gregor2e15c842012-02-01 21:00:38 +00001810 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001811 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1812 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001813
Douglas Gregore2e50d332010-12-01 01:36:18 +00001814 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001815 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001816}
1817
Balazs Keri3b30d652018-10-19 13:32:20 +00001818Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001819 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001820 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001821
Gabor Martonac3a5d62018-09-17 12:04:52 +00001822 Expr *FromInit = From->getInit();
1823 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001824 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001825
Balazs Keri3b30d652018-10-19 13:32:20 +00001826 ExpectedExpr ToInitOrErr = import(FromInit);
1827 if (!ToInitOrErr)
1828 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001829
Balazs Keri3b30d652018-10-19 13:32:20 +00001830 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001831 if (From->isInitKnownICE()) {
1832 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1833 Eval->CheckedICE = true;
1834 Eval->IsICE = From->isInitICE();
1835 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001836
1837 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001838 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001839}
1840
Balazs Keri3b30d652018-10-19 13:32:20 +00001841Error ASTNodeImporter::ImportDefinition(
1842 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001843 if (To->getDefinition() || To->isBeingDefined()) {
1844 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001845 return ImportDeclContext(From, /*ForceImport=*/true);
1846 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001847 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001848
Douglas Gregord451ea92011-07-29 23:31:30 +00001849 To->startDefinition();
1850
Balazs Keri3b30d652018-10-19 13:32:20 +00001851 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1852 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001853
Balazs Keri3b30d652018-10-19 13:32:20 +00001854 ExpectedType ToTypeOrErr =
1855 import(Importer.getFromContext().getTypeDeclType(From));
1856 if (!ToTypeOrErr)
1857 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001858
Balazs Keri3b30d652018-10-19 13:32:20 +00001859 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1860 if (!ToPromotionTypeOrErr)
1861 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001862
1863 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001864 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1865 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001866
Douglas Gregord451ea92011-07-29 23:31:30 +00001867 // FIXME: we might need to merge the number of positive or negative bits
1868 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001869 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001870 From->getNumPositiveBits(),
1871 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001872 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001873}
1874
Balazs Keri3b30d652018-10-19 13:32:20 +00001875Error ASTNodeImporter::ImportTemplateArguments(
1876 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1877 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001878 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001879 if (auto ToOrErr = import(FromArgs[I]))
1880 ToArgs.push_back(*ToOrErr);
1881 else
1882 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001883 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001884
Balazs Keri3b30d652018-10-19 13:32:20 +00001885 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001886}
1887
Balazs Keri3b30d652018-10-19 13:32:20 +00001888// FIXME: Do not forget to remove this and use only 'import'.
1889Expected<TemplateArgument>
1890ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1891 return import(From);
1892}
1893
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001894template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001895Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001896 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1897 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001898 if (auto ToLocOrErr = import(FromLoc))
1899 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001900 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001901 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001902 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001903 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001904}
1905
Gabor Marton26f72a92018-07-12 09:42:05 +00001906static StructuralEquivalenceKind
1907getStructuralEquivalenceKind(const ASTImporter &Importer) {
1908 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1909 : StructuralEquivalenceKind::Default;
1910}
1911
Gabor Marton950fb572018-07-17 12:39:27 +00001912bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1913 StructuralEquivalenceContext Ctx(
1914 Importer.getFromContext(), Importer.getToContext(),
1915 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1916 false, Complain);
1917 return Ctx.IsEquivalent(From, To);
1918}
1919
1920bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001921 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001922 // Eliminate a potential failure point where we attempt to re-import
1923 // something we're trying to import while completing ToRecord.
1924 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1925 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001926 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001927 if (ToOriginRecord)
1928 ToRecord = ToOriginRecord;
1929 }
1930
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001931 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001932 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001933 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001934 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001935 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001936 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001937}
1938
Larisse Voufo39a1e502013-08-06 01:03:05 +00001939bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1940 bool Complain) {
1941 StructuralEquivalenceContext Ctx(
1942 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001943 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1944 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001945 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001946}
1947
Douglas Gregor98c10182010-02-12 22:17:39 +00001948bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001949 // Eliminate a potential failure point where we attempt to re-import
1950 // something we're trying to import while completin ToEnum
1951 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
1952 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
1953 ToEnum = ToOriginEnum;
1954
Gabor Marton26f72a92018-07-12 09:42:05 +00001955 StructuralEquivalenceContext Ctx(
1956 Importer.getFromContext(), Importer.getToContext(),
1957 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001958 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001959}
1960
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001961bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1962 FunctionTemplateDecl *To) {
1963 StructuralEquivalenceContext Ctx(
1964 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001965 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1966 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001967 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001968}
1969
Balazs Keric7797c42018-07-11 09:37:24 +00001970bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1971 StructuralEquivalenceContext Ctx(
1972 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001973 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1974 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001975 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001976}
1977
Douglas Gregor91155082012-11-14 22:29:20 +00001978bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001979 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001980 const llvm::APSInt &FromVal = FromEC->getInitVal();
1981 const llvm::APSInt &ToVal = ToEC->getInitVal();
1982
1983 return FromVal.isSigned() == ToVal.isSigned() &&
1984 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1985 FromVal == ToVal;
1986}
1987
1988bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001989 ClassTemplateDecl *To) {
1990 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1991 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001992 Importer.getNonEquivalentDecls(),
1993 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001994 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001995}
1996
Larisse Voufo39a1e502013-08-06 01:03:05 +00001997bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1998 VarTemplateDecl *To) {
1999 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2000 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002001 Importer.getNonEquivalentDecls(),
2002 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002003 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002004}
2005
Balazs Keri3b30d652018-10-19 13:32:20 +00002006ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002007 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002008 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002009 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002010}
2011
Balazs Keri3b30d652018-10-19 13:32:20 +00002012ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2013 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2014 << D->getDeclKindName();
2015 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2016}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002017
Balazs Keri3b30d652018-10-19 13:32:20 +00002018ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2019 // Import the context of this declaration.
2020 DeclContext *DC, *LexicalDC;
2021 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2022 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002023
2024 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002025 ExpectedSLoc LocOrErr = import(D->getLocation());
2026 if (!LocOrErr)
2027 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002028
Gabor Marton26f72a92018-07-12 09:42:05 +00002029 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002030 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002031 return ToD;
2032
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002033 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002034 LexicalDC->addDeclInternal(ToD);
2035 return ToD;
2036}
2037
Balazs Keri3b30d652018-10-19 13:32:20 +00002038ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002039 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002040 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002041
Gabor Marton26f72a92018-07-12 09:42:05 +00002042 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002043
Sean Callanan65198272011-11-17 23:20:56 +00002044 return ToD;
2045}
2046
Balazs Keri3b30d652018-10-19 13:32:20 +00002047ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2048 ExpectedSLoc LocOrErr = import(D->getLocation());
2049 if (!LocOrErr)
2050 return LocOrErr.takeError();
2051 auto ColonLocOrErr = import(D->getColonLoc());
2052 if (!ColonLocOrErr)
2053 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002054
2055 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002056 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2057 if (!DCOrErr)
2058 return DCOrErr.takeError();
2059 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002060
Gabor Marton26f72a92018-07-12 09:42:05 +00002061 AccessSpecDecl *ToD;
2062 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002063 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002064 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002065
2066 // Lexical DeclContext and Semantic DeclContext
2067 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002068 ToD->setLexicalDeclContext(DC);
2069 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002070
Gabor Marton26f72a92018-07-12 09:42:05 +00002071 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002072}
2073
Balazs Keri3b30d652018-10-19 13:32:20 +00002074ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2075 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2076 if (!DCOrErr)
2077 return DCOrErr.takeError();
2078 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002079 DeclContext *LexicalDC = DC;
2080
Balazs Keri3b30d652018-10-19 13:32:20 +00002081 SourceLocation ToLocation, ToRParenLoc;
2082 Expr *ToAssertExpr;
2083 StringLiteral *ToMessage;
2084 if (auto Imp = importSeq(
2085 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2086 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2087 else
2088 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002089
Gabor Marton26f72a92018-07-12 09:42:05 +00002090 StaticAssertDecl *ToD;
2091 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002092 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2093 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002094 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002095
2096 ToD->setLexicalDeclContext(LexicalDC);
2097 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002098 return ToD;
2099}
2100
Balazs Keri3b30d652018-10-19 13:32:20 +00002101ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002102 // Import the major distinguishing characteristics of this namespace.
2103 DeclContext *DC, *LexicalDC;
2104 DeclarationName Name;
2105 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002106 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002107 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2108 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002109 if (ToD)
2110 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002111
2112 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002113 if (!Name) {
2114 // This is an anonymous namespace. Adopt an existing anonymous
2115 // namespace if we can.
2116 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002117 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002118 MergeWithNamespace = TU->getAnonymousNamespace();
2119 else
2120 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2121 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002122 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002123 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002124 for (auto *FoundDecl : FoundDecls) {
2125 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002126 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002127
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002128 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002129 MergeWithNamespace = FoundNS;
2130 ConflictingDecls.clear();
2131 break;
2132 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002133
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002134 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002135 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002136
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002137 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002138 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002139 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002140 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002141 if (!Name)
2142 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002143 }
2144 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002145
Balazs Keri3b30d652018-10-19 13:32:20 +00002146 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2147 if (!BeginLocOrErr)
2148 return BeginLocOrErr.takeError();
2149
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002150 // Create the "to" namespace, if needed.
2151 NamespaceDecl *ToNamespace = MergeWithNamespace;
2152 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002153 if (GetImportedOrCreateDecl(
2154 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002155 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002156 /*PrevDecl=*/nullptr))
2157 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002158 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002159 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002160
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002161 // If this is an anonymous namespace, register it as the anonymous
2162 // namespace within its context.
2163 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002164 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002165 TU->setAnonymousNamespace(ToNamespace);
2166 else
2167 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2168 }
2169 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002170 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002171
Balazs Keri3b30d652018-10-19 13:32:20 +00002172 if (Error Err = ImportDeclContext(D))
2173 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002174
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002175 return ToNamespace;
2176}
2177
Balazs Keri3b30d652018-10-19 13:32:20 +00002178ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002179 // Import the major distinguishing characteristics of this namespace.
2180 DeclContext *DC, *LexicalDC;
2181 DeclarationName Name;
2182 SourceLocation Loc;
2183 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002184 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2185 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002186 if (LookupD)
2187 return LookupD;
2188
2189 // NOTE: No conflict resolution is done for namespace aliases now.
2190
Balazs Keri3b30d652018-10-19 13:32:20 +00002191 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2192 NestedNameSpecifierLoc ToQualifierLoc;
2193 NamespaceDecl *ToNamespace;
2194 if (auto Imp = importSeq(
2195 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2196 D->getTargetNameLoc(), D->getNamespace()))
2197 std::tie(
2198 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2199 ToNamespace) = *Imp;
2200 else
2201 return Imp.takeError();
2202 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002203
Gabor Marton26f72a92018-07-12 09:42:05 +00002204 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002205 if (GetImportedOrCreateDecl(
2206 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2207 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002208 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002209
2210 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002211 LexicalDC->addDeclInternal(ToD);
2212
2213 return ToD;
2214}
2215
Balazs Keri3b30d652018-10-19 13:32:20 +00002216ExpectedDecl
2217ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002218 // Import the major distinguishing characteristics of this typedef.
2219 DeclContext *DC, *LexicalDC;
2220 DeclarationName Name;
2221 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002222 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002223 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2224 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002225 if (ToD)
2226 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002227
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002228 // If this typedef is not in block scope, determine whether we've
2229 // seen a typedef with the same name (that we can merge with) or any
2230 // other entity by that name (which name lookup could conflict with).
2231 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002232 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002233 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002234 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002235 for (auto *FoundDecl : FoundDecls) {
2236 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002237 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002238 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002239 QualType FromUT = D->getUnderlyingType();
2240 QualType FoundUT = FoundTypedef->getUnderlyingType();
2241 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2242 // If the "From" context has a complete underlying type but we
2243 // already have a complete underlying type then return with that.
2244 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002245 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002246 }
2247 // FIXME Handle redecl chain.
2248 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002249 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002250
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002251 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002252 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002253
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002254 if (!ConflictingDecls.empty()) {
2255 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002256 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002257 ConflictingDecls.size());
2258 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002259 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002260 }
2261 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002262
Balazs Keri3b30d652018-10-19 13:32:20 +00002263 QualType ToUnderlyingType;
2264 TypeSourceInfo *ToTypeSourceInfo;
2265 SourceLocation ToBeginLoc;
2266 if (auto Imp = importSeq(
2267 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2268 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2269 else
2270 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002271
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002272 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002273 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002274 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002275 if (IsAlias) {
2276 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002277 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2278 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002279 return ToTypedef;
2280 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002281 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2282 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002283 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002284
Douglas Gregordd483172010-02-22 17:42:47 +00002285 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002286 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002287
2288 // Templated declarations should not appear in DeclContext.
2289 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2290 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2291 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002292
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002293 return ToTypedef;
2294}
2295
Balazs Keri3b30d652018-10-19 13:32:20 +00002296ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002297 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2298}
2299
Balazs Keri3b30d652018-10-19 13:32:20 +00002300ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002301 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2302}
2303
Balazs Keri3b30d652018-10-19 13:32:20 +00002304ExpectedDecl
2305ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002306 // Import the major distinguishing characteristics of this typedef.
2307 DeclContext *DC, *LexicalDC;
2308 DeclarationName Name;
2309 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002310 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002311 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2312 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002313 if (FoundD)
2314 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002315
2316 // If this typedef is not in block scope, determine whether we've
2317 // seen a typedef with the same name (that we can merge with) or any
2318 // other entity by that name (which name lookup could conflict with).
2319 if (!DC->isFunctionOrMethod()) {
2320 SmallVector<NamedDecl *, 4> ConflictingDecls;
2321 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002322 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002323 for (auto *FoundDecl : FoundDecls) {
2324 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002325 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002326 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002327 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002328 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002329 }
2330
2331 if (!ConflictingDecls.empty()) {
2332 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2333 ConflictingDecls.data(),
2334 ConflictingDecls.size());
2335 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002336 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002337 }
2338 }
2339
Balazs Keri3b30d652018-10-19 13:32:20 +00002340 TemplateParameterList *ToTemplateParameters;
2341 TypeAliasDecl *ToTemplatedDecl;
2342 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2343 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2344 else
2345 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002346
Gabor Marton26f72a92018-07-12 09:42:05 +00002347 TypeAliasTemplateDecl *ToAlias;
2348 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002349 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002350 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002351
Balazs Keri3b30d652018-10-19 13:32:20 +00002352 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002353
Gabor Horvath7a91c082017-11-14 11:30:38 +00002354 ToAlias->setAccess(D->getAccess());
2355 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002356 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002357 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002358}
2359
Balazs Keri3b30d652018-10-19 13:32:20 +00002360ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002361 // Import the major distinguishing characteristics of this label.
2362 DeclContext *DC, *LexicalDC;
2363 DeclarationName Name;
2364 SourceLocation Loc;
2365 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002366 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2367 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002368 if (ToD)
2369 return ToD;
2370
2371 assert(LexicalDC->isFunctionOrMethod());
2372
Gabor Marton26f72a92018-07-12 09:42:05 +00002373 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002374 if (D->isGnuLocal()) {
2375 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2376 if (!BeginLocOrErr)
2377 return BeginLocOrErr.takeError();
2378 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2379 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2380 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002381
Balazs Keri3b30d652018-10-19 13:32:20 +00002382 } else {
2383 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2384 Name.getAsIdentifierInfo()))
2385 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002386
Balazs Keri3b30d652018-10-19 13:32:20 +00002387 }
2388
2389 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2390 if (!ToStmtOrErr)
2391 return ToStmtOrErr.takeError();
2392
2393 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002394 ToLabel->setLexicalDeclContext(LexicalDC);
2395 LexicalDC->addDeclInternal(ToLabel);
2396 return ToLabel;
2397}
2398
Balazs Keri3b30d652018-10-19 13:32:20 +00002399ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002400 // Import the major distinguishing characteristics of this enum.
2401 DeclContext *DC, *LexicalDC;
2402 DeclarationName Name;
2403 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002404 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002405 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2406 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002407 if (ToD)
2408 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002409
Douglas Gregor98c10182010-02-12 22:17:39 +00002410 // Figure out what enum name we're looking for.
2411 unsigned IDNS = Decl::IDNS_Tag;
2412 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002413 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002414 if (Error Err = importInto(
2415 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2416 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002417 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002418 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002419 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002420
Douglas Gregor98c10182010-02-12 22:17:39 +00002421 // We may already have an enum of the same name; try to find and match it.
2422 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002423 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002424 auto FoundDecls =
2425 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002426 for (auto *FoundDecl : FoundDecls) {
2427 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002428 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002429
Balazs Keri3b30d652018-10-19 13:32:20 +00002430 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002431 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002432 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002433 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002434
Balazs Keri3b30d652018-10-19 13:32:20 +00002435 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002436 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002437 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002438 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002439
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002440 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002441 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002442
Douglas Gregor98c10182010-02-12 22:17:39 +00002443 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002444 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002445 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002446 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002447 if (!Name)
2448 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002449 }
2450 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002451
Balazs Keri3b30d652018-10-19 13:32:20 +00002452 SourceLocation ToBeginLoc;
2453 NestedNameSpecifierLoc ToQualifierLoc;
2454 QualType ToIntegerType;
2455 if (auto Imp = importSeq(
2456 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2457 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2458 else
2459 return Imp.takeError();
2460
Douglas Gregor98c10182010-02-12 22:17:39 +00002461 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002462 EnumDecl *D2;
2463 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002464 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002465 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2466 D->isScopedUsingClassTag(), D->isFixed()))
2467 return D2;
2468
Balazs Keri3b30d652018-10-19 13:32:20 +00002469 D2->setQualifierInfo(ToQualifierLoc);
2470 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002471 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002472 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002473 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002474
Douglas Gregor98c10182010-02-12 22:17:39 +00002475 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002476 if (D->isCompleteDefinition())
2477 if (Error Err = ImportDefinition(D, D2))
2478 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002479
Douglas Gregor3996e242010-02-15 22:01:00 +00002480 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002481}
2482
Balazs Keri3b30d652018-10-19 13:32:20 +00002483ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002484 bool IsFriendTemplate = false;
2485 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2486 IsFriendTemplate =
2487 DCXX->getDescribedClassTemplate() &&
2488 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2489 Decl::FOK_None;
2490 }
2491
Douglas Gregor5c73e912010-02-11 00:48:18 +00002492 // Import the major distinguishing characteristics of this record.
2493 DeclContext *DC, *LexicalDC;
2494 DeclarationName Name;
2495 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002496 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002497 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2498 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002499 if (ToD)
2500 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002501
Douglas Gregor5c73e912010-02-11 00:48:18 +00002502 // Figure out what structure name we're looking for.
2503 unsigned IDNS = Decl::IDNS_Tag;
2504 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002505 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002506 if (Error Err = importInto(
2507 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2508 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002509 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002510 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002511 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002512
2513 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002514 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002515 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002516 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002517 auto FoundDecls =
2518 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002519 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002520 // We're going to have to compare D against potentially conflicting Decls,
2521 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002522 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2523 D->getASTContext().getExternalSource()->CompleteType(D);
2524 }
2525
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002526 for (auto *FoundDecl : FoundDecls) {
2527 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002528 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002529
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002530 Decl *Found = FoundDecl;
2531 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2532 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002533 Found = Tag->getDecl();
2534 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002535
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002536 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002537 // Do not emit false positive diagnostic in case of unnamed
2538 // struct/union and in case of anonymous structs. Would be false
2539 // because there may be several anonymous/unnamed structs in a class.
2540 // E.g. these are both valid:
2541 // struct A { // unnamed structs
2542 // struct { struct A *next; } entry0;
2543 // struct { struct A *next; } entry1;
2544 // };
2545 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2546 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002547 if (!IsStructuralMatch(D, FoundRecord, false))
2548 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002549
Gabor Marton7df342a2018-12-17 12:42:12 +00002550 if (IsStructuralMatch(D, FoundRecord)) {
2551 RecordDecl *FoundDef = FoundRecord->getDefinition();
2552 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002553 // FIXME: Structural equivalence check should check for same
2554 // user-defined methods.
2555 Importer.MapImported(D, FoundDef);
2556 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2557 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2558 assert(FoundCXX && "Record type mismatch");
2559
Gabor Marton7df342a2018-12-17 12:42:12 +00002560 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002561 // FoundDef may not have every implicit method that D has
2562 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002563 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2564 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002565 }
Douglas Gregor25791052010-02-12 00:09:27 +00002566 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002567 PrevDecl = FoundRecord->getMostRecentDecl();
2568 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002569 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002570 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002571
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002572 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002573 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002574
Douglas Gregordd6006f2012-07-17 21:16:27 +00002575 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002576 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002577 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002578 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002579 if (!Name)
2580 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002581 }
2582 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002583
Balazs Keri3b30d652018-10-19 13:32:20 +00002584 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2585 if (!BeginLocOrErr)
2586 return BeginLocOrErr.takeError();
2587
Douglas Gregor5c73e912010-02-11 00:48:18 +00002588 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002589 RecordDecl *D2 = nullptr;
2590 CXXRecordDecl *D2CXX = nullptr;
2591 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2592 if (DCXX->isLambda()) {
2593 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2594 if (!TInfoOrErr)
2595 return TInfoOrErr.takeError();
2596 if (GetImportedOrCreateSpecialDecl(
2597 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2598 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2599 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2600 return D2CXX;
2601 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2602 if (!CDeclOrErr)
2603 return CDeclOrErr.takeError();
2604 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2605 } else if (DCXX->isInjectedClassName()) {
2606 // We have to be careful to do a similar dance to the one in
2607 // Sema::ActOnStartCXXMemberDeclarations
2608 const bool DelayTypeCreation = true;
2609 if (GetImportedOrCreateDecl(
2610 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2611 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2612 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2613 return D2CXX;
2614 Importer.getToContext().getTypeDeclType(
2615 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2616 } else {
2617 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2618 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2619 Name.getAsIdentifierInfo(),
2620 cast_or_null<CXXRecordDecl>(PrevDecl)))
2621 return D2CXX;
2622 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002623
Gabor Marton7df342a2018-12-17 12:42:12 +00002624 D2 = D2CXX;
2625 D2->setAccess(D->getAccess());
2626 D2->setLexicalDeclContext(LexicalDC);
2627 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2628 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002629
Gabor Marton7df342a2018-12-17 12:42:12 +00002630 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2631 DC->makeDeclVisibleInContext(D2);
2632
2633 if (ClassTemplateDecl *FromDescribed =
2634 DCXX->getDescribedClassTemplate()) {
2635 ClassTemplateDecl *ToDescribed;
2636 if (Error Err = importInto(ToDescribed, FromDescribed))
2637 return std::move(Err);
2638 D2CXX->setDescribedClassTemplate(ToDescribed);
2639 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2640 // In a record describing a template the type should be an
2641 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2642 // previously set type to the correct value here (ToDescribed is not
2643 // available at record create).
2644 // FIXME: The previous type is cleared but not removed from
2645 // ASTContext's internal storage.
2646 CXXRecordDecl *Injected = nullptr;
2647 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2648 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2649 if (Record && Record->isInjectedClassName()) {
2650 Injected = Record;
2651 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002652 }
2653 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002654 // Create an injected type for the whole redecl chain.
2655 SmallVector<Decl *, 2> Redecls =
2656 getCanonicalForwardRedeclChain(D2CXX);
2657 for (auto *R : Redecls) {
2658 auto *RI = cast<CXXRecordDecl>(R);
2659 RI->setTypeForDecl(nullptr);
2660 // Below we create a new injected type and assign that to the
2661 // canonical decl, subsequent declarations in the chain will reuse
2662 // that type.
2663 Importer.getToContext().getInjectedClassNameType(
2664 RI, ToDescribed->getInjectedClassNameSpecialization());
2665 }
2666 // Set the new type for the previous injected decl too.
2667 if (Injected) {
2668 Injected->setTypeForDecl(nullptr);
2669 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2670 }
2671 }
2672 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002673 DCXX->getMemberSpecializationInfo()) {
2674 TemplateSpecializationKind SK =
2675 MemberInfo->getTemplateSpecializationKind();
2676 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002677
2678 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2679 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2680 else
2681 return ToInstOrErr.takeError();
2682
2683 if (ExpectedSLoc POIOrErr =
2684 import(MemberInfo->getPointOfInstantiation()))
2685 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2686 *POIOrErr);
2687 else
2688 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002689 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002690
Gabor Marton7df342a2018-12-17 12:42:12 +00002691 } else {
2692 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2693 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2694 Name.getAsIdentifierInfo(), PrevDecl))
2695 return D2;
2696 D2->setLexicalDeclContext(LexicalDC);
2697 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002698 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002699
Gabor Marton7df342a2018-12-17 12:42:12 +00002700 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2701 D2->setQualifierInfo(*QualifierLocOrErr);
2702 else
2703 return QualifierLocOrErr.takeError();
2704
2705 if (D->isAnonymousStructOrUnion())
2706 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002707
Balazs Keri3b30d652018-10-19 13:32:20 +00002708 if (D->isCompleteDefinition())
2709 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2710 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002711
Douglas Gregor3996e242010-02-15 22:01:00 +00002712 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002713}
2714
Balazs Keri3b30d652018-10-19 13:32:20 +00002715ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002716 // Import the major distinguishing characteristics of this enumerator.
2717 DeclContext *DC, *LexicalDC;
2718 DeclarationName Name;
2719 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002720 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002721 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2722 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002723 if (ToD)
2724 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002725
Fangrui Song6907ce22018-07-30 19:24:48 +00002726 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002727 // in the same context.
2728 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002729 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002730 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002731 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002732 for (auto *FoundDecl : FoundDecls) {
2733 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002734 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002735
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002736 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002737 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002738 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002739 }
2740
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002741 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002742 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002743
Douglas Gregor98c10182010-02-12 22:17:39 +00002744 if (!ConflictingDecls.empty()) {
2745 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002746 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002747 ConflictingDecls.size());
2748 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002749 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002750 }
2751 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002752
Balazs Keri3b30d652018-10-19 13:32:20 +00002753 ExpectedType TypeOrErr = import(D->getType());
2754 if (!TypeOrErr)
2755 return TypeOrErr.takeError();
2756
2757 ExpectedExpr InitOrErr = import(D->getInitExpr());
2758 if (!InitOrErr)
2759 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002760
Gabor Marton26f72a92018-07-12 09:42:05 +00002761 EnumConstantDecl *ToEnumerator;
2762 if (GetImportedOrCreateDecl(
2763 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002764 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002765 return ToEnumerator;
2766
Douglas Gregordd483172010-02-22 17:42:47 +00002767 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002768 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002769 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002770 return ToEnumerator;
2771}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002772
Balazs Keri3b30d652018-10-19 13:32:20 +00002773Error ASTNodeImporter::ImportTemplateInformation(
2774 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002775 switch (FromFD->getTemplatedKind()) {
2776 case FunctionDecl::TK_NonTemplate:
2777 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002778 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002779
2780 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002781 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002782
2783 if (Expected<FunctionDecl *> InstFDOrErr =
2784 import(FromFD->getInstantiatedFromMemberFunction()))
2785 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2786 else
2787 return InstFDOrErr.takeError();
2788
2789 if (ExpectedSLoc POIOrErr = import(
2790 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2791 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2792 else
2793 return POIOrErr.takeError();
2794
2795 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002796 }
2797
2798 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002799 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002800 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002801 if (!FunctionAndArgsOrErr)
2802 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002803
2804 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002805 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002806
Gabor Marton5254e642018-06-27 13:32:50 +00002807 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002808 TemplateArgumentListInfo ToTAInfo;
2809 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002810 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002811 if (Error Err = ImportTemplateArgumentListInfo(
2812 *FromTAArgsAsWritten, ToTAInfo))
2813 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002814
Balazs Keri3b30d652018-10-19 13:32:20 +00002815 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2816 if (!POIOrErr)
2817 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002818
Gabor Marton5254e642018-06-27 13:32:50 +00002819 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002820 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002821 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2822 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2823 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002824 }
2825
2826 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2827 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2828 UnresolvedSet<8> TemplDecls;
2829 unsigned NumTemplates = FromInfo->getNumTemplates();
2830 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002831 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2832 import(FromInfo->getTemplate(I)))
2833 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002834 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002835 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002836 }
2837
2838 // Import TemplateArgumentListInfo.
2839 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002840 if (Error Err = ImportTemplateArgumentListInfo(
2841 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2842 llvm::makeArrayRef(
2843 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2844 ToTAInfo))
2845 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002846
2847 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2848 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002849 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002850 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002851 }
Sam McCallfdc32072018-01-26 12:06:44 +00002852 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002853}
2854
Balazs Keri3b30d652018-10-19 13:32:20 +00002855Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002856ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002857 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002858 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002859 if (!FunctionAndArgsOrErr)
2860 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002861
Balazs Keri3b30d652018-10-19 13:32:20 +00002862 FunctionTemplateDecl *Template;
2863 TemplateArgsTy ToTemplArgs;
2864 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002865 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002866 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002867 return FoundSpec;
2868}
2869
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002870Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2871 FunctionDecl *ToFD) {
2872 if (Stmt *FromBody = FromFD->getBody()) {
2873 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2874 ToFD->setBody(*ToBodyOrErr);
2875 else
2876 return ToBodyOrErr.takeError();
2877 }
2878 return Error::success();
2879}
2880
Gabor Marton458d1452019-02-14 13:07:03 +00002881template <typename T>
2882bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2883 if (From->hasExternalFormalLinkage())
2884 return Found->hasExternalFormalLinkage();
2885 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2886 return false;
2887 if (From->isInAnonymousNamespace())
2888 return Found->isInAnonymousNamespace();
2889 else
2890 return !Found->isInAnonymousNamespace() &&
2891 !Found->hasExternalFormalLinkage();
2892}
2893
Balazs Keri3b30d652018-10-19 13:32:20 +00002894ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002895
Balazs Keri3b30d652018-10-19 13:32:20 +00002896 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002897 auto RedeclIt = Redecls.begin();
2898 // Import the first part of the decl chain. I.e. import all previous
2899 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002900 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2901 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2902 if (!ToRedeclOrErr)
2903 return ToRedeclOrErr.takeError();
2904 }
Gabor Marton5254e642018-06-27 13:32:50 +00002905 assert(*RedeclIt == D);
2906
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002907 // Import the major distinguishing characteristics of this function.
2908 DeclContext *DC, *LexicalDC;
2909 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002910 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002911 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002912 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2913 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002914 if (ToD)
2915 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002916
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002917 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002918 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002919
Gabor Marton5254e642018-06-27 13:32:50 +00002920 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002921 // existing specialization in the "to" context. The lookup below will not
2922 // find any specialization, but would find the primary template; thus, we
2923 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00002924 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2925 if (D->getTemplatedKind() ==
2926 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002927 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
2928 if (!FoundFunctionOrErr)
2929 return FoundFunctionOrErr.takeError();
2930 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00002931 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2932 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00002933 FoundByLookup = FoundFunction;
2934 }
2935 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002936 // Try to find a function in our own ("to") context with the same name, same
2937 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002938 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002939 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002940 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00002941 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002942 for (auto *FoundDecl : FoundDecls) {
2943 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002944 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002945
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002946 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00002947 if (!hasSameVisibilityContext(FoundFunction, D))
2948 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002949
Gabor Marton458d1452019-02-14 13:07:03 +00002950 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00002951 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2952 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00002953 FoundByLookup = FoundFunction;
2954 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002955 }
Gabor Marton458d1452019-02-14 13:07:03 +00002956 // FIXME: Check for overloading more carefully, e.g., by boosting
2957 // Sema::IsOverload out to the AST library.
2958
2959 // Function overloading is okay in C++.
2960 if (Importer.getToContext().getLangOpts().CPlusPlus)
2961 continue;
2962
2963 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00002964 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00002965 << Name << D->getType() << FoundFunction->getType();
2966 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
2967 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002968 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002969
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002970 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002971 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002972
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002973 if (!ConflictingDecls.empty()) {
2974 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002975 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002976 ConflictingDecls.size());
2977 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002978 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00002979 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002980 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002981
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002982 // We do not allow more than one in-class declaration of a function. This is
2983 // because AST clients like VTableBuilder asserts on this. VTableBuilder
2984 // assumes there is only one in-class declaration. Building a redecl
2985 // chain would result in more than one in-class declaration for
2986 // overrides (even if they are part of the same redecl chain inside the
2987 // derived class.)
2988 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00002989 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002990 if (D->getLexicalDeclContext() == D->getDeclContext()) {
2991 if (!D->doesThisDeclarationHaveABody())
2992 return Importer.MapImported(D, FoundByLookup);
2993 else {
2994 // Let's continue and build up the redecl chain in this case.
2995 // FIXME Merge the functions into one decl.
2996 }
2997 }
2998 }
2999 }
3000
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003001 DeclarationNameInfo NameInfo(Name, Loc);
3002 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003003 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3004 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003005
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003006 QualType FromTy = D->getType();
3007 bool usedDifferentExceptionSpec = false;
3008
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003009 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003010 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3011 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3012 // FunctionDecl that we are importing the FunctionProtoType for.
3013 // To avoid an infinite recursion when importing, create the FunctionDecl
3014 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003015 if (FromEPI.ExceptionSpec.SourceDecl ||
3016 FromEPI.ExceptionSpec.SourceTemplate ||
3017 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003018 FunctionProtoType::ExtProtoInfo DefaultEPI;
3019 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003020 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003021 usedDifferentExceptionSpec = true;
3022 }
3023 }
3024
Balazs Keri3b30d652018-10-19 13:32:20 +00003025 QualType T;
3026 TypeSourceInfo *TInfo;
3027 SourceLocation ToInnerLocStart, ToEndLoc;
3028 NestedNameSpecifierLoc ToQualifierLoc;
3029 if (auto Imp = importSeq(
3030 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3031 D->getQualifierLoc(), D->getEndLoc()))
3032 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3033 else
3034 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003035
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003036 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003037 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003038 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003039 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3040 Parameters.push_back(*ToPOrErr);
3041 else
3042 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003043 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003044
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003045 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003046 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003047 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003048 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003049 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3050 ToInnerLocStart, NameInfo, T, TInfo,
3051 FromConstructor->isExplicit(),
3052 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003053 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003054 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3055
3056 auto Imp =
3057 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3058 FromDtor->getOperatorDeleteThisArg());
3059
3060 if (!Imp)
3061 return Imp.takeError();
3062
3063 FunctionDecl *ToOperatorDelete;
3064 Expr *ToThisArg;
3065 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3066
Gabor Marton26f72a92018-07-12 09:42:05 +00003067 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003068 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3069 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3070 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003071 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003072
3073 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3074
3075 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003076 } else if (CXXConversionDecl *FromConversion =
3077 dyn_cast<CXXConversionDecl>(D)) {
3078 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3079 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003080 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003081 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3082 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003083 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003084 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3085 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003086 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003087 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3088 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003089 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003090 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003091 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003092 D->getStorageClass(), D->isInlineSpecified(),
3093 D->hasWrittenPrototype(), D->isConstexpr()))
3094 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003095 }
John McCall3e11ebe2010-03-15 10:12:16 +00003096
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003097 // Connect the redecl chain.
3098 if (FoundByLookup) {
3099 auto *Recent = const_cast<FunctionDecl *>(
3100 FoundByLookup->getMostRecentDecl());
3101 ToFunction->setPreviousDecl(Recent);
3102 }
3103
3104 // Import Ctor initializers.
3105 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3106 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3107 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3108 // Import first, then allocate memory and copy if there was no error.
3109 if (Error Err = ImportContainerChecked(
3110 FromConstructor->inits(), CtorInitializers))
3111 return std::move(Err);
3112 auto **Memory =
3113 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3114 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3115 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3116 ToCtor->setCtorInitializers(Memory);
3117 ToCtor->setNumCtorInitializers(NumInitializers);
3118 }
3119 }
3120
Balazs Keri3b30d652018-10-19 13:32:20 +00003121 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003122 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003123 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003124 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3125 ToFunction->setTrivial(D->isTrivial());
3126 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003127 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003128
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003129 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003130 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003131 Param->setOwningFunction(ToFunction);
3132 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003133 }
David Blaikie9c70e042011-09-21 18:16:56 +00003134 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003135
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003136 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3137 // params it refers to.
3138 if (TInfo) {
3139 if (auto ProtoLoc =
3140 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3141 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3142 ProtoLoc.setParam(I, Parameters[I]);
3143 }
3144 }
3145
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003146 if (usedDifferentExceptionSpec) {
3147 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003148 if (ExpectedType TyOrErr = import(D->getType()))
3149 ToFunction->setType(*TyOrErr);
3150 else
3151 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003152 }
3153
Balazs Keria35798d2018-07-17 09:52:41 +00003154 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003155 if (FromFT) {
3156 auto ToFTOrErr = import(FromFT);
3157 if (!ToFTOrErr)
3158 return ToFTOrErr.takeError();
3159 }
Balazs Keria35798d2018-07-17 09:52:41 +00003160
Gabor Marton5254e642018-06-27 13:32:50 +00003161 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003162 Error Err = ImportFunctionDeclBody(D, ToFunction);
3163
3164 if (Err)
3165 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003166 }
3167
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003168 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003169
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003170 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003171 if (Error Err = ImportTemplateInformation(D, ToFunction))
3172 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003173
Gabor Marton5254e642018-06-27 13:32:50 +00003174 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3175
3176 // TODO Can we generalize this approach to other AST nodes as well?
3177 if (D->getDeclContext()->containsDeclAndLoad(D))
3178 DC->addDeclInternal(ToFunction);
3179 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003180 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003181
Gabor Marton5254e642018-06-27 13:32:50 +00003182 // Friend declaration's lexical context is the befriending class, but the
3183 // semantic context is the enclosing scope of the befriending class.
3184 // We want the friend functions to be found in the semantic context by lookup.
3185 // FIXME should we handle this generically in VisitFriendDecl?
3186 // In Other cases when LexicalDC != DC we don't want it to be added,
3187 // e.g out-of-class definitions like void B::f() {} .
3188 if (LexicalDC != DC && IsFriend) {
3189 DC->makeDeclVisibleInContext(ToFunction);
3190 }
3191
Gabor Marton7a0841e2018-10-29 10:18:28 +00003192 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3193 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3194
Gabor Marton5254e642018-06-27 13:32:50 +00003195 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003196 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3197 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3198 if (!ToRedeclOrErr)
3199 return ToRedeclOrErr.takeError();
3200 }
Gabor Marton5254e642018-06-27 13:32:50 +00003201
Douglas Gregor43f54792010-02-17 02:12:47 +00003202 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003203}
3204
Balazs Keri3b30d652018-10-19 13:32:20 +00003205ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003206 return VisitFunctionDecl(D);
3207}
3208
Balazs Keri3b30d652018-10-19 13:32:20 +00003209ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003210 return VisitCXXMethodDecl(D);
3211}
3212
Balazs Keri3b30d652018-10-19 13:32:20 +00003213ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003214 return VisitCXXMethodDecl(D);
3215}
3216
Balazs Keri3b30d652018-10-19 13:32:20 +00003217ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003218 return VisitCXXMethodDecl(D);
3219}
3220
Balazs Keri3b30d652018-10-19 13:32:20 +00003221ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003222 // Import the major distinguishing characteristics of a variable.
3223 DeclContext *DC, *LexicalDC;
3224 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003225 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003226 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003227 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3228 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003229 if (ToD)
3230 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003231
Fangrui Song6907ce22018-07-30 19:24:48 +00003232 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003233 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003234 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003235 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003236 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003237 if (!Name &&
3238 ASTImporter::getFieldIndex(D) !=
3239 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003240 continue;
3241
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003242 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003243 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003244 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003245 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3246 // initializer of a FieldDecl might not had been instantiated in the
3247 // "To" context. However, the "From" context might instantiated that,
3248 // thus we have to merge that.
3249 if (Expr *FromInitializer = D->getInClassInitializer()) {
3250 // We don't have yet the initializer set.
3251 if (FoundField->hasInClassInitializer() &&
3252 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003253 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3254 FoundField->setInClassInitializer(*ToInitializerOrErr);
3255 else {
3256 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003257 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003258 // FIXME: warning message?
3259 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003260 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003261 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003262 }
3263 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003264 return FoundField;
3265 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003266
Balazs Keri3b30d652018-10-19 13:32:20 +00003267 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003268 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003269 << Name << D->getType() << FoundField->getType();
3270 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3271 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003272
3273 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003274 }
3275 }
3276
Balazs Keri3b30d652018-10-19 13:32:20 +00003277 QualType ToType;
3278 TypeSourceInfo *ToTInfo;
3279 Expr *ToBitWidth;
3280 SourceLocation ToInnerLocStart;
3281 Expr *ToInitializer;
3282 if (auto Imp = importSeq(
3283 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3284 D->getInnerLocStart(), D->getInClassInitializer()))
3285 std::tie(
3286 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3287 else
3288 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003289
Gabor Marton26f72a92018-07-12 09:42:05 +00003290 FieldDecl *ToField;
3291 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003292 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3293 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3294 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003295 return ToField;
3296
Douglas Gregordd483172010-02-22 17:42:47 +00003297 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003298 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003299 if (ToInitializer)
3300 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003301 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003302 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003303 return ToField;
3304}
3305
Balazs Keri3b30d652018-10-19 13:32:20 +00003306ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003307 // Import the major distinguishing characteristics of a variable.
3308 DeclContext *DC, *LexicalDC;
3309 DeclarationName Name;
3310 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003311 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003312 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3313 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003314 if (ToD)
3315 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003316
Fangrui Song6907ce22018-07-30 19:24:48 +00003317 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003318 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003319 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003320 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003321 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003322 if (!Name &&
3323 ASTImporter::getFieldIndex(D) !=
3324 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003325 continue;
3326
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003327 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003328 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003329 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003330 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003331 return FoundField;
3332 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003333
3334 // If there are more anonymous fields to check, continue.
3335 if (!Name && I < N-1)
3336 continue;
3337
Balazs Keri3b30d652018-10-19 13:32:20 +00003338 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003339 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003340 << Name << D->getType() << FoundField->getType();
3341 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3342 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003343
3344 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003345 }
3346 }
3347
Francois Pichet783dd6e2010-11-21 06:08:52 +00003348 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003349 auto TypeOrErr = import(D->getType());
3350 if (!TypeOrErr)
3351 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003352
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003353 auto **NamedChain =
3354 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003355
3356 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003357 for (auto *PI : D->chain())
3358 if (Expected<NamedDecl *> ToD = import(PI))
3359 NamedChain[i++] = *ToD;
3360 else
3361 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003362
Gabor Marton26f72a92018-07-12 09:42:05 +00003363 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3364 IndirectFieldDecl *ToIndirectField;
3365 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003366 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003367 // FIXME here we leak `NamedChain` which is allocated before
3368 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003369
Francois Pichet783dd6e2010-11-21 06:08:52 +00003370 ToIndirectField->setAccess(D->getAccess());
3371 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003372 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003373 return ToIndirectField;
3374}
3375
Balazs Keri3b30d652018-10-19 13:32:20 +00003376ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003377 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003378 DeclContext *DC, *LexicalDC;
3379 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3380 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003381
3382 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003383 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003384 auto *RD = cast<CXXRecordDecl>(DC);
3385 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003386
3387 while (ImportedFriend) {
3388 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003389 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3390 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003391 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003392
3393 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3394 if (Importer.IsStructurallyEquivalent(
3395 D->getFriendType()->getType(),
3396 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003397 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003398 }
3399 ImportedFriend = ImportedFriend->getNextFriend();
3400 }
3401
3402 // Not found. Create it.
3403 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003404 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003405 NamedDecl *ToFriendD;
3406 if (Error Err = importInto(ToFriendD, FriendD))
3407 return std::move(Err);
3408
3409 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003410 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3411 ToFriendD->setObjectOfFriendDecl(false);
3412
3413 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003414 } else { // The friend is a type, not a decl.
3415 if (auto TSIOrErr = import(D->getFriendType()))
3416 ToFU = *TSIOrErr;
3417 else
3418 return TSIOrErr.takeError();
3419 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003420
3421 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003422 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003423 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003424 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003425 ToTPLists[I] = *ListOrErr;
3426 else
3427 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003428 }
3429
Balazs Keri3b30d652018-10-19 13:32:20 +00003430 auto LocationOrErr = import(D->getLocation());
3431 if (!LocationOrErr)
3432 return LocationOrErr.takeError();
3433 auto FriendLocOrErr = import(D->getFriendLoc());
3434 if (!FriendLocOrErr)
3435 return FriendLocOrErr.takeError();
3436
Gabor Marton26f72a92018-07-12 09:42:05 +00003437 FriendDecl *FrD;
3438 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003439 *LocationOrErr, ToFU,
3440 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003441 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003442
3443 FrD->setAccess(D->getAccess());
3444 FrD->setLexicalDeclContext(LexicalDC);
3445 LexicalDC->addDeclInternal(FrD);
3446 return FrD;
3447}
3448
Balazs Keri3b30d652018-10-19 13:32:20 +00003449ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003450 // Import the major distinguishing characteristics of an ivar.
3451 DeclContext *DC, *LexicalDC;
3452 DeclarationName Name;
3453 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003454 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003455 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3456 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003457 if (ToD)
3458 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003459
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003460 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003461 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003462 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003463 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003464 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003465 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003466 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003467 return FoundIvar;
3468 }
3469
Gabor Marton410f32c2019-04-01 15:29:55 +00003470 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003471 << Name << D->getType() << FoundIvar->getType();
3472 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3473 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003474
3475 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003476 }
3477 }
3478
Balazs Keri3b30d652018-10-19 13:32:20 +00003479 QualType ToType;
3480 TypeSourceInfo *ToTypeSourceInfo;
3481 Expr *ToBitWidth;
3482 SourceLocation ToInnerLocStart;
3483 if (auto Imp = importSeq(
3484 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3485 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3486 else
3487 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003488
Gabor Marton26f72a92018-07-12 09:42:05 +00003489 ObjCIvarDecl *ToIvar;
3490 if (GetImportedOrCreateDecl(
3491 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003492 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3493 ToType, ToTypeSourceInfo,
3494 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003495 return ToIvar;
3496
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003497 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003498 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003499 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003500}
3501
Balazs Keri3b30d652018-10-19 13:32:20 +00003502ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003503
3504 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3505 auto RedeclIt = Redecls.begin();
3506 // Import the first part of the decl chain. I.e. import all previous
3507 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003508 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3509 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3510 if (!RedeclOrErr)
3511 return RedeclOrErr.takeError();
3512 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003513 assert(*RedeclIt == D);
3514
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003515 // Import the major distinguishing characteristics of a variable.
3516 DeclContext *DC, *LexicalDC;
3517 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003518 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003519 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003520 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3521 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003522 if (ToD)
3523 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003524
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003525 // Try to find a variable in our own ("to") context with the same name and
3526 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003527 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003528 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003529 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003530 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003531 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003532 for (auto *FoundDecl : FoundDecls) {
3533 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003534 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003535
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003536 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003537 if (!hasSameVisibilityContext(FoundVar, D))
3538 continue;
3539 if (Importer.IsStructurallyEquivalent(D->getType(),
3540 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003541
Gabor Marton458d1452019-02-14 13:07:03 +00003542 // The VarDecl in the "From" context has a definition, but in the
3543 // "To" context we already have a definition.
3544 VarDecl *FoundDef = FoundVar->getDefinition();
3545 if (D->isThisDeclarationADefinition() && FoundDef)
3546 // FIXME Check for ODR error if the two definitions have
3547 // different initializers?
3548 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003549
Gabor Marton458d1452019-02-14 13:07:03 +00003550 // The VarDecl in the "From" context has an initializer, but in the
3551 // "To" context we already have an initializer.
3552 const VarDecl *FoundDInit = nullptr;
3553 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3554 // FIXME Diagnose ODR error if the two initializers are different?
3555 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3556
3557 FoundByLookup = FoundVar;
3558 break;
3559 }
3560
3561 const ArrayType *FoundArray
3562 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3563 const ArrayType *TArray
3564 = Importer.getToContext().getAsArrayType(D->getType());
3565 if (FoundArray && TArray) {
3566 if (isa<IncompleteArrayType>(FoundArray) &&
3567 isa<ConstantArrayType>(TArray)) {
3568 // Import the type.
3569 if (auto TyOrErr = import(D->getType()))
3570 FoundVar->setType(*TyOrErr);
3571 else
3572 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003573
3574 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003575 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003576 } else if (isa<IncompleteArrayType>(TArray) &&
3577 isa<ConstantArrayType>(FoundArray)) {
3578 FoundByLookup = FoundVar;
3579 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003580 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003581 }
Gabor Marton458d1452019-02-14 13:07:03 +00003582
Gabor Marton410f32c2019-04-01 15:29:55 +00003583 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003584 << Name << D->getType() << FoundVar->getType();
3585 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3586 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003587 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003588
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003589 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003590 }
3591
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003592 if (!ConflictingDecls.empty()) {
3593 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003594 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003595 ConflictingDecls.size());
3596 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003597 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003598 }
3599 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003600
Balazs Keri3b30d652018-10-19 13:32:20 +00003601 QualType ToType;
3602 TypeSourceInfo *ToTypeSourceInfo;
3603 SourceLocation ToInnerLocStart;
3604 NestedNameSpecifierLoc ToQualifierLoc;
3605 if (auto Imp = importSeq(
3606 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3607 D->getQualifierLoc()))
3608 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3609 else
3610 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003611
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003612 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003613 VarDecl *ToVar;
3614 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003615 ToInnerLocStart, Loc,
3616 Name.getAsIdentifierInfo(),
3617 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003618 D->getStorageClass()))
3619 return ToVar;
3620
Balazs Keri3b30d652018-10-19 13:32:20 +00003621 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003622 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003623 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003624
Gabor Martonac3a5d62018-09-17 12:04:52 +00003625 if (FoundByLookup) {
3626 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3627 ToVar->setPreviousDecl(Recent);
3628 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003629
Balazs Keri3b30d652018-10-19 13:32:20 +00003630 if (Error Err = ImportInitializer(D, ToVar))
3631 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003632
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003633 if (D->isConstexpr())
3634 ToVar->setConstexpr(true);
3635
Gabor Martonac3a5d62018-09-17 12:04:52 +00003636 if (D->getDeclContext()->containsDeclAndLoad(D))
3637 DC->addDeclInternal(ToVar);
3638 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3639 LexicalDC->addDeclInternal(ToVar);
3640
3641 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003642 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3643 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3644 if (!RedeclOrErr)
3645 return RedeclOrErr.takeError();
3646 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003647
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003648 return ToVar;
3649}
3650
Balazs Keri3b30d652018-10-19 13:32:20 +00003651ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003652 // Parameters are created in the translation unit's context, then moved
3653 // into the function declaration's context afterward.
3654 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003655
Balazs Keri3b30d652018-10-19 13:32:20 +00003656 DeclarationName ToDeclName;
3657 SourceLocation ToLocation;
3658 QualType ToType;
3659 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3660 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3661 else
3662 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003663
Douglas Gregor8b228d72010-02-17 21:22:52 +00003664 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003665 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003666 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3667 ToLocation, ToDeclName.getAsIdentifierInfo(),
3668 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003669 return ToParm;
3670 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003671}
3672
Balazs Keri3b30d652018-10-19 13:32:20 +00003673ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003674 // Parameters are created in the translation unit's context, then moved
3675 // into the function declaration's context afterward.
3676 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003677
Balazs Keri3b30d652018-10-19 13:32:20 +00003678 DeclarationName ToDeclName;
3679 SourceLocation ToLocation, ToInnerLocStart;
3680 QualType ToType;
3681 TypeSourceInfo *ToTypeSourceInfo;
3682 if (auto Imp = importSeq(
3683 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3684 D->getTypeSourceInfo()))
3685 std::tie(
3686 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3687 ToTypeSourceInfo) = *Imp;
3688 else
3689 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003690
Gabor Marton26f72a92018-07-12 09:42:05 +00003691 ParmVarDecl *ToParm;
3692 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003693 ToInnerLocStart, ToLocation,
3694 ToDeclName.getAsIdentifierInfo(), ToType,
3695 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003696 /*DefaultArg*/ nullptr))
3697 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003698
3699 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003700 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003701 ToParm->setKNRPromoted(D->isKNRPromoted());
3702
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003703 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003704 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3705 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3706 else
3707 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003708 } else if (D->hasUnparsedDefaultArg()) {
3709 ToParm->setUnparsedDefaultArg();
3710 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003711 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3712 ToParm->setDefaultArg(*ToDefArgOrErr);
3713 else
3714 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003715 }
Sean Callanan59721b32015-04-28 18:41:46 +00003716
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003717 if (D->isObjCMethodParameter()) {
3718 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3719 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3720 } else {
3721 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3722 D->getFunctionScopeIndex());
3723 }
3724
Gabor Marton26f72a92018-07-12 09:42:05 +00003725 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003726}
3727
Balazs Keri3b30d652018-10-19 13:32:20 +00003728ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003729 // Import the major distinguishing characteristics of a method.
3730 DeclContext *DC, *LexicalDC;
3731 DeclarationName Name;
3732 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003733 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003734 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3735 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003736 if (ToD)
3737 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003738
Gabor Marton54058b52018-12-17 13:53:12 +00003739 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003740 for (auto *FoundDecl : FoundDecls) {
3741 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003742 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3743 continue;
3744
3745 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003746 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3747 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003748 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003749 << D->isInstanceMethod() << Name << D->getReturnType()
3750 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003751 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003752 diag::note_odr_objc_method_here)
3753 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003754
3755 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003756 }
3757
3758 // Check the number of parameters.
3759 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003760 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003761 << D->isInstanceMethod() << Name
3762 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003763 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003764 diag::note_odr_objc_method_here)
3765 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003766
3767 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003768 }
3769
3770 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003771 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003772 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3773 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003774 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003775 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003776 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003777 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003778 << D->isInstanceMethod() << Name
3779 << (*P)->getType() << (*FoundP)->getType();
3780 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3781 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003782
3783 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003784 }
3785 }
3786
3787 // Check variadic/non-variadic.
3788 // Check the number of parameters.
3789 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003790 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003791 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003792 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003793 diag::note_odr_objc_method_here)
3794 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003795
3796 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003797 }
3798
3799 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003800 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003801 }
3802 }
3803
Balazs Keri3b30d652018-10-19 13:32:20 +00003804 SourceLocation ToEndLoc;
3805 QualType ToReturnType;
3806 TypeSourceInfo *ToReturnTypeSourceInfo;
3807 if (auto Imp = importSeq(
3808 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3809 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3810 else
3811 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003812
Gabor Marton26f72a92018-07-12 09:42:05 +00003813 ObjCMethodDecl *ToMethod;
3814 if (GetImportedOrCreateDecl(
3815 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003816 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3817 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003818 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3819 D->getImplementationControl(), D->hasRelatedResultType()))
3820 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003821
3822 // FIXME: When we decide to merge method definitions, we'll need to
3823 // deal with implicit parameters.
3824
3825 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003826 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003827 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003828 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3829 ToParams.push_back(*ToPOrErr);
3830 else
3831 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003832 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003833
Douglas Gregor43f54792010-02-17 02:12:47 +00003834 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003835 for (auto *ToParam : ToParams) {
3836 ToParam->setOwningFunction(ToMethod);
3837 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003838 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003839
Balazs Keri3b30d652018-10-19 13:32:20 +00003840 SmallVector<SourceLocation, 12> FromSelLocs;
3841 D->getSelectorLocs(FromSelLocs);
3842 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3843 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3844 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003845
Balazs Keri3b30d652018-10-19 13:32:20 +00003846 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003847
3848 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003849 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003850 return ToMethod;
3851}
3852
Balazs Keri3b30d652018-10-19 13:32:20 +00003853ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003854 // Import the major distinguishing characteristics of a category.
3855 DeclContext *DC, *LexicalDC;
3856 DeclarationName Name;
3857 SourceLocation Loc;
3858 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003859 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3860 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003861 if (ToD)
3862 return ToD;
3863
Balazs Keri3b30d652018-10-19 13:32:20 +00003864 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3865 TypeSourceInfo *ToTypeSourceInfo;
3866 if (auto Imp = importSeq(
3867 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3868 D->getTypeSourceInfo()))
3869 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3870 else
3871 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003872
Gabor Marton26f72a92018-07-12 09:42:05 +00003873 ObjCTypeParamDecl *Result;
3874 if (GetImportedOrCreateDecl(
3875 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003876 ToVarianceLoc, D->getIndex(),
3877 ToLocation, Name.getAsIdentifierInfo(),
3878 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003879 return Result;
3880
Douglas Gregor85f3f952015-07-07 03:57:15 +00003881 Result->setLexicalDeclContext(LexicalDC);
3882 return Result;
3883}
3884
Balazs Keri3b30d652018-10-19 13:32:20 +00003885ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003886 // Import the major distinguishing characteristics of a category.
3887 DeclContext *DC, *LexicalDC;
3888 DeclarationName Name;
3889 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003890 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003891 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3892 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003893 if (ToD)
3894 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003895
Balazs Keri3b30d652018-10-19 13:32:20 +00003896 ObjCInterfaceDecl *ToInterface;
3897 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3898 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003899
Douglas Gregor84c51c32010-02-18 01:47:50 +00003900 // Determine if we've already encountered this category.
3901 ObjCCategoryDecl *MergeWithCategory
3902 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3903 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3904 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003905 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3906 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3907 if (auto Imp = importSeq(
3908 D->getAtStartLoc(), D->getCategoryNameLoc(),
3909 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3910 std::tie(
3911 ToAtStartLoc, ToCategoryNameLoc,
3912 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3913 else
3914 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003915
3916 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003917 ToAtStartLoc, Loc,
3918 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003919 Name.getAsIdentifierInfo(), ToInterface,
3920 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00003921 ToIvarLBraceLoc,
3922 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00003923 return ToCategory;
3924
Douglas Gregor84c51c32010-02-18 01:47:50 +00003925 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003926 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00003927 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003928 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00003929 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
3930 ToCategory->setTypeParamList(*PListOrErr);
3931 else
3932 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003933
Douglas Gregor84c51c32010-02-18 01:47:50 +00003934 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003935 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3936 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003937 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3938 = D->protocol_loc_begin();
3939 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3940 FromProtoEnd = D->protocol_end();
3941 FromProto != FromProtoEnd;
3942 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003943 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
3944 Protocols.push_back(*ToProtoOrErr);
3945 else
3946 return ToProtoOrErr.takeError();
3947
3948 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
3949 ProtocolLocs.push_back(*ToProtoLocOrErr);
3950 else
3951 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00003952 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003953
Douglas Gregor84c51c32010-02-18 01:47:50 +00003954 // FIXME: If we're merging, make sure that the protocol list is the same.
3955 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3956 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00003957
Douglas Gregor84c51c32010-02-18 01:47:50 +00003958 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003959 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003960 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003961
Douglas Gregor84c51c32010-02-18 01:47:50 +00003962 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00003963 if (Error Err = ImportDeclContext(D))
3964 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00003965
Douglas Gregor84c51c32010-02-18 01:47:50 +00003966 // If we have an implementation, import it as well.
3967 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003968 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
3969 import(D->getImplementation()))
3970 ToCategory->setImplementation(*ToImplOrErr);
3971 else
3972 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00003973 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003974
Douglas Gregor84c51c32010-02-18 01:47:50 +00003975 return ToCategory;
3976}
3977
Balazs Keri3b30d652018-10-19 13:32:20 +00003978Error ASTNodeImporter::ImportDefinition(
3979 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003980 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003981 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00003982 if (Error Err = ImportDeclContext(From))
3983 return Err;
3984 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00003985 }
3986
3987 // Start the protocol definition
3988 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00003989
Douglas Gregor2aa53772012-01-24 17:42:07 +00003990 // Import protocols
3991 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3992 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00003993 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
3994 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00003995 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3996 FromProtoEnd = From->protocol_end();
3997 FromProto != FromProtoEnd;
3998 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003999 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4000 Protocols.push_back(*ToProtoOrErr);
4001 else
4002 return ToProtoOrErr.takeError();
4003
4004 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4005 ProtocolLocs.push_back(*ToProtoLocOrErr);
4006 else
4007 return ToProtoLocOrErr.takeError();
4008
Douglas Gregor2aa53772012-01-24 17:42:07 +00004009 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004010
Douglas Gregor2aa53772012-01-24 17:42:07 +00004011 // FIXME: If we're merging, make sure that the protocol list is the same.
4012 To->setProtocolList(Protocols.data(), Protocols.size(),
4013 ProtocolLocs.data(), Importer.getToContext());
4014
Douglas Gregor2e15c842012-02-01 21:00:38 +00004015 if (shouldForceImportDeclContext(Kind)) {
4016 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004017 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4018 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004019 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004020 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004021}
4022
Balazs Keri3b30d652018-10-19 13:32:20 +00004023ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004024 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004025 // from, but this particular declaration is not that definition, import the
4026 // definition and map to that.
4027 ObjCProtocolDecl *Definition = D->getDefinition();
4028 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004029 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4030 return Importer.MapImported(D, *ImportedDefOrErr);
4031 else
4032 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004033 }
4034
Douglas Gregor84c51c32010-02-18 01:47:50 +00004035 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004036 DeclContext *DC, *LexicalDC;
4037 DeclarationName Name;
4038 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004039 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004040 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4041 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004042 if (ToD)
4043 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004044
Craig Topper36250ad2014-05-12 05:36:57 +00004045 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004046 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004047 for (auto *FoundDecl : FoundDecls) {
4048 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004049 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004050
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004051 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004052 break;
4053 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004054
Douglas Gregor98d156a2010-02-17 16:12:00 +00004055 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004056 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004057 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4058 if (!ToAtBeginLocOrErr)
4059 return ToAtBeginLocOrErr.takeError();
4060
Gabor Marton26f72a92018-07-12 09:42:05 +00004061 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4062 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004063 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004064 /*PrevDecl=*/nullptr))
4065 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004066 ToProto->setLexicalDeclContext(LexicalDC);
4067 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004068 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004069
4070 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004071
Balazs Keri3b30d652018-10-19 13:32:20 +00004072 if (D->isThisDeclarationADefinition())
4073 if (Error Err = ImportDefinition(D, ToProto))
4074 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004075
Douglas Gregor98d156a2010-02-17 16:12:00 +00004076 return ToProto;
4077}
4078
Balazs Keri3b30d652018-10-19 13:32:20 +00004079ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4080 DeclContext *DC, *LexicalDC;
4081 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4082 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004083
Balazs Keri3b30d652018-10-19 13:32:20 +00004084 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4085 if (!ExternLocOrErr)
4086 return ExternLocOrErr.takeError();
4087
4088 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4089 if (!LangLocOrErr)
4090 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004091
4092 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004093
4094 LinkageSpecDecl *ToLinkageSpec;
4095 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004096 *ExternLocOrErr, *LangLocOrErr,
4097 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004098 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004099
4100 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004101 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4102 if (!RBraceLocOrErr)
4103 return RBraceLocOrErr.takeError();
4104 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004105 }
4106
4107 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4108 LexicalDC->addDeclInternal(ToLinkageSpec);
4109
Sean Callanan0aae0412014-12-10 00:00:37 +00004110 return ToLinkageSpec;
4111}
4112
Balazs Keri3b30d652018-10-19 13:32:20 +00004113ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004114 DeclContext *DC, *LexicalDC;
4115 DeclarationName Name;
4116 SourceLocation Loc;
4117 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004118 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4119 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004120 if (ToD)
4121 return ToD;
4122
Balazs Keri3b30d652018-10-19 13:32:20 +00004123 SourceLocation ToLoc, ToUsingLoc;
4124 NestedNameSpecifierLoc ToQualifierLoc;
4125 if (auto Imp = importSeq(
4126 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4127 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4128 else
4129 return Imp.takeError();
4130
4131 DeclarationNameInfo NameInfo(Name, ToLoc);
4132 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4133 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004134
Gabor Marton26f72a92018-07-12 09:42:05 +00004135 UsingDecl *ToUsing;
4136 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004137 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004138 D->hasTypename()))
4139 return ToUsing;
4140
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004141 ToUsing->setLexicalDeclContext(LexicalDC);
4142 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004143
4144 if (NamedDecl *FromPattern =
4145 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004146 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4147 Importer.getToContext().setInstantiatedFromUsingDecl(
4148 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004149 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004150 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004151 }
4152
Balazs Keri3b30d652018-10-19 13:32:20 +00004153 for (UsingShadowDecl *FromShadow : D->shadows()) {
4154 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4155 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004156 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004157 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004158 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004159 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004160 }
4161 return ToUsing;
4162}
4163
Balazs Keri3b30d652018-10-19 13:32:20 +00004164ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004165 DeclContext *DC, *LexicalDC;
4166 DeclarationName Name;
4167 SourceLocation Loc;
4168 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004169 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4170 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004171 if (ToD)
4172 return ToD;
4173
Balazs Keri3b30d652018-10-19 13:32:20 +00004174 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4175 if (!ToUsingOrErr)
4176 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004177
Balazs Keri3b30d652018-10-19 13:32:20 +00004178 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4179 if (!ToTargetOrErr)
4180 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004181
Gabor Marton26f72a92018-07-12 09:42:05 +00004182 UsingShadowDecl *ToShadow;
4183 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004184 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004185 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004186
4187 ToShadow->setLexicalDeclContext(LexicalDC);
4188 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004189
4190 if (UsingShadowDecl *FromPattern =
4191 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004192 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4193 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4194 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004195 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004196 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004197 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004198 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004199 }
4200
4201 LexicalDC->addDeclInternal(ToShadow);
4202
4203 return ToShadow;
4204}
4205
Balazs Keri3b30d652018-10-19 13:32:20 +00004206ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004207 DeclContext *DC, *LexicalDC;
4208 DeclarationName Name;
4209 SourceLocation Loc;
4210 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004211 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4212 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004213 if (ToD)
4214 return ToD;
4215
Balazs Keri3b30d652018-10-19 13:32:20 +00004216 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4217 if (!ToComAncestorOrErr)
4218 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004219
Balazs Keri3b30d652018-10-19 13:32:20 +00004220 NamespaceDecl *ToNominatedNamespace;
4221 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4222 NestedNameSpecifierLoc ToQualifierLoc;
4223 if (auto Imp = importSeq(
4224 D->getNominatedNamespace(), D->getUsingLoc(),
4225 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4226 D->getIdentLocation()))
4227 std::tie(
4228 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4229 ToQualifierLoc, ToIdentLocation) = *Imp;
4230 else
4231 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004232
Gabor Marton26f72a92018-07-12 09:42:05 +00004233 UsingDirectiveDecl *ToUsingDir;
4234 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004235 ToUsingLoc,
4236 ToNamespaceKeyLocation,
4237 ToQualifierLoc,
4238 ToIdentLocation,
4239 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004240 return ToUsingDir;
4241
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004242 ToUsingDir->setLexicalDeclContext(LexicalDC);
4243 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004244
4245 return ToUsingDir;
4246}
4247
Balazs Keri3b30d652018-10-19 13:32:20 +00004248ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004249 UnresolvedUsingValueDecl *D) {
4250 DeclContext *DC, *LexicalDC;
4251 DeclarationName Name;
4252 SourceLocation Loc;
4253 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004254 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4255 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004256 if (ToD)
4257 return ToD;
4258
Balazs Keri3b30d652018-10-19 13:32:20 +00004259 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4260 NestedNameSpecifierLoc ToQualifierLoc;
4261 if (auto Imp = importSeq(
4262 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4263 D->getEllipsisLoc()))
4264 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4265 else
4266 return Imp.takeError();
4267
4268 DeclarationNameInfo NameInfo(Name, ToLoc);
4269 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4270 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004271
Gabor Marton26f72a92018-07-12 09:42:05 +00004272 UnresolvedUsingValueDecl *ToUsingValue;
4273 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004274 ToUsingLoc, ToQualifierLoc, NameInfo,
4275 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004276 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004277
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004278 ToUsingValue->setAccess(D->getAccess());
4279 ToUsingValue->setLexicalDeclContext(LexicalDC);
4280 LexicalDC->addDeclInternal(ToUsingValue);
4281
4282 return ToUsingValue;
4283}
4284
Balazs Keri3b30d652018-10-19 13:32:20 +00004285ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004286 UnresolvedUsingTypenameDecl *D) {
4287 DeclContext *DC, *LexicalDC;
4288 DeclarationName Name;
4289 SourceLocation Loc;
4290 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004291 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4292 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004293 if (ToD)
4294 return ToD;
4295
Balazs Keri3b30d652018-10-19 13:32:20 +00004296 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4297 NestedNameSpecifierLoc ToQualifierLoc;
4298 if (auto Imp = importSeq(
4299 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4300 D->getEllipsisLoc()))
4301 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4302 else
4303 return Imp.takeError();
4304
Gabor Marton26f72a92018-07-12 09:42:05 +00004305 UnresolvedUsingTypenameDecl *ToUsing;
4306 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004307 ToUsingLoc, ToTypenameLoc,
4308 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004309 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004310
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004311 ToUsing->setAccess(D->getAccess());
4312 ToUsing->setLexicalDeclContext(LexicalDC);
4313 LexicalDC->addDeclInternal(ToUsing);
4314
4315 return ToUsing;
4316}
4317
Balazs Keri3b30d652018-10-19 13:32:20 +00004318
4319Error ASTNodeImporter::ImportDefinition(
4320 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004321 if (To->getDefinition()) {
4322 // Check consistency of superclass.
4323 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4324 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004325 if (auto FromSuperOrErr = import(FromSuper))
4326 FromSuper = *FromSuperOrErr;
4327 else
4328 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004329 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004330
4331 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004332 if ((bool)FromSuper != (bool)ToSuper ||
4333 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004334 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004335 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004336 << To->getDeclName();
4337 if (ToSuper)
4338 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4339 << To->getSuperClass()->getDeclName();
4340 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004341 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004342 diag::note_odr_objc_missing_superclass);
4343 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004344 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004345 diag::note_odr_objc_superclass)
4346 << From->getSuperClass()->getDeclName();
4347 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004348 Importer.FromDiag(From->getLocation(),
4349 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004350 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004351
Douglas Gregor2e15c842012-02-01 21:00:38 +00004352 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004353 if (Error Err = ImportDeclContext(From))
4354 return Err;
4355 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004356 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004357
Douglas Gregor2aa53772012-01-24 17:42:07 +00004358 // Start the definition.
4359 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004360
Douglas Gregor2aa53772012-01-24 17:42:07 +00004361 // If this class has a superclass, import it.
4362 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004363 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4364 To->setSuperClass(*SuperTInfoOrErr);
4365 else
4366 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004367 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004368
Douglas Gregor2aa53772012-01-24 17:42:07 +00004369 // Import protocols
4370 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4371 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004372 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4373 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004374
Douglas Gregor2aa53772012-01-24 17:42:07 +00004375 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4376 FromProtoEnd = From->protocol_end();
4377 FromProto != FromProtoEnd;
4378 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004379 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4380 Protocols.push_back(*ToProtoOrErr);
4381 else
4382 return ToProtoOrErr.takeError();
4383
4384 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4385 ProtocolLocs.push_back(*ToProtoLocOrErr);
4386 else
4387 return ToProtoLocOrErr.takeError();
4388
Douglas Gregor2aa53772012-01-24 17:42:07 +00004389 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004390
Douglas Gregor2aa53772012-01-24 17:42:07 +00004391 // FIXME: If we're merging, make sure that the protocol list is the same.
4392 To->setProtocolList(Protocols.data(), Protocols.size(),
4393 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004394
Douglas Gregor2aa53772012-01-24 17:42:07 +00004395 // Import categories. When the categories themselves are imported, they'll
4396 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004397 for (auto *Cat : From->known_categories()) {
4398 auto ToCatOrErr = import(Cat);
4399 if (!ToCatOrErr)
4400 return ToCatOrErr.takeError();
4401 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004402
Douglas Gregor2aa53772012-01-24 17:42:07 +00004403 // If we have an @implementation, import it as well.
4404 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004405 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4406 import(From->getImplementation()))
4407 To->setImplementation(*ToImplOrErr);
4408 else
4409 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004410 }
4411
Douglas Gregor2e15c842012-02-01 21:00:38 +00004412 if (shouldForceImportDeclContext(Kind)) {
4413 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004414 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4415 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004416 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004417 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004418}
4419
Balazs Keri3b30d652018-10-19 13:32:20 +00004420Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004421ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4422 if (!list)
4423 return nullptr;
4424
4425 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004426 for (auto *fromTypeParam : *list) {
4427 if (auto toTypeParamOrErr = import(fromTypeParam))
4428 toTypeParams.push_back(*toTypeParamOrErr);
4429 else
4430 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004431 }
4432
Balazs Keri3b30d652018-10-19 13:32:20 +00004433 auto LAngleLocOrErr = import(list->getLAngleLoc());
4434 if (!LAngleLocOrErr)
4435 return LAngleLocOrErr.takeError();
4436
4437 auto RAngleLocOrErr = import(list->getRAngleLoc());
4438 if (!RAngleLocOrErr)
4439 return RAngleLocOrErr.takeError();
4440
Douglas Gregor85f3f952015-07-07 03:57:15 +00004441 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004442 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004443 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004444 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004445}
4446
Balazs Keri3b30d652018-10-19 13:32:20 +00004447ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004448 // If this class has a definition in the translation unit we're coming from,
4449 // but this particular declaration is not that definition, import the
4450 // definition and map to that.
4451 ObjCInterfaceDecl *Definition = D->getDefinition();
4452 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004453 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4454 return Importer.MapImported(D, *ImportedDefOrErr);
4455 else
4456 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004457 }
4458
Douglas Gregor45635322010-02-16 01:20:57 +00004459 // Import the major distinguishing characteristics of an @interface.
4460 DeclContext *DC, *LexicalDC;
4461 DeclarationName Name;
4462 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004463 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004464 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4465 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004466 if (ToD)
4467 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004468
Douglas Gregor2aa53772012-01-24 17:42:07 +00004469 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004470 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004471 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004472 for (auto *FoundDecl : FoundDecls) {
4473 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004474 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004475
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004476 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004477 break;
4478 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004479
Douglas Gregor2aa53772012-01-24 17:42:07 +00004480 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004481 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004482 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004483 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4484 if (!AtBeginLocOrErr)
4485 return AtBeginLocOrErr.takeError();
4486
Gabor Marton26f72a92018-07-12 09:42:05 +00004487 if (GetImportedOrCreateDecl(
4488 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004489 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004490 /*TypeParamList=*/nullptr,
4491 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4492 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004493 ToIface->setLexicalDeclContext(LexicalDC);
4494 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004495 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004496 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004497 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004498 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004499 if (auto ToPListOrErr =
4500 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4501 ToIface->setTypeParamList(*ToPListOrErr);
4502 else
4503 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004504
Balazs Keri3b30d652018-10-19 13:32:20 +00004505 if (D->isThisDeclarationADefinition())
4506 if (Error Err = ImportDefinition(D, ToIface))
4507 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004508
Douglas Gregor98d156a2010-02-17 16:12:00 +00004509 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004510}
4511
Balazs Keri3b30d652018-10-19 13:32:20 +00004512ExpectedDecl
4513ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4514 ObjCCategoryDecl *Category;
4515 if (Error Err = importInto(Category, D->getCategoryDecl()))
4516 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004517
Douglas Gregor4da9d682010-12-07 15:32:12 +00004518 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4519 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004520 DeclContext *DC, *LexicalDC;
4521 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4522 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004523
Balazs Keri3b30d652018-10-19 13:32:20 +00004524 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4525 if (auto Imp = importSeq(
4526 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4527 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4528 else
4529 return Imp.takeError();
4530
Gabor Marton26f72a92018-07-12 09:42:05 +00004531 if (GetImportedOrCreateDecl(
4532 ToImpl, D, Importer.getToContext(), DC,
4533 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004534 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004535 return ToImpl;
4536
Balazs Keri3b30d652018-10-19 13:32:20 +00004537 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004538 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004539 Category->setImplementation(ToImpl);
4540 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004541
Gabor Marton26f72a92018-07-12 09:42:05 +00004542 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004543 if (Error Err = ImportDeclContext(D))
4544 return std::move(Err);
4545
Douglas Gregor4da9d682010-12-07 15:32:12 +00004546 return ToImpl;
4547}
4548
Balazs Keri3b30d652018-10-19 13:32:20 +00004549ExpectedDecl
4550ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004551 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004552 ObjCInterfaceDecl *Iface;
4553 if (Error Err = importInto(Iface, D->getClassInterface()))
4554 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004555
4556 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004557 ObjCInterfaceDecl *Super;
4558 if (Error Err = importInto(Super, D->getSuperClass()))
4559 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004560
4561 ObjCImplementationDecl *Impl = Iface->getImplementation();
4562 if (!Impl) {
4563 // We haven't imported an implementation yet. Create a new @implementation
4564 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004565 DeclContext *DC, *LexicalDC;
4566 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4567 return std::move(Err);
4568
4569 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4570 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4571 if (auto Imp = importSeq(
4572 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4573 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4574 std::tie(
4575 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4576 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4577 else
4578 return Imp.takeError();
4579
Gabor Marton26f72a92018-07-12 09:42:05 +00004580 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004581 DC, Iface, Super,
4582 ToLocation,
4583 ToAtStartLoc,
4584 ToSuperClassLoc,
4585 ToIvarLBraceLoc,
4586 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004587 return Impl;
4588
Balazs Keri3b30d652018-10-19 13:32:20 +00004589 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004590
Douglas Gregorda8025c2010-12-07 01:26:03 +00004591 // Associate the implementation with the class it implements.
4592 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004593 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004594 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004595 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004596
4597 // Verify that the existing @implementation has the same superclass.
4598 if ((Super && !Impl->getSuperClass()) ||
4599 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004600 (Super && Impl->getSuperClass() &&
4601 !declaresSameEntity(Super->getCanonicalDecl(),
4602 Impl->getSuperClass()))) {
4603 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004604 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004605 << Iface->getDeclName();
4606 // FIXME: It would be nice to have the location of the superclass
4607 // below.
4608 if (Impl->getSuperClass())
4609 Importer.ToDiag(Impl->getLocation(),
4610 diag::note_odr_objc_superclass)
4611 << Impl->getSuperClass()->getDeclName();
4612 else
4613 Importer.ToDiag(Impl->getLocation(),
4614 diag::note_odr_objc_missing_superclass);
4615 if (D->getSuperClass())
4616 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004617 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004618 << D->getSuperClass()->getDeclName();
4619 else
4620 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004621 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004622
4623 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004624 }
4625 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004626
Douglas Gregorda8025c2010-12-07 01:26:03 +00004627 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004628 if (Error Err = ImportDeclContext(D))
4629 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004630
4631 return Impl;
4632}
4633
Balazs Keri3b30d652018-10-19 13:32:20 +00004634ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004635 // Import the major distinguishing characteristics of an @property.
4636 DeclContext *DC, *LexicalDC;
4637 DeclarationName Name;
4638 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004639 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004640 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4641 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004642 if (ToD)
4643 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004644
4645 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004646 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004647 for (auto *FoundDecl : FoundDecls) {
4648 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004649 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004650 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004651 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004652 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004653 << Name << D->getType() << FoundProp->getType();
4654 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4655 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004656
4657 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004658 }
4659
4660 // FIXME: Check property attributes, getters, setters, etc.?
4661
4662 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004663 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004664 return FoundProp;
4665 }
4666 }
4667
Balazs Keri3b30d652018-10-19 13:32:20 +00004668 QualType ToType;
4669 TypeSourceInfo *ToTypeSourceInfo;
4670 SourceLocation ToAtLoc, ToLParenLoc;
4671 if (auto Imp = importSeq(
4672 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4673 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4674 else
4675 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004676
4677 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004678 ObjCPropertyDecl *ToProperty;
4679 if (GetImportedOrCreateDecl(
4680 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004681 Name.getAsIdentifierInfo(), ToAtLoc,
4682 ToLParenLoc, ToType,
4683 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004684 return ToProperty;
4685
Balazs Keri3b30d652018-10-19 13:32:20 +00004686 Selector ToGetterName, ToSetterName;
4687 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4688 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4689 ObjCIvarDecl *ToPropertyIvarDecl;
4690 if (auto Imp = importSeq(
4691 D->getGetterName(), D->getSetterName(),
4692 D->getGetterNameLoc(), D->getSetterNameLoc(),
4693 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4694 D->getPropertyIvarDecl()))
4695 std::tie(
4696 ToGetterName, ToSetterName,
4697 ToGetterNameLoc, ToSetterNameLoc,
4698 ToGetterMethodDecl, ToSetterMethodDecl,
4699 ToPropertyIvarDecl) = *Imp;
4700 else
4701 return Imp.takeError();
4702
Douglas Gregora11c4582010-02-17 18:02:10 +00004703 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004704 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004705
4706 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004707 ToProperty->setPropertyAttributesAsWritten(
4708 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004709 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4710 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4711 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4712 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4713 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004714 return ToProperty;
4715}
4716
Balazs Keri3b30d652018-10-19 13:32:20 +00004717ExpectedDecl
4718ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4719 ObjCPropertyDecl *Property;
4720 if (Error Err = importInto(Property, D->getPropertyDecl()))
4721 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004722
Balazs Keri3b30d652018-10-19 13:32:20 +00004723 DeclContext *DC, *LexicalDC;
4724 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4725 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004726
Balazs Keri3b30d652018-10-19 13:32:20 +00004727 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004728
4729 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004730 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004731 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4732 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004733
4734 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004735 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4736 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004737 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004738 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4739 if (auto Imp = importSeq(
4740 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4741 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4742 else
4743 return Imp.takeError();
4744
Gabor Marton26f72a92018-07-12 09:42:05 +00004745 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004746 ToBeginLoc,
4747 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004748 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004749 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004750 return ToImpl;
4751
Douglas Gregor14a49e22010-12-07 18:32:03 +00004752 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004753 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004754 } else {
4755 // Check that we have the same kind of property implementation (@synthesize
4756 // vs. @dynamic).
4757 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004758 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004759 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004760 << Property->getDeclName()
4761 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004762 == ObjCPropertyImplDecl::Dynamic);
4763 Importer.FromDiag(D->getLocation(),
4764 diag::note_odr_objc_property_impl_kind)
4765 << D->getPropertyDecl()->getDeclName()
4766 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004767
4768 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004769 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004770
4771 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004772 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4773 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004774 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004775 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004776 << Property->getDeclName()
4777 << ToImpl->getPropertyIvarDecl()->getDeclName()
4778 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004779 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004780 diag::note_odr_objc_synthesize_ivar_here)
4781 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004782
4783 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004784 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004785
Douglas Gregor14a49e22010-12-07 18:32:03 +00004786 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004787 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004788 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004789
Douglas Gregor14a49e22010-12-07 18:32:03 +00004790 return ToImpl;
4791}
4792
Balazs Keri3b30d652018-10-19 13:32:20 +00004793ExpectedDecl
4794ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004795 // For template arguments, we adopt the translation unit as our declaration
4796 // context. This context will be fixed when the actual template declaration
4797 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004798
Douglas Gregora082a492010-11-30 19:14:50 +00004799 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004800
4801 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4802 if (!BeginLocOrErr)
4803 return BeginLocOrErr.takeError();
4804
4805 ExpectedSLoc LocationOrErr = import(D->getLocation());
4806 if (!LocationOrErr)
4807 return LocationOrErr.takeError();
4808
Gabor Marton26f72a92018-07-12 09:42:05 +00004809 TemplateTypeParmDecl *ToD = nullptr;
4810 (void)GetImportedOrCreateDecl(
4811 ToD, D, Importer.getToContext(),
4812 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004813 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004814 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4815 D->wasDeclaredWithTypename(), D->isParameterPack());
4816 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004817}
4818
Balazs Keri3b30d652018-10-19 13:32:20 +00004819ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004820ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004821 DeclarationName ToDeclName;
4822 SourceLocation ToLocation, ToInnerLocStart;
4823 QualType ToType;
4824 TypeSourceInfo *ToTypeSourceInfo;
4825 if (auto Imp = importSeq(
4826 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4827 D->getInnerLocStart()))
4828 std::tie(
4829 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4830 ToInnerLocStart) = *Imp;
4831 else
4832 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004833
Douglas Gregora082a492010-11-30 19:14:50 +00004834 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004835
4836 NonTypeTemplateParmDecl *ToD = nullptr;
4837 (void)GetImportedOrCreateDecl(
4838 ToD, D, Importer.getToContext(),
4839 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004840 ToInnerLocStart, ToLocation, D->getDepth(),
4841 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4842 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004843 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004844}
4845
Balazs Keri3b30d652018-10-19 13:32:20 +00004846ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004847ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4848 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004849 auto NameOrErr = import(D->getDeclName());
4850 if (!NameOrErr)
4851 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004852
Douglas Gregora082a492010-11-30 19:14:50 +00004853 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004854 ExpectedSLoc LocationOrErr = import(D->getLocation());
4855 if (!LocationOrErr)
4856 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004857
Douglas Gregora082a492010-11-30 19:14:50 +00004858 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004859 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004860 if (!TemplateParamsOrErr)
4861 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004862
Douglas Gregora082a492010-11-30 19:14:50 +00004863 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004864
4865 TemplateTemplateParmDecl *ToD = nullptr;
4866 (void)GetImportedOrCreateDecl(
4867 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004868 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4869 D->getDepth(), D->getPosition(), D->isParameterPack(),
4870 (*NameOrErr).getAsIdentifierInfo(),
4871 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004872 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004873}
4874
Gabor Marton16d98c22019-03-07 13:01:51 +00004875// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004876// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004877template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4878 assert(D->getTemplatedDecl() && "Should be called on templates only");
4879 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004880 if (!ToTemplatedDef)
4881 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004882 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4883 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004884}
4885
Balazs Keri3b30d652018-10-19 13:32:20 +00004886ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004887 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4888
Douglas Gregora082a492010-11-30 19:14:50 +00004889 // Import the major distinguishing characteristics of this class template.
4890 DeclContext *DC, *LexicalDC;
4891 DeclarationName Name;
4892 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004893 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004894 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4895 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004896 if (ToD)
4897 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004898
Gabor Marton7df342a2018-12-17 12:42:12 +00004899 ClassTemplateDecl *FoundByLookup = nullptr;
4900
Douglas Gregora082a492010-11-30 19:14:50 +00004901 // We may already have a template of the same name; try to find and match it.
4902 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004903 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004904 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004905 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004906 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4907 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004908 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004909
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004910 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004911 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4912 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004913
Douglas Gregora082a492010-11-30 19:14:50 +00004914 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004915 ClassTemplateDecl *TemplateWithDef =
4916 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004917 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4918 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004919 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004920 FoundByLookup = FoundTemplate;
4921 break;
Gabor Marton9581c332018-05-23 13:53:36 +00004922 }
Douglas Gregora082a492010-11-30 19:14:50 +00004923 }
Gabor Marton9581c332018-05-23 13:53:36 +00004924
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004925 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004926 }
Gabor Marton9581c332018-05-23 13:53:36 +00004927
Douglas Gregora082a492010-11-30 19:14:50 +00004928 if (!ConflictingDecls.empty()) {
4929 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004930 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004931 ConflictingDecls.size());
4932 }
Gabor Marton9581c332018-05-23 13:53:36 +00004933
Douglas Gregora082a492010-11-30 19:14:50 +00004934 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00004935 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00004936 }
4937
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004938 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4939
Douglas Gregora082a492010-11-30 19:14:50 +00004940 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00004941 CXXRecordDecl *ToTemplated;
4942 if (Error Err = importInto(ToTemplated, FromTemplated))
4943 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004944
Douglas Gregora082a492010-11-30 19:14:50 +00004945 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00004946 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004947 if (!TemplateParamsOrErr)
4948 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004949
Gabor Marton26f72a92018-07-12 09:42:05 +00004950 ClassTemplateDecl *D2;
4951 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00004952 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00004953 return D2;
4954
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004955 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004956
Douglas Gregora082a492010-11-30 19:14:50 +00004957 D2->setAccess(D->getAccess());
4958 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00004959
4960 if (D->getDeclContext()->containsDeclAndLoad(D))
4961 DC->addDeclInternal(D2);
4962 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00004963 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004964
Gabor Marton7df342a2018-12-17 12:42:12 +00004965 if (FoundByLookup) {
4966 auto *Recent =
4967 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
4968
4969 // It is possible that during the import of the class template definition
4970 // we start the import of a fwd friend decl of the very same class template
4971 // and we add the fwd friend decl to the lookup table. But the ToTemplated
4972 // had been created earlier and by that time the lookup could not find
4973 // anything existing, so it has no previous decl. Later, (still during the
4974 // import of the fwd friend decl) we start to import the definition again
4975 // and this time the lookup finds the previous fwd friend class template.
4976 // In this case we must set up the previous decl for the templated decl.
4977 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004978 assert(FoundByLookup->getTemplatedDecl() &&
4979 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00004980 CXXRecordDecl *PrevTemplated =
4981 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
4982 if (ToTemplated != PrevTemplated)
4983 ToTemplated->setPreviousDecl(PrevTemplated);
4984 }
4985
4986 D2->setPreviousDecl(Recent);
4987 }
4988
4989 if (LexicalDC != DC && IsFriend)
4990 DC->makeDeclVisibleInContext(D2);
4991
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004992 if (FromTemplated->isCompleteDefinition() &&
4993 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004994 // FIXME: Import definition!
4995 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004996
Douglas Gregora082a492010-11-30 19:14:50 +00004997 return D2;
4998}
4999
Balazs Keri3b30d652018-10-19 13:32:20 +00005000ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005001 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005002 ClassTemplateDecl *ClassTemplate;
5003 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5004 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005005
Douglas Gregore2e50d332010-12-01 01:36:18 +00005006 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005007 DeclContext *DC, *LexicalDC;
5008 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5009 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005010
5011 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005012 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005013 if (Error Err = ImportTemplateArguments(
5014 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5015 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005016
Douglas Gregore2e50d332010-12-01 01:36:18 +00005017 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005018 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005019 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005020 ClassTemplatePartialSpecializationDecl *PartialSpec =
5021 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5022 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005023 PrevDecl =
5024 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005025 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005026 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005027
Gabor Marton7f8c4002019-03-19 13:34:10 +00005028 if (PrevDecl) {
5029 if (IsStructuralMatch(D, PrevDecl)) {
5030 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5031 Importer.MapImported(D, PrevDecl->getDefinition());
5032 // Import those default field initializers which have been
5033 // instantiated in the "From" context, but not in the "To" context.
5034 for (auto *FromField : D->fields())
5035 Importer.Import(FromField);
Gabor Marton42e15de2018-08-22 11:52:14 +00005036
Gabor Marton7f8c4002019-03-19 13:34:10 +00005037 // Import those methods which have been instantiated in the
5038 // "From" context, but not in the "To" context.
5039 for (CXXMethodDecl *FromM : D->methods())
5040 Importer.Import(FromM);
Gabor Marton42e15de2018-08-22 11:52:14 +00005041
Gabor Marton7f8c4002019-03-19 13:34:10 +00005042 // TODO Import instantiated default arguments.
5043 // TODO Import instantiated exception specifications.
5044 //
5045 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5046 // what else could be fused during an AST merge.
5047 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005048 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005049 } else { // ODR violation.
5050 // FIXME HandleNameConflict
5051 return nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005052 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005053 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005054
Gabor Marton7f8c4002019-03-19 13:34:10 +00005055 // Import the location of this declaration.
5056 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5057 if (!BeginLocOrErr)
5058 return BeginLocOrErr.takeError();
5059 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5060 if (!IdLocOrErr)
5061 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005062
Gabor Marton7f8c4002019-03-19 13:34:10 +00005063 // Create the specialization.
5064 ClassTemplateSpecializationDecl *D2 = nullptr;
5065 if (PartialSpec) {
5066 // Import TemplateArgumentListInfo.
5067 TemplateArgumentListInfo ToTAInfo;
5068 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5069 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5070 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005071
Gabor Marton7f8c4002019-03-19 13:34:10 +00005072 QualType CanonInjType;
5073 if (Error Err = importInto(
5074 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5075 return std::move(Err);
5076 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005077
Balazs Keridec09162019-03-20 15:42:42 +00005078 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005079 if (!ToTPListOrErr)
5080 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005081
Gabor Marton7f8c4002019-03-19 13:34:10 +00005082 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5083 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5084 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5085 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5086 ToTAInfo, CanonInjType,
5087 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5088 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005089
Gabor Marton7f8c4002019-03-19 13:34:10 +00005090 // Update InsertPos, because preceding import calls may have invalidated
5091 // it by adding new specializations.
5092 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5093 // Add this partial specialization to the class template.
5094 ClassTemplate->AddPartialSpecialization(
5095 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005096
Gabor Marton7f8c4002019-03-19 13:34:10 +00005097 } else { // Not a partial specialization.
5098 if (GetImportedOrCreateDecl(
5099 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5100 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5101 PrevDecl))
5102 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005103
Gabor Marton7f8c4002019-03-19 13:34:10 +00005104 // Update InsertPos, because preceding import calls may have invalidated
5105 // it by adding new specializations.
5106 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5107 // Add this specialization to the class template.
5108 ClassTemplate->AddSpecialization(D2, InsertPos);
5109 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005110
Gabor Marton7f8c4002019-03-19 13:34:10 +00005111 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005112
Gabor Marton7f8c4002019-03-19 13:34:10 +00005113 // Set the context of this specialization/instantiation.
5114 D2->setLexicalDeclContext(LexicalDC);
5115
5116 // Add to the DC only if it was an explicit specialization/instantiation.
5117 if (D2->isExplicitInstantiationOrSpecialization()) {
5118 LexicalDC->addDeclInternal(D2);
5119 }
5120
5121 // Import the qualifier, if any.
5122 if (auto LocOrErr = import(D->getQualifierLoc()))
5123 D2->setQualifierInfo(*LocOrErr);
5124 else
5125 return LocOrErr.takeError();
5126
5127 if (auto *TSI = D->getTypeAsWritten()) {
5128 if (auto TInfoOrErr = import(TSI))
5129 D2->setTypeAsWritten(*TInfoOrErr);
5130 else
5131 return TInfoOrErr.takeError();
5132
5133 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5134 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005135 else
5136 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005137
Gabor Marton7f8c4002019-03-19 13:34:10 +00005138 if (auto LocOrErr = import(D->getExternLoc()))
5139 D2->setExternLoc(*LocOrErr);
5140 else
5141 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005142 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005143
5144 if (D->getPointOfInstantiation().isValid()) {
5145 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5146 D2->setPointOfInstantiation(*POIOrErr);
5147 else
5148 return POIOrErr.takeError();
5149 }
5150
5151 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5152
Balazs Keri3b30d652018-10-19 13:32:20 +00005153 if (D->isCompleteDefinition())
5154 if (Error Err = ImportDefinition(D, D2))
5155 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005156
Douglas Gregore2e50d332010-12-01 01:36:18 +00005157 return D2;
5158}
5159
Balazs Keri3b30d652018-10-19 13:32:20 +00005160ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005161 // If this variable has a definition in the translation unit we're coming
5162 // from,
5163 // but this particular declaration is not that definition, import the
5164 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005165 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005166 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5167 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005168 if (ExpectedDecl ImportedDefOrErr = import(
5169 Definition->getDescribedVarTemplate()))
5170 return Importer.MapImported(D, *ImportedDefOrErr);
5171 else
5172 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005173 }
5174
5175 // Import the major distinguishing characteristics of this variable template.
5176 DeclContext *DC, *LexicalDC;
5177 DeclarationName Name;
5178 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005179 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005180 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5181 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005182 if (ToD)
5183 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005184
5185 // We may already have a template of the same name; try to find and match it.
5186 assert(!DC->isFunctionOrMethod() &&
5187 "Variable templates cannot be declared at function scope");
5188 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005189 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005190 for (auto *FoundDecl : FoundDecls) {
5191 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005192 continue;
5193
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005194 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005195 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005196 if (IsStructuralMatch(D, FoundTemplate)) {
5197 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005198 Importer.MapImported(D->getTemplatedDecl(),
5199 FoundTemplate->getTemplatedDecl());
5200 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005201 }
5202 }
5203
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005204 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005205 }
5206
5207 if (!ConflictingDecls.empty()) {
5208 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5209 ConflictingDecls.data(),
5210 ConflictingDecls.size());
5211 }
5212
5213 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005214 // FIXME: Is it possible to get other error than name conflict?
5215 // (Put this `if` into the previous `if`?)
5216 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005217
5218 VarDecl *DTemplated = D->getTemplatedDecl();
5219
5220 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005221 // FIXME: Value not used?
5222 ExpectedType TypeOrErr = import(DTemplated->getType());
5223 if (!TypeOrErr)
5224 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005225
5226 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005227 VarDecl *ToTemplated;
5228 if (Error Err = importInto(ToTemplated, DTemplated))
5229 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005230
5231 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005232 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005233 if (!TemplateParamsOrErr)
5234 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005235
Gabor Marton26f72a92018-07-12 09:42:05 +00005236 VarTemplateDecl *ToVarTD;
5237 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005238 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005239 return ToVarTD;
5240
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005241 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005242
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005243 ToVarTD->setAccess(D->getAccess());
5244 ToVarTD->setLexicalDeclContext(LexicalDC);
5245 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005246
Larisse Voufo39a1e502013-08-06 01:03:05 +00005247 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005248 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005249 // FIXME: Import definition!
5250 }
5251
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005252 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005253}
5254
Balazs Keri3b30d652018-10-19 13:32:20 +00005255ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005256 VarTemplateSpecializationDecl *D) {
5257 // If this record has a definition in the translation unit we're coming from,
5258 // but this particular declaration is not that definition, import the
5259 // definition and map to that.
5260 VarDecl *Definition = D->getDefinition();
5261 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005262 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5263 return Importer.MapImported(D, *ImportedDefOrErr);
5264 else
5265 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005266 }
5267
Balazs Keri3b30d652018-10-19 13:32:20 +00005268 VarTemplateDecl *VarTemplate;
5269 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5270 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005271
5272 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005273 DeclContext *DC, *LexicalDC;
5274 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5275 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005276
5277 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005278 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5279 if (!BeginLocOrErr)
5280 return BeginLocOrErr.takeError();
5281
5282 auto IdLocOrErr = import(D->getLocation());
5283 if (!IdLocOrErr)
5284 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005285
5286 // Import template arguments.
5287 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005288 if (Error Err = ImportTemplateArguments(
5289 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5290 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005291
5292 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005293 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005294 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005295 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005296 if (D2) {
5297 // We already have a variable template specialization with these template
5298 // arguments.
5299
5300 // FIXME: Check for specialization vs. instantiation errors.
5301
5302 if (VarDecl *FoundDef = D2->getDefinition()) {
5303 if (!D->isThisDeclarationADefinition() ||
5304 IsStructuralMatch(D, FoundDef)) {
5305 // The record types structurally match, or the "from" translation
5306 // unit only had a forward declaration anyway; call it the same
5307 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005308 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005309 }
5310 }
5311 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005312 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005313 QualType T;
5314 if (Error Err = importInto(T, D->getType()))
5315 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005316
Balazs Keri3b30d652018-10-19 13:32:20 +00005317 auto TInfoOrErr = import(D->getTypeSourceInfo());
5318 if (!TInfoOrErr)
5319 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005320
5321 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005322 if (Error Err = ImportTemplateArgumentListInfo(
5323 D->getTemplateArgsInfo(), ToTAInfo))
5324 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005325
5326 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005327 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005328 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5329 // Import TemplateArgumentListInfo
5330 TemplateArgumentListInfo ArgInfos;
5331 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5332 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005333 if (Error Err = ImportTemplateArgumentListInfo(
5334 *FromTAArgsAsWritten, ArgInfos))
5335 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005336
Balazs Keridec09162019-03-20 15:42:42 +00005337 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005338 if (!ToTPListOrErr)
5339 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005340
Gabor Marton26f72a92018-07-12 09:42:05 +00005341 PartVarSpecDecl *ToPartial;
5342 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005343 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5344 VarTemplate, T, *TInfoOrErr,
5345 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005346 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005347
Balazs Keri3b30d652018-10-19 13:32:20 +00005348 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5349 FromPartial->getInstantiatedFromMember()))
5350 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5351 else
5352 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005353
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005354 if (FromPartial->isMemberSpecialization())
5355 ToPartial->setMemberSpecialization();
5356
5357 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005358
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005359 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005360 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5361 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5362 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005363 D->getStorageClass(), TemplateArgs))
5364 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005365 }
5366
Balazs Keri3b30d652018-10-19 13:32:20 +00005367 if (D->getPointOfInstantiation().isValid()) {
5368 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5369 D2->setPointOfInstantiation(*POIOrErr);
5370 else
5371 return POIOrErr.takeError();
5372 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005373
Larisse Voufo39a1e502013-08-06 01:03:05 +00005374 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005375 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005376
5377 // Add this specialization to the class template.
5378 VarTemplate->AddSpecialization(D2, InsertPos);
5379
5380 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005381 if (auto LocOrErr = import(D->getQualifierLoc()))
5382 D2->setQualifierInfo(*LocOrErr);
5383 else
5384 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005385
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005386 if (D->isConstexpr())
5387 D2->setConstexpr(true);
5388
Larisse Voufo39a1e502013-08-06 01:03:05 +00005389 // Add the specialization to this context.
5390 D2->setLexicalDeclContext(LexicalDC);
5391 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005392
5393 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005394 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005395
Balazs Keri3b30d652018-10-19 13:32:20 +00005396 if (Error Err = ImportInitializer(D, D2))
5397 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005398
5399 return D2;
5400}
5401
Balazs Keri3b30d652018-10-19 13:32:20 +00005402ExpectedDecl
5403ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005404 DeclContext *DC, *LexicalDC;
5405 DeclarationName Name;
5406 SourceLocation Loc;
5407 NamedDecl *ToD;
5408
Balazs Keri3b30d652018-10-19 13:32:20 +00005409 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5410 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005411
5412 if (ToD)
5413 return ToD;
5414
Gabor Marton16d98c22019-03-07 13:01:51 +00005415 const FunctionTemplateDecl *FoundByLookup = nullptr;
5416
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005417 // Try to find a function in our own ("to") context with the same name, same
5418 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005419 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005420 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005421 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005422 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005423 for (auto *FoundDecl : FoundDecls) {
5424 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005425 continue;
5426
Gabor Marton16d98c22019-03-07 13:01:51 +00005427 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5428 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005429 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005430 if (IsStructuralMatch(D, FoundTemplate)) {
5431 FunctionTemplateDecl *TemplateWithDef =
5432 getTemplateDefinition(FoundTemplate);
5433 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5434 return Importer.MapImported(D, TemplateWithDef);
5435 }
5436 FoundByLookup = FoundTemplate;
5437 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005438 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005439 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005440 }
5441 }
5442 }
5443 }
5444
Balazs Keridec09162019-03-20 15:42:42 +00005445 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005446 if (!ParamsOrErr)
5447 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005448
Balazs Keri3b30d652018-10-19 13:32:20 +00005449 FunctionDecl *TemplatedFD;
5450 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5451 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005452
Gabor Marton26f72a92018-07-12 09:42:05 +00005453 FunctionTemplateDecl *ToFunc;
5454 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005455 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005456 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005457
5458 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005459
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005460 ToFunc->setAccess(D->getAccess());
5461 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005462 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005463
5464 if (FoundByLookup) {
5465 auto *Recent =
5466 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5467 if (!TemplatedFD->getPreviousDecl()) {
5468 assert(FoundByLookup->getTemplatedDecl() &&
5469 "Found decl must have its templated decl set");
5470 auto *PrevTemplated =
5471 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5472 if (TemplatedFD != PrevTemplated)
5473 TemplatedFD->setPreviousDecl(PrevTemplated);
5474 }
5475 ToFunc->setPreviousDecl(Recent);
5476 }
5477
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005478 return ToFunc;
5479}
5480
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005481//----------------------------------------------------------------------------
5482// Import Statements
5483//----------------------------------------------------------------------------
5484
Balazs Keri3b30d652018-10-19 13:32:20 +00005485ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005486 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5487 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005488 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005489}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005490
Balazs Keri3b30d652018-10-19 13:32:20 +00005491
5492ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005493 SmallVector<IdentifierInfo *, 4> Names;
5494 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5495 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005496 // ToII is nullptr when no symbolic name is given for output operand
5497 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005498 Names.push_back(ToII);
5499 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005500
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005501 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5502 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005503 // ToII is nullptr when no symbolic name is given for input operand
5504 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005505 Names.push_back(ToII);
5506 }
5507
5508 SmallVector<StringLiteral *, 4> Clobbers;
5509 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005510 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5511 Clobbers.push_back(*ClobberOrErr);
5512 else
5513 return ClobberOrErr.takeError();
5514
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005515 }
5516
5517 SmallVector<StringLiteral *, 4> Constraints;
5518 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005519 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5520 Constraints.push_back(*OutputOrErr);
5521 else
5522 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005523 }
5524
5525 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005526 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5527 Constraints.push_back(*InputOrErr);
5528 else
5529 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005530 }
5531
5532 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005533 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5534 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005535
Balazs Keri3b30d652018-10-19 13:32:20 +00005536 if (Error Err = ImportArrayChecked(
5537 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5538 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005539
Balazs Keri3b30d652018-10-19 13:32:20 +00005540 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5541 if (!AsmLocOrErr)
5542 return AsmLocOrErr.takeError();
5543 auto AsmStrOrErr = import(S->getAsmString());
5544 if (!AsmStrOrErr)
5545 return AsmStrOrErr.takeError();
5546 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5547 if (!RParenLocOrErr)
5548 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005549
5550 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005551 Importer.getToContext(),
5552 *AsmLocOrErr,
5553 S->isSimple(),
5554 S->isVolatile(),
5555 S->getNumOutputs(),
5556 S->getNumInputs(),
5557 Names.data(),
5558 Constraints.data(),
5559 Exprs.data(),
5560 *AsmStrOrErr,
5561 S->getNumClobbers(),
5562 Clobbers.data(),
5563 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005564}
5565
Balazs Keri3b30d652018-10-19 13:32:20 +00005566ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5567 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5568 if (!Imp)
5569 return Imp.takeError();
5570
5571 DeclGroupRef ToDG;
5572 SourceLocation ToBeginLoc, ToEndLoc;
5573 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5574
5575 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005576}
5577
Balazs Keri3b30d652018-10-19 13:32:20 +00005578ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5579 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5580 if (!ToSemiLocOrErr)
5581 return ToSemiLocOrErr.takeError();
5582 return new (Importer.getToContext()) NullStmt(
5583 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005584}
5585
Balazs Keri3b30d652018-10-19 13:32:20 +00005586ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005587 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005588
Balazs Keri3b30d652018-10-19 13:32:20 +00005589 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5590 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005591
Balazs Keri3b30d652018-10-19 13:32:20 +00005592 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5593 if (!ToLBracLocOrErr)
5594 return ToLBracLocOrErr.takeError();
5595
5596 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5597 if (!ToRBracLocOrErr)
5598 return ToRBracLocOrErr.takeError();
5599
5600 return CompoundStmt::Create(
5601 Importer.getToContext(), ToStmts,
5602 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005603}
5604
Balazs Keri3b30d652018-10-19 13:32:20 +00005605ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5606 auto Imp = importSeq(
5607 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5608 S->getEllipsisLoc(), S->getColonLoc());
5609 if (!Imp)
5610 return Imp.takeError();
5611
5612 Expr *ToLHS, *ToRHS;
5613 Stmt *ToSubStmt;
5614 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5615 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5616 *Imp;
5617
Bruno Ricci5b30571752018-10-28 12:30:53 +00005618 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5619 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005620 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005621
Gabor Horvath480892b2017-10-18 09:25:18 +00005622 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005623}
5624
Balazs Keri3b30d652018-10-19 13:32:20 +00005625ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5626 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5627 if (!Imp)
5628 return Imp.takeError();
5629
5630 SourceLocation ToDefaultLoc, ToColonLoc;
5631 Stmt *ToSubStmt;
5632 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5633
5634 return new (Importer.getToContext()) DefaultStmt(
5635 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005636}
5637
Balazs Keri3b30d652018-10-19 13:32:20 +00005638ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5639 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5640 if (!Imp)
5641 return Imp.takeError();
5642
5643 SourceLocation ToIdentLoc;
5644 LabelDecl *ToLabelDecl;
5645 Stmt *ToSubStmt;
5646 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5647
5648 return new (Importer.getToContext()) LabelStmt(
5649 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005650}
5651
Balazs Keri3b30d652018-10-19 13:32:20 +00005652ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5653 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5654 if (!ToAttrLocOrErr)
5655 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005656 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5657 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005658 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5659 return std::move(Err);
5660 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5661 if (!ToSubStmtOrErr)
5662 return ToSubStmtOrErr.takeError();
5663
5664 return AttributedStmt::Create(
5665 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005666}
5667
Balazs Keri3b30d652018-10-19 13:32:20 +00005668ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5669 auto Imp = importSeq(
5670 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5671 S->getThen(), S->getElseLoc(), S->getElse());
5672 if (!Imp)
5673 return Imp.takeError();
5674
5675 SourceLocation ToIfLoc, ToElseLoc;
5676 Stmt *ToInit, *ToThen, *ToElse;
5677 VarDecl *ToConditionVariable;
5678 Expr *ToCond;
5679 std::tie(
5680 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5681 *Imp;
5682
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005683 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5684 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5685 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005686}
5687
Balazs Keri3b30d652018-10-19 13:32:20 +00005688ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5689 auto Imp = importSeq(
5690 S->getInit(), S->getConditionVariable(), S->getCond(),
5691 S->getBody(), S->getSwitchLoc());
5692 if (!Imp)
5693 return Imp.takeError();
5694
5695 Stmt *ToInit, *ToBody;
5696 VarDecl *ToConditionVariable;
5697 Expr *ToCond;
5698 SourceLocation ToSwitchLoc;
5699 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5700
Bruno Riccie2806f82018-10-29 16:12:37 +00005701 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5702 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005703 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005704 ToStmt->setSwitchLoc(ToSwitchLoc);
5705
Sean Callanan59721b32015-04-28 18:41:46 +00005706 // Now we have to re-chain the cases.
5707 SwitchCase *LastChainedSwitchCase = nullptr;
5708 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5709 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005710 Expected<SwitchCase *> ToSCOrErr = import(SC);
5711 if (!ToSCOrErr)
5712 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005713 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005714 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005715 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005716 ToStmt->setSwitchCaseList(*ToSCOrErr);
5717 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005718 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005719
Sean Callanan59721b32015-04-28 18:41:46 +00005720 return ToStmt;
5721}
5722
Balazs Keri3b30d652018-10-19 13:32:20 +00005723ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5724 auto Imp = importSeq(
5725 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5726 if (!Imp)
5727 return Imp.takeError();
5728
5729 VarDecl *ToConditionVariable;
5730 Expr *ToCond;
5731 Stmt *ToBody;
5732 SourceLocation ToWhileLoc;
5733 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5734
Bruno Riccibacf7512018-10-30 13:42:41 +00005735 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5736 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005737}
5738
Balazs Keri3b30d652018-10-19 13:32:20 +00005739ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5740 auto Imp = importSeq(
5741 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5742 S->getRParenLoc());
5743 if (!Imp)
5744 return Imp.takeError();
5745
5746 Stmt *ToBody;
5747 Expr *ToCond;
5748 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5749 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5750
5751 return new (Importer.getToContext()) DoStmt(
5752 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005753}
5754
Balazs Keri3b30d652018-10-19 13:32:20 +00005755ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5756 auto Imp = importSeq(
5757 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5758 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5759 if (!Imp)
5760 return Imp.takeError();
5761
5762 Stmt *ToInit;
5763 Expr *ToCond, *ToInc;
5764 VarDecl *ToConditionVariable;
5765 Stmt *ToBody;
5766 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5767 std::tie(
5768 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5769 ToLParenLoc, ToRParenLoc) = *Imp;
5770
5771 return new (Importer.getToContext()) ForStmt(
5772 Importer.getToContext(),
5773 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5774 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005775}
5776
Balazs Keri3b30d652018-10-19 13:32:20 +00005777ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5778 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5779 if (!Imp)
5780 return Imp.takeError();
5781
5782 LabelDecl *ToLabel;
5783 SourceLocation ToGotoLoc, ToLabelLoc;
5784 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5785
5786 return new (Importer.getToContext()) GotoStmt(
5787 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005788}
5789
Balazs Keri3b30d652018-10-19 13:32:20 +00005790ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5791 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5792 if (!Imp)
5793 return Imp.takeError();
5794
5795 SourceLocation ToGotoLoc, ToStarLoc;
5796 Expr *ToTarget;
5797 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5798
5799 return new (Importer.getToContext()) IndirectGotoStmt(
5800 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005801}
5802
Balazs Keri3b30d652018-10-19 13:32:20 +00005803ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5804 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5805 if (!ToContinueLocOrErr)
5806 return ToContinueLocOrErr.takeError();
5807 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005808}
5809
Balazs Keri3b30d652018-10-19 13:32:20 +00005810ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5811 auto ToBreakLocOrErr = import(S->getBreakLoc());
5812 if (!ToBreakLocOrErr)
5813 return ToBreakLocOrErr.takeError();
5814 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005815}
5816
Balazs Keri3b30d652018-10-19 13:32:20 +00005817ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5818 auto Imp = importSeq(
5819 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5820 if (!Imp)
5821 return Imp.takeError();
5822
5823 SourceLocation ToReturnLoc;
5824 Expr *ToRetValue;
5825 const VarDecl *ToNRVOCandidate;
5826 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5827
Bruno Ricci023b1d12018-10-30 14:40:49 +00005828 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5829 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005830}
5831
Balazs Keri3b30d652018-10-19 13:32:20 +00005832ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5833 auto Imp = importSeq(
5834 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5835 if (!Imp)
5836 return Imp.takeError();
5837
5838 SourceLocation ToCatchLoc;
5839 VarDecl *ToExceptionDecl;
5840 Stmt *ToHandlerBlock;
5841 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5842
5843 return new (Importer.getToContext()) CXXCatchStmt (
5844 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005845}
5846
Balazs Keri3b30d652018-10-19 13:32:20 +00005847ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5848 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5849 if (!ToTryLocOrErr)
5850 return ToTryLocOrErr.takeError();
5851
5852 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5853 if (!ToTryBlockOrErr)
5854 return ToTryBlockOrErr.takeError();
5855
Sean Callanan59721b32015-04-28 18:41:46 +00005856 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5857 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5858 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005859 if (auto ToHandlerOrErr = import(FromHandler))
5860 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005861 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005862 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005863 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005864
5865 return CXXTryStmt::Create(
5866 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005867}
5868
Balazs Keri3b30d652018-10-19 13:32:20 +00005869ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5870 auto Imp1 = importSeq(
5871 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5872 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5873 if (!Imp1)
5874 return Imp1.takeError();
5875 auto Imp2 = importSeq(
5876 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5877 if (!Imp2)
5878 return Imp2.takeError();
5879
5880 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5881 Expr *ToCond, *ToInc;
5882 Stmt *ToInit, *ToBody;
5883 std::tie(
5884 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5885 ToBody) = *Imp1;
5886 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5887 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5888
5889 return new (Importer.getToContext()) CXXForRangeStmt(
5890 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5891 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005892}
5893
Balazs Keri3b30d652018-10-19 13:32:20 +00005894ExpectedStmt
5895ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5896 auto Imp = importSeq(
5897 S->getElement(), S->getCollection(), S->getBody(),
5898 S->getForLoc(), S->getRParenLoc());
5899 if (!Imp)
5900 return Imp.takeError();
5901
5902 Stmt *ToElement, *ToBody;
5903 Expr *ToCollection;
5904 SourceLocation ToForLoc, ToRParenLoc;
5905 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5906
5907 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
5908 ToCollection,
5909 ToBody,
5910 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00005911 ToRParenLoc);
5912}
5913
Balazs Keri3b30d652018-10-19 13:32:20 +00005914ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5915 auto Imp = importSeq(
5916 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
5917 S->getCatchBody());
5918 if (!Imp)
5919 return Imp.takeError();
5920
5921 SourceLocation ToAtCatchLoc, ToRParenLoc;
5922 VarDecl *ToCatchParamDecl;
5923 Stmt *ToCatchBody;
5924 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
5925
5926 return new (Importer.getToContext()) ObjCAtCatchStmt (
5927 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00005928}
5929
Balazs Keri3b30d652018-10-19 13:32:20 +00005930ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5931 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
5932 if (!ToAtFinallyLocOrErr)
5933 return ToAtFinallyLocOrErr.takeError();
5934 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
5935 if (!ToAtFinallyStmtOrErr)
5936 return ToAtFinallyStmtOrErr.takeError();
5937 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
5938 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005939}
5940
Balazs Keri3b30d652018-10-19 13:32:20 +00005941ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5942 auto Imp = importSeq(
5943 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
5944 if (!Imp)
5945 return Imp.takeError();
5946
5947 SourceLocation ToAtTryLoc;
5948 Stmt *ToTryBody, *ToFinallyStmt;
5949 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
5950
Sean Callanan59721b32015-04-28 18:41:46 +00005951 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5952 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5953 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005954 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
5955 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005956 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005957 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005958 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005959
Sean Callanan59721b32015-04-28 18:41:46 +00005960 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005961 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00005962 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005963 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005964}
5965
Balazs Keri3b30d652018-10-19 13:32:20 +00005966ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00005967 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005968 auto Imp = importSeq(
5969 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
5970 if (!Imp)
5971 return Imp.takeError();
5972
5973 SourceLocation ToAtSynchronizedLoc;
5974 Expr *ToSynchExpr;
5975 Stmt *ToSynchBody;
5976 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
5977
Sean Callanan59721b32015-04-28 18:41:46 +00005978 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5979 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5980}
5981
Balazs Keri3b30d652018-10-19 13:32:20 +00005982ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5983 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
5984 if (!ToThrowLocOrErr)
5985 return ToThrowLocOrErr.takeError();
5986 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
5987 if (!ToThrowExprOrErr)
5988 return ToThrowExprOrErr.takeError();
5989 return new (Importer.getToContext()) ObjCAtThrowStmt(
5990 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005991}
5992
Balazs Keri3b30d652018-10-19 13:32:20 +00005993ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
5994 ObjCAutoreleasePoolStmt *S) {
5995 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
5996 if (!ToAtLocOrErr)
5997 return ToAtLocOrErr.takeError();
5998 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5999 if (!ToSubStmtOrErr)
6000 return ToSubStmtOrErr.takeError();
6001 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6002 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006003}
6004
6005//----------------------------------------------------------------------------
6006// Import Expressions
6007//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006008ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006009 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6010 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006011 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006012}
6013
Balazs Keri3b30d652018-10-19 13:32:20 +00006014ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6015 auto Imp = importSeq(
6016 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6017 E->getRParenLoc(), E->getType());
6018 if (!Imp)
6019 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006020
Balazs Keri3b30d652018-10-19 13:32:20 +00006021 SourceLocation ToBuiltinLoc, ToRParenLoc;
6022 Expr *ToSubExpr;
6023 TypeSourceInfo *ToWrittenTypeInfo;
6024 QualType ToType;
6025 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6026 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006027
6028 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006029 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6030 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006031}
6032
Tom Roeder521f0042019-02-26 19:26:41 +00006033ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6034 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6035 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6036 if (!Imp)
6037 return Imp.takeError();
6038
6039 Expr *ToCond;
6040 Expr *ToLHS;
6041 Expr *ToRHS;
6042 SourceLocation ToBuiltinLoc, ToRParenLoc;
6043 QualType ToType;
6044 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6045
6046 ExprValueKind VK = E->getValueKind();
6047 ExprObjectKind OK = E->getObjectKind();
6048
6049 bool TypeDependent = ToCond->isTypeDependent();
6050 bool ValueDependent = ToCond->isValueDependent();
6051
6052 // The value of CondIsTrue only matters if the value is not
6053 // condition-dependent.
6054 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6055
6056 return new (Importer.getToContext())
6057 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6058 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6059}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006060
Balazs Keri3b30d652018-10-19 13:32:20 +00006061ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6062 ExpectedType TypeOrErr = import(E->getType());
6063 if (!TypeOrErr)
6064 return TypeOrErr.takeError();
6065
6066 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6067 if (!BeginLocOrErr)
6068 return BeginLocOrErr.takeError();
6069
6070 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006071}
6072
Balazs Keri3b30d652018-10-19 13:32:20 +00006073ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6074 auto Imp = importSeq(
6075 E->getBeginLoc(), E->getType(), E->getFunctionName());
6076 if (!Imp)
6077 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006078
Balazs Keri3b30d652018-10-19 13:32:20 +00006079 SourceLocation ToBeginLoc;
6080 QualType ToType;
6081 StringLiteral *ToFunctionName;
6082 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006083
Bruno Ricci17ff0262018-10-27 19:21:19 +00006084 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6085 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006086}
6087
Balazs Keri3b30d652018-10-19 13:32:20 +00006088ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6089 auto Imp = importSeq(
6090 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6091 E->getLocation(), E->getType());
6092 if (!Imp)
6093 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006094
Balazs Keri3b30d652018-10-19 13:32:20 +00006095 NestedNameSpecifierLoc ToQualifierLoc;
6096 SourceLocation ToTemplateKeywordLoc, ToLocation;
6097 ValueDecl *ToDecl;
6098 QualType ToType;
6099 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6100 *Imp;
6101
6102 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006103 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006104 auto FoundDOrErr = import(E->getFoundDecl());
6105 if (!FoundDOrErr)
6106 return FoundDOrErr.takeError();
6107 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006108 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006109
Aleksei Sidorina693b372016-09-28 10:16:56 +00006110 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006111 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006112 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006113 if (Error Err =
6114 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6115 return std::move(Err);
6116 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006117 }
6118
Balazs Keri3b30d652018-10-19 13:32:20 +00006119 auto *ToE = DeclRefExpr::Create(
6120 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6121 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6122 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006123 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006124 ToE->setHadMultipleCandidates(true);
6125 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006126}
6127
Balazs Keri3b30d652018-10-19 13:32:20 +00006128ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6129 ExpectedType TypeOrErr = import(E->getType());
6130 if (!TypeOrErr)
6131 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006132
Balazs Keri3b30d652018-10-19 13:32:20 +00006133 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006134}
6135
Balazs Keri3b30d652018-10-19 13:32:20 +00006136ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6137 ExpectedExpr ToInitOrErr = import(E->getInit());
6138 if (!ToInitOrErr)
6139 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006140
Balazs Keri3b30d652018-10-19 13:32:20 +00006141 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6142 if (!ToEqualOrColonLocOrErr)
6143 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006144
Balazs Keri3b30d652018-10-19 13:32:20 +00006145 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006146 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006147 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6148 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6149 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006150 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006151 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006152 }
6153
Balazs Keri3b30d652018-10-19 13:32:20 +00006154 SmallVector<Designator, 4> ToDesignators(E->size());
6155 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6156 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006157
6158 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006159 Importer.getToContext(), ToDesignators,
6160 ToIndexExprs, *ToEqualOrColonLocOrErr,
6161 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006162}
6163
Balazs Keri3b30d652018-10-19 13:32:20 +00006164ExpectedStmt
6165ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6166 ExpectedType ToTypeOrErr = import(E->getType());
6167 if (!ToTypeOrErr)
6168 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006169
Balazs Keri3b30d652018-10-19 13:32:20 +00006170 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6171 if (!ToLocationOrErr)
6172 return ToLocationOrErr.takeError();
6173
6174 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6175 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006176}
6177
Balazs Keri3b30d652018-10-19 13:32:20 +00006178ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6179 ExpectedType ToTypeOrErr = import(E->getType());
6180 if (!ToTypeOrErr)
6181 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006182
Balazs Keri3b30d652018-10-19 13:32:20 +00006183 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6184 if (!ToLocationOrErr)
6185 return ToLocationOrErr.takeError();
6186
6187 return IntegerLiteral::Create(
6188 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006189}
6190
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006191
Balazs Keri3b30d652018-10-19 13:32:20 +00006192ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6193 ExpectedType ToTypeOrErr = import(E->getType());
6194 if (!ToTypeOrErr)
6195 return ToTypeOrErr.takeError();
6196
6197 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6198 if (!ToLocationOrErr)
6199 return ToLocationOrErr.takeError();
6200
6201 return FloatingLiteral::Create(
6202 Importer.getToContext(), E->getValue(), E->isExact(),
6203 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006204}
6205
Balazs Keri3b30d652018-10-19 13:32:20 +00006206ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6207 auto ToTypeOrErr = import(E->getType());
6208 if (!ToTypeOrErr)
6209 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006210
Balazs Keri3b30d652018-10-19 13:32:20 +00006211 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6212 if (!ToSubExprOrErr)
6213 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006214
Balazs Keri3b30d652018-10-19 13:32:20 +00006215 return new (Importer.getToContext()) ImaginaryLiteral(
6216 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006217}
6218
Balazs Keri3b30d652018-10-19 13:32:20 +00006219ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6220 ExpectedType ToTypeOrErr = import(E->getType());
6221 if (!ToTypeOrErr)
6222 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006223
Balazs Keri3b30d652018-10-19 13:32:20 +00006224 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6225 if (!ToLocationOrErr)
6226 return ToLocationOrErr.takeError();
6227
6228 return new (Importer.getToContext()) CharacterLiteral(
6229 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006230}
6231
Balazs Keri3b30d652018-10-19 13:32:20 +00006232ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6233 ExpectedType ToTypeOrErr = import(E->getType());
6234 if (!ToTypeOrErr)
6235 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006236
Balazs Keri3b30d652018-10-19 13:32:20 +00006237 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6238 if (Error Err = ImportArrayChecked(
6239 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6240 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006241
Balazs Keri3b30d652018-10-19 13:32:20 +00006242 return StringLiteral::Create(
6243 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6244 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006245}
6246
Balazs Keri3b30d652018-10-19 13:32:20 +00006247ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6248 auto Imp = importSeq(
6249 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6250 E->getInitializer());
6251 if (!Imp)
6252 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006253
Balazs Keri3b30d652018-10-19 13:32:20 +00006254 SourceLocation ToLParenLoc;
6255 TypeSourceInfo *ToTypeSourceInfo;
6256 QualType ToType;
6257 Expr *ToInitializer;
6258 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006259
6260 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006261 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6262 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006263}
6264
Balazs Keri3b30d652018-10-19 13:32:20 +00006265ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6266 auto Imp = importSeq(
6267 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6268 if (!Imp)
6269 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006270
Balazs Keri3b30d652018-10-19 13:32:20 +00006271 SourceLocation ToBuiltinLoc, ToRParenLoc;
6272 QualType ToType;
6273 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6274
6275 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6276 if (Error Err = ImportArrayChecked(
6277 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6278 ToExprs.begin()))
6279 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006280
6281 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006282 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006283}
6284
Balazs Keri3b30d652018-10-19 13:32:20 +00006285ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6286 auto Imp = importSeq(
6287 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6288 if (!Imp)
6289 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006290
Balazs Keri3b30d652018-10-19 13:32:20 +00006291 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6292 LabelDecl *ToLabel;
6293 QualType ToType;
6294 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006295
6296 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006297 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006298}
6299
Bill Wendling8003edc2018-11-09 00:41:36 +00006300ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6301 auto Imp = importSeq(E->getSubExpr());
6302 if (!Imp)
6303 return Imp.takeError();
6304
6305 Expr *ToSubExpr;
6306 std::tie(ToSubExpr) = *Imp;
6307
Fangrui Song407659a2018-11-30 23:41:18 +00006308 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006309}
6310
Balazs Keri3b30d652018-10-19 13:32:20 +00006311ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6312 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6313 if (!Imp)
6314 return Imp.takeError();
6315
6316 SourceLocation ToLParen, ToRParen;
6317 Expr *ToSubExpr;
6318 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006319
Fangrui Song6907ce22018-07-30 19:24:48 +00006320 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006321 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006322}
6323
Balazs Keri3b30d652018-10-19 13:32:20 +00006324ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6325 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6326 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6327 return std::move(Err);
6328
6329 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6330 if (!ToLParenLocOrErr)
6331 return ToLParenLocOrErr.takeError();
6332
6333 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6334 if (!ToRParenLocOrErr)
6335 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006336
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006337 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6338 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006339}
6340
Balazs Keri3b30d652018-10-19 13:32:20 +00006341ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6342 auto Imp = importSeq(
6343 E->getSubStmt(), E->getType(), E->getLParenLoc(), 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 CompoundStmt *ToSubStmt;
6348 QualType ToType;
6349 SourceLocation ToLParenLoc, ToRParenLoc;
6350 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006351
Balazs Keri3b30d652018-10-19 13:32:20 +00006352 return new (Importer.getToContext()) StmtExpr(
6353 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006354}
6355
Balazs Keri3b30d652018-10-19 13:32:20 +00006356ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6357 auto Imp = importSeq(
6358 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6359 if (!Imp)
6360 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006361
Balazs Keri3b30d652018-10-19 13:32:20 +00006362 Expr *ToSubExpr;
6363 QualType ToType;
6364 SourceLocation ToOperatorLoc;
6365 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006366
Aaron Ballmana5038552018-01-09 13:07:03 +00006367 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006368 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6369 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006370}
6371
Balazs Keri3b30d652018-10-19 13:32:20 +00006372ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006373ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006374 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6375 if (!Imp)
6376 return Imp.takeError();
6377
6378 QualType ToType;
6379 SourceLocation ToOperatorLoc, ToRParenLoc;
6380 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006381
Douglas Gregord8552cd2010-02-19 01:24:23 +00006382 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006383 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6384 import(E->getArgumentTypeInfo());
6385 if (!ToArgumentTypeInfoOrErr)
6386 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006387
Balazs Keri3b30d652018-10-19 13:32:20 +00006388 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6389 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6390 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006391 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006392
Balazs Keri3b30d652018-10-19 13:32:20 +00006393 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6394 if (!ToArgumentExprOrErr)
6395 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006396
Balazs Keri3b30d652018-10-19 13:32:20 +00006397 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6398 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006399}
6400
Balazs Keri3b30d652018-10-19 13:32:20 +00006401ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6402 auto Imp = importSeq(
6403 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6404 if (!Imp)
6405 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006406
Balazs Keri3b30d652018-10-19 13:32:20 +00006407 Expr *ToLHS, *ToRHS;
6408 QualType ToType;
6409 SourceLocation ToOperatorLoc;
6410 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006411
Balazs Keri3b30d652018-10-19 13:32:20 +00006412 return new (Importer.getToContext()) BinaryOperator(
6413 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6414 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006415}
6416
Balazs Keri3b30d652018-10-19 13:32:20 +00006417ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6418 auto Imp = importSeq(
6419 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6420 E->getRHS(), E->getType());
6421 if (!Imp)
6422 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006423
Balazs Keri3b30d652018-10-19 13:32:20 +00006424 Expr *ToCond, *ToLHS, *ToRHS;
6425 SourceLocation ToQuestionLoc, ToColonLoc;
6426 QualType ToType;
6427 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006428
6429 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006430 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6431 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006432}
6433
Balazs Keri3b30d652018-10-19 13:32:20 +00006434ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006435 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006436 auto Imp = importSeq(
6437 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6438 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6439 if (!Imp)
6440 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006441
Balazs Keri3b30d652018-10-19 13:32:20 +00006442 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6443 OpaqueValueExpr *ToOpaqueValue;
6444 SourceLocation ToQuestionLoc, ToColonLoc;
6445 QualType ToType;
6446 std::tie(
6447 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6448 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006449
6450 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006451 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6452 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6453 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006454}
6455
Balazs Keri3b30d652018-10-19 13:32:20 +00006456ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6457 auto Imp = importSeq(
6458 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6459 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6460 if (!Imp)
6461 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006462
Balazs Keri3b30d652018-10-19 13:32:20 +00006463 SourceLocation ToBeginLoc, ToEndLoc;
6464 TypeSourceInfo *ToQueriedTypeSourceInfo;
6465 Expr *ToDimensionExpression;
6466 QualType ToType;
6467 std::tie(
6468 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6469 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006470
6471 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006472 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6473 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006474}
6475
Balazs Keri3b30d652018-10-19 13:32:20 +00006476ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6477 auto Imp = importSeq(
6478 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6479 if (!Imp)
6480 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006481
Balazs Keri3b30d652018-10-19 13:32:20 +00006482 SourceLocation ToBeginLoc, ToEndLoc;
6483 Expr *ToQueriedExpression;
6484 QualType ToType;
6485 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006486
6487 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006488 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6489 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006490}
6491
Balazs Keri3b30d652018-10-19 13:32:20 +00006492ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6493 auto Imp = importSeq(
6494 E->getLocation(), E->getType(), E->getSourceExpr());
6495 if (!Imp)
6496 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006497
Balazs Keri3b30d652018-10-19 13:32:20 +00006498 SourceLocation ToLocation;
6499 QualType ToType;
6500 Expr *ToSourceExpr;
6501 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006502
6503 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006504 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006505}
6506
Balazs Keri3b30d652018-10-19 13:32:20 +00006507ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6508 auto Imp = importSeq(
6509 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6510 if (!Imp)
6511 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006512
Balazs Keri3b30d652018-10-19 13:32:20 +00006513 Expr *ToLHS, *ToRHS;
6514 SourceLocation ToRBracketLoc;
6515 QualType ToType;
6516 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006517
6518 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006519 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6520 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006521}
6522
Balazs Keri3b30d652018-10-19 13:32:20 +00006523ExpectedStmt
6524ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6525 auto Imp = importSeq(
6526 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6527 E->getComputationResultType(), E->getOperatorLoc());
6528 if (!Imp)
6529 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006530
Balazs Keri3b30d652018-10-19 13:32:20 +00006531 Expr *ToLHS, *ToRHS;
6532 QualType ToType, ToComputationLHSType, ToComputationResultType;
6533 SourceLocation ToOperatorLoc;
6534 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6535 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006536
Balazs Keri3b30d652018-10-19 13:32:20 +00006537 return new (Importer.getToContext()) CompoundAssignOperator(
6538 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6539 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6540 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006541}
6542
Balazs Keri3b30d652018-10-19 13:32:20 +00006543Expected<CXXCastPath>
6544ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6545 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006546 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006547 if (auto SpecOrErr = import(*I))
6548 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006549 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006550 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006551 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006552 return Path;
John McCallcf142162010-08-07 06:22:56 +00006553}
6554
Balazs Keri3b30d652018-10-19 13:32:20 +00006555ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6556 ExpectedType ToTypeOrErr = import(E->getType());
6557 if (!ToTypeOrErr)
6558 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006559
Balazs Keri3b30d652018-10-19 13:32:20 +00006560 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6561 if (!ToSubExprOrErr)
6562 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006563
Balazs Keri3b30d652018-10-19 13:32:20 +00006564 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6565 if (!ToBasePathOrErr)
6566 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006567
Balazs Keri3b30d652018-10-19 13:32:20 +00006568 return ImplicitCastExpr::Create(
6569 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6570 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006571}
6572
Balazs Keri3b30d652018-10-19 13:32:20 +00006573ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6574 auto Imp1 = importSeq(
6575 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6576 if (!Imp1)
6577 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006578
Balazs Keri3b30d652018-10-19 13:32:20 +00006579 QualType ToType;
6580 Expr *ToSubExpr;
6581 TypeSourceInfo *ToTypeInfoAsWritten;
6582 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006583
Balazs Keri3b30d652018-10-19 13:32:20 +00006584 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6585 if (!ToBasePathOrErr)
6586 return ToBasePathOrErr.takeError();
6587 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006588
Aleksei Sidorina693b372016-09-28 10:16:56 +00006589 switch (E->getStmtClass()) {
6590 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006591 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006592 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6593 if (!ToLParenLocOrErr)
6594 return ToLParenLocOrErr.takeError();
6595 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6596 if (!ToRParenLocOrErr)
6597 return ToRParenLocOrErr.takeError();
6598 return CStyleCastExpr::Create(
6599 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6600 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6601 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006602 }
6603
6604 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006605 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006606 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6607 if (!ToLParenLocOrErr)
6608 return ToLParenLocOrErr.takeError();
6609 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6610 if (!ToRParenLocOrErr)
6611 return ToRParenLocOrErr.takeError();
6612 return CXXFunctionalCastExpr::Create(
6613 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6614 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6615 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006616 }
6617
6618 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006619 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6620 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6621 if (!ToLParenLocOrErr)
6622 return ToLParenLocOrErr.takeError();
6623 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6624 if (!ToBridgeKeywordLocOrErr)
6625 return ToBridgeKeywordLocOrErr.takeError();
6626 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6627 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6628 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006629 }
6630 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006631 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006632 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006633 }
6634}
6635
Balazs Keri3b30d652018-10-19 13:32:20 +00006636ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6637 SmallVector<OffsetOfNode, 4> ToNodes;
6638 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6639 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006640
Balazs Keri3b30d652018-10-19 13:32:20 +00006641 SourceLocation ToBeginLoc, ToEndLoc;
6642 if (FromNode.getKind() != OffsetOfNode::Base) {
6643 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6644 if (!Imp)
6645 return Imp.takeError();
6646 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6647 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006648
Balazs Keri3b30d652018-10-19 13:32:20 +00006649 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006650 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006651 ToNodes.push_back(
6652 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006653 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006654 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006655 auto ToBSOrErr = import(FromNode.getBase());
6656 if (!ToBSOrErr)
6657 return ToBSOrErr.takeError();
6658 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006659 break;
6660 }
6661 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006662 auto ToFieldOrErr = import(FromNode.getField());
6663 if (!ToFieldOrErr)
6664 return ToFieldOrErr.takeError();
6665 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006666 break;
6667 }
6668 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006669 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6670 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006671 break;
6672 }
6673 }
6674 }
6675
Balazs Keri3b30d652018-10-19 13:32:20 +00006676 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6677 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6678 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6679 if (!ToIndexExprOrErr)
6680 return ToIndexExprOrErr.takeError();
6681 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006682 }
6683
Balazs Keri3b30d652018-10-19 13:32:20 +00006684 auto Imp = importSeq(
6685 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6686 E->getRParenLoc());
6687 if (!Imp)
6688 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006689
Balazs Keri3b30d652018-10-19 13:32:20 +00006690 QualType ToType;
6691 TypeSourceInfo *ToTypeSourceInfo;
6692 SourceLocation ToOperatorLoc, ToRParenLoc;
6693 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6694
6695 return OffsetOfExpr::Create(
6696 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6697 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006698}
6699
Balazs Keri3b30d652018-10-19 13:32:20 +00006700ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6701 auto Imp = importSeq(
6702 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6703 if (!Imp)
6704 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006705
Balazs Keri3b30d652018-10-19 13:32:20 +00006706 QualType ToType;
6707 Expr *ToOperand;
6708 SourceLocation ToBeginLoc, ToEndLoc;
6709 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006710
Balazs Keri3b30d652018-10-19 13:32:20 +00006711 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006712 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006713 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006714 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006715 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006716
Balazs Keri3b30d652018-10-19 13:32:20 +00006717 return new (Importer.getToContext()) CXXNoexceptExpr(
6718 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006719}
6720
Balazs Keri3b30d652018-10-19 13:32:20 +00006721ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6722 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6723 if (!Imp)
6724 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006725
Balazs Keri3b30d652018-10-19 13:32:20 +00006726 Expr *ToSubExpr;
6727 QualType ToType;
6728 SourceLocation ToThrowLoc;
6729 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006730
6731 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006732 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006733}
6734
Balazs Keri3b30d652018-10-19 13:32:20 +00006735ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6736 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6737 if (!ToUsedLocOrErr)
6738 return ToUsedLocOrErr.takeError();
6739
6740 auto ToParamOrErr = import(E->getParam());
6741 if (!ToParamOrErr)
6742 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006743
6744 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006745 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006746}
6747
Balazs Keri3b30d652018-10-19 13:32:20 +00006748ExpectedStmt
6749ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6750 auto Imp = importSeq(
6751 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6752 if (!Imp)
6753 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006754
Balazs Keri3b30d652018-10-19 13:32:20 +00006755 QualType ToType;
6756 TypeSourceInfo *ToTypeSourceInfo;
6757 SourceLocation ToRParenLoc;
6758 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006759
6760 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006761 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006762}
6763
Balazs Keri3b30d652018-10-19 13:32:20 +00006764ExpectedStmt
6765ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6766 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6767 if (!ToSubExprOrErr)
6768 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006769
Balazs Keri3b30d652018-10-19 13:32:20 +00006770 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6771 if (!ToDtorOrErr)
6772 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006773
6774 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006775 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6776 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006777}
6778
Balazs Keri3b30d652018-10-19 13:32:20 +00006779ExpectedStmt
6780ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6781 auto Imp = importSeq(
6782 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6783 E->getParenOrBraceRange());
6784 if (!Imp)
6785 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006786
Balazs Keri3b30d652018-10-19 13:32:20 +00006787 CXXConstructorDecl *ToConstructor;
6788 QualType ToType;
6789 TypeSourceInfo *ToTypeSourceInfo;
6790 SourceRange ToParenOrBraceRange;
6791 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006792
Balazs Keri3b30d652018-10-19 13:32:20 +00006793 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6794 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6795 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006796
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006797 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006798 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6799 ToParenOrBraceRange, E->hadMultipleCandidates(),
6800 E->isListInitialization(), E->isStdInitListInitialization(),
6801 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006802}
6803
Balazs Keri3b30d652018-10-19 13:32:20 +00006804ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006805ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006806 auto Imp = importSeq(
6807 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6808 if (!Imp)
6809 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006810
Balazs Keri3b30d652018-10-19 13:32:20 +00006811 QualType ToType;
6812 Expr *ToTemporaryExpr;
6813 const ValueDecl *ToExtendingDecl;
6814 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006815
6816 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006817 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006818
6819 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006820 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006821 return ToMTE;
6822}
6823
Balazs Keri3b30d652018-10-19 13:32:20 +00006824ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6825 auto Imp = importSeq(
6826 E->getType(), E->getPattern(), E->getEllipsisLoc());
6827 if (!Imp)
6828 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006829
Balazs Keri3b30d652018-10-19 13:32:20 +00006830 QualType ToType;
6831 Expr *ToPattern;
6832 SourceLocation ToEllipsisLoc;
6833 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006834
6835 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006836 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006837}
6838
Balazs Keri3b30d652018-10-19 13:32:20 +00006839ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6840 auto Imp = importSeq(
6841 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6842 if (!Imp)
6843 return Imp.takeError();
6844
6845 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6846 NamedDecl *ToPack;
6847 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006848
6849 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006850 if (!E->isValueDependent())
6851 Length = E->getPackLength();
6852
Balazs Keri3b30d652018-10-19 13:32:20 +00006853 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006854 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006855 if (Error Err = ImportTemplateArguments(
6856 E->getPartialArguments().data(),
6857 E->getPartialArguments().size(),
6858 ToPartialArguments))
6859 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006860 }
6861
6862 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006863 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6864 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006865}
6866
Aleksei Sidorina693b372016-09-28 10:16:56 +00006867
Balazs Keri3b30d652018-10-19 13:32:20 +00006868ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6869 auto Imp = importSeq(
6870 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6871 E->getArraySize(), E->getInitializer(), E->getType(),
6872 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6873 E->getDirectInitRange());
6874 if (!Imp)
6875 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006876
Balazs Keri3b30d652018-10-19 13:32:20 +00006877 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6878 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6879 Expr *ToArraySize, *ToInitializer;
6880 QualType ToType;
6881 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6882 std::tie(
6883 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6884 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006885
Balazs Keri3b30d652018-10-19 13:32:20 +00006886 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6887 if (Error Err =
6888 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6889 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006890
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006891 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006892 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6893 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6894 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6895 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6896 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006897}
6898
Balazs Keri3b30d652018-10-19 13:32:20 +00006899ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6900 auto Imp = importSeq(
6901 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6902 if (!Imp)
6903 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006904
Balazs Keri3b30d652018-10-19 13:32:20 +00006905 QualType ToType;
6906 FunctionDecl *ToOperatorDelete;
6907 Expr *ToArgument;
6908 SourceLocation ToBeginLoc;
6909 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006910
6911 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006912 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6913 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6914 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006915}
6916
Balazs Keri3b30d652018-10-19 13:32:20 +00006917ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6918 auto Imp = importSeq(
6919 E->getType(), E->getLocation(), E->getConstructor(),
6920 E->getParenOrBraceRange());
6921 if (!Imp)
6922 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006923
Balazs Keri3b30d652018-10-19 13:32:20 +00006924 QualType ToType;
6925 SourceLocation ToLocation;
6926 CXXConstructorDecl *ToConstructor;
6927 SourceRange ToParenOrBraceRange;
6928 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006929
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006930 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006931 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6932 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006933
Balazs Keri3b30d652018-10-19 13:32:20 +00006934 return CXXConstructExpr::Create(
6935 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6936 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6937 E->isListInitialization(), E->isStdInitListInitialization(),
6938 E->requiresZeroInitialization(), E->getConstructionKind(),
6939 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006940}
6941
Balazs Keri3b30d652018-10-19 13:32:20 +00006942ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
6943 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6944 if (!ToSubExprOrErr)
6945 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006946
Balazs Keri3b30d652018-10-19 13:32:20 +00006947 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
6948 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
6949 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006950
Balazs Keri3b30d652018-10-19 13:32:20 +00006951 return ExprWithCleanups::Create(
6952 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
6953 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006954}
6955
Balazs Keri3b30d652018-10-19 13:32:20 +00006956ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6957 auto Imp = importSeq(
6958 E->getCallee(), E->getType(), E->getRParenLoc());
6959 if (!Imp)
6960 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006961
Balazs Keri3b30d652018-10-19 13:32:20 +00006962 Expr *ToCallee;
6963 QualType ToType;
6964 SourceLocation ToRParenLoc;
6965 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006966
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006967 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006968 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6969 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00006970
Bruno Riccic5885cf2018-12-21 15:20:32 +00006971 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
6972 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00006973}
6974
Balazs Keri3b30d652018-10-19 13:32:20 +00006975ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6976 ExpectedType ToTypeOrErr = import(E->getType());
6977 if (!ToTypeOrErr)
6978 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006979
Balazs Keri3b30d652018-10-19 13:32:20 +00006980 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6981 if (!ToLocationOrErr)
6982 return ToLocationOrErr.takeError();
6983
6984 return new (Importer.getToContext()) CXXThisExpr(
6985 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00006986}
6987
Balazs Keri3b30d652018-10-19 13:32:20 +00006988ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6989 ExpectedType ToTypeOrErr = import(E->getType());
6990 if (!ToTypeOrErr)
6991 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006992
Balazs Keri3b30d652018-10-19 13:32:20 +00006993 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6994 if (!ToLocationOrErr)
6995 return ToLocationOrErr.takeError();
6996
6997 return new (Importer.getToContext()) CXXBoolLiteralExpr(
6998 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00006999}
7000
Balazs Keri3b30d652018-10-19 13:32:20 +00007001ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7002 auto Imp1 = importSeq(
7003 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7004 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7005 if (!Imp1)
7006 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007007
Balazs Keri3b30d652018-10-19 13:32:20 +00007008 Expr *ToBase;
7009 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7010 NestedNameSpecifierLoc ToQualifierLoc;
7011 ValueDecl *ToMemberDecl;
7012 QualType ToType;
7013 std::tie(
7014 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7015 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007016
Balazs Keri3b30d652018-10-19 13:32:20 +00007017 auto Imp2 = importSeq(
7018 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7019 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7020 if (!Imp2)
7021 return Imp2.takeError();
7022 NamedDecl *ToDecl;
7023 DeclarationName ToName;
7024 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7025 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007026
7027 DeclAccessPair ToFoundDecl =
7028 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007029
Balazs Keri3b30d652018-10-19 13:32:20 +00007030 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007031
Gabor Marton5caba302019-03-07 13:38:20 +00007032 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007033 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007034 if (Error Err =
7035 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7036 E->template_arguments(), ToTAInfo))
7037 return std::move(Err);
7038 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007039 }
7040
Balazs Keri3b30d652018-10-19 13:32:20 +00007041 return MemberExpr::Create(
7042 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7043 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
Gabor Marton5caba302019-03-07 13:38:20 +00007044 ToMemberNameInfo, ResInfo, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007045}
7046
Balazs Keri3b30d652018-10-19 13:32:20 +00007047ExpectedStmt
7048ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7049 auto Imp = importSeq(
7050 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7051 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7052 if (!Imp)
7053 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007054
Balazs Keri3b30d652018-10-19 13:32:20 +00007055 Expr *ToBase;
7056 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7057 NestedNameSpecifierLoc ToQualifierLoc;
7058 TypeSourceInfo *ToScopeTypeInfo;
7059 std::tie(
7060 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7061 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007062
7063 PseudoDestructorTypeStorage Storage;
7064 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7065 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007066 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7067 if (!ToDestroyedTypeLocOrErr)
7068 return ToDestroyedTypeLocOrErr.takeError();
7069 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007070 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007071 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7072 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7073 else
7074 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007075 }
7076
7077 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007078 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7079 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007080}
7081
Balazs Keri3b30d652018-10-19 13:32:20 +00007082ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007083 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007084 auto Imp = importSeq(
7085 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7086 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7087 if (!Imp)
7088 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007089
Balazs Keri3b30d652018-10-19 13:32:20 +00007090 QualType ToType;
7091 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7092 NestedNameSpecifierLoc ToQualifierLoc;
7093 NamedDecl *ToFirstQualifierFoundInScope;
7094 std::tie(
7095 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7096 ToFirstQualifierFoundInScope) = *Imp;
7097
7098 Expr *ToBase = nullptr;
7099 if (!E->isImplicitAccess()) {
7100 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7101 ToBase = *ToBaseOrErr;
7102 else
7103 return ToBaseOrErr.takeError();
7104 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007105
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007106 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007107 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007108 if (Error Err = ImportTemplateArgumentListInfo(
7109 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7110 ToTAInfo))
7111 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007112 ResInfo = &ToTAInfo;
7113 }
7114
Balazs Keri3b30d652018-10-19 13:32:20 +00007115 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7116 if (!ToMemberNameInfoOrErr)
7117 return ToMemberNameInfoOrErr.takeError();
7118 DeclarationNameInfo ToMemberNameInfo(
7119 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007120 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007121 if (Error Err = ImportDeclarationNameLoc(
7122 E->getMemberNameInfo(), ToMemberNameInfo))
7123 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007124
7125 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007126 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7127 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7128 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007129}
7130
Balazs Keri3b30d652018-10-19 13:32:20 +00007131ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007132ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007133 auto Imp = importSeq(
7134 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7135 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7136 if (!Imp)
7137 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007138
Balazs Keri3b30d652018-10-19 13:32:20 +00007139 NestedNameSpecifierLoc ToQualifierLoc;
7140 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7141 DeclarationName ToDeclName;
7142 std::tie(
7143 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7144 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007145
Balazs Keri3b30d652018-10-19 13:32:20 +00007146 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7147 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7148 return std::move(Err);
7149
7150 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007151 TemplateArgumentListInfo *ResInfo = nullptr;
7152 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007153 if (Error Err =
7154 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7155 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007156 ResInfo = &ToTAInfo;
7157 }
7158
7159 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007160 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7161 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007162}
7163
Balazs Keri3b30d652018-10-19 13:32:20 +00007164ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7165 CXXUnresolvedConstructExpr *E) {
7166 auto Imp = importSeq(
7167 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7168 if (!Imp)
7169 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007170
Balazs Keri3b30d652018-10-19 13:32:20 +00007171 SourceLocation ToLParenLoc, ToRParenLoc;
7172 TypeSourceInfo *ToTypeSourceInfo;
7173 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7174
7175 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7176 if (Error Err =
7177 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7178 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007179
7180 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007181 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7182 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007183}
7184
Balazs Keri3b30d652018-10-19 13:32:20 +00007185ExpectedStmt
7186ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7187 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7188 if (!ToNamingClassOrErr)
7189 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007190
Balazs Keri3b30d652018-10-19 13:32:20 +00007191 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7192 if (!ToQualifierLocOrErr)
7193 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007194
Balazs Keri3b30d652018-10-19 13:32:20 +00007195 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7196 if (!ToNameInfoOrErr)
7197 return ToNameInfoOrErr.takeError();
7198 DeclarationNameInfo ToNameInfo(
7199 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007200 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007201 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7202 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007203
7204 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007205 for (auto *D : E->decls())
7206 if (auto ToDOrErr = import(D))
7207 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007208 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007209 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007210
Balazs Keri3b30d652018-10-19 13:32:20 +00007211 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7212 TemplateArgumentListInfo ToTAInfo;
7213 if (Error Err = ImportTemplateArgumentListInfo(
7214 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7215 ToTAInfo))
7216 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007217
Balazs Keri3b30d652018-10-19 13:32:20 +00007218 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7219 if (!ToTemplateKeywordLocOrErr)
7220 return ToTemplateKeywordLocOrErr.takeError();
7221
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007222 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007223 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7224 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7225 ToDecls.begin(), ToDecls.end());
7226 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007227
7228 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007229 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7230 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7231 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007232}
7233
Balazs Keri3b30d652018-10-19 13:32:20 +00007234ExpectedStmt
7235ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7236 auto Imp1 = importSeq(
7237 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7238 E->getTemplateKeywordLoc());
7239 if (!Imp1)
7240 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007241
Balazs Keri3b30d652018-10-19 13:32:20 +00007242 QualType ToType;
7243 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7244 NestedNameSpecifierLoc ToQualifierLoc;
7245 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7246
7247 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7248 if (!Imp2)
7249 return Imp2.takeError();
7250 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7251 // Import additional name location/type info.
7252 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7253 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007254
7255 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007256 for (Decl *D : E->decls())
7257 if (auto ToDOrErr = import(D))
7258 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007259 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007260 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007261
7262 TemplateArgumentListInfo ToTAInfo;
7263 TemplateArgumentListInfo *ResInfo = nullptr;
7264 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007265 if (Error Err =
7266 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7267 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007268 ResInfo = &ToTAInfo;
7269 }
7270
Balazs Keri3b30d652018-10-19 13:32:20 +00007271 Expr *ToBase = nullptr;
7272 if (!E->isImplicitAccess()) {
7273 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7274 ToBase = *ToBaseOrErr;
7275 else
7276 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007277 }
7278
7279 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007280 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7281 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7282 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007283}
7284
Balazs Keri3b30d652018-10-19 13:32:20 +00007285ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7286 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7287 if (!Imp)
7288 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007289
Balazs Keri3b30d652018-10-19 13:32:20 +00007290 Expr *ToCallee;
7291 QualType ToType;
7292 SourceLocation ToRParenLoc;
7293 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007294
7295 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007296 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7297 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7298 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007299
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007300 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007301 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007302 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007303 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7304 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007305 }
7306
Bruno Riccic5885cf2018-12-21 15:20:32 +00007307 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7308 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7309 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007310}
7311
Balazs Keri3b30d652018-10-19 13:32:20 +00007312ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7313 CXXRecordDecl *FromClass = E->getLambdaClass();
7314 auto ToClassOrErr = import(FromClass);
7315 if (!ToClassOrErr)
7316 return ToClassOrErr.takeError();
7317 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007318
7319 // NOTE: lambda classes are created with BeingDefined flag set up.
7320 // It means that ImportDefinition doesn't work for them and we should fill it
7321 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007322 if (ToClass->isBeingDefined())
7323 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7324 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007325
Balazs Keri3b30d652018-10-19 13:32:20 +00007326 auto ToCallOpOrErr = import(E->getCallOperator());
7327 if (!ToCallOpOrErr)
7328 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007329
7330 ToClass->completeDefinition();
7331
Balazs Keri3b30d652018-10-19 13:32:20 +00007332 SmallVector<LambdaCapture, 8> ToCaptures;
7333 ToCaptures.reserve(E->capture_size());
7334 for (const auto &FromCapture : E->captures()) {
7335 if (auto ToCaptureOrErr = import(FromCapture))
7336 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007337 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007338 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007339 }
7340
Balazs Keri3b30d652018-10-19 13:32:20 +00007341 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7342 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7343 return std::move(Err);
7344
7345 auto Imp = importSeq(
7346 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7347 if (!Imp)
7348 return Imp.takeError();
7349
7350 SourceRange ToIntroducerRange;
7351 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7352 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007353
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007354 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007355 Importer.getToContext(), ToClass, ToIntroducerRange,
7356 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7357 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7358 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007359}
7360
Sean Callanan8bca9962016-03-28 21:43:01 +00007361
Balazs Keri3b30d652018-10-19 13:32:20 +00007362ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7363 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7364 if (!Imp)
7365 return Imp.takeError();
7366
7367 SourceLocation ToLBraceLoc, ToRBraceLoc;
7368 QualType ToType;
7369 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7370
7371 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7372 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7373 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007374
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007375 ASTContext &ToCtx = Importer.getToContext();
7376 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007377 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7378 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007379
Balazs Keri3b30d652018-10-19 13:32:20 +00007380 if (E->hasArrayFiller()) {
7381 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7382 To->setArrayFiller(*ToFillerOrErr);
7383 else
7384 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007385 }
7386
Balazs Keri3b30d652018-10-19 13:32:20 +00007387 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7388 if (auto ToFDOrErr = import(FromFD))
7389 To->setInitializedFieldInUnion(*ToFDOrErr);
7390 else
7391 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007392 }
7393
Balazs Keri3b30d652018-10-19 13:32:20 +00007394 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7395 if (auto ToSyntFormOrErr = import(SyntForm))
7396 To->setSyntacticForm(*ToSyntFormOrErr);
7397 else
7398 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007399 }
7400
Gabor Martona20ce602018-09-03 13:10:53 +00007401 // Copy InitListExprBitfields, which are not handled in the ctor of
7402 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007403 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007404
7405 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007406}
7407
Balazs Keri3b30d652018-10-19 13:32:20 +00007408ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007409 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007410 ExpectedType ToTypeOrErr = import(E->getType());
7411 if (!ToTypeOrErr)
7412 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007413
Balazs Keri3b30d652018-10-19 13:32:20 +00007414 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7415 if (!ToSubExprOrErr)
7416 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007417
Balazs Keri3b30d652018-10-19 13:32:20 +00007418 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7419 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007420}
7421
Balazs Keri3b30d652018-10-19 13:32:20 +00007422ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007423 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007424 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7425 if (!Imp)
7426 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007427
Balazs Keri3b30d652018-10-19 13:32:20 +00007428 SourceLocation ToLocation;
7429 QualType ToType;
7430 CXXConstructorDecl *ToConstructor;
7431 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007432
7433 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007434 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7435 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007436}
7437
Balazs Keri3b30d652018-10-19 13:32:20 +00007438ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7439 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7440 if (!Imp)
7441 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007442
Balazs Keri3b30d652018-10-19 13:32:20 +00007443 QualType ToType;
7444 Expr *ToCommonExpr, *ToSubExpr;
7445 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007446
Balazs Keri3b30d652018-10-19 13:32:20 +00007447 return new (Importer.getToContext()) ArrayInitLoopExpr(
7448 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007449}
7450
Balazs Keri3b30d652018-10-19 13:32:20 +00007451ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7452 ExpectedType ToTypeOrErr = import(E->getType());
7453 if (!ToTypeOrErr)
7454 return ToTypeOrErr.takeError();
7455 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007456}
7457
Balazs Keri3b30d652018-10-19 13:32:20 +00007458ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7459 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7460 if (!ToBeginLocOrErr)
7461 return ToBeginLocOrErr.takeError();
7462
7463 auto ToFieldOrErr = import(E->getField());
7464 if (!ToFieldOrErr)
7465 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007466
7467 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007468 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007469}
7470
Balazs Keri3b30d652018-10-19 13:32:20 +00007471ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7472 auto Imp = importSeq(
7473 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7474 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7475 if (!Imp)
7476 return Imp.takeError();
7477
7478 QualType ToType;
7479 Expr *ToSubExpr;
7480 TypeSourceInfo *ToTypeInfoAsWritten;
7481 SourceLocation ToOperatorLoc, ToRParenLoc;
7482 SourceRange ToAngleBrackets;
7483 std::tie(
7484 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7485 ToAngleBrackets) = *Imp;
7486
Sean Callanandd2c1742016-05-16 20:48:03 +00007487 ExprValueKind VK = E->getValueKind();
7488 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007489 auto ToBasePathOrErr = ImportCastPath(E);
7490 if (!ToBasePathOrErr)
7491 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007492
Sean Callanandd2c1742016-05-16 20:48:03 +00007493 if (isa<CXXStaticCastExpr>(E)) {
7494 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007495 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7496 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007497 } else if (isa<CXXDynamicCastExpr>(E)) {
7498 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007499 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7500 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007501 } else if (isa<CXXReinterpretCastExpr>(E)) {
7502 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007503 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7504 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007505 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007506 return CXXConstCastExpr::Create(
7507 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7508 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007509 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007510 llvm_unreachable("Unknown cast type");
7511 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007512 }
7513}
7514
Balazs Keri3b30d652018-10-19 13:32:20 +00007515ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007516 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007517 auto Imp = importSeq(
7518 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7519 if (!Imp)
7520 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007521
Balazs Keri3b30d652018-10-19 13:32:20 +00007522 QualType ToType;
7523 SourceLocation ToExprLoc;
7524 NonTypeTemplateParmDecl *ToParameter;
7525 Expr *ToReplacement;
7526 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007527
7528 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007529 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007530}
7531
Balazs Keri3b30d652018-10-19 13:32:20 +00007532ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7533 auto Imp = importSeq(
7534 E->getType(), E->getBeginLoc(), E->getEndLoc());
7535 if (!Imp)
7536 return Imp.takeError();
7537
7538 QualType ToType;
7539 SourceLocation ToBeginLoc, ToEndLoc;
7540 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007541
7542 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007543 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7544 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007545
7546 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7547 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007548 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007549
7550 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007551 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7552 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007553}
7554
Balazs Keri3b30d652018-10-19 13:32:20 +00007555ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7556 ExpectedType ToTypeOrErr = import(E->getType());
7557 if (!ToTypeOrErr)
7558 return ToTypeOrErr.takeError();
7559
7560 auto ToSourceRangeOrErr = import(E->getSourceRange());
7561 if (!ToSourceRangeOrErr)
7562 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007563
7564 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007565 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7566 return new (Importer.getToContext()) CXXTypeidExpr(
7567 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7568 else
7569 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007570 }
7571
Balazs Keri3b30d652018-10-19 13:32:20 +00007572 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7573 if (!ToExprOperandOrErr)
7574 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007575
Balazs Keri3b30d652018-10-19 13:32:20 +00007576 return new (Importer.getToContext()) CXXTypeidExpr(
7577 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007578}
7579
Lang Hames19e07e12017-06-20 21:06:00 +00007580void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7581 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007582 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7583 if (auto ImportedOrErr = import(FromOverriddenMethod))
7584 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7585 (*ImportedOrErr)->getCanonicalDecl()));
7586 else
7587 consumeError(ImportedOrErr.takeError());
7588 }
Lang Hames19e07e12017-06-20 21:06:00 +00007589}
7590
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007591ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007592 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007593 bool MinimalImport,
7594 ASTImporterLookupTable *LookupTable)
7595 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007596 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7597 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007598
7599 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7600 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007601}
7602
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007603ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007604
Gabor Marton54058b52018-12-17 13:53:12 +00007605Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7606 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7607 "Try to get field index for non-field.");
7608
7609 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7610 if (!Owner)
7611 return None;
7612
7613 unsigned Index = 0;
7614 for (const auto *D : Owner->decls()) {
7615 if (D == F)
7616 return Index;
7617
7618 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7619 ++Index;
7620 }
7621
7622 llvm_unreachable("Field was not found in its parent context.");
7623
7624 return None;
7625}
7626
7627ASTImporter::FoundDeclsTy
7628ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7629 // We search in the redecl context because of transparent contexts.
7630 // E.g. a simple C language enum is a transparent context:
7631 // enum E { A, B };
7632 // Now if we had a global variable in the TU
7633 // int A;
7634 // then the enum constant 'A' and the variable 'A' violates ODR.
7635 // We can diagnose this only if we search in the redecl context.
7636 DeclContext *ReDC = DC->getRedeclContext();
7637 if (LookupTable) {
7638 ASTImporterLookupTable::LookupResult LookupResult =
7639 LookupTable->lookup(ReDC, Name);
7640 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7641 } else {
7642 // FIXME Can we remove this kind of lookup?
7643 // Or lldb really needs this C/C++ lookup?
7644 FoundDeclsTy Result;
7645 ReDC->localUncachedLookup(Name, Result);
7646 return Result;
7647 }
7648}
7649
7650void ASTImporter::AddToLookupTable(Decl *ToD) {
7651 if (LookupTable)
7652 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7653 LookupTable->add(ToND);
7654}
7655
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007656Expected<QualType> ASTImporter::Import_New(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007657 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007658 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007659
Balazs Keri3b30d652018-10-19 13:32:20 +00007660 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007661
7662 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007663 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007664 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007665 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007666 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007667
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007668 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007669 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007670 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007671 if (!ToTOrErr)
7672 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007673
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007674 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007675 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007676
Balazs Keri3b30d652018-10-19 13:32:20 +00007677 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007678}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007679QualType ASTImporter::Import(QualType From) {
7680 llvm::Expected<QualType> To = Import_New(From);
7681 if (To)
7682 return *To;
7683 else
7684 llvm::consumeError(To.takeError());
7685 return {};
7686}
Douglas Gregor96e578d2010-02-05 17:54:41 +00007687
Balazs Keri4a3d7582018-11-27 18:36:31 +00007688Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007689 if (!FromTSI)
7690 return FromTSI;
7691
7692 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007693 // on the type and a single location. Implement a real version of this.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007694 ExpectedType TOrErr = Import_New(FromTSI->getType());
7695 if (!TOrErr)
7696 return TOrErr.takeError();
7697 ExpectedSLoc BeginLocOrErr = Import_New(FromTSI->getTypeLoc().getBeginLoc());
7698 if (!BeginLocOrErr)
7699 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007700
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007701 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7702}
7703TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *From) {
7704 llvm::Expected<TypeSourceInfo *> To = Import_New(From);
7705 if (To)
7706 return *To;
7707 else
7708 llvm::consumeError(To.takeError());
7709 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007710}
7711
Balazs Keri4a3d7582018-11-27 18:36:31 +00007712Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007713 Attr *ToAttr = FromAttr->clone(ToContext);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007714 if (auto ToRangeOrErr = Import_New(FromAttr->getRange()))
7715 ToAttr->setRange(*ToRangeOrErr);
7716 else
7717 return ToRangeOrErr.takeError();
7718
Davide Italianofaee83d2018-11-28 19:15:23 +00007719 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007720}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007721Attr *ASTImporter::Import(const Attr *From) {
7722 llvm::Expected<Attr *> To = Import_New(From);
7723 if (To)
7724 return *To;
7725 else
7726 llvm::consumeError(To.takeError());
7727 return nullptr;
7728}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007729
Gabor Martonbe77a982018-12-12 11:22:55 +00007730Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7731 auto Pos = ImportedDecls.find(FromD);
7732 if (Pos != ImportedDecls.end())
7733 return Pos->second;
7734 else
Sean Callanan59721b32015-04-28 18:41:46 +00007735 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007736}
7737
Gabor Marton458d1452019-02-14 13:07:03 +00007738TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7739 auto FromDPos = ImportedFromDecls.find(ToD);
7740 if (FromDPos == ImportedFromDecls.end())
7741 return nullptr;
7742 return FromDPos->second->getTranslationUnitDecl();
7743}
7744
Balazs Keri4a3d7582018-11-27 18:36:31 +00007745Expected<Decl *> ASTImporter::Import_New(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007746 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007747 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007748
Douglas Gregord451ea92011-07-29 23:31:30 +00007749 ASTNodeImporter Importer(*this);
7750
Gabor Marton26f72a92018-07-12 09:42:05 +00007751 // Check whether we've already imported this declaration.
7752 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7753 if (ToD) {
7754 // If FromD has some updated flags after last import, apply it
7755 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007756 return ToD;
7757 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007758
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007759 // Import the declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007760 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007761 if (!ToDOrErr)
7762 return ToDOrErr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007763 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007764
Gabor Marton7f8c4002019-03-19 13:34:10 +00007765 // FIXME Use getImportDeclErrorIfAny() here (and return with the error) once
7766 // the error handling is finished in GetImportedOrCreateSpecialDecl().
7767 if (!ToD) {
7768 return nullptr;
7769 }
7770
Gabor Marton54058b52018-12-17 13:53:12 +00007771 // Once the decl is connected to the existing declarations, i.e. when the
7772 // redecl chain is properly set then we populate the lookup again.
7773 // This way the primary context will be able to find all decls.
7774 AddToLookupTable(ToD);
7775
Gabor Marton26f72a92018-07-12 09:42:05 +00007776 // Notify subclasses.
7777 Imported(FromD, ToD);
7778
Gabor Martonac3a5d62018-09-17 12:04:52 +00007779 updateFlags(FromD, ToD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007780 return ToDOrErr;
7781}
7782Decl *ASTImporter::Import(Decl *From) {
7783 llvm::Expected<Decl *> To = Import_New(From);
7784 if (To)
7785 return *To;
7786 else
7787 llvm::consumeError(To.takeError());
7788 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007789}
7790
Balazs Keri3b30d652018-10-19 13:32:20 +00007791Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007792 if (!FromDC)
7793 return FromDC;
7794
Balazs Keria1f6b102019-04-08 13:59:15 +00007795 ExpectedDecl ToDCOrErr = Import_New(cast<Decl>(FromDC));
7796 if (!ToDCOrErr)
7797 return ToDCOrErr.takeError();
7798 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00007799
Fangrui Song6907ce22018-07-30 19:24:48 +00007800 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007801 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007802 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7803 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007804 if (ToRecord->isCompleteDefinition()) {
7805 // Do nothing.
7806 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007807 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7808 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7809 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007810 } else {
7811 CompleteDecl(ToRecord);
7812 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007813 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7814 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007815 if (ToEnum->isCompleteDefinition()) {
7816 // Do nothing.
7817 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007818 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7819 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7820 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007821 } else {
7822 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007823 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007824 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7825 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007826 if (ToClass->getDefinition()) {
7827 // Do nothing.
7828 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007829 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7830 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7831 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007832 } else {
7833 CompleteDecl(ToClass);
7834 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007835 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7836 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007837 if (ToProto->getDefinition()) {
7838 // Do nothing.
7839 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007840 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7841 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7842 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007843 } else {
7844 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007845 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007846 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007847
Douglas Gregor95d82832012-01-24 18:36:04 +00007848 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007849}
7850
Balazs Keri4a3d7582018-11-27 18:36:31 +00007851Expected<Expr *> ASTImporter::Import_New(Expr *FromE) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007852 if (ExpectedStmt ToSOrErr = Import_New(cast_or_null<Stmt>(FromE)))
7853 return cast_or_null<Expr>(*ToSOrErr);
7854 else
7855 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00007856}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007857Expr *ASTImporter::Import(Expr *From) {
7858 llvm::Expected<Expr *> To = Import_New(From);
7859 if (To)
7860 return *To;
7861 else
7862 llvm::consumeError(To.takeError());
7863 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007864}
7865
Balazs Keri4a3d7582018-11-27 18:36:31 +00007866Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007867 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007868 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007869
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007870 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007871 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7872 if (Pos != ImportedStmts.end())
7873 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007874
Balazs Keri3b30d652018-10-19 13:32:20 +00007875 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007876 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007877 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007878 if (!ToSOrErr)
7879 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007880
Balazs Keri3b30d652018-10-19 13:32:20 +00007881 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007882 auto *FromE = cast<Expr>(FromS);
7883 // Copy ExprBitfields, which may not be handled in Expr subclasses
7884 // constructors.
7885 ToE->setValueKind(FromE->getValueKind());
7886 ToE->setObjectKind(FromE->getObjectKind());
7887 ToE->setTypeDependent(FromE->isTypeDependent());
7888 ToE->setValueDependent(FromE->isValueDependent());
7889 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7890 ToE->setContainsUnexpandedParameterPack(
7891 FromE->containsUnexpandedParameterPack());
7892 }
7893
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007894 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00007895 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007896 return ToSOrErr;
7897}
7898Stmt *ASTImporter::Import(Stmt *From) {
7899 llvm::Expected<Stmt *> To = Import_New(From);
7900 if (To)
7901 return *To;
7902 else
7903 llvm::consumeError(To.takeError());
7904 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007905}
7906
Balazs Keri4a3d7582018-11-27 18:36:31 +00007907Expected<NestedNameSpecifier *>
7908ASTImporter::Import_New(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007909 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007910 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007911
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007912 NestedNameSpecifier *Prefix;
7913 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
7914 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00007915
7916 switch (FromNNS->getKind()) {
7917 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007918 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
7919 return NestedNameSpecifier::Create(ToContext, Prefix,
7920 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00007921
7922 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007923 if (ExpectedDecl NSOrErr = Import_New(FromNNS->getAsNamespace())) {
7924 return NestedNameSpecifier::Create(ToContext, Prefix,
7925 cast<NamespaceDecl>(*NSOrErr));
7926 } else
7927 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007928
7929 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007930 if (ExpectedDecl NSADOrErr = Import_New(FromNNS->getAsNamespaceAlias()))
7931 return NestedNameSpecifier::Create(ToContext, Prefix,
7932 cast<NamespaceAliasDecl>(*NSADOrErr));
7933 else
7934 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007935
7936 case NestedNameSpecifier::Global:
7937 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7938
Nikola Smiljanic67860242014-09-26 00:28:20 +00007939 case NestedNameSpecifier::Super:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007940 if (ExpectedDecl RDOrErr = Import_New(FromNNS->getAsRecordDecl()))
7941 return NestedNameSpecifier::SuperSpecifier(ToContext,
7942 cast<CXXRecordDecl>(*RDOrErr));
7943 else
7944 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00007945
Douglas Gregor90ebf252011-04-27 16:48:40 +00007946 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007947 case NestedNameSpecifier::TypeSpecWithTemplate:
7948 if (Expected<QualType> TyOrErr =
7949 Import_New(QualType(FromNNS->getAsType(), 0u))) {
7950 bool TSTemplate =
7951 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
7952 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
7953 TyOrErr->getTypePtr());
7954 } else {
7955 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007956 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00007957 }
7958
7959 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007960}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007961NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *From) {
7962 llvm::Expected<NestedNameSpecifier *> To = Import_New(From);
7963 if (To)
7964 return *To;
7965 else
7966 llvm::consumeError(To.takeError());
7967 return nullptr;
7968}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007969
Balazs Keri4a3d7582018-11-27 18:36:31 +00007970Expected<NestedNameSpecifierLoc>
7971ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007972 // Copied from NestedNameSpecifier mostly.
7973 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7974 NestedNameSpecifierLoc NNS = FromNNS;
7975
7976 // Push each of the nested-name-specifiers's onto a stack for
7977 // serialization in reverse order.
7978 while (NNS) {
7979 NestedNames.push_back(NNS);
7980 NNS = NNS.getPrefix();
7981 }
7982
7983 NestedNameSpecifierLocBuilder Builder;
7984
7985 while (!NestedNames.empty()) {
7986 NNS = NestedNames.pop_back_val();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007987 NestedNameSpecifier *Spec;
7988 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
7989 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007990
7991 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007992
7993 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
7994 if (Kind != NestedNameSpecifier::Super) {
7995 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
7996 return std::move(Err);
7997
7998 if (Kind != NestedNameSpecifier::Global)
7999 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8000 return std::move(Err);
8001 }
8002
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008003 switch (Kind) {
8004 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008005 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8006 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008007 break;
8008
8009 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008010 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8011 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008012 break;
8013
8014 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008015 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8016 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008017 break;
8018
8019 case NestedNameSpecifier::TypeSpec:
8020 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008021 SourceLocation ToTLoc;
8022 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8023 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008024 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008025 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008026 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8027 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008028 break;
8029 }
8030
8031 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008032 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008033 break;
8034
8035 case NestedNameSpecifier::Super: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008036 auto ToSourceRangeOrErr = Import_New(NNS.getSourceRange());
8037 if (!ToSourceRangeOrErr)
8038 return ToSourceRangeOrErr.takeError();
8039
8040 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8041 ToSourceRangeOrErr->getBegin(),
8042 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008043 }
8044 }
8045 }
8046
8047 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008048}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008049NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc From) {
8050 llvm::Expected<NestedNameSpecifierLoc> To = Import_New(From);
8051 if (To)
8052 return *To;
8053 else
8054 llvm::consumeError(To.takeError());
8055 return {};
8056}
Douglas Gregor14454802011-02-25 02:25:35 +00008057
Balazs Keri4a3d7582018-11-27 18:36:31 +00008058Expected<TemplateName> ASTImporter::Import_New(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008059 switch (From.getKind()) {
8060 case TemplateName::Template:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008061 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8062 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8063 else
8064 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008065
Douglas Gregore2e50d332010-12-01 01:36:18 +00008066 case TemplateName::OverloadedTemplate: {
8067 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8068 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008069 for (auto *I : *FromStorage) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008070 if (auto ToOrErr = Import_New(I))
8071 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008072 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008073 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008074 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008075 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008076 ToTemplates.end());
8077 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008078
Douglas Gregore2e50d332010-12-01 01:36:18 +00008079 case TemplateName::QualifiedTemplate: {
8080 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008081 auto QualifierOrErr = Import_New(QTN->getQualifier());
8082 if (!QualifierOrErr)
8083 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008084
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008085 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8086 return ToContext.getQualifiedTemplateName(
8087 *QualifierOrErr, QTN->hasTemplateKeyword(),
8088 cast<TemplateDecl>(*ToTemplateOrErr));
8089 else
8090 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008091 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008092
Douglas Gregore2e50d332010-12-01 01:36:18 +00008093 case TemplateName::DependentTemplate: {
8094 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008095 auto QualifierOrErr = Import_New(DTN->getQualifier());
8096 if (!QualifierOrErr)
8097 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008098
Douglas Gregore2e50d332010-12-01 01:36:18 +00008099 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008100 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008101 Import(DTN->getIdentifier()));
8102 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008103
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008104 return ToContext.getDependentTemplateName(*QualifierOrErr,
8105 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008106 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008107
8108 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008109 SubstTemplateTemplateParmStorage *Subst =
8110 From.getAsSubstTemplateTemplateParm();
8111 ExpectedDecl ParamOrErr = Import_New(Subst->getParameter());
8112 if (!ParamOrErr)
8113 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008114
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008115 auto ReplacementOrErr = Import_New(Subst->getReplacement());
8116 if (!ReplacementOrErr)
8117 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008118
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008119 return ToContext.getSubstTemplateTemplateParm(
8120 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008121 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008122
Douglas Gregor5590be02011-01-15 06:45:20 +00008123 case TemplateName::SubstTemplateTemplateParmPack: {
8124 SubstTemplateTemplateParmPackStorage *SubstPack
8125 = From.getAsSubstTemplateTemplateParmPack();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008126 ExpectedDecl ParamOrErr = Import_New(SubstPack->getParameterPack());
8127 if (!ParamOrErr)
8128 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008129
Douglas Gregor5590be02011-01-15 06:45:20 +00008130 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008131 auto ArgPackOrErr =
8132 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8133 if (!ArgPackOrErr)
8134 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008135
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008136 return ToContext.getSubstTemplateTemplateParmPack(
8137 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008138 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008139 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008140
Douglas Gregore2e50d332010-12-01 01:36:18 +00008141 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008142}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008143TemplateName ASTImporter::Import(TemplateName From) {
8144 llvm::Expected<TemplateName> To = Import_New(From);
8145 if (To)
8146 return *To;
8147 else
8148 llvm::consumeError(To.takeError());
8149 return {};
8150}
Douglas Gregore2e50d332010-12-01 01:36:18 +00008151
Balazs Keri4a3d7582018-11-27 18:36:31 +00008152Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008153 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008154 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008155
Douglas Gregor811663e2010-02-10 00:15:17 +00008156 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008157 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008158
Douglas Gregor811663e2010-02-10 00:15:17 +00008159 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008160 Expected<FileID> ToFileIDOrErr = Import_New(Decomposed.first, IsBuiltin);
8161 if (!ToFileIDOrErr)
8162 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008163 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008164 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8165}
8166SourceLocation ASTImporter::Import(SourceLocation From) {
8167 llvm::Expected<SourceLocation> To = Import_New(From);
8168 if (To)
8169 return *To;
8170 else
8171 llvm::consumeError(To.takeError());
8172 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008173}
8174
Balazs Keri4a3d7582018-11-27 18:36:31 +00008175Expected<SourceRange> ASTImporter::Import_New(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008176 SourceLocation ToBegin, ToEnd;
8177 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8178 return std::move(Err);
8179 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8180 return std::move(Err);
8181
8182 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008183}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008184SourceRange ASTImporter::Import(SourceRange From) {
8185 llvm::Expected<SourceRange> To = Import_New(From);
8186 if (To)
8187 return *To;
8188 else
8189 llvm::consumeError(To.takeError());
8190 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008191}
8192
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008193Expected<FileID> ASTImporter::Import_New(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008194 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008195 if (Pos != ImportedFileIDs.end())
8196 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008197
Douglas Gregor811663e2010-02-10 00:15:17 +00008198 SourceManager &FromSM = FromContext.getSourceManager();
8199 SourceManager &ToSM = ToContext.getSourceManager();
8200 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008201
8202 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008203 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008204 if (FromSLoc.isExpansion()) {
8205 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008206 ExpectedSLoc ToSpLoc = Import_New(FromEx.getSpellingLoc());
8207 if (!ToSpLoc)
8208 return ToSpLoc.takeError();
8209 ExpectedSLoc ToExLocS = Import_New(FromEx.getExpansionLocStart());
8210 if (!ToExLocS)
8211 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008212 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8213 SourceLocation MLoc;
8214 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008215 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008216 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008217 if (ExpectedSLoc ToExLocE = Import_New(FromEx.getExpansionLocEnd()))
8218 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8219 FromEx.isExpansionTokenRange());
8220 else
8221 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008222 }
8223 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008224 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008225 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008226
8227 if (!IsBuiltin) {
8228 // Include location of this file.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008229 ExpectedSLoc ToIncludeLoc =
8230 Import_New(FromSLoc.getFile().getIncludeLoc());
8231 if (!ToIncludeLoc)
8232 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008233
8234 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8235 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8236 // disk again
8237 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8238 // than mmap the files several times.
8239 const FileEntry *Entry =
8240 ToFileManager.getFile(Cache->OrigEntry->getName());
8241 // FIXME: The filename may be a virtual name that does probably not
8242 // point to a valid file and we get no Entry here. In this case try with
8243 // the memory buffer below.
8244 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008245 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008246 FromSLoc.getFile().getFileCharacteristic());
8247 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008248 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008249
8250 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008251 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008252 bool Invalid = true;
8253 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8254 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8255 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008256 // FIXME: Use a new error kind?
8257 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008258
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008259 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8260 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8261 FromBuf->getBufferIdentifier());
8262 ToID = ToSM.createFileID(std::move(ToBuf),
8263 FromSLoc.getFile().getFileCharacteristic());
8264 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008265 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008266
Balazs Keri9cf39df2019-02-27 16:31:48 +00008267 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8268
Sebastian Redl99219f12010-09-30 01:03:06 +00008269 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008270 return ToID;
8271}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008272FileID ASTImporter::Import(FileID From, bool IsBuiltin) {
8273 llvm::Expected<FileID> To = Import_New(From, IsBuiltin);
8274 if (To)
8275 return *To;
8276 else
8277 llvm::consumeError(To.takeError());
8278 return {};
8279}
Douglas Gregor811663e2010-02-10 00:15:17 +00008280
Balazs Keri4a3d7582018-11-27 18:36:31 +00008281Expected<CXXCtorInitializer *>
8282ASTImporter::Import_New(CXXCtorInitializer *From) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008283 ExpectedExpr ToExprOrErr = Import_New(From->getInit());
8284 if (!ToExprOrErr)
8285 return ToExprOrErr.takeError();
8286
8287 auto LParenLocOrErr = Import_New(From->getLParenLoc());
8288 if (!LParenLocOrErr)
8289 return LParenLocOrErr.takeError();
8290
8291 auto RParenLocOrErr = Import_New(From->getRParenLoc());
8292 if (!RParenLocOrErr)
8293 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008294
8295 if (From->isBaseInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008296 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8297 if (!ToTInfoOrErr)
8298 return ToTInfoOrErr.takeError();
8299
8300 SourceLocation EllipsisLoc;
8301 if (From->isPackExpansion())
8302 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8303 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008304
8305 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008306 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8307 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008308 } else if (From->isMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008309 ExpectedDecl ToFieldOrErr = Import_New(From->getMember());
8310 if (!ToFieldOrErr)
8311 return ToFieldOrErr.takeError();
8312
8313 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8314 if (!MemberLocOrErr)
8315 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008316
8317 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008318 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8319 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008320 } else if (From->isIndirectMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008321 ExpectedDecl ToIFieldOrErr = Import_New(From->getIndirectMember());
8322 if (!ToIFieldOrErr)
8323 return ToIFieldOrErr.takeError();
8324
8325 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8326 if (!MemberLocOrErr)
8327 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008328
8329 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008330 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8331 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008332 } else if (From->isDelegatingInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008333 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8334 if (!ToTInfoOrErr)
8335 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008336
8337 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008338 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8339 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008340 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008341 // FIXME: assert?
8342 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008343 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008344}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008345CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8346 llvm::Expected<CXXCtorInitializer *> To = Import_New(From);
8347 if (To)
8348 return *To;
8349 else
8350 llvm::consumeError(To.takeError());
8351 return nullptr;
8352}
Sean Callanandd2c1742016-05-16 20:48:03 +00008353
Balazs Keri4a3d7582018-11-27 18:36:31 +00008354Expected<CXXBaseSpecifier *>
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008355ASTImporter::Import_New(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008356 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8357 if (Pos != ImportedCXXBaseSpecifiers.end())
8358 return Pos->second;
8359
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008360 Expected<SourceRange> ToSourceRange = Import_New(BaseSpec->getSourceRange());
8361 if (!ToSourceRange)
8362 return ToSourceRange.takeError();
8363 Expected<TypeSourceInfo *> ToTSI = Import_New(BaseSpec->getTypeSourceInfo());
8364 if (!ToTSI)
8365 return ToTSI.takeError();
8366 ExpectedSLoc ToEllipsisLoc = Import_New(BaseSpec->getEllipsisLoc());
8367 if (!ToEllipsisLoc)
8368 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008369 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008370 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8371 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008372 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8373 return Imported;
8374}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008375CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *From) {
8376 llvm::Expected<CXXBaseSpecifier *> To = Import_New(From);
8377 if (To)
8378 return *To;
8379 else
8380 llvm::consumeError(To.takeError());
8381 return nullptr;
8382}
Aleksei Sidorina693b372016-09-28 10:16:56 +00008383
Balazs Keri3b30d652018-10-19 13:32:20 +00008384Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008385 Decl *To = Import(From);
8386 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008387 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008388
Don Hintonf170dff2019-03-19 06:14:14 +00008389 auto *FromDC = cast<DeclContext>(From);
8390 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008391
Don Hintonf170dff2019-03-19 06:14:14 +00008392 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8393 if (!ToRecord->getDefinition()) {
8394 return Importer.ImportDefinition(
8395 cast<RecordDecl>(FromDC), ToRecord,
8396 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008397 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008398 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008399
Don Hintonf170dff2019-03-19 06:14:14 +00008400 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8401 if (!ToEnum->getDefinition()) {
8402 return Importer.ImportDefinition(
8403 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8404 }
8405 }
8406
8407 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8408 if (!ToIFace->getDefinition()) {
8409 return Importer.ImportDefinition(
8410 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8411 ASTNodeImporter::IDK_Everything);
8412 }
8413 }
8414
8415 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8416 if (!ToProto->getDefinition()) {
8417 return Importer.ImportDefinition(
8418 cast<ObjCProtocolDecl>(FromDC), ToProto,
8419 ASTNodeImporter::IDK_Everything);
8420 }
8421 }
8422
8423 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008424}
8425
8426void ASTImporter::ImportDefinition(Decl *From) {
8427 Error Err = ImportDefinition_New(From);
8428 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008429}
8430
Balazs Keri4a3d7582018-11-27 18:36:31 +00008431Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008432 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008433 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008434
8435 switch (FromName.getNameKind()) {
8436 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008437 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008438
8439 case DeclarationName::ObjCZeroArgSelector:
8440 case DeclarationName::ObjCOneArgSelector:
8441 case DeclarationName::ObjCMultiArgSelector:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008442 if (auto ToSelOrErr = Import_New(FromName.getObjCSelector()))
8443 return DeclarationName(*ToSelOrErr);
8444 else
8445 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008446
8447 case DeclarationName::CXXConstructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008448 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8449 return ToContext.DeclarationNames.getCXXConstructorName(
8450 ToContext.getCanonicalType(*ToTyOrErr));
8451 else
8452 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008453 }
8454
8455 case DeclarationName::CXXDestructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008456 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8457 return ToContext.DeclarationNames.getCXXDestructorName(
8458 ToContext.getCanonicalType(*ToTyOrErr));
8459 else
8460 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008461 }
8462
Richard Smith35845152017-02-07 01:37:30 +00008463 case DeclarationName::CXXDeductionGuideName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008464 if (auto ToTemplateOrErr =
8465 Import_New(FromName.getCXXDeductionGuideTemplate()))
8466 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8467 cast<TemplateDecl>(*ToTemplateOrErr));
8468 else
8469 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008470 }
8471
Douglas Gregor96e578d2010-02-05 17:54:41 +00008472 case DeclarationName::CXXConversionFunctionName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008473 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8474 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8475 ToContext.getCanonicalType(*ToTyOrErr));
8476 else
8477 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008478 }
8479
8480 case DeclarationName::CXXOperatorName:
8481 return ToContext.DeclarationNames.getCXXOperatorName(
8482 FromName.getCXXOverloadedOperator());
8483
8484 case DeclarationName::CXXLiteralOperatorName:
8485 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008486 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008487
8488 case DeclarationName::CXXUsingDirective:
8489 // FIXME: STATICS!
8490 return DeclarationName::getUsingDirectiveName();
8491 }
8492
David Blaikiee4d798f2012-01-20 21:50:17 +00008493 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008494}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008495DeclarationName ASTImporter::Import(DeclarationName From) {
8496 llvm::Expected<DeclarationName> To = Import_New(From);
8497 if (To)
8498 return *To;
8499 else
8500 llvm::consumeError(To.takeError());
8501 return {};
8502}
Douglas Gregor96e578d2010-02-05 17:54:41 +00008503
Douglas Gregore2e50d332010-12-01 01:36:18 +00008504IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008505 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008506 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008507
Sean Callananf94ef1d2016-05-14 06:11:19 +00008508 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8509
8510 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8511 ToId->setBuiltinID(FromId->getBuiltinID());
8512
8513 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008514}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008515
Balazs Keri4a3d7582018-11-27 18:36:31 +00008516Expected<Selector> ASTImporter::Import_New(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008517 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008518 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008519
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008520 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008521 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8522 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8523 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8524 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8525}
8526
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008527DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8528 DeclContext *DC,
8529 unsigned IDNS,
8530 NamedDecl **Decls,
8531 unsigned NumDecls) {
8532 return Name;
8533}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008534Selector ASTImporter::Import(Selector From) {
8535 llvm::Expected<Selector> To = Import_New(From);
8536 if (To)
8537 return *To;
8538 else
8539 llvm::consumeError(To.takeError());
8540 return {};
8541}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008542
8543DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008544 if (LastDiagFromFrom)
8545 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8546 FromContext.getDiagnostics());
8547 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008548 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008549}
8550
8551DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008552 if (!LastDiagFromFrom)
8553 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8554 ToContext.getDiagnostics());
8555 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008556 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008557}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008558
Douglas Gregor2e15c842012-02-01 21:00:38 +00008559void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008560 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008561 if (!ID->getDefinition())
8562 ID->startDefinition();
8563 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008564 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008565 if (!PD->getDefinition())
8566 PD->startDefinition();
8567 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008568 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008569 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8570 TD->startDefinition();
8571 TD->setCompleteDefinition(true);
8572 }
8573 }
8574 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008575 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008576 }
8577}
8578
Gabor Marton26f72a92018-07-12 09:42:05 +00008579Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8580 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8581 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8582 "Try to import an already imported Decl");
8583 if (Pos != ImportedDecls.end())
8584 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008585 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008586 // This mapping should be maintained only in this function. Therefore do not
8587 // check for additional consistency.
8588 ImportedFromDecls[To] = From;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008589 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008590}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008591
Douglas Gregordd6006f2012-07-17 21:16:27 +00008592bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8593 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008594 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8595 ImportedTypes.find(From.getTypePtr());
8596 if (Pos != ImportedTypes.end()) {
8597 if (ExpectedType ToFromOrErr = Import_New(From)) {
8598 if (ToContext.hasSameType(*ToFromOrErr, To))
8599 return true;
8600 } else {
8601 llvm::consumeError(ToFromOrErr.takeError());
8602 }
8603 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008604
Douglas Gregordd6006f2012-07-17 21:16:27 +00008605 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008606 getStructuralEquivalenceKind(*this), false,
8607 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008608 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008609}