blob: edefa45fb561e73b7e423b9cd2b5bcc1a59a8204 [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) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000151 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000152 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) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000161 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000162 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) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000175 return Importer.Import(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000176 }
177
Richard Smithb9fb1212019-05-06 03:47:15 +0000178 // Import an Optional<T> by importing the contained T, if any.
179 template<typename T>
180 Expected<Optional<T>> import(Optional<T> From) {
181 if (!From)
182 return Optional<T>();
183 return import(*From);
184 }
185
Balazs Keri3b30d652018-10-19 13:32:20 +0000186 template <class T>
187 Expected<std::tuple<T>>
188 importSeq(const T &From) {
189 Expected<T> ToOrErr = import(From);
190 if (!ToOrErr)
191 return ToOrErr.takeError();
192 return std::make_tuple<T>(std::move(*ToOrErr));
193 }
194
195 // Import multiple objects with a single function call.
196 // This should work for every type for which a variant of `import` exists.
197 // The arguments are processed from left to right and import is stopped on
198 // first error.
199 template <class THead, class... TTail>
200 Expected<std::tuple<THead, TTail...>>
201 importSeq(const THead &FromHead, const TTail &...FromTail) {
202 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
203 if (!ToHeadOrErr)
204 return ToHeadOrErr.takeError();
205 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
206 if (!ToTailOrErr)
207 return ToTailOrErr.takeError();
208 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
209 }
210
211// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000212 template <typename ToDeclT> struct CallOverloadedCreateFun {
213 template <typename... Args>
214 auto operator()(Args &&... args)
215 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
216 return ToDeclT::Create(std::forward<Args>(args)...);
217 }
218 };
219
220 // Always use these functions to create a Decl during import. There are
221 // certain tasks which must be done after the Decl was created, e.g. we
222 // must immediately register that as an imported Decl. The parameter `ToD`
223 // will be set to the newly created Decl or if had been imported before
224 // then to the already imported Decl. Returns a bool value set to true if
225 // the `FromD` had been imported before.
226 template <typename ToDeclT, typename FromDeclT, typename... Args>
227 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
228 Args &&... args) {
229 // There may be several overloads of ToDeclT::Create. We must make sure
230 // to call the one which would be chosen by the arguments, thus we use a
231 // wrapper for the overload set.
232 CallOverloadedCreateFun<ToDeclT> OC;
233 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
234 std::forward<Args>(args)...);
235 }
236 // Use this overload if a special Type is needed to be created. E.g if we
237 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
238 // then:
239 // TypedefNameDecl *ToTypedef;
240 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
241 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
242 typename... Args>
243 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
244 Args &&... args) {
245 CallOverloadedCreateFun<NewDeclT> OC;
246 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
247 std::forward<Args>(args)...);
248 }
249 // Use this version if a special create function must be
250 // used, e.g. CXXRecordDecl::CreateLambda .
251 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
252 typename... Args>
253 LLVM_NODISCARD bool
254 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
255 FromDeclT *FromD, Args &&... args) {
Gabor Marton303c98612019-06-25 08:00:51 +0000256 if (Importer.getImportDeclErrorIfAny(FromD)) {
257 ToD = nullptr;
258 return true; // Already imported but with error.
259 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000260 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
261 if (ToD)
262 return true; // Already imported.
263 ToD = CreateFun(std::forward<Args>(args)...);
Gabor Marton54058b52018-12-17 13:53:12 +0000264 // Keep track of imported Decls.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +0000265 Importer.RegisterImportedDecl(FromD, ToD);
Gabor Marton26f72a92018-07-12 09:42:05 +0000266 InitializeImportedDecl(FromD, ToD);
267 return false; // A new Decl is created.
268 }
269
270 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
Gabor Marton26f72a92018-07-12 09:42:05 +0000271 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
272 if (FromD->hasAttrs())
Balazs Keri57949eb2019-03-25 09:16:39 +0000273 for (const Attr *FromAttr : FromD->getAttrs()) {
274 // FIXME: Return of the error here is not possible until store of
275 // import errors is implemented.
276 auto ToAttrOrErr = import(FromAttr);
277 if (ToAttrOrErr)
278 ToD->addAttr(*ToAttrOrErr);
279 else
280 llvm::consumeError(ToAttrOrErr.takeError());
281 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000282 if (FromD->isUsed())
283 ToD->setIsUsed();
284 if (FromD->isImplicit())
285 ToD->setImplicit();
286 }
287
Gabor Martondd59d272019-03-19 14:04:50 +0000288 // Check if we have found an existing definition. Returns with that
289 // definition if yes, otherwise returns null.
290 Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) {
291 const FunctionDecl *Definition = nullptr;
292 if (D->doesThisDeclarationHaveABody() &&
293 FoundFunction->hasBody(Definition))
294 return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition));
295 return nullptr;
296 }
297
Douglas Gregor96e578d2010-02-05 17:54:41 +0000298 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000299 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000300
Balazs Keri3b30d652018-10-19 13:32:20 +0000301 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
302 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
303 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000304
305 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000306 ExpectedType VisitType(const Type *T);
307 ExpectedType VisitAtomicType(const AtomicType *T);
308 ExpectedType VisitBuiltinType(const BuiltinType *T);
309 ExpectedType VisitDecayedType(const DecayedType *T);
310 ExpectedType VisitComplexType(const ComplexType *T);
311 ExpectedType VisitPointerType(const PointerType *T);
312 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
313 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
314 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
315 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
316 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
317 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
318 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
319 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000320 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000321 ExpectedType VisitVectorType(const VectorType *T);
322 ExpectedType VisitExtVectorType(const ExtVectorType *T);
323 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
324 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
325 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
326 ExpectedType VisitParenType(const ParenType *T);
327 ExpectedType VisitTypedefType(const TypedefType *T);
328 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000329 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000330 ExpectedType VisitTypeOfType(const TypeOfType *T);
331 ExpectedType VisitDecltypeType(const DecltypeType *T);
332 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
333 ExpectedType VisitAutoType(const AutoType *T);
334 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000335 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000336 ExpectedType VisitRecordType(const RecordType *T);
337 ExpectedType VisitEnumType(const EnumType *T);
338 ExpectedType VisitAttributedType(const AttributedType *T);
339 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
340 ExpectedType VisitSubstTemplateTypeParmType(
341 const SubstTemplateTypeParmType *T);
342 ExpectedType VisitTemplateSpecializationType(
343 const TemplateSpecializationType *T);
344 ExpectedType VisitElaboratedType(const ElaboratedType *T);
345 ExpectedType VisitDependentNameType(const DependentNameType *T);
346 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
347 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000348 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000349 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
350 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
351 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000352
353 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000354 Error ImportDeclParts(
355 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
356 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
357 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
358 Error ImportDeclarationNameLoc(
359 const DeclarationNameInfo &From, DeclarationNameInfo &To);
360 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
361 Error ImportDeclContext(
362 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
363 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000364
Balazs Keri3b30d652018-10-19 13:32:20 +0000365 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000366
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000367 using Designator = DesignatedInitExpr::Designator;
368
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000369 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000370 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000371 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000372 /// nothing (if minimal import is set) or might be everything (if minimal
373 /// import is not set).
374 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000375 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000376 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000377 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000378 /// DeclContext.
379 IDK_Basic
380 };
381
Douglas Gregor2e15c842012-02-01 21:00:38 +0000382 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
383 return IDK == IDK_Everything ||
384 (IDK == IDK_Default && !Importer.isMinimalImport());
385 }
386
Balazs Keri3b30d652018-10-19 13:32:20 +0000387 Error ImportInitializer(VarDecl *From, VarDecl *To);
388 Error ImportDefinition(
389 RecordDecl *From, RecordDecl *To,
390 ImportDefinitionKind Kind = IDK_Default);
391 Error ImportDefinition(
392 EnumDecl *From, EnumDecl *To,
393 ImportDefinitionKind Kind = IDK_Default);
394 Error ImportDefinition(
395 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
396 ImportDefinitionKind Kind = IDK_Default);
397 Error ImportDefinition(
398 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
399 ImportDefinitionKind Kind = IDK_Default);
Balazs Keri3b30d652018-10-19 13:32:20 +0000400 Error ImportTemplateArguments(
401 const TemplateArgument *FromArgs, unsigned NumFromArgs,
402 SmallVectorImpl<TemplateArgument> &ToArgs);
403 Expected<TemplateArgument>
404 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000405
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000406 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000407 Error ImportTemplateArgumentListInfo(
408 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000409
410 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000411 Error ImportTemplateArgumentListInfo(
412 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
413 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000414
Gabor Marton5254e642018-06-27 13:32:50 +0000415 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000416 using FunctionTemplateAndArgsTy =
417 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
418 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000419 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
420 FunctionDecl *FromFD);
Balazs Keri1efc9742019-05-07 10:55:11 +0000421 Error ImportTemplateParameterLists(const DeclaratorDecl *FromD,
422 DeclaratorDecl *ToD);
Gabor Marton5254e642018-06-27 13:32:50 +0000423
Balazs Keri3b30d652018-10-19 13:32:20 +0000424 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000425
Shafik Yaghmour96b3d202019-01-28 21:55:33 +0000426 Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD);
427
Gabor Marton458d1452019-02-14 13:07:03 +0000428 template <typename T>
429 bool hasSameVisibilityContext(T *Found, T *From);
430
Gabor Marton950fb572018-07-17 12:39:27 +0000431 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000432 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
433 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000434 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
435 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000436 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000437 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000438 bool IsStructuralMatch(FunctionTemplateDecl *From,
439 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000440 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000441 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000442 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000443 ExpectedDecl VisitDecl(Decl *D);
444 ExpectedDecl VisitImportDecl(ImportDecl *D);
445 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
446 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
447 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
448 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
449 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
450 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
451 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
452 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
453 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
454 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
455 ExpectedDecl VisitLabelDecl(LabelDecl *D);
456 ExpectedDecl VisitEnumDecl(EnumDecl *D);
457 ExpectedDecl VisitRecordDecl(RecordDecl *D);
458 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
459 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
460 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
461 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
462 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
463 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
464 ExpectedDecl VisitFieldDecl(FieldDecl *D);
465 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
466 ExpectedDecl VisitFriendDecl(FriendDecl *D);
467 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
468 ExpectedDecl VisitVarDecl(VarDecl *D);
469 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
470 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
471 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
472 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
473 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
474 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
475 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
476 ExpectedDecl VisitUsingDecl(UsingDecl *D);
477 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
478 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
479 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
480 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000481
Balazs Keri3b30d652018-10-19 13:32:20 +0000482 Expected<ObjCTypeParamList *>
483 ImportObjCTypeParamList(ObjCTypeParamList *list);
484
485 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
486 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
487 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
488 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
489 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
490 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
491 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
492 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
493 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
494 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000495 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000496 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
497 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
498 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000499
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000500 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000501 ExpectedStmt VisitStmt(Stmt *S);
502 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
503 ExpectedStmt VisitDeclStmt(DeclStmt *S);
504 ExpectedStmt VisitNullStmt(NullStmt *S);
505 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
506 ExpectedStmt VisitCaseStmt(CaseStmt *S);
507 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
508 ExpectedStmt VisitLabelStmt(LabelStmt *S);
509 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
510 ExpectedStmt VisitIfStmt(IfStmt *S);
511 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
512 ExpectedStmt VisitWhileStmt(WhileStmt *S);
513 ExpectedStmt VisitDoStmt(DoStmt *S);
514 ExpectedStmt VisitForStmt(ForStmt *S);
515 ExpectedStmt VisitGotoStmt(GotoStmt *S);
516 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
517 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
518 ExpectedStmt VisitBreakStmt(BreakStmt *S);
519 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000520 // FIXME: MSAsmStmt
521 // FIXME: SEHExceptStmt
522 // FIXME: SEHFinallyStmt
523 // FIXME: SEHTryStmt
524 // FIXME: SEHLeaveStmt
525 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000526 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
527 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
528 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000529 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000530 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
531 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
532 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
533 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
534 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
535 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
536 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000537
538 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000539 ExpectedStmt VisitExpr(Expr *E);
540 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
Tom Roeder521f0042019-02-26 19:26:41 +0000541 ExpectedStmt VisitChooseExpr(ChooseExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000542 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
543 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
544 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
545 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
546 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
547 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
548 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
549 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
550 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
551 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
552 ExpectedStmt VisitStringLiteral(StringLiteral *E);
553 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
554 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
555 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000556 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000557 ExpectedStmt VisitParenExpr(ParenExpr *E);
558 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
559 ExpectedStmt VisitStmtExpr(StmtExpr *E);
560 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
561 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
562 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
563 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
564 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
565 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
566 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
567 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
568 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
569 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
570 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
571 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
572 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
573 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
574 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
575 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
576 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
577 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
578 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
579 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
580 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
581 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
582 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
583 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
584 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
585 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
586 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
587 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
588 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
589 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
590 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
591 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
592 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
593 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
594 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
595 ExpectedStmt VisitMemberExpr(MemberExpr *E);
596 ExpectedStmt VisitCallExpr(CallExpr *E);
597 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
598 ExpectedStmt VisitInitListExpr(InitListExpr *E);
599 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
600 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
601 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
602 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
603 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
604 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
605 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
606 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
607 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000608
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000609 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000610 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000611 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000612 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
613 Expected<ItemT> ToOrErr = import(*Ibegin);
614 if (!ToOrErr)
615 return ToOrErr.takeError();
616 *Obegin = *ToOrErr;
617 }
618 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000619 }
620
Balazs Keri3b30d652018-10-19 13:32:20 +0000621 // Import every item from a container structure into an output container.
622 // If error occurs, stops at first error and returns the error.
623 // The output container should have space for all needed elements (it is not
624 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000625 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000626 Error ImportContainerChecked(
627 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
628 return ImportArrayChecked(
629 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000630 }
631
632 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000633 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000634 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
635 }
Lang Hames19e07e12017-06-20 21:06:00 +0000636
Lang Hames19e07e12017-06-20 21:06:00 +0000637 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000638
Balazs Keri3b30d652018-10-19 13:32:20 +0000639 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
640 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000641 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000642
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000643template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000644Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000645 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
646 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000647 auto ToLAngleLocOrErr = import(FromLAngleLoc);
648 if (!ToLAngleLocOrErr)
649 return ToLAngleLocOrErr.takeError();
650 auto ToRAngleLocOrErr = import(FromRAngleLoc);
651 if (!ToRAngleLocOrErr)
652 return ToRAngleLocOrErr.takeError();
653
654 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
655 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
656 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000657 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000658 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000659}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000660
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000661template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000662Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000663 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
664 return ImportTemplateArgumentListInfo(
665 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
666}
667
668template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000669Error ASTNodeImporter::ImportTemplateArgumentListInfo<
670 ASTTemplateArgumentListInfo>(
671 const ASTTemplateArgumentListInfo &From,
672 TemplateArgumentListInfo &Result) {
673 return ImportTemplateArgumentListInfo(
674 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000675}
676
Balazs Keri3b30d652018-10-19 13:32:20 +0000677Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000678ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
679 FunctionDecl *FromFD) {
680 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000681 FunctionDecl::TK_FunctionTemplateSpecialization);
682
683 FunctionTemplateAndArgsTy Result;
684
Gabor Marton5254e642018-06-27 13:32:50 +0000685 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000686 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
687 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000688
689 // Import template arguments.
690 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000691 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
692 std::get<1>(Result)))
693 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000694
Balazs Keri3b30d652018-10-19 13:32:20 +0000695 return Result;
696}
697
698template <>
699Expected<TemplateParameterList *>
700ASTNodeImporter::import(TemplateParameterList *From) {
701 SmallVector<NamedDecl *, 4> To(From->size());
702 if (Error Err = ImportContainerChecked(*From, To))
703 return std::move(Err);
704
705 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
706 if (!ToRequiresClause)
707 return ToRequiresClause.takeError();
708
709 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
710 if (!ToTemplateLocOrErr)
711 return ToTemplateLocOrErr.takeError();
712 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
713 if (!ToLAngleLocOrErr)
714 return ToLAngleLocOrErr.takeError();
715 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
716 if (!ToRAngleLocOrErr)
717 return ToRAngleLocOrErr.takeError();
718
719 return TemplateParameterList::Create(
720 Importer.getToContext(),
721 *ToTemplateLocOrErr,
722 *ToLAngleLocOrErr,
723 To,
724 *ToRAngleLocOrErr,
725 *ToRequiresClause);
726}
727
728template <>
729Expected<TemplateArgument>
730ASTNodeImporter::import(const TemplateArgument &From) {
731 switch (From.getKind()) {
732 case TemplateArgument::Null:
733 return TemplateArgument();
734
735 case TemplateArgument::Type: {
736 ExpectedType ToTypeOrErr = import(From.getAsType());
737 if (!ToTypeOrErr)
738 return ToTypeOrErr.takeError();
739 return TemplateArgument(*ToTypeOrErr);
740 }
741
742 case TemplateArgument::Integral: {
743 ExpectedType ToTypeOrErr = import(From.getIntegralType());
744 if (!ToTypeOrErr)
745 return ToTypeOrErr.takeError();
746 return TemplateArgument(From, *ToTypeOrErr);
747 }
748
749 case TemplateArgument::Declaration: {
750 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
751 if (!ToOrErr)
752 return ToOrErr.takeError();
753 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
754 if (!ToTypeOrErr)
755 return ToTypeOrErr.takeError();
756 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
757 }
758
759 case TemplateArgument::NullPtr: {
760 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
761 if (!ToTypeOrErr)
762 return ToTypeOrErr.takeError();
763 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
764 }
765
766 case TemplateArgument::Template: {
767 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
768 if (!ToTemplateOrErr)
769 return ToTemplateOrErr.takeError();
770
771 return TemplateArgument(*ToTemplateOrErr);
772 }
773
774 case TemplateArgument::TemplateExpansion: {
775 Expected<TemplateName> ToTemplateOrErr =
776 import(From.getAsTemplateOrTemplatePattern());
777 if (!ToTemplateOrErr)
778 return ToTemplateOrErr.takeError();
779
780 return TemplateArgument(
781 *ToTemplateOrErr, From.getNumTemplateExpansions());
782 }
783
784 case TemplateArgument::Expression:
785 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
786 return TemplateArgument(*ToExpr);
787 else
788 return ToExpr.takeError();
789
790 case TemplateArgument::Pack: {
791 SmallVector<TemplateArgument, 2> ToPack;
792 ToPack.reserve(From.pack_size());
793 if (Error Err = ImportTemplateArguments(
794 From.pack_begin(), From.pack_size(), ToPack))
795 return std::move(Err);
796
797 return TemplateArgument(
798 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
799 }
800 }
801
802 llvm_unreachable("Invalid template argument kind");
803}
804
805template <>
806Expected<TemplateArgumentLoc>
807ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
808 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
809 if (!ArgOrErr)
810 return ArgOrErr.takeError();
811 TemplateArgument Arg = *ArgOrErr;
812
813 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
814
815 TemplateArgumentLocInfo ToInfo;
816 if (Arg.getKind() == TemplateArgument::Expression) {
817 ExpectedExpr E = import(FromInfo.getAsExpr());
818 if (!E)
819 return E.takeError();
820 ToInfo = TemplateArgumentLocInfo(*E);
821 } else if (Arg.getKind() == TemplateArgument::Type) {
822 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
823 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
824 else
825 return TSIOrErr.takeError();
826 } else {
827 auto ToTemplateQualifierLocOrErr =
828 import(FromInfo.getTemplateQualifierLoc());
829 if (!ToTemplateQualifierLocOrErr)
830 return ToTemplateQualifierLocOrErr.takeError();
831 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
832 if (!ToTemplateNameLocOrErr)
833 return ToTemplateNameLocOrErr.takeError();
834 auto ToTemplateEllipsisLocOrErr =
835 import(FromInfo.getTemplateEllipsisLoc());
836 if (!ToTemplateEllipsisLocOrErr)
837 return ToTemplateEllipsisLocOrErr.takeError();
838
839 ToInfo = TemplateArgumentLocInfo(
840 *ToTemplateQualifierLocOrErr,
841 *ToTemplateNameLocOrErr,
842 *ToTemplateEllipsisLocOrErr);
843 }
844
845 return TemplateArgumentLoc(Arg, ToInfo);
846}
847
848template <>
849Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
850 if (DG.isNull())
851 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
852 size_t NumDecls = DG.end() - DG.begin();
853 SmallVector<Decl *, 1> ToDecls;
854 ToDecls.reserve(NumDecls);
855 for (Decl *FromD : DG) {
856 if (auto ToDOrErr = import(FromD))
857 ToDecls.push_back(*ToDOrErr);
858 else
859 return ToDOrErr.takeError();
860 }
861 return DeclGroupRef::Create(Importer.getToContext(),
862 ToDecls.begin(),
863 NumDecls);
864}
865
866template <>
867Expected<ASTNodeImporter::Designator>
868ASTNodeImporter::import(const Designator &D) {
869 if (D.isFieldDesignator()) {
870 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
871
872 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
873 if (!ToDotLocOrErr)
874 return ToDotLocOrErr.takeError();
875
876 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
877 if (!ToFieldLocOrErr)
878 return ToFieldLocOrErr.takeError();
879
880 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
881 }
882
883 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
884 if (!ToLBracketLocOrErr)
885 return ToLBracketLocOrErr.takeError();
886
887 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
888 if (!ToRBracketLocOrErr)
889 return ToRBracketLocOrErr.takeError();
890
891 if (D.isArrayDesignator())
892 return Designator(D.getFirstExprIndex(),
893 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
894
895 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
896 if (!ToEllipsisLocOrErr)
897 return ToEllipsisLocOrErr.takeError();
898
899 assert(D.isArrayRangeDesignator());
900 return Designator(
901 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
902 *ToRBracketLocOrErr);
903}
904
905template <>
906Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
907 VarDecl *Var = nullptr;
908 if (From.capturesVariable()) {
909 if (auto VarOrErr = import(From.getCapturedVar()))
910 Var = *VarOrErr;
911 else
912 return VarOrErr.takeError();
913 }
914
915 auto LocationOrErr = import(From.getLocation());
916 if (!LocationOrErr)
917 return LocationOrErr.takeError();
918
919 SourceLocation EllipsisLoc;
920 if (From.isPackExpansion())
921 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
922 return std::move(Err);
923
924 return LambdaCapture(
925 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
926 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000927}
928
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000929} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000930
Douglas Gregor3996e242010-02-15 22:01:00 +0000931//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000932// Import Types
933//----------------------------------------------------------------------------
934
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000935using namespace clang;
936
Balazs Keri3b30d652018-10-19 13:32:20 +0000937ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000938 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
939 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000940 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000941}
942
Balazs Keri3b30d652018-10-19 13:32:20 +0000943ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
944 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
945 if (!UnderlyingTypeOrErr)
946 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000947
Balazs Keri3b30d652018-10-19 13:32:20 +0000948 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000949}
950
Balazs Keri3b30d652018-10-19 13:32:20 +0000951ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000952 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000953#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
954 case BuiltinType::Id: \
955 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000956#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000957#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
958 case BuiltinType::Id: \
959 return Importer.getToContext().Id##Ty;
960#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000961#define SHARED_SINGLETON_TYPE(Expansion)
962#define BUILTIN_TYPE(Id, SingletonId) \
963 case BuiltinType::Id: return Importer.getToContext().SingletonId;
964#include "clang/AST/BuiltinTypes.def"
965
966 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
967 // context supports C++.
968
969 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
970 // context supports ObjC.
971
Douglas Gregor96e578d2010-02-05 17:54:41 +0000972 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000973 // The context we're importing from has an unsigned 'char'. If we're
974 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000975 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000976 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000977 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000978
Douglas Gregor96e578d2010-02-05 17:54:41 +0000979 return Importer.getToContext().CharTy;
980
Douglas Gregor96e578d2010-02-05 17:54:41 +0000981 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000982 // The context we're importing from has an unsigned 'char'. If we're
983 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000984 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000985 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000986 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000987
Douglas Gregor96e578d2010-02-05 17:54:41 +0000988 return Importer.getToContext().CharTy;
989
Chris Lattnerad3467e2010-12-25 23:25:43 +0000990 case BuiltinType::WChar_S:
991 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000992 // FIXME: If not in C++, shall we translate to the C equivalent of
993 // wchar_t?
994 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000995 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000996
997 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000998}
999
Balazs Keri3b30d652018-10-19 13:32:20 +00001000ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1001 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1002 if (!ToOriginalTypeOrErr)
1003 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001004
Balazs Keri3b30d652018-10-19 13:32:20 +00001005 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001006}
1007
Balazs Keri3b30d652018-10-19 13:32:20 +00001008ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1009 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1010 if (!ToElementTypeOrErr)
1011 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001012
Balazs Keri3b30d652018-10-19 13:32:20 +00001013 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014}
1015
Balazs Keri3b30d652018-10-19 13:32:20 +00001016ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1017 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1018 if (!ToPointeeTypeOrErr)
1019 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001020
Balazs Keri3b30d652018-10-19 13:32:20 +00001021 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001022}
1023
Balazs Keri3b30d652018-10-19 13:32:20 +00001024ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001025 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001026 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1027 if (!ToPointeeTypeOrErr)
1028 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001029
Balazs Keri3b30d652018-10-19 13:32:20 +00001030 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001031}
1032
Balazs Keri3b30d652018-10-19 13:32:20 +00001033ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001034ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001035 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001036 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1037 if (!ToPointeeTypeOrErr)
1038 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001039
Balazs Keri3b30d652018-10-19 13:32:20 +00001040 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001041}
1042
Balazs Keri3b30d652018-10-19 13:32:20 +00001043ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001044ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001045 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001046 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1047 if (!ToPointeeTypeOrErr)
1048 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001049
Balazs Keri3b30d652018-10-19 13:32:20 +00001050 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001051}
1052
Balazs Keri3b30d652018-10-19 13:32:20 +00001053ExpectedType
1054ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001055 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001056 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1057 if (!ToPointeeTypeOrErr)
1058 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001059
Balazs Keri3b30d652018-10-19 13:32:20 +00001060 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1061 if (!ClassTypeOrErr)
1062 return ClassTypeOrErr.takeError();
1063
1064 return Importer.getToContext().getMemberPointerType(
1065 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001066}
1067
Balazs Keri3b30d652018-10-19 13:32:20 +00001068ExpectedType
1069ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1070 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1071 if (!ToElementTypeOrErr)
1072 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001073
Balazs Keri3b30d652018-10-19 13:32:20 +00001074 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001075 T->getSize(),
1076 T->getSizeModifier(),
1077 T->getIndexTypeCVRQualifiers());
1078}
1079
Balazs Keri3b30d652018-10-19 13:32:20 +00001080ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001081ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001082 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1083 if (!ToElementTypeOrErr)
1084 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001085
Balazs Keri3b30d652018-10-19 13:32:20 +00001086 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001087 T->getSizeModifier(),
1088 T->getIndexTypeCVRQualifiers());
1089}
1090
Balazs Keri3b30d652018-10-19 13:32:20 +00001091ExpectedType
1092ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1093 QualType ToElementType;
1094 Expr *ToSizeExpr;
1095 SourceRange ToBracketsRange;
1096 if (auto Imp = importSeq(
1097 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1098 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1099 else
1100 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001101
Balazs Keri3b30d652018-10-19 13:32:20 +00001102 return Importer.getToContext().getVariableArrayType(
1103 ToElementType, ToSizeExpr, T->getSizeModifier(),
1104 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001105}
1106
Balazs Keri3b30d652018-10-19 13:32:20 +00001107ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001108 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001109 QualType ToElementType;
1110 Expr *ToSizeExpr;
1111 SourceRange ToBracketsRange;
1112 if (auto Imp = importSeq(
1113 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1114 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1115 else
1116 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001117 // SizeExpr may be null if size is not specified directly.
1118 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001119
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001120 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001121 ToElementType, ToSizeExpr, T->getSizeModifier(),
1122 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001123}
1124
Balazs Keri3b30d652018-10-19 13:32:20 +00001125ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1126 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1127 if (!ToElementTypeOrErr)
1128 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001129
Balazs Keri3b30d652018-10-19 13:32:20 +00001130 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001131 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001132 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001133}
1134
Balazs Keri3b30d652018-10-19 13:32:20 +00001135ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1136 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1137 if (!ToElementTypeOrErr)
1138 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001139
Balazs Keri3b30d652018-10-19 13:32:20 +00001140 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001141 T->getNumElements());
1142}
1143
Balazs Keri3b30d652018-10-19 13:32:20 +00001144ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001145ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001146 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001147 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001148 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1149 if (!ToReturnTypeOrErr)
1150 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001151
Balazs Keri3b30d652018-10-19 13:32:20 +00001152 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001153 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001154}
1155
Balazs Keri3b30d652018-10-19 13:32:20 +00001156ExpectedType
1157ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1158 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1159 if (!ToReturnTypeOrErr)
1160 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001161
Douglas Gregor96e578d2010-02-05 17:54:41 +00001162 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001163 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001164 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001165 ExpectedType TyOrErr = import(A);
1166 if (!TyOrErr)
1167 return TyOrErr.takeError();
1168 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001169 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001170
Douglas Gregor96e578d2010-02-05 17:54:41 +00001171 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001172 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001173 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001174 ExpectedType TyOrErr = import(E);
1175 if (!TyOrErr)
1176 return TyOrErr.takeError();
1177 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001178 }
John McCalldb40c7f2010-12-14 08:05:40 +00001179
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001180 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1181 FunctionProtoType::ExtProtoInfo ToEPI;
1182
Balazs Keri3b30d652018-10-19 13:32:20 +00001183 auto Imp = importSeq(
1184 FromEPI.ExceptionSpec.NoexceptExpr,
1185 FromEPI.ExceptionSpec.SourceDecl,
1186 FromEPI.ExceptionSpec.SourceTemplate);
1187 if (!Imp)
1188 return Imp.takeError();
1189
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001190 ToEPI.ExtInfo = FromEPI.ExtInfo;
1191 ToEPI.Variadic = FromEPI.Variadic;
1192 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1193 ToEPI.TypeQuals = FromEPI.TypeQuals;
1194 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001195 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1196 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001197 std::tie(
1198 ToEPI.ExceptionSpec.NoexceptExpr,
1199 ToEPI.ExceptionSpec.SourceDecl,
1200 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001201
Balazs Keri3b30d652018-10-19 13:32:20 +00001202 return Importer.getToContext().getFunctionType(
1203 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001204}
1205
Balazs Keri3b30d652018-10-19 13:32:20 +00001206ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001207 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001208 UnresolvedUsingTypenameDecl *ToD;
1209 Decl *ToPrevD;
1210 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1211 std::tie(ToD, ToPrevD) = *Imp;
1212 else
1213 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001214
Balazs Keri3b30d652018-10-19 13:32:20 +00001215 return Importer.getToContext().getTypeDeclType(
1216 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001217}
1218
Balazs Keri3b30d652018-10-19 13:32:20 +00001219ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1220 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1221 if (!ToInnerTypeOrErr)
1222 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001223
Balazs Keri3b30d652018-10-19 13:32:20 +00001224 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001225}
1226
Balazs Keri3b30d652018-10-19 13:32:20 +00001227ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1228 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1229 if (!ToDeclOrErr)
1230 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001231
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001233}
1234
Balazs Keri3b30d652018-10-19 13:32:20 +00001235ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1236 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1237 if (!ToExprOrErr)
1238 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001239
Balazs Keri3b30d652018-10-19 13:32:20 +00001240 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001241}
1242
Balazs Keri3b30d652018-10-19 13:32:20 +00001243ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1244 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1245 if (!ToUnderlyingTypeOrErr)
1246 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001247
Balazs Keri3b30d652018-10-19 13:32:20 +00001248 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001249}
1250
Balazs Keri3b30d652018-10-19 13:32:20 +00001251ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001252 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001253 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1254 if (!ToExprOrErr)
1255 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001256
Balazs Keri3b30d652018-10-19 13:32:20 +00001257 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1258 if (!ToUnderlyingTypeOrErr)
1259 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001260
Balazs Keri3b30d652018-10-19 13:32:20 +00001261 return Importer.getToContext().getDecltypeType(
1262 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001263}
1264
Balazs Keri3b30d652018-10-19 13:32:20 +00001265ExpectedType
1266ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1267 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1268 if (!ToBaseTypeOrErr)
1269 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001270
Balazs Keri3b30d652018-10-19 13:32:20 +00001271 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1272 if (!ToUnderlyingTypeOrErr)
1273 return ToUnderlyingTypeOrErr.takeError();
1274
1275 return Importer.getToContext().getUnaryTransformType(
1276 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001277}
1278
Balazs Keri3b30d652018-10-19 13:32:20 +00001279ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001280 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001281 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1282 if (!ToDeducedTypeOrErr)
1283 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001284
Balazs Keri3b30d652018-10-19 13:32:20 +00001285 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1286 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001287 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001288}
1289
Balazs Keri3b30d652018-10-19 13:32:20 +00001290ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001291 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001292 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1293 if (!ToDeclOrErr)
1294 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001295
Balazs Keri3b30d652018-10-19 13:32:20 +00001296 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1297 if (!ToInjTypeOrErr)
1298 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001299
1300 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1301 // See comments in InjectedClassNameType definition for details
1302 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1303 enum {
1304 TypeAlignmentInBits = 4,
1305 TypeAlignment = 1 << TypeAlignmentInBits
1306 };
1307
1308 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001309 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001310}
1311
Balazs Keri3b30d652018-10-19 13:32:20 +00001312ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1313 Expected<RecordDecl *> 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::VisitEnumType(const EnumType *T) {
1321 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1322 if (!ToDeclOrErr)
1323 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001324
Balazs Keri3b30d652018-10-19 13:32:20 +00001325 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001326}
1327
Balazs Keri3b30d652018-10-19 13:32:20 +00001328ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1329 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1330 if (!ToModifiedTypeOrErr)
1331 return ToModifiedTypeOrErr.takeError();
1332 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1333 if (!ToEquivalentTypeOrErr)
1334 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001335
1336 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001337 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001338}
1339
Balazs Keri3b30d652018-10-19 13:32:20 +00001340ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001341 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001342 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1343 if (!ToDeclOrErr)
1344 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001345
1346 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001347 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001348}
1349
Balazs Keri3b30d652018-10-19 13:32:20 +00001350ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001351 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001352 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1353 if (!ReplacedOrErr)
1354 return ReplacedOrErr.takeError();
1355 const TemplateTypeParmType *Replaced =
1356 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001357
Balazs Keri3b30d652018-10-19 13:32:20 +00001358 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1359 if (!ToReplacementTypeOrErr)
1360 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001361
1362 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001363 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001364}
1365
Balazs Keri3b30d652018-10-19 13:32:20 +00001366ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001367 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001368 auto ToTemplateOrErr = import(T->getTemplateName());
1369 if (!ToTemplateOrErr)
1370 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001371
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001372 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001373 if (Error Err = ImportTemplateArguments(
1374 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1375 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001376
Douglas Gregore2e50d332010-12-01 01:36:18 +00001377 QualType ToCanonType;
1378 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001379 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001380 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001381 if (ExpectedType TyOrErr = import(FromCanonType))
1382 ToCanonType = *TyOrErr;
1383 else
1384 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001385 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001386 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001387 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001388 ToCanonType);
1389}
1390
Balazs Keri3b30d652018-10-19 13:32:20 +00001391ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001392 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 auto ToQualifierOrErr = import(T->getQualifier());
1394 if (!ToQualifierOrErr)
1395 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001396
Balazs Keri3b30d652018-10-19 13:32:20 +00001397 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1398 if (!ToNamedTypeOrErr)
1399 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001400
Balazs Keri3b30d652018-10-19 13:32:20 +00001401 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1402 if (!ToOwnedTagDeclOrErr)
1403 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001404
Abramo Bagnara6150c882010-05-11 21:36:43 +00001405 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001406 *ToQualifierOrErr,
1407 *ToNamedTypeOrErr,
1408 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001409}
1410
Balazs Keri3b30d652018-10-19 13:32:20 +00001411ExpectedType
1412ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1413 ExpectedType ToPatternOrErr = import(T->getPattern());
1414 if (!ToPatternOrErr)
1415 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001416
Balazs Keri3b30d652018-10-19 13:32:20 +00001417 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001418 T->getNumExpansions());
1419}
1420
Balazs Keri3b30d652018-10-19 13:32:20 +00001421ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001422 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001423 auto ToQualifierOrErr = import(T->getQualifier());
1424 if (!ToQualifierOrErr)
1425 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001426
Balazs Keri3b30d652018-10-19 13:32:20 +00001427 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001428
1429 SmallVector<TemplateArgument, 2> ToPack;
1430 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001431 if (Error Err = ImportTemplateArguments(
1432 T->getArgs(), T->getNumArgs(), ToPack))
1433 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001434
1435 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001437}
1438
Balazs Keri3b30d652018-10-19 13:32:20 +00001439ExpectedType
1440ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1441 auto ToQualifierOrErr = import(T->getQualifier());
1442 if (!ToQualifierOrErr)
1443 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001444
1445 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001446
Balazs Keri3b30d652018-10-19 13:32:20 +00001447 QualType Canon;
1448 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1449 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1450 Canon = (*TyOrErr).getCanonicalType();
1451 else
1452 return TyOrErr.takeError();
1453 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001454
Balazs Keri3b30d652018-10-19 13:32:20 +00001455 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1456 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001457 Name, Canon);
1458}
1459
Balazs Keri3b30d652018-10-19 13:32:20 +00001460ExpectedType
1461ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1462 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1463 if (!ToDeclOrErr)
1464 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001465
Balazs Keri3b30d652018-10-19 13:32:20 +00001466 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001467}
1468
Balazs Keri3b30d652018-10-19 13:32:20 +00001469ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1470 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1471 if (!ToBaseTypeOrErr)
1472 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001473
Douglas Gregore9d95f12015-07-07 03:57:35 +00001474 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001475 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001476 if (ExpectedType TyOrErr = import(TypeArg))
1477 TypeArgs.push_back(*TyOrErr);
1478 else
1479 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001480 }
1481
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001482 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001483 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001484 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1485 Protocols.push_back(*ProtocolOrErr);
1486 else
1487 return ProtocolOrErr.takeError();
1488
Douglas Gregor96e578d2010-02-05 17:54:41 +00001489 }
1490
Balazs Keri3b30d652018-10-19 13:32:20 +00001491 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001492 Protocols,
1493 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001494}
1495
Balazs Keri3b30d652018-10-19 13:32:20 +00001496ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001497ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001498 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1499 if (!ToPointeeTypeOrErr)
1500 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001501
Balazs Keri3b30d652018-10-19 13:32:20 +00001502 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503}
1504
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001505//----------------------------------------------------------------------------
1506// Import Declarations
1507//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001508Error ASTNodeImporter::ImportDeclParts(
1509 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1510 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001511 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1512 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1513 DeclContext *OrigDC = D->getDeclContext();
1514 FunctionDecl *FunDecl;
1515 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1516 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001517 auto getLeafPointeeType = [](const Type *T) {
1518 while (T->isPointerType() || T->isArrayType()) {
1519 T = T->getPointeeOrArrayElementType();
1520 }
1521 return T;
1522 };
1523 for (const ParmVarDecl *P : FunDecl->parameters()) {
1524 const Type *LeafT =
1525 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1526 auto *RT = dyn_cast<RecordType>(LeafT);
1527 if (RT && RT->getDecl() == D) {
1528 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1529 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001530 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001531 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001532 }
1533 }
1534
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001535 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001536 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1537 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001538
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001539 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001540 if (Error Err = importInto(Name, D->getDeclName()))
1541 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001542
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001543 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001544 if (Error Err = importInto(Loc, D->getLocation()))
1545 return Err;
1546
Sean Callanan59721b32015-04-28 18:41:46 +00001547 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001548 if (ToD)
1549 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1550 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001551
1552 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001553}
1554
Balazs Keri3b30d652018-10-19 13:32:20 +00001555Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001556 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001557 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001558
Balazs Keri3b30d652018-10-19 13:32:20 +00001559 if (!ToD)
1560 if (Error Err = importInto(ToD, FromD))
1561 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001562
Balazs Keri3b30d652018-10-19 13:32:20 +00001563 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1564 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1565 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1566 !ToRecord->getDefinition()) {
1567 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1568 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001569 }
1570 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001571 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001572 }
1573
Balazs Keri3b30d652018-10-19 13:32:20 +00001574 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1575 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001576 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001577 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1578 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001579 }
1580 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001581 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001582 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001583
1584 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001585}
1586
Balazs Keri3b30d652018-10-19 13:32:20 +00001587Error
1588ASTNodeImporter::ImportDeclarationNameLoc(
1589 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001590 // NOTE: To.Name and To.Loc are already imported.
1591 // We only have to import To.LocInfo.
1592 switch (To.getName().getNameKind()) {
1593 case DeclarationName::Identifier:
1594 case DeclarationName::ObjCZeroArgSelector:
1595 case DeclarationName::ObjCOneArgSelector:
1596 case DeclarationName::ObjCMultiArgSelector:
1597 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001598 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001599 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001600
1601 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001602 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1603 To.setCXXOperatorNameRange(*ToRangeOrErr);
1604 else
1605 return ToRangeOrErr.takeError();
1606 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001607 }
1608 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001609 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1610 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1611 else
1612 return LocOrErr.takeError();
1613 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001614 }
1615 case DeclarationName::CXXConstructorName:
1616 case DeclarationName::CXXDestructorName:
1617 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001618 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1619 To.setNamedTypeInfo(*ToTInfoOrErr);
1620 else
1621 return ToTInfoOrErr.takeError();
1622 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001623 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001624 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001625 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001626}
1627
Balazs Keri3b30d652018-10-19 13:32:20 +00001628Error
1629ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001630 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001631 auto ToDCOrErr = Importer.ImportContext(FromDC);
1632 return ToDCOrErr.takeError();
1633 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001634 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001635 for (auto *From : FromDC->decls()) {
1636 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001637 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001638 // Ignore the error, continue with next Decl.
1639 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001640 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001641 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001642
Balazs Keri3b30d652018-10-19 13:32:20 +00001643 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001644}
1645
Balazs Keri3b30d652018-10-19 13:32:20 +00001646Error ASTNodeImporter::ImportDeclContext(
1647 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1648 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1649 if (!ToDCOrErr)
1650 return ToDCOrErr.takeError();
1651 ToDC = *ToDCOrErr;
1652
1653 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1654 auto ToLexicalDCOrErr = Importer.ImportContext(
1655 FromD->getLexicalDeclContext());
1656 if (!ToLexicalDCOrErr)
1657 return ToLexicalDCOrErr.takeError();
1658 ToLexicalDC = *ToLexicalDCOrErr;
1659 } else
1660 ToLexicalDC = ToDC;
1661
1662 return Error::success();
1663}
1664
1665Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001666 const CXXRecordDecl *From, CXXRecordDecl *To) {
1667 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1668 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001669
Balazs Keri1d20cc22018-07-16 12:16:39 +00001670 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001671 if (FromM->isImplicit()) {
1672 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1673 if (!ToMOrErr)
1674 return ToMOrErr.takeError();
1675 }
1676
1677 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001678}
1679
Balazs Keri3b30d652018-10-19 13:32:20 +00001680static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1681 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001682 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00001683 if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef))
Balazs Keri57949eb2019-03-25 09:16:39 +00001684 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1685 else
1686 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001687 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001688 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001689}
1690
Balazs Keri3b30d652018-10-19 13:32:20 +00001691Error ASTNodeImporter::ImportDefinition(
1692 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001693 if (To->getDefinition() || To->isBeingDefined()) {
1694 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001695 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001696
Balazs Keri3b30d652018-10-19 13:32:20 +00001697 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001698 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001699
Douglas Gregore2e50d332010-12-01 01:36:18 +00001700 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001701
Balazs Keri3b30d652018-10-19 13:32:20 +00001702 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1703 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001704
Douglas Gregore2e50d332010-12-01 01:36:18 +00001705 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001706 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1707 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1708 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001709
1710 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1711 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1712 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001713 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001714 ToData.Aggregate = FromData.Aggregate;
1715 ToData.PlainOldData = FromData.PlainOldData;
1716 ToData.Empty = FromData.Empty;
1717 ToData.Polymorphic = FromData.Polymorphic;
1718 ToData.Abstract = FromData.Abstract;
1719 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001720 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1721 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1722 ToData.HasBasesWithNonStaticDataMembers =
1723 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001724 ToData.HasPrivateFields = FromData.HasPrivateFields;
1725 ToData.HasProtectedFields = FromData.HasProtectedFields;
1726 ToData.HasPublicFields = FromData.HasPublicFields;
1727 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001728 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001729 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001730 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001731 ToData.HasUninitializedReferenceMember
1732 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001733 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001734 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1735 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001736 ToData.NeedOverloadResolutionForCopyConstructor
1737 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001738 ToData.NeedOverloadResolutionForMoveConstructor
1739 = FromData.NeedOverloadResolutionForMoveConstructor;
1740 ToData.NeedOverloadResolutionForMoveAssignment
1741 = FromData.NeedOverloadResolutionForMoveAssignment;
1742 ToData.NeedOverloadResolutionForDestructor
1743 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001744 ToData.DefaultedCopyConstructorIsDeleted
1745 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001746 ToData.DefaultedMoveConstructorIsDeleted
1747 = FromData.DefaultedMoveConstructorIsDeleted;
1748 ToData.DefaultedMoveAssignmentIsDeleted
1749 = FromData.DefaultedMoveAssignmentIsDeleted;
1750 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001751 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1752 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001753 ToData.HasConstexprNonCopyMoveConstructor
1754 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001755 ToData.HasDefaultedDefaultConstructor
1756 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001757 ToData.DefaultedDefaultConstructorIsConstexpr
1758 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001759 ToData.HasConstexprDefaultConstructor
1760 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001761 ToData.HasNonLiteralTypeFieldsOrBases
1762 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001763 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001764 ToData.UserProvidedDefaultConstructor
1765 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001766 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001767 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1768 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1769 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1770 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001771 ToData.ImplicitCopyAssignmentHasConstParam
1772 = FromData.ImplicitCopyAssignmentHasConstParam;
1773 ToData.HasDeclaredCopyConstructorWithConstParam
1774 = FromData.HasDeclaredCopyConstructorWithConstParam;
1775 ToData.HasDeclaredCopyAssignmentWithConstParam
1776 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001777
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001778 // Copy over the data stored in RecordDeclBits
1779 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1780
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001781 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001782 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001783 ExpectedType TyOrErr = import(Base1.getType());
1784 if (!TyOrErr)
1785 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001786
1787 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001788 if (Base1.isPackExpansion()) {
1789 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1790 EllipsisLoc = *LocOrErr;
1791 else
1792 return LocOrErr.takeError();
1793 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001794
1795 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001796 if (Error Err =
1797 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1798 return Err;
1799
1800 auto RangeOrErr = import(Base1.getSourceRange());
1801 if (!RangeOrErr)
1802 return RangeOrErr.takeError();
1803
1804 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1805 if (!TSIOrErr)
1806 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001807
Douglas Gregore2e50d332010-12-01 01:36:18 +00001808 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001809 new (Importer.getToContext()) CXXBaseSpecifier(
1810 *RangeOrErr,
1811 Base1.isVirtual(),
1812 Base1.isBaseOfClass(),
1813 Base1.getAccessSpecifierAsWritten(),
1814 *TSIOrErr,
1815 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001816 }
1817 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001818 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001819 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001820
Douglas Gregor2e15c842012-02-01 21:00:38 +00001821 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001822 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1823 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001824
Douglas Gregore2e50d332010-12-01 01:36:18 +00001825 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001826 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001827}
1828
Balazs Keri3b30d652018-10-19 13:32:20 +00001829Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001830 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001831 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001832
Gabor Martonac3a5d62018-09-17 12:04:52 +00001833 Expr *FromInit = From->getInit();
1834 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001835 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001836
Balazs Keri3b30d652018-10-19 13:32:20 +00001837 ExpectedExpr ToInitOrErr = import(FromInit);
1838 if (!ToInitOrErr)
1839 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001840
Balazs Keri3b30d652018-10-19 13:32:20 +00001841 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001842 if (From->isInitKnownICE()) {
1843 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1844 Eval->CheckedICE = true;
1845 Eval->IsICE = From->isInitICE();
1846 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001847
1848 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001849 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001850}
1851
Balazs Keri3b30d652018-10-19 13:32:20 +00001852Error ASTNodeImporter::ImportDefinition(
1853 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001854 if (To->getDefinition() || To->isBeingDefined()) {
1855 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001856 return ImportDeclContext(From, /*ForceImport=*/true);
1857 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001858 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001859
Douglas Gregord451ea92011-07-29 23:31:30 +00001860 To->startDefinition();
1861
Balazs Keri3b30d652018-10-19 13:32:20 +00001862 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1863 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001864
Balazs Keri3b30d652018-10-19 13:32:20 +00001865 ExpectedType ToTypeOrErr =
1866 import(Importer.getFromContext().getTypeDeclType(From));
1867 if (!ToTypeOrErr)
1868 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001869
Balazs Keri3b30d652018-10-19 13:32:20 +00001870 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1871 if (!ToPromotionTypeOrErr)
1872 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001873
1874 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001875 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1876 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001877
Douglas Gregord451ea92011-07-29 23:31:30 +00001878 // FIXME: we might need to merge the number of positive or negative bits
1879 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001880 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001881 From->getNumPositiveBits(),
1882 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001883 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001884}
1885
Balazs Keri3b30d652018-10-19 13:32:20 +00001886Error ASTNodeImporter::ImportTemplateArguments(
1887 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1888 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001889 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001890 if (auto ToOrErr = import(FromArgs[I]))
1891 ToArgs.push_back(*ToOrErr);
1892 else
1893 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001894 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001895
Balazs Keri3b30d652018-10-19 13:32:20 +00001896 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001897}
1898
Balazs Keri3b30d652018-10-19 13:32:20 +00001899// FIXME: Do not forget to remove this and use only 'import'.
1900Expected<TemplateArgument>
1901ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1902 return import(From);
1903}
1904
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001905template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001906Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001907 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1908 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001909 if (auto ToLocOrErr = import(FromLoc))
1910 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001911 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001912 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001913 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001914 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001915}
1916
Gabor Marton26f72a92018-07-12 09:42:05 +00001917static StructuralEquivalenceKind
1918getStructuralEquivalenceKind(const ASTImporter &Importer) {
1919 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1920 : StructuralEquivalenceKind::Default;
1921}
1922
Gabor Marton950fb572018-07-17 12:39:27 +00001923bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1924 StructuralEquivalenceContext Ctx(
1925 Importer.getFromContext(), Importer.getToContext(),
1926 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1927 false, Complain);
1928 return Ctx.IsEquivalent(From, To);
1929}
1930
1931bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001932 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001933 // Eliminate a potential failure point where we attempt to re-import
1934 // something we're trying to import while completing ToRecord.
1935 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1936 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001937 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001938 if (ToOriginRecord)
1939 ToRecord = ToOriginRecord;
1940 }
1941
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001942 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001943 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001944 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001945 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001946 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001947 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001948}
1949
Larisse Voufo39a1e502013-08-06 01:03:05 +00001950bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1951 bool Complain) {
1952 StructuralEquivalenceContext Ctx(
1953 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001954 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1955 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001956 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001957}
1958
Douglas Gregor98c10182010-02-12 22:17:39 +00001959bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001960 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00001961 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001962 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
1963 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
1964 ToEnum = ToOriginEnum;
1965
Gabor Marton26f72a92018-07-12 09:42:05 +00001966 StructuralEquivalenceContext Ctx(
1967 Importer.getFromContext(), Importer.getToContext(),
1968 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001969 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001970}
1971
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001972bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1973 FunctionTemplateDecl *To) {
1974 StructuralEquivalenceContext Ctx(
1975 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001976 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1977 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001978 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001979}
1980
Balazs Keric7797c42018-07-11 09:37:24 +00001981bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1982 StructuralEquivalenceContext Ctx(
1983 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001984 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1985 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001986 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001987}
1988
Douglas Gregor91155082012-11-14 22:29:20 +00001989bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001990 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001991 const llvm::APSInt &FromVal = FromEC->getInitVal();
1992 const llvm::APSInt &ToVal = ToEC->getInitVal();
1993
1994 return FromVal.isSigned() == ToVal.isSigned() &&
1995 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1996 FromVal == ToVal;
1997}
1998
1999bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002000 ClassTemplateDecl *To) {
2001 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2002 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002003 Importer.getNonEquivalentDecls(),
2004 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002005 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002006}
2007
Larisse Voufo39a1e502013-08-06 01:03:05 +00002008bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2009 VarTemplateDecl *To) {
2010 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2011 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002012 Importer.getNonEquivalentDecls(),
2013 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002014 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002015}
2016
Balazs Keri3b30d652018-10-19 13:32:20 +00002017ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002018 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002019 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002020 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002021}
2022
Balazs Keri3b30d652018-10-19 13:32:20 +00002023ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2024 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2025 << D->getDeclKindName();
2026 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2027}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002028
Balazs Keri3b30d652018-10-19 13:32:20 +00002029ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2030 // Import the context of this declaration.
2031 DeclContext *DC, *LexicalDC;
2032 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2033 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002034
2035 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002036 ExpectedSLoc LocOrErr = import(D->getLocation());
2037 if (!LocOrErr)
2038 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002039
Gabor Marton26f72a92018-07-12 09:42:05 +00002040 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002041 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002042 return ToD;
2043
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002044 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002045 LexicalDC->addDeclInternal(ToD);
2046 return ToD;
2047}
2048
Balazs Keri3b30d652018-10-19 13:32:20 +00002049ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002050 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002051 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002052
Gabor Marton26f72a92018-07-12 09:42:05 +00002053 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002054
Sean Callanan65198272011-11-17 23:20:56 +00002055 return ToD;
2056}
2057
Balazs Keri3b30d652018-10-19 13:32:20 +00002058ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2059 ExpectedSLoc LocOrErr = import(D->getLocation());
2060 if (!LocOrErr)
2061 return LocOrErr.takeError();
2062 auto ColonLocOrErr = import(D->getColonLoc());
2063 if (!ColonLocOrErr)
2064 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002065
2066 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002067 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2068 if (!DCOrErr)
2069 return DCOrErr.takeError();
2070 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002071
Gabor Marton26f72a92018-07-12 09:42:05 +00002072 AccessSpecDecl *ToD;
2073 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002074 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002075 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002076
2077 // Lexical DeclContext and Semantic DeclContext
2078 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002079 ToD->setLexicalDeclContext(DC);
2080 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002081
Gabor Marton26f72a92018-07-12 09:42:05 +00002082 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002083}
2084
Balazs Keri3b30d652018-10-19 13:32:20 +00002085ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2086 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2087 if (!DCOrErr)
2088 return DCOrErr.takeError();
2089 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002090 DeclContext *LexicalDC = DC;
2091
Balazs Keri3b30d652018-10-19 13:32:20 +00002092 SourceLocation ToLocation, ToRParenLoc;
2093 Expr *ToAssertExpr;
2094 StringLiteral *ToMessage;
2095 if (auto Imp = importSeq(
2096 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2097 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2098 else
2099 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002100
Gabor Marton26f72a92018-07-12 09:42:05 +00002101 StaticAssertDecl *ToD;
2102 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002103 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2104 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002105 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002106
2107 ToD->setLexicalDeclContext(LexicalDC);
2108 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002109 return ToD;
2110}
2111
Balazs Keri3b30d652018-10-19 13:32:20 +00002112ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002113 // Import the major distinguishing characteristics of this namespace.
2114 DeclContext *DC, *LexicalDC;
2115 DeclarationName Name;
2116 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002117 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002118 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2119 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002120 if (ToD)
2121 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002122
2123 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002124 if (!Name) {
2125 // This is an anonymous namespace. Adopt an existing anonymous
2126 // namespace if we can.
2127 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002128 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002129 MergeWithNamespace = TU->getAnonymousNamespace();
2130 else
2131 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2132 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002133 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002134 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002135 for (auto *FoundDecl : FoundDecls) {
2136 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002137 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002138
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002139 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002140 MergeWithNamespace = FoundNS;
2141 ConflictingDecls.clear();
2142 break;
2143 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002144
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002145 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002146 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002147
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002148 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002149 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002150 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002151 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002152 if (!Name)
2153 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002154 }
2155 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002156
Balazs Keri3b30d652018-10-19 13:32:20 +00002157 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2158 if (!BeginLocOrErr)
2159 return BeginLocOrErr.takeError();
2160
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002161 // Create the "to" namespace, if needed.
2162 NamespaceDecl *ToNamespace = MergeWithNamespace;
2163 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002164 if (GetImportedOrCreateDecl(
2165 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002166 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002167 /*PrevDecl=*/nullptr))
2168 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002169 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002170 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002171
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002172 // If this is an anonymous namespace, register it as the anonymous
2173 // namespace within its context.
2174 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002175 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002176 TU->setAnonymousNamespace(ToNamespace);
2177 else
2178 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2179 }
2180 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002181 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002182
Balazs Keri3b30d652018-10-19 13:32:20 +00002183 if (Error Err = ImportDeclContext(D))
2184 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002185
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002186 return ToNamespace;
2187}
2188
Balazs Keri3b30d652018-10-19 13:32:20 +00002189ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002190 // Import the major distinguishing characteristics of this namespace.
2191 DeclContext *DC, *LexicalDC;
2192 DeclarationName Name;
2193 SourceLocation Loc;
2194 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002195 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2196 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002197 if (LookupD)
2198 return LookupD;
2199
2200 // NOTE: No conflict resolution is done for namespace aliases now.
2201
Balazs Keri3b30d652018-10-19 13:32:20 +00002202 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2203 NestedNameSpecifierLoc ToQualifierLoc;
2204 NamespaceDecl *ToNamespace;
2205 if (auto Imp = importSeq(
2206 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2207 D->getTargetNameLoc(), D->getNamespace()))
2208 std::tie(
2209 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2210 ToNamespace) = *Imp;
2211 else
2212 return Imp.takeError();
2213 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002214
Gabor Marton26f72a92018-07-12 09:42:05 +00002215 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002216 if (GetImportedOrCreateDecl(
2217 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2218 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002219 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002220
2221 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002222 LexicalDC->addDeclInternal(ToD);
2223
2224 return ToD;
2225}
2226
Balazs Keri3b30d652018-10-19 13:32:20 +00002227ExpectedDecl
2228ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002229 // Import the major distinguishing characteristics of this typedef.
2230 DeclContext *DC, *LexicalDC;
2231 DeclarationName Name;
2232 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002233 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002234 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2235 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002236 if (ToD)
2237 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002238
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002239 // If this typedef is not in block scope, determine whether we've
2240 // seen a typedef with the same name (that we can merge with) or any
2241 // other entity by that name (which name lookup could conflict with).
2242 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002243 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002244 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002245 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002246 for (auto *FoundDecl : FoundDecls) {
2247 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002248 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002249 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002250 QualType FromUT = D->getUnderlyingType();
2251 QualType FoundUT = FoundTypedef->getUnderlyingType();
2252 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2253 // If the "From" context has a complete underlying type but we
2254 // already have a complete underlying type then return with that.
2255 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002256 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002257 }
Gabor Martondd2b76e2019-06-11 13:35:25 +00002258 // FIXME Handle redecl chain. When you do that make consistent changes
2259 // in ASTImporterLookupTable too.
Gabor Martonb93baf62018-11-27 09:51:36 +00002260 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002261 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002262
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002263 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002264 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002265
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002266 if (!ConflictingDecls.empty()) {
2267 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002268 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002269 ConflictingDecls.size());
2270 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002271 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002272 }
2273 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002274
Balazs Keri3b30d652018-10-19 13:32:20 +00002275 QualType ToUnderlyingType;
2276 TypeSourceInfo *ToTypeSourceInfo;
2277 SourceLocation ToBeginLoc;
2278 if (auto Imp = importSeq(
2279 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2280 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2281 else
2282 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002283
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002284 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002285 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002286 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002287 if (IsAlias) {
2288 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002289 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2290 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002291 return ToTypedef;
2292 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002293 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2294 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002295 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002296
Douglas Gregordd483172010-02-22 17:42:47 +00002297 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002298 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002299
2300 // Templated declarations should not appear in DeclContext.
2301 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2302 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2303 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002304
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002305 return ToTypedef;
2306}
2307
Balazs Keri3b30d652018-10-19 13:32:20 +00002308ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002309 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2310}
2311
Balazs Keri3b30d652018-10-19 13:32:20 +00002312ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002313 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2314}
2315
Balazs Keri3b30d652018-10-19 13:32:20 +00002316ExpectedDecl
2317ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002318 // Import the major distinguishing characteristics of this typedef.
2319 DeclContext *DC, *LexicalDC;
2320 DeclarationName Name;
2321 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002322 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002323 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2324 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002325 if (FoundD)
2326 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002327
2328 // If this typedef is not in block scope, determine whether we've
2329 // seen a typedef with the same name (that we can merge with) or any
2330 // other entity by that name (which name lookup could conflict with).
2331 if (!DC->isFunctionOrMethod()) {
2332 SmallVector<NamedDecl *, 4> ConflictingDecls;
2333 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002334 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002335 for (auto *FoundDecl : FoundDecls) {
2336 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002337 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002338 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002339 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002340 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002341 }
2342
2343 if (!ConflictingDecls.empty()) {
2344 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2345 ConflictingDecls.data(),
2346 ConflictingDecls.size());
2347 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002348 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002349 }
2350 }
2351
Balazs Keri3b30d652018-10-19 13:32:20 +00002352 TemplateParameterList *ToTemplateParameters;
2353 TypeAliasDecl *ToTemplatedDecl;
2354 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2355 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2356 else
2357 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002358
Gabor Marton26f72a92018-07-12 09:42:05 +00002359 TypeAliasTemplateDecl *ToAlias;
2360 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002361 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002362 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002363
Balazs Keri3b30d652018-10-19 13:32:20 +00002364 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002365
Gabor Horvath7a91c082017-11-14 11:30:38 +00002366 ToAlias->setAccess(D->getAccess());
2367 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002368 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002369 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002370}
2371
Balazs Keri3b30d652018-10-19 13:32:20 +00002372ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002373 // Import the major distinguishing characteristics of this label.
2374 DeclContext *DC, *LexicalDC;
2375 DeclarationName Name;
2376 SourceLocation Loc;
2377 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002378 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2379 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002380 if (ToD)
2381 return ToD;
2382
2383 assert(LexicalDC->isFunctionOrMethod());
2384
Gabor Marton26f72a92018-07-12 09:42:05 +00002385 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002386 if (D->isGnuLocal()) {
2387 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2388 if (!BeginLocOrErr)
2389 return BeginLocOrErr.takeError();
2390 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2391 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2392 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002393
Balazs Keri3b30d652018-10-19 13:32:20 +00002394 } else {
2395 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2396 Name.getAsIdentifierInfo()))
2397 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002398
Balazs Keri3b30d652018-10-19 13:32:20 +00002399 }
2400
2401 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2402 if (!ToStmtOrErr)
2403 return ToStmtOrErr.takeError();
2404
2405 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002406 ToLabel->setLexicalDeclContext(LexicalDC);
2407 LexicalDC->addDeclInternal(ToLabel);
2408 return ToLabel;
2409}
2410
Balazs Keri3b30d652018-10-19 13:32:20 +00002411ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002412 // Import the major distinguishing characteristics of this enum.
2413 DeclContext *DC, *LexicalDC;
2414 DeclarationName Name;
2415 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002416 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002417 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2418 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002419 if (ToD)
2420 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002421
Douglas Gregor98c10182010-02-12 22:17:39 +00002422 // Figure out what enum name we're looking for.
2423 unsigned IDNS = Decl::IDNS_Tag;
2424 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002425 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002426 if (Error Err = importInto(
2427 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2428 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002429 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002430 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002431 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002432
Douglas Gregor98c10182010-02-12 22:17:39 +00002433 // We may already have an enum of the same name; try to find and match it.
2434 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002435 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002436 auto FoundDecls =
2437 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002438 for (auto *FoundDecl : FoundDecls) {
2439 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002440 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002441
Balazs Keri3b30d652018-10-19 13:32:20 +00002442 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002443 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002444 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002445 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002446
Balazs Keri3b30d652018-10-19 13:32:20 +00002447 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002448 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002449 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002450 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002451
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002452 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002453 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002454
Douglas Gregor98c10182010-02-12 22:17:39 +00002455 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002456 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002457 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002458 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002459 if (!Name)
2460 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002461 }
2462 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002463
Balazs Keri3b30d652018-10-19 13:32:20 +00002464 SourceLocation ToBeginLoc;
2465 NestedNameSpecifierLoc ToQualifierLoc;
2466 QualType ToIntegerType;
2467 if (auto Imp = importSeq(
2468 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2469 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2470 else
2471 return Imp.takeError();
2472
Douglas Gregor98c10182010-02-12 22:17:39 +00002473 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002474 EnumDecl *D2;
2475 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002476 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002477 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2478 D->isScopedUsingClassTag(), D->isFixed()))
2479 return D2;
2480
Balazs Keri3b30d652018-10-19 13:32:20 +00002481 D2->setQualifierInfo(ToQualifierLoc);
2482 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002483 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002484 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002485 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002486
Douglas Gregor98c10182010-02-12 22:17:39 +00002487 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002488 if (D->isCompleteDefinition())
2489 if (Error Err = ImportDefinition(D, D2))
2490 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002491
Douglas Gregor3996e242010-02-15 22:01:00 +00002492 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002493}
2494
Balazs Keri3b30d652018-10-19 13:32:20 +00002495ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002496 bool IsFriendTemplate = false;
2497 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2498 IsFriendTemplate =
2499 DCXX->getDescribedClassTemplate() &&
2500 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2501 Decl::FOK_None;
2502 }
2503
Douglas Gregor5c73e912010-02-11 00:48:18 +00002504 // Import the major distinguishing characteristics of this record.
2505 DeclContext *DC, *LexicalDC;
2506 DeclarationName Name;
2507 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002508 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002509 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2510 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002511 if (ToD)
2512 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002513
Douglas Gregor5c73e912010-02-11 00:48:18 +00002514 // Figure out what structure name we're looking for.
2515 unsigned IDNS = Decl::IDNS_Tag;
2516 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002517 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002518 if (Error Err = importInto(
2519 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2520 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002521 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002522 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002523 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002524
2525 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002526 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002527 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002528 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002529 auto FoundDecls =
2530 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002531 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002532 // We're going to have to compare D against potentially conflicting Decls,
2533 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002534 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2535 D->getASTContext().getExternalSource()->CompleteType(D);
2536 }
2537
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002538 for (auto *FoundDecl : FoundDecls) {
2539 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002540 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002541
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002542 Decl *Found = FoundDecl;
2543 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2544 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002545 Found = Tag->getDecl();
2546 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002547
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002548 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002549 // Do not emit false positive diagnostic in case of unnamed
2550 // struct/union and in case of anonymous structs. Would be false
2551 // because there may be several anonymous/unnamed structs in a class.
2552 // E.g. these are both valid:
2553 // struct A { // unnamed structs
2554 // struct { struct A *next; } entry0;
2555 // struct { struct A *next; } entry1;
2556 // };
2557 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2558 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002559 if (!IsStructuralMatch(D, FoundRecord, false))
2560 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002561
Balazs Keric8272192019-05-27 09:36:00 +00002562 if (!hasSameVisibilityContext(FoundRecord, D))
2563 continue;
2564
Gabor Marton7df342a2018-12-17 12:42:12 +00002565 if (IsStructuralMatch(D, FoundRecord)) {
2566 RecordDecl *FoundDef = FoundRecord->getDefinition();
2567 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002568 // FIXME: Structural equivalence check should check for same
2569 // user-defined methods.
2570 Importer.MapImported(D, FoundDef);
2571 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2572 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2573 assert(FoundCXX && "Record type mismatch");
2574
Gabor Marton7df342a2018-12-17 12:42:12 +00002575 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002576 // FoundDef may not have every implicit method that D has
2577 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002578 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2579 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002580 }
Douglas Gregor25791052010-02-12 00:09:27 +00002581 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002582 PrevDecl = FoundRecord->getMostRecentDecl();
2583 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002584 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002585 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002586
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002587 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002588 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002589
Douglas Gregordd6006f2012-07-17 21:16:27 +00002590 if (!ConflictingDecls.empty() && SearchName) {
Shafik Yaghmour468724e2019-05-24 16:53:44 +00002591 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002592 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002593 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002594 if (!Name)
2595 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002596 }
2597 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002598
Balazs Keri3b30d652018-10-19 13:32:20 +00002599 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2600 if (!BeginLocOrErr)
2601 return BeginLocOrErr.takeError();
2602
Douglas Gregor5c73e912010-02-11 00:48:18 +00002603 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002604 RecordDecl *D2 = nullptr;
2605 CXXRecordDecl *D2CXX = nullptr;
2606 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2607 if (DCXX->isLambda()) {
2608 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2609 if (!TInfoOrErr)
2610 return TInfoOrErr.takeError();
2611 if (GetImportedOrCreateSpecialDecl(
2612 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2613 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2614 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2615 return D2CXX;
2616 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2617 if (!CDeclOrErr)
2618 return CDeclOrErr.takeError();
2619 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2620 } else if (DCXX->isInjectedClassName()) {
2621 // We have to be careful to do a similar dance to the one in
2622 // Sema::ActOnStartCXXMemberDeclarations
2623 const bool DelayTypeCreation = true;
2624 if (GetImportedOrCreateDecl(
2625 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2626 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2627 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2628 return D2CXX;
2629 Importer.getToContext().getTypeDeclType(
2630 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2631 } else {
2632 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2633 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2634 Name.getAsIdentifierInfo(),
2635 cast_or_null<CXXRecordDecl>(PrevDecl)))
2636 return D2CXX;
2637 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002638
Gabor Marton7df342a2018-12-17 12:42:12 +00002639 D2 = D2CXX;
2640 D2->setAccess(D->getAccess());
2641 D2->setLexicalDeclContext(LexicalDC);
2642 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2643 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002644
Gabor Marton7df342a2018-12-17 12:42:12 +00002645 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2646 DC->makeDeclVisibleInContext(D2);
2647
2648 if (ClassTemplateDecl *FromDescribed =
2649 DCXX->getDescribedClassTemplate()) {
2650 ClassTemplateDecl *ToDescribed;
2651 if (Error Err = importInto(ToDescribed, FromDescribed))
2652 return std::move(Err);
2653 D2CXX->setDescribedClassTemplate(ToDescribed);
2654 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2655 // In a record describing a template the type should be an
2656 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2657 // previously set type to the correct value here (ToDescribed is not
2658 // available at record create).
2659 // FIXME: The previous type is cleared but not removed from
2660 // ASTContext's internal storage.
2661 CXXRecordDecl *Injected = nullptr;
2662 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2663 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2664 if (Record && Record->isInjectedClassName()) {
2665 Injected = Record;
2666 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002667 }
2668 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002669 // Create an injected type for the whole redecl chain.
2670 SmallVector<Decl *, 2> Redecls =
2671 getCanonicalForwardRedeclChain(D2CXX);
2672 for (auto *R : Redecls) {
2673 auto *RI = cast<CXXRecordDecl>(R);
2674 RI->setTypeForDecl(nullptr);
2675 // Below we create a new injected type and assign that to the
2676 // canonical decl, subsequent declarations in the chain will reuse
2677 // that type.
2678 Importer.getToContext().getInjectedClassNameType(
2679 RI, ToDescribed->getInjectedClassNameSpecialization());
2680 }
2681 // Set the new type for the previous injected decl too.
2682 if (Injected) {
2683 Injected->setTypeForDecl(nullptr);
2684 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2685 }
2686 }
2687 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002688 DCXX->getMemberSpecializationInfo()) {
2689 TemplateSpecializationKind SK =
2690 MemberInfo->getTemplateSpecializationKind();
2691 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002692
2693 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2694 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2695 else
2696 return ToInstOrErr.takeError();
2697
2698 if (ExpectedSLoc POIOrErr =
2699 import(MemberInfo->getPointOfInstantiation()))
2700 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2701 *POIOrErr);
2702 else
2703 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002704 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002705
Gabor Marton7df342a2018-12-17 12:42:12 +00002706 } else {
2707 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2708 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2709 Name.getAsIdentifierInfo(), PrevDecl))
2710 return D2;
2711 D2->setLexicalDeclContext(LexicalDC);
2712 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002713 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002714
Gabor Marton7df342a2018-12-17 12:42:12 +00002715 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2716 D2->setQualifierInfo(*QualifierLocOrErr);
2717 else
2718 return QualifierLocOrErr.takeError();
2719
2720 if (D->isAnonymousStructOrUnion())
2721 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002722
Balazs Keri3b30d652018-10-19 13:32:20 +00002723 if (D->isCompleteDefinition())
2724 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2725 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002726
Douglas Gregor3996e242010-02-15 22:01:00 +00002727 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002728}
2729
Balazs Keri3b30d652018-10-19 13:32:20 +00002730ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002731 // Import the major distinguishing characteristics of this enumerator.
2732 DeclContext *DC, *LexicalDC;
2733 DeclarationName Name;
2734 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002735 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002736 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2737 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002738 if (ToD)
2739 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002740
Fangrui Song6907ce22018-07-30 19:24:48 +00002741 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002742 // in the same context.
2743 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002744 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002745 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002746 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002747 for (auto *FoundDecl : FoundDecls) {
2748 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002749 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002750
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002751 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002752 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002753 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002754 }
2755
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002756 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002757 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002758
Douglas Gregor98c10182010-02-12 22:17:39 +00002759 if (!ConflictingDecls.empty()) {
2760 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002761 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002762 ConflictingDecls.size());
2763 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002764 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002765 }
2766 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002767
Balazs Keri3b30d652018-10-19 13:32:20 +00002768 ExpectedType TypeOrErr = import(D->getType());
2769 if (!TypeOrErr)
2770 return TypeOrErr.takeError();
2771
2772 ExpectedExpr InitOrErr = import(D->getInitExpr());
2773 if (!InitOrErr)
2774 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002775
Gabor Marton26f72a92018-07-12 09:42:05 +00002776 EnumConstantDecl *ToEnumerator;
2777 if (GetImportedOrCreateDecl(
2778 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002779 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002780 return ToEnumerator;
2781
Douglas Gregordd483172010-02-22 17:42:47 +00002782 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002783 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002784 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002785 return ToEnumerator;
2786}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002787
Balazs Keri1efc9742019-05-07 10:55:11 +00002788Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2789 DeclaratorDecl *ToD) {
2790 unsigned int Num = FromD->getNumTemplateParameterLists();
2791 if (Num == 0)
2792 return Error::success();
2793 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2794 for (unsigned int I = 0; I < Num; ++I)
2795 if (Expected<TemplateParameterList *> ToTPListOrErr =
2796 import(FromD->getTemplateParameterList(I)))
2797 ToTPLists[I] = *ToTPListOrErr;
2798 else
2799 return ToTPListOrErr.takeError();
2800 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2801 return Error::success();
2802}
2803
Balazs Keri3b30d652018-10-19 13:32:20 +00002804Error ASTNodeImporter::ImportTemplateInformation(
2805 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002806 switch (FromFD->getTemplatedKind()) {
2807 case FunctionDecl::TK_NonTemplate:
2808 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002809 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002810
2811 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002812 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002813
2814 if (Expected<FunctionDecl *> InstFDOrErr =
2815 import(FromFD->getInstantiatedFromMemberFunction()))
2816 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2817 else
2818 return InstFDOrErr.takeError();
2819
2820 if (ExpectedSLoc POIOrErr = import(
2821 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2822 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2823 else
2824 return POIOrErr.takeError();
2825
2826 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002827 }
2828
2829 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002830 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002831 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002832 if (!FunctionAndArgsOrErr)
2833 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002834
2835 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002836 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002837
Gabor Marton5254e642018-06-27 13:32:50 +00002838 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002839 TemplateArgumentListInfo ToTAInfo;
2840 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002841 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002842 if (Error Err = ImportTemplateArgumentListInfo(
2843 *FromTAArgsAsWritten, ToTAInfo))
2844 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002845
Balazs Keri3b30d652018-10-19 13:32:20 +00002846 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2847 if (!POIOrErr)
2848 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002849
Balazs Keri1efc9742019-05-07 10:55:11 +00002850 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
2851 return Err;
2852
Gabor Marton5254e642018-06-27 13:32:50 +00002853 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002854 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002855 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2856 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2857 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002858 }
2859
2860 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2861 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2862 UnresolvedSet<8> TemplDecls;
2863 unsigned NumTemplates = FromInfo->getNumTemplates();
2864 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002865 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2866 import(FromInfo->getTemplate(I)))
2867 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002868 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002869 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002870 }
2871
2872 // Import TemplateArgumentListInfo.
2873 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002874 if (Error Err = ImportTemplateArgumentListInfo(
2875 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2876 llvm::makeArrayRef(
2877 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2878 ToTAInfo))
2879 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002880
2881 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2882 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002883 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002884 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002885 }
Sam McCallfdc32072018-01-26 12:06:44 +00002886 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002887}
2888
Balazs Keri3b30d652018-10-19 13:32:20 +00002889Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002890ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002891 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002892 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002893 if (!FunctionAndArgsOrErr)
2894 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002895
Balazs Keri3b30d652018-10-19 13:32:20 +00002896 FunctionTemplateDecl *Template;
2897 TemplateArgsTy ToTemplArgs;
2898 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002899 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002900 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002901 return FoundSpec;
2902}
2903
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002904Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2905 FunctionDecl *ToFD) {
2906 if (Stmt *FromBody = FromFD->getBody()) {
2907 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2908 ToFD->setBody(*ToBodyOrErr);
2909 else
2910 return ToBodyOrErr.takeError();
2911 }
2912 return Error::success();
2913}
2914
Gabor Marton458d1452019-02-14 13:07:03 +00002915template <typename T>
2916bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2917 if (From->hasExternalFormalLinkage())
2918 return Found->hasExternalFormalLinkage();
2919 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2920 return false;
2921 if (From->isInAnonymousNamespace())
2922 return Found->isInAnonymousNamespace();
2923 else
2924 return !Found->isInAnonymousNamespace() &&
2925 !Found->hasExternalFormalLinkage();
2926}
2927
Balazs Keri3b30d652018-10-19 13:32:20 +00002928ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002929
Balazs Keri3b30d652018-10-19 13:32:20 +00002930 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002931 auto RedeclIt = Redecls.begin();
2932 // Import the first part of the decl chain. I.e. import all previous
2933 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002934 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2935 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2936 if (!ToRedeclOrErr)
2937 return ToRedeclOrErr.takeError();
2938 }
Gabor Marton5254e642018-06-27 13:32:50 +00002939 assert(*RedeclIt == D);
2940
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002941 // Import the major distinguishing characteristics of this function.
2942 DeclContext *DC, *LexicalDC;
2943 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002944 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002945 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002946 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2947 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002948 if (ToD)
2949 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002950
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002951 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002952 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002953
Gabor Marton5254e642018-06-27 13:32:50 +00002954 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002955 // existing specialization in the "to" context. The lookup below will not
2956 // find any specialization, but would find the primary template; thus, we
2957 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00002958 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2959 if (D->getTemplatedKind() ==
2960 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002961 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
2962 if (!FoundFunctionOrErr)
2963 return FoundFunctionOrErr.takeError();
2964 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00002965 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2966 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00002967 FoundByLookup = FoundFunction;
2968 }
2969 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002970 // Try to find a function in our own ("to") context with the same name, same
2971 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002972 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002973 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002974 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00002975 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002976 for (auto *FoundDecl : FoundDecls) {
2977 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002978 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002979
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002980 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00002981 if (!hasSameVisibilityContext(FoundFunction, D))
2982 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002983
Gabor Marton458d1452019-02-14 13:07:03 +00002984 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00002985 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2986 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00002987 FoundByLookup = FoundFunction;
2988 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002989 }
Gabor Marton458d1452019-02-14 13:07:03 +00002990 // FIXME: Check for overloading more carefully, e.g., by boosting
2991 // Sema::IsOverload out to the AST library.
2992
2993 // Function overloading is okay in C++.
2994 if (Importer.getToContext().getLangOpts().CPlusPlus)
2995 continue;
2996
2997 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00002998 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00002999 << Name << D->getType() << FoundFunction->getType();
3000 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
3001 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003002 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003003
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003004 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003005 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003006
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003007 if (!ConflictingDecls.empty()) {
3008 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003009 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003010 ConflictingDecls.size());
3011 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003012 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003013 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003014 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003015
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003016 // We do not allow more than one in-class declaration of a function. This is
3017 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3018 // assumes there is only one in-class declaration. Building a redecl
3019 // chain would result in more than one in-class declaration for
3020 // overrides (even if they are part of the same redecl chain inside the
3021 // derived class.)
3022 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003023 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003024 if (D->getLexicalDeclContext() == D->getDeclContext()) {
3025 if (!D->doesThisDeclarationHaveABody())
3026 return Importer.MapImported(D, FoundByLookup);
3027 else {
3028 // Let's continue and build up the redecl chain in this case.
3029 // FIXME Merge the functions into one decl.
3030 }
3031 }
3032 }
3033 }
3034
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003035 DeclarationNameInfo NameInfo(Name, Loc);
3036 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003037 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3038 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003039
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003040 QualType FromTy = D->getType();
3041 bool usedDifferentExceptionSpec = false;
3042
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003043 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003044 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3045 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3046 // FunctionDecl that we are importing the FunctionProtoType for.
3047 // To avoid an infinite recursion when importing, create the FunctionDecl
3048 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003049 if (FromEPI.ExceptionSpec.SourceDecl ||
3050 FromEPI.ExceptionSpec.SourceTemplate ||
3051 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003052 FunctionProtoType::ExtProtoInfo DefaultEPI;
3053 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003054 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003055 usedDifferentExceptionSpec = true;
3056 }
3057 }
3058
Balazs Keri3b30d652018-10-19 13:32:20 +00003059 QualType T;
3060 TypeSourceInfo *TInfo;
3061 SourceLocation ToInnerLocStart, ToEndLoc;
3062 NestedNameSpecifierLoc ToQualifierLoc;
3063 if (auto Imp = importSeq(
3064 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3065 D->getQualifierLoc(), D->getEndLoc()))
3066 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3067 else
3068 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003069
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003070 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003071 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003072 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003073 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3074 Parameters.push_back(*ToPOrErr);
3075 else
3076 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003077 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003078
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003079 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003080 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003081 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003082 Expr *ExplicitExpr = nullptr;
3083 if (FromConstructor->getExplicitSpecifier().getExpr()) {
3084 auto Imp = importSeq(FromConstructor->getExplicitSpecifier().getExpr());
3085 if (!Imp)
3086 return Imp.takeError();
3087 std::tie(ExplicitExpr) = *Imp;
3088 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003089 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Richard Smith76b90272019-05-09 03:59:21 +00003090 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3091 ToInnerLocStart, NameInfo, T, TInfo,
3092 ExplicitSpecifier(
3093 ExplicitExpr,
3094 FromConstructor->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003095 D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003096 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003097 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3098
3099 auto Imp =
3100 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3101 FromDtor->getOperatorDeleteThisArg());
3102
3103 if (!Imp)
3104 return Imp.takeError();
3105
3106 FunctionDecl *ToOperatorDelete;
3107 Expr *ToThisArg;
3108 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3109
Gabor Marton26f72a92018-07-12 09:42:05 +00003110 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003111 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3112 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3113 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003114 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003115
3116 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3117
3118 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003119 } else if (CXXConversionDecl *FromConversion =
3120 dyn_cast<CXXConversionDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003121 Expr *ExplicitExpr = nullptr;
3122 if (FromConversion->getExplicitSpecifier().getExpr()) {
3123 auto Imp = importSeq(FromConversion->getExplicitSpecifier().getExpr());
3124 if (!Imp)
3125 return Imp.takeError();
3126 std::tie(ExplicitExpr) = *Imp;
3127 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003128 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3129 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003130 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith76b90272019-05-09 03:59:21 +00003131 ExplicitSpecifier(ExplicitExpr,
3132 FromConversion->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003133 D->getConstexprKind(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003134 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003135 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003136 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3137 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003138 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003139 Method->isInlineSpecified(), D->getConstexprKind(),
3140 SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003141 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003142 } else {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003143 if (GetImportedOrCreateDecl(
3144 ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart,
3145 NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(),
3146 D->hasWrittenPrototype(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003147 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003148 }
John McCall3e11ebe2010-03-15 10:12:16 +00003149
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003150 // Connect the redecl chain.
3151 if (FoundByLookup) {
3152 auto *Recent = const_cast<FunctionDecl *>(
3153 FoundByLookup->getMostRecentDecl());
3154 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003155 // FIXME Probably we should merge exception specifications. E.g. In the
3156 // "To" context the existing function may have exception specification with
3157 // noexcept-unevaluated, while the newly imported function may have an
3158 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3159 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003160 }
3161
3162 // Import Ctor initializers.
3163 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3164 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3165 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3166 // Import first, then allocate memory and copy if there was no error.
3167 if (Error Err = ImportContainerChecked(
3168 FromConstructor->inits(), CtorInitializers))
3169 return std::move(Err);
3170 auto **Memory =
3171 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3172 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3173 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3174 ToCtor->setCtorInitializers(Memory);
3175 ToCtor->setNumCtorInitializers(NumInitializers);
3176 }
3177 }
3178
Balazs Keri3b30d652018-10-19 13:32:20 +00003179 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003180 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003181 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003182 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3183 ToFunction->setTrivial(D->isTrivial());
3184 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003185 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003186
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003187 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003188 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003189 Param->setOwningFunction(ToFunction);
3190 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003191 }
David Blaikie9c70e042011-09-21 18:16:56 +00003192 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003193
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003194 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3195 // params it refers to.
3196 if (TInfo) {
3197 if (auto ProtoLoc =
3198 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3199 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3200 ProtoLoc.setParam(I, Parameters[I]);
3201 }
3202 }
3203
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003204 if (usedDifferentExceptionSpec) {
3205 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003206 if (ExpectedType TyOrErr = import(D->getType()))
3207 ToFunction->setType(*TyOrErr);
3208 else
3209 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003210 }
3211
Balazs Keria35798d2018-07-17 09:52:41 +00003212 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003213 if (FromFT) {
3214 auto ToFTOrErr = import(FromFT);
3215 if (!ToFTOrErr)
3216 return ToFTOrErr.takeError();
3217 }
Balazs Keria35798d2018-07-17 09:52:41 +00003218
Gabor Marton5254e642018-06-27 13:32:50 +00003219 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003220 Error Err = ImportFunctionDeclBody(D, ToFunction);
3221
3222 if (Err)
3223 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003224 }
3225
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003226 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003227
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003228 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003229 if (Error Err = ImportTemplateInformation(D, ToFunction))
3230 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003231
Gabor Marton5254e642018-06-27 13:32:50 +00003232 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3233
3234 // TODO Can we generalize this approach to other AST nodes as well?
3235 if (D->getDeclContext()->containsDeclAndLoad(D))
3236 DC->addDeclInternal(ToFunction);
3237 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003238 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003239
Gabor Marton5254e642018-06-27 13:32:50 +00003240 // Friend declaration's lexical context is the befriending class, but the
3241 // semantic context is the enclosing scope of the befriending class.
3242 // We want the friend functions to be found in the semantic context by lookup.
3243 // FIXME should we handle this generically in VisitFriendDecl?
3244 // In Other cases when LexicalDC != DC we don't want it to be added,
3245 // e.g out-of-class definitions like void B::f() {} .
3246 if (LexicalDC != DC && IsFriend) {
3247 DC->makeDeclVisibleInContext(ToFunction);
3248 }
3249
Gabor Marton7a0841e2018-10-29 10:18:28 +00003250 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3251 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3252
Gabor Marton5254e642018-06-27 13:32:50 +00003253 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003254 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3255 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3256 if (!ToRedeclOrErr)
3257 return ToRedeclOrErr.takeError();
3258 }
Gabor Marton5254e642018-06-27 13:32:50 +00003259
Douglas Gregor43f54792010-02-17 02:12:47 +00003260 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003261}
3262
Balazs Keri3b30d652018-10-19 13:32:20 +00003263ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003264 return VisitFunctionDecl(D);
3265}
3266
Balazs Keri3b30d652018-10-19 13:32:20 +00003267ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003268 return VisitCXXMethodDecl(D);
3269}
3270
Balazs Keri3b30d652018-10-19 13:32:20 +00003271ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003272 return VisitCXXMethodDecl(D);
3273}
3274
Balazs Keri3b30d652018-10-19 13:32:20 +00003275ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003276 return VisitCXXMethodDecl(D);
3277}
3278
Balazs Keri3b30d652018-10-19 13:32:20 +00003279ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003280 // Import the major distinguishing characteristics of a variable.
3281 DeclContext *DC, *LexicalDC;
3282 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003283 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003284 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003285 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3286 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003287 if (ToD)
3288 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003289
Fangrui Song6907ce22018-07-30 19:24:48 +00003290 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003291 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003292 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003293 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003294 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003295 if (!Name &&
3296 ASTImporter::getFieldIndex(D) !=
3297 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003298 continue;
3299
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003300 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003301 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003302 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003303 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3304 // initializer of a FieldDecl might not had been instantiated in the
3305 // "To" context. However, the "From" context might instantiated that,
3306 // thus we have to merge that.
3307 if (Expr *FromInitializer = D->getInClassInitializer()) {
3308 // We don't have yet the initializer set.
3309 if (FoundField->hasInClassInitializer() &&
3310 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003311 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3312 FoundField->setInClassInitializer(*ToInitializerOrErr);
3313 else {
3314 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003315 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003316 // FIXME: warning message?
3317 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003318 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003319 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003320 }
3321 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003322 return FoundField;
3323 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003324
Balazs Keri3b30d652018-10-19 13:32:20 +00003325 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003326 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003327 << Name << D->getType() << FoundField->getType();
3328 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3329 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003330
3331 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003332 }
3333 }
3334
Balazs Keri3b30d652018-10-19 13:32:20 +00003335 QualType ToType;
3336 TypeSourceInfo *ToTInfo;
3337 Expr *ToBitWidth;
3338 SourceLocation ToInnerLocStart;
3339 Expr *ToInitializer;
3340 if (auto Imp = importSeq(
3341 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3342 D->getInnerLocStart(), D->getInClassInitializer()))
3343 std::tie(
3344 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3345 else
3346 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003347
Gabor Marton26f72a92018-07-12 09:42:05 +00003348 FieldDecl *ToField;
3349 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003350 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3351 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3352 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003353 return ToField;
3354
Douglas Gregordd483172010-02-22 17:42:47 +00003355 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003356 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003357 if (ToInitializer)
3358 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003359 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003360 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003361 return ToField;
3362}
3363
Balazs Keri3b30d652018-10-19 13:32:20 +00003364ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003365 // Import the major distinguishing characteristics of a variable.
3366 DeclContext *DC, *LexicalDC;
3367 DeclarationName Name;
3368 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003369 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003370 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3371 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003372 if (ToD)
3373 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003374
Fangrui Song6907ce22018-07-30 19:24:48 +00003375 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003376 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003377 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003378 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003379 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003380 if (!Name &&
3381 ASTImporter::getFieldIndex(D) !=
3382 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003383 continue;
3384
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003385 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003386 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003387 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003388 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003389 return FoundField;
3390 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003391
3392 // If there are more anonymous fields to check, continue.
3393 if (!Name && I < N-1)
3394 continue;
3395
Balazs Keri3b30d652018-10-19 13:32:20 +00003396 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003397 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003398 << Name << D->getType() << FoundField->getType();
3399 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3400 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003401
3402 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003403 }
3404 }
3405
Francois Pichet783dd6e2010-11-21 06:08:52 +00003406 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003407 auto TypeOrErr = import(D->getType());
3408 if (!TypeOrErr)
3409 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003410
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003411 auto **NamedChain =
3412 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003413
3414 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003415 for (auto *PI : D->chain())
3416 if (Expected<NamedDecl *> ToD = import(PI))
3417 NamedChain[i++] = *ToD;
3418 else
3419 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003420
Gabor Marton26f72a92018-07-12 09:42:05 +00003421 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3422 IndirectFieldDecl *ToIndirectField;
3423 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003424 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003425 // FIXME here we leak `NamedChain` which is allocated before
3426 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003427
Francois Pichet783dd6e2010-11-21 06:08:52 +00003428 ToIndirectField->setAccess(D->getAccess());
3429 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003430 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003431 return ToIndirectField;
3432}
3433
Balazs Keri3b30d652018-10-19 13:32:20 +00003434ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003435 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003436 DeclContext *DC, *LexicalDC;
3437 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3438 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003439
3440 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003441 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003442 auto *RD = cast<CXXRecordDecl>(DC);
3443 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003444
3445 while (ImportedFriend) {
3446 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003447 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3448 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003449 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003450
3451 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3452 if (Importer.IsStructurallyEquivalent(
3453 D->getFriendType()->getType(),
3454 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003455 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003456 }
3457 ImportedFriend = ImportedFriend->getNextFriend();
3458 }
3459
3460 // Not found. Create it.
3461 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003462 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003463 NamedDecl *ToFriendD;
3464 if (Error Err = importInto(ToFriendD, FriendD))
3465 return std::move(Err);
3466
3467 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003468 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3469 ToFriendD->setObjectOfFriendDecl(false);
3470
3471 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003472 } else { // The friend is a type, not a decl.
3473 if (auto TSIOrErr = import(D->getFriendType()))
3474 ToFU = *TSIOrErr;
3475 else
3476 return TSIOrErr.takeError();
3477 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003478
3479 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003480 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003481 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003482 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003483 ToTPLists[I] = *ListOrErr;
3484 else
3485 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003486 }
3487
Balazs Keri3b30d652018-10-19 13:32:20 +00003488 auto LocationOrErr = import(D->getLocation());
3489 if (!LocationOrErr)
3490 return LocationOrErr.takeError();
3491 auto FriendLocOrErr = import(D->getFriendLoc());
3492 if (!FriendLocOrErr)
3493 return FriendLocOrErr.takeError();
3494
Gabor Marton26f72a92018-07-12 09:42:05 +00003495 FriendDecl *FrD;
3496 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003497 *LocationOrErr, ToFU,
3498 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003499 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003500
3501 FrD->setAccess(D->getAccess());
3502 FrD->setLexicalDeclContext(LexicalDC);
3503 LexicalDC->addDeclInternal(FrD);
3504 return FrD;
3505}
3506
Balazs Keri3b30d652018-10-19 13:32:20 +00003507ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003508 // Import the major distinguishing characteristics of an ivar.
3509 DeclContext *DC, *LexicalDC;
3510 DeclarationName Name;
3511 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003512 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003513 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3514 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003515 if (ToD)
3516 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003517
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003518 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003519 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003520 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003521 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003522 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003523 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003524 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003525 return FoundIvar;
3526 }
3527
Gabor Marton410f32c2019-04-01 15:29:55 +00003528 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003529 << Name << D->getType() << FoundIvar->getType();
3530 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3531 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003532
3533 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003534 }
3535 }
3536
Balazs Keri3b30d652018-10-19 13:32:20 +00003537 QualType ToType;
3538 TypeSourceInfo *ToTypeSourceInfo;
3539 Expr *ToBitWidth;
3540 SourceLocation ToInnerLocStart;
3541 if (auto Imp = importSeq(
3542 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3543 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3544 else
3545 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003546
Gabor Marton26f72a92018-07-12 09:42:05 +00003547 ObjCIvarDecl *ToIvar;
3548 if (GetImportedOrCreateDecl(
3549 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003550 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3551 ToType, ToTypeSourceInfo,
3552 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003553 return ToIvar;
3554
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003555 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003556 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003557 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003558}
3559
Balazs Keri3b30d652018-10-19 13:32:20 +00003560ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003561
3562 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3563 auto RedeclIt = Redecls.begin();
3564 // Import the first part of the decl chain. I.e. import all previous
3565 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003566 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3567 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3568 if (!RedeclOrErr)
3569 return RedeclOrErr.takeError();
3570 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003571 assert(*RedeclIt == D);
3572
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003573 // Import the major distinguishing characteristics of a variable.
3574 DeclContext *DC, *LexicalDC;
3575 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003576 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003577 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003578 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3579 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003580 if (ToD)
3581 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003582
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003583 // Try to find a variable in our own ("to") context with the same name and
3584 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003585 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003586 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003587 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003588 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003589 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003590 for (auto *FoundDecl : FoundDecls) {
3591 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003592 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003593
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003594 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003595 if (!hasSameVisibilityContext(FoundVar, D))
3596 continue;
3597 if (Importer.IsStructurallyEquivalent(D->getType(),
3598 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003599
Gabor Marton458d1452019-02-14 13:07:03 +00003600 // The VarDecl in the "From" context has a definition, but in the
3601 // "To" context we already have a definition.
3602 VarDecl *FoundDef = FoundVar->getDefinition();
3603 if (D->isThisDeclarationADefinition() && FoundDef)
3604 // FIXME Check for ODR error if the two definitions have
3605 // different initializers?
3606 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003607
Gabor Marton458d1452019-02-14 13:07:03 +00003608 // The VarDecl in the "From" context has an initializer, but in the
3609 // "To" context we already have an initializer.
3610 const VarDecl *FoundDInit = nullptr;
3611 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3612 // FIXME Diagnose ODR error if the two initializers are different?
3613 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3614
3615 FoundByLookup = FoundVar;
3616 break;
3617 }
3618
3619 const ArrayType *FoundArray
3620 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3621 const ArrayType *TArray
3622 = Importer.getToContext().getAsArrayType(D->getType());
3623 if (FoundArray && TArray) {
3624 if (isa<IncompleteArrayType>(FoundArray) &&
3625 isa<ConstantArrayType>(TArray)) {
3626 // Import the type.
3627 if (auto TyOrErr = import(D->getType()))
3628 FoundVar->setType(*TyOrErr);
3629 else
3630 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003631
3632 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003633 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003634 } else if (isa<IncompleteArrayType>(TArray) &&
3635 isa<ConstantArrayType>(FoundArray)) {
3636 FoundByLookup = FoundVar;
3637 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003638 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003639 }
Gabor Marton458d1452019-02-14 13:07:03 +00003640
Gabor Marton410f32c2019-04-01 15:29:55 +00003641 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003642 << Name << D->getType() << FoundVar->getType();
3643 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3644 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003645 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003646
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003647 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003648 }
3649
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003650 if (!ConflictingDecls.empty()) {
3651 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003652 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003653 ConflictingDecls.size());
3654 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003655 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003656 }
3657 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003658
Balazs Keri3b30d652018-10-19 13:32:20 +00003659 QualType ToType;
3660 TypeSourceInfo *ToTypeSourceInfo;
3661 SourceLocation ToInnerLocStart;
3662 NestedNameSpecifierLoc ToQualifierLoc;
3663 if (auto Imp = importSeq(
3664 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3665 D->getQualifierLoc()))
3666 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3667 else
3668 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003669
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003670 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003671 VarDecl *ToVar;
3672 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003673 ToInnerLocStart, Loc,
3674 Name.getAsIdentifierInfo(),
3675 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003676 D->getStorageClass()))
3677 return ToVar;
3678
Balazs Keri3b30d652018-10-19 13:32:20 +00003679 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003680 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003681 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003682
Gabor Martonac3a5d62018-09-17 12:04:52 +00003683 if (FoundByLookup) {
3684 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3685 ToVar->setPreviousDecl(Recent);
3686 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003687
Balazs Keri3b30d652018-10-19 13:32:20 +00003688 if (Error Err = ImportInitializer(D, ToVar))
3689 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003690
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003691 if (D->isConstexpr())
3692 ToVar->setConstexpr(true);
3693
Gabor Martonac3a5d62018-09-17 12:04:52 +00003694 if (D->getDeclContext()->containsDeclAndLoad(D))
3695 DC->addDeclInternal(ToVar);
3696 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3697 LexicalDC->addDeclInternal(ToVar);
3698
3699 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003700 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3701 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3702 if (!RedeclOrErr)
3703 return RedeclOrErr.takeError();
3704 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003705
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003706 return ToVar;
3707}
3708
Balazs Keri3b30d652018-10-19 13:32:20 +00003709ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003710 // Parameters are created in the translation unit's context, then moved
3711 // into the function declaration's context afterward.
3712 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003713
Balazs Keri3b30d652018-10-19 13:32:20 +00003714 DeclarationName ToDeclName;
3715 SourceLocation ToLocation;
3716 QualType ToType;
3717 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3718 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3719 else
3720 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003721
Douglas Gregor8b228d72010-02-17 21:22:52 +00003722 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003723 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003724 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3725 ToLocation, ToDeclName.getAsIdentifierInfo(),
3726 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003727 return ToParm;
3728 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003729}
3730
Balazs Keri3b30d652018-10-19 13:32:20 +00003731ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003732 // Parameters are created in the translation unit's context, then moved
3733 // into the function declaration's context afterward.
3734 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003735
Balazs Keri3b30d652018-10-19 13:32:20 +00003736 DeclarationName ToDeclName;
3737 SourceLocation ToLocation, ToInnerLocStart;
3738 QualType ToType;
3739 TypeSourceInfo *ToTypeSourceInfo;
3740 if (auto Imp = importSeq(
3741 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3742 D->getTypeSourceInfo()))
3743 std::tie(
3744 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3745 ToTypeSourceInfo) = *Imp;
3746 else
3747 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003748
Gabor Marton26f72a92018-07-12 09:42:05 +00003749 ParmVarDecl *ToParm;
3750 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003751 ToInnerLocStart, ToLocation,
3752 ToDeclName.getAsIdentifierInfo(), ToType,
3753 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003754 /*DefaultArg*/ nullptr))
3755 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003756
3757 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003758 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003759 ToParm->setKNRPromoted(D->isKNRPromoted());
3760
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003761 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003762 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3763 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3764 else
3765 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003766 } else if (D->hasUnparsedDefaultArg()) {
3767 ToParm->setUnparsedDefaultArg();
3768 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003769 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3770 ToParm->setDefaultArg(*ToDefArgOrErr);
3771 else
3772 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003773 }
Sean Callanan59721b32015-04-28 18:41:46 +00003774
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003775 if (D->isObjCMethodParameter()) {
3776 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3777 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3778 } else {
3779 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3780 D->getFunctionScopeIndex());
3781 }
3782
Gabor Marton26f72a92018-07-12 09:42:05 +00003783 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003784}
3785
Balazs Keri3b30d652018-10-19 13:32:20 +00003786ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003787 // Import the major distinguishing characteristics of a method.
3788 DeclContext *DC, *LexicalDC;
3789 DeclarationName Name;
3790 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003791 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003792 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3793 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003794 if (ToD)
3795 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003796
Gabor Marton54058b52018-12-17 13:53:12 +00003797 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003798 for (auto *FoundDecl : FoundDecls) {
3799 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003800 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3801 continue;
3802
3803 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003804 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3805 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003806 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003807 << D->isInstanceMethod() << Name << D->getReturnType()
3808 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003809 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003810 diag::note_odr_objc_method_here)
3811 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003812
3813 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003814 }
3815
3816 // Check the number of parameters.
3817 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003818 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003819 << D->isInstanceMethod() << Name
3820 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003821 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003822 diag::note_odr_objc_method_here)
3823 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003824
3825 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003826 }
3827
3828 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003829 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003830 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3831 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003832 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003833 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003834 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003835 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003836 << D->isInstanceMethod() << Name
3837 << (*P)->getType() << (*FoundP)->getType();
3838 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3839 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003840
3841 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003842 }
3843 }
3844
3845 // Check variadic/non-variadic.
3846 // Check the number of parameters.
3847 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003848 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003849 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003850 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003851 diag::note_odr_objc_method_here)
3852 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003853
3854 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003855 }
3856
3857 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003858 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003859 }
3860 }
3861
Balazs Keri3b30d652018-10-19 13:32:20 +00003862 SourceLocation ToEndLoc;
3863 QualType ToReturnType;
3864 TypeSourceInfo *ToReturnTypeSourceInfo;
3865 if (auto Imp = importSeq(
3866 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3867 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3868 else
3869 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003870
Gabor Marton26f72a92018-07-12 09:42:05 +00003871 ObjCMethodDecl *ToMethod;
3872 if (GetImportedOrCreateDecl(
3873 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003874 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3875 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003876 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3877 D->getImplementationControl(), D->hasRelatedResultType()))
3878 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003879
3880 // FIXME: When we decide to merge method definitions, we'll need to
3881 // deal with implicit parameters.
3882
3883 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003884 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003885 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003886 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3887 ToParams.push_back(*ToPOrErr);
3888 else
3889 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003890 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003891
Douglas Gregor43f54792010-02-17 02:12:47 +00003892 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003893 for (auto *ToParam : ToParams) {
3894 ToParam->setOwningFunction(ToMethod);
3895 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003896 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003897
Balazs Keri3b30d652018-10-19 13:32:20 +00003898 SmallVector<SourceLocation, 12> FromSelLocs;
3899 D->getSelectorLocs(FromSelLocs);
3900 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3901 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3902 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003903
Balazs Keri3b30d652018-10-19 13:32:20 +00003904 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003905
3906 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003907 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003908 return ToMethod;
3909}
3910
Balazs Keri3b30d652018-10-19 13:32:20 +00003911ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003912 // Import the major distinguishing characteristics of a category.
3913 DeclContext *DC, *LexicalDC;
3914 DeclarationName Name;
3915 SourceLocation Loc;
3916 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003917 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3918 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003919 if (ToD)
3920 return ToD;
3921
Balazs Keri3b30d652018-10-19 13:32:20 +00003922 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3923 TypeSourceInfo *ToTypeSourceInfo;
3924 if (auto Imp = importSeq(
3925 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3926 D->getTypeSourceInfo()))
3927 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3928 else
3929 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003930
Gabor Marton26f72a92018-07-12 09:42:05 +00003931 ObjCTypeParamDecl *Result;
3932 if (GetImportedOrCreateDecl(
3933 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003934 ToVarianceLoc, D->getIndex(),
3935 ToLocation, Name.getAsIdentifierInfo(),
3936 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003937 return Result;
3938
Douglas Gregor85f3f952015-07-07 03:57:15 +00003939 Result->setLexicalDeclContext(LexicalDC);
3940 return Result;
3941}
3942
Balazs Keri3b30d652018-10-19 13:32:20 +00003943ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003944 // Import the major distinguishing characteristics of a category.
3945 DeclContext *DC, *LexicalDC;
3946 DeclarationName Name;
3947 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003948 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003949 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3950 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003951 if (ToD)
3952 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003953
Balazs Keri3b30d652018-10-19 13:32:20 +00003954 ObjCInterfaceDecl *ToInterface;
3955 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3956 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003957
Douglas Gregor84c51c32010-02-18 01:47:50 +00003958 // Determine if we've already encountered this category.
3959 ObjCCategoryDecl *MergeWithCategory
3960 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3961 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3962 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003963 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3964 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3965 if (auto Imp = importSeq(
3966 D->getAtStartLoc(), D->getCategoryNameLoc(),
3967 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3968 std::tie(
3969 ToAtStartLoc, ToCategoryNameLoc,
3970 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3971 else
3972 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003973
3974 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003975 ToAtStartLoc, Loc,
3976 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003977 Name.getAsIdentifierInfo(), ToInterface,
3978 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00003979 ToIvarLBraceLoc,
3980 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00003981 return ToCategory;
3982
Douglas Gregor84c51c32010-02-18 01:47:50 +00003983 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003984 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00003985 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003986 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00003987 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
3988 ToCategory->setTypeParamList(*PListOrErr);
3989 else
3990 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003991
Douglas Gregor84c51c32010-02-18 01:47:50 +00003992 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003993 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3994 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003995 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3996 = D->protocol_loc_begin();
3997 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3998 FromProtoEnd = D->protocol_end();
3999 FromProto != FromProtoEnd;
4000 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004001 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4002 Protocols.push_back(*ToProtoOrErr);
4003 else
4004 return ToProtoOrErr.takeError();
4005
4006 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4007 ProtocolLocs.push_back(*ToProtoLocOrErr);
4008 else
4009 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004010 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004011
Douglas Gregor84c51c32010-02-18 01:47:50 +00004012 // FIXME: If we're merging, make sure that the protocol list is the same.
4013 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4014 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004015
Douglas Gregor84c51c32010-02-18 01:47:50 +00004016 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004017 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004018 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004019
Douglas Gregor84c51c32010-02-18 01:47:50 +00004020 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004021 if (Error Err = ImportDeclContext(D))
4022 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004023
Douglas Gregor84c51c32010-02-18 01:47:50 +00004024 // If we have an implementation, import it as well.
4025 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004026 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4027 import(D->getImplementation()))
4028 ToCategory->setImplementation(*ToImplOrErr);
4029 else
4030 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004031 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004032
Douglas Gregor84c51c32010-02-18 01:47:50 +00004033 return ToCategory;
4034}
4035
Balazs Keri3b30d652018-10-19 13:32:20 +00004036Error ASTNodeImporter::ImportDefinition(
4037 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004038 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004039 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004040 if (Error Err = ImportDeclContext(From))
4041 return Err;
4042 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004043 }
4044
4045 // Start the protocol definition
4046 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004047
Douglas Gregor2aa53772012-01-24 17:42:07 +00004048 // Import protocols
4049 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4050 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004051 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4052 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004053 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4054 FromProtoEnd = From->protocol_end();
4055 FromProto != FromProtoEnd;
4056 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004057 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4058 Protocols.push_back(*ToProtoOrErr);
4059 else
4060 return ToProtoOrErr.takeError();
4061
4062 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4063 ProtocolLocs.push_back(*ToProtoLocOrErr);
4064 else
4065 return ToProtoLocOrErr.takeError();
4066
Douglas Gregor2aa53772012-01-24 17:42:07 +00004067 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004068
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 // FIXME: If we're merging, make sure that the protocol list is the same.
4070 To->setProtocolList(Protocols.data(), Protocols.size(),
4071 ProtocolLocs.data(), Importer.getToContext());
4072
Douglas Gregor2e15c842012-02-01 21:00:38 +00004073 if (shouldForceImportDeclContext(Kind)) {
4074 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004075 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4076 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004077 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004078 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004079}
4080
Balazs Keri3b30d652018-10-19 13:32:20 +00004081ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004082 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004083 // from, but this particular declaration is not that definition, import the
4084 // definition and map to that.
4085 ObjCProtocolDecl *Definition = D->getDefinition();
4086 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004087 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4088 return Importer.MapImported(D, *ImportedDefOrErr);
4089 else
4090 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004091 }
4092
Douglas Gregor84c51c32010-02-18 01:47:50 +00004093 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004094 DeclContext *DC, *LexicalDC;
4095 DeclarationName Name;
4096 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004097 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004098 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4099 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004100 if (ToD)
4101 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004102
Craig Topper36250ad2014-05-12 05:36:57 +00004103 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004104 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004105 for (auto *FoundDecl : FoundDecls) {
4106 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004107 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004108
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004109 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004110 break;
4111 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004112
Douglas Gregor98d156a2010-02-17 16:12:00 +00004113 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004114 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004115 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4116 if (!ToAtBeginLocOrErr)
4117 return ToAtBeginLocOrErr.takeError();
4118
Gabor Marton26f72a92018-07-12 09:42:05 +00004119 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4120 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004121 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004122 /*PrevDecl=*/nullptr))
4123 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004124 ToProto->setLexicalDeclContext(LexicalDC);
4125 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004126 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004127
4128 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004129
Balazs Keri3b30d652018-10-19 13:32:20 +00004130 if (D->isThisDeclarationADefinition())
4131 if (Error Err = ImportDefinition(D, ToProto))
4132 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004133
Douglas Gregor98d156a2010-02-17 16:12:00 +00004134 return ToProto;
4135}
4136
Balazs Keri3b30d652018-10-19 13:32:20 +00004137ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4138 DeclContext *DC, *LexicalDC;
4139 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4140 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004141
Balazs Keri3b30d652018-10-19 13:32:20 +00004142 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4143 if (!ExternLocOrErr)
4144 return ExternLocOrErr.takeError();
4145
4146 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4147 if (!LangLocOrErr)
4148 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004149
4150 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004151
4152 LinkageSpecDecl *ToLinkageSpec;
4153 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004154 *ExternLocOrErr, *LangLocOrErr,
4155 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004156 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004157
4158 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004159 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4160 if (!RBraceLocOrErr)
4161 return RBraceLocOrErr.takeError();
4162 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004163 }
4164
4165 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4166 LexicalDC->addDeclInternal(ToLinkageSpec);
4167
Sean Callanan0aae0412014-12-10 00:00:37 +00004168 return ToLinkageSpec;
4169}
4170
Balazs Keri3b30d652018-10-19 13:32:20 +00004171ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004172 DeclContext *DC, *LexicalDC;
4173 DeclarationName Name;
4174 SourceLocation Loc;
4175 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004176 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4177 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004178 if (ToD)
4179 return ToD;
4180
Balazs Keri3b30d652018-10-19 13:32:20 +00004181 SourceLocation ToLoc, ToUsingLoc;
4182 NestedNameSpecifierLoc ToQualifierLoc;
4183 if (auto Imp = importSeq(
4184 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4185 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4186 else
4187 return Imp.takeError();
4188
4189 DeclarationNameInfo NameInfo(Name, ToLoc);
4190 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4191 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004192
Gabor Marton26f72a92018-07-12 09:42:05 +00004193 UsingDecl *ToUsing;
4194 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004195 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004196 D->hasTypename()))
4197 return ToUsing;
4198
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004199 ToUsing->setLexicalDeclContext(LexicalDC);
4200 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004201
4202 if (NamedDecl *FromPattern =
4203 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004204 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4205 Importer.getToContext().setInstantiatedFromUsingDecl(
4206 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004207 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004208 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004209 }
4210
Balazs Keri3b30d652018-10-19 13:32:20 +00004211 for (UsingShadowDecl *FromShadow : D->shadows()) {
4212 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4213 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004214 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004215 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004216 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004217 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004218 }
4219 return ToUsing;
4220}
4221
Balazs Keri3b30d652018-10-19 13:32:20 +00004222ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004223 DeclContext *DC, *LexicalDC;
4224 DeclarationName Name;
4225 SourceLocation Loc;
4226 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004227 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4228 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004229 if (ToD)
4230 return ToD;
4231
Balazs Keri3b30d652018-10-19 13:32:20 +00004232 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4233 if (!ToUsingOrErr)
4234 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004235
Balazs Keri3b30d652018-10-19 13:32:20 +00004236 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4237 if (!ToTargetOrErr)
4238 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004239
Gabor Marton26f72a92018-07-12 09:42:05 +00004240 UsingShadowDecl *ToShadow;
4241 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004242 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004243 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004244
4245 ToShadow->setLexicalDeclContext(LexicalDC);
4246 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004247
4248 if (UsingShadowDecl *FromPattern =
4249 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004250 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4251 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4252 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004253 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004254 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004255 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004256 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004257 }
4258
4259 LexicalDC->addDeclInternal(ToShadow);
4260
4261 return ToShadow;
4262}
4263
Balazs Keri3b30d652018-10-19 13:32:20 +00004264ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004265 DeclContext *DC, *LexicalDC;
4266 DeclarationName Name;
4267 SourceLocation Loc;
4268 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004269 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4270 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004271 if (ToD)
4272 return ToD;
4273
Balazs Keri3b30d652018-10-19 13:32:20 +00004274 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4275 if (!ToComAncestorOrErr)
4276 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004277
Balazs Keri3b30d652018-10-19 13:32:20 +00004278 NamespaceDecl *ToNominatedNamespace;
4279 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4280 NestedNameSpecifierLoc ToQualifierLoc;
4281 if (auto Imp = importSeq(
4282 D->getNominatedNamespace(), D->getUsingLoc(),
4283 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4284 D->getIdentLocation()))
4285 std::tie(
4286 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4287 ToQualifierLoc, ToIdentLocation) = *Imp;
4288 else
4289 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004290
Gabor Marton26f72a92018-07-12 09:42:05 +00004291 UsingDirectiveDecl *ToUsingDir;
4292 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004293 ToUsingLoc,
4294 ToNamespaceKeyLocation,
4295 ToQualifierLoc,
4296 ToIdentLocation,
4297 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004298 return ToUsingDir;
4299
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004300 ToUsingDir->setLexicalDeclContext(LexicalDC);
4301 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004302
4303 return ToUsingDir;
4304}
4305
Balazs Keri3b30d652018-10-19 13:32:20 +00004306ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004307 UnresolvedUsingValueDecl *D) {
4308 DeclContext *DC, *LexicalDC;
4309 DeclarationName Name;
4310 SourceLocation Loc;
4311 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004312 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4313 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004314 if (ToD)
4315 return ToD;
4316
Balazs Keri3b30d652018-10-19 13:32:20 +00004317 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4318 NestedNameSpecifierLoc ToQualifierLoc;
4319 if (auto Imp = importSeq(
4320 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4321 D->getEllipsisLoc()))
4322 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4323 else
4324 return Imp.takeError();
4325
4326 DeclarationNameInfo NameInfo(Name, ToLoc);
4327 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4328 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004329
Gabor Marton26f72a92018-07-12 09:42:05 +00004330 UnresolvedUsingValueDecl *ToUsingValue;
4331 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004332 ToUsingLoc, ToQualifierLoc, NameInfo,
4333 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004334 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004335
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004336 ToUsingValue->setAccess(D->getAccess());
4337 ToUsingValue->setLexicalDeclContext(LexicalDC);
4338 LexicalDC->addDeclInternal(ToUsingValue);
4339
4340 return ToUsingValue;
4341}
4342
Balazs Keri3b30d652018-10-19 13:32:20 +00004343ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004344 UnresolvedUsingTypenameDecl *D) {
4345 DeclContext *DC, *LexicalDC;
4346 DeclarationName Name;
4347 SourceLocation Loc;
4348 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004349 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4350 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004351 if (ToD)
4352 return ToD;
4353
Balazs Keri3b30d652018-10-19 13:32:20 +00004354 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4355 NestedNameSpecifierLoc ToQualifierLoc;
4356 if (auto Imp = importSeq(
4357 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4358 D->getEllipsisLoc()))
4359 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4360 else
4361 return Imp.takeError();
4362
Gabor Marton26f72a92018-07-12 09:42:05 +00004363 UnresolvedUsingTypenameDecl *ToUsing;
4364 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004365 ToUsingLoc, ToTypenameLoc,
4366 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004367 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004368
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004369 ToUsing->setAccess(D->getAccess());
4370 ToUsing->setLexicalDeclContext(LexicalDC);
4371 LexicalDC->addDeclInternal(ToUsing);
4372
4373 return ToUsing;
4374}
4375
Balazs Keri3b30d652018-10-19 13:32:20 +00004376
4377Error ASTNodeImporter::ImportDefinition(
4378 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004379 if (To->getDefinition()) {
4380 // Check consistency of superclass.
4381 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4382 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004383 if (auto FromSuperOrErr = import(FromSuper))
4384 FromSuper = *FromSuperOrErr;
4385 else
4386 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004387 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004388
4389 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004390 if ((bool)FromSuper != (bool)ToSuper ||
4391 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004392 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004393 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004394 << To->getDeclName();
4395 if (ToSuper)
4396 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4397 << To->getSuperClass()->getDeclName();
4398 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004399 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004400 diag::note_odr_objc_missing_superclass);
4401 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004402 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004403 diag::note_odr_objc_superclass)
4404 << From->getSuperClass()->getDeclName();
4405 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004406 Importer.FromDiag(From->getLocation(),
4407 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004408 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004409
Douglas Gregor2e15c842012-02-01 21:00:38 +00004410 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004411 if (Error Err = ImportDeclContext(From))
4412 return Err;
4413 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004414 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004415
Douglas Gregor2aa53772012-01-24 17:42:07 +00004416 // Start the definition.
4417 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004418
Douglas Gregor2aa53772012-01-24 17:42:07 +00004419 // If this class has a superclass, import it.
4420 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004421 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4422 To->setSuperClass(*SuperTInfoOrErr);
4423 else
4424 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004425 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004426
Douglas Gregor2aa53772012-01-24 17:42:07 +00004427 // Import protocols
4428 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4429 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004430 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4431 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004432
Douglas Gregor2aa53772012-01-24 17:42:07 +00004433 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4434 FromProtoEnd = From->protocol_end();
4435 FromProto != FromProtoEnd;
4436 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004437 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4438 Protocols.push_back(*ToProtoOrErr);
4439 else
4440 return ToProtoOrErr.takeError();
4441
4442 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4443 ProtocolLocs.push_back(*ToProtoLocOrErr);
4444 else
4445 return ToProtoLocOrErr.takeError();
4446
Douglas Gregor2aa53772012-01-24 17:42:07 +00004447 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004448
Douglas Gregor2aa53772012-01-24 17:42:07 +00004449 // FIXME: If we're merging, make sure that the protocol list is the same.
4450 To->setProtocolList(Protocols.data(), Protocols.size(),
4451 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004452
Douglas Gregor2aa53772012-01-24 17:42:07 +00004453 // Import categories. When the categories themselves are imported, they'll
4454 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004455 for (auto *Cat : From->known_categories()) {
4456 auto ToCatOrErr = import(Cat);
4457 if (!ToCatOrErr)
4458 return ToCatOrErr.takeError();
4459 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004460
Douglas Gregor2aa53772012-01-24 17:42:07 +00004461 // If we have an @implementation, import it as well.
4462 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004463 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4464 import(From->getImplementation()))
4465 To->setImplementation(*ToImplOrErr);
4466 else
4467 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004468 }
4469
Douglas Gregor2e15c842012-02-01 21:00:38 +00004470 if (shouldForceImportDeclContext(Kind)) {
4471 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004472 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4473 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004474 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004475 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004476}
4477
Balazs Keri3b30d652018-10-19 13:32:20 +00004478Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004479ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4480 if (!list)
4481 return nullptr;
4482
4483 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004484 for (auto *fromTypeParam : *list) {
4485 if (auto toTypeParamOrErr = import(fromTypeParam))
4486 toTypeParams.push_back(*toTypeParamOrErr);
4487 else
4488 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004489 }
4490
Balazs Keri3b30d652018-10-19 13:32:20 +00004491 auto LAngleLocOrErr = import(list->getLAngleLoc());
4492 if (!LAngleLocOrErr)
4493 return LAngleLocOrErr.takeError();
4494
4495 auto RAngleLocOrErr = import(list->getRAngleLoc());
4496 if (!RAngleLocOrErr)
4497 return RAngleLocOrErr.takeError();
4498
Douglas Gregor85f3f952015-07-07 03:57:15 +00004499 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004500 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004501 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004502 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004503}
4504
Balazs Keri3b30d652018-10-19 13:32:20 +00004505ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004506 // If this class has a definition in the translation unit we're coming from,
4507 // but this particular declaration is not that definition, import the
4508 // definition and map to that.
4509 ObjCInterfaceDecl *Definition = D->getDefinition();
4510 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004511 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4512 return Importer.MapImported(D, *ImportedDefOrErr);
4513 else
4514 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004515 }
4516
Douglas Gregor45635322010-02-16 01:20:57 +00004517 // Import the major distinguishing characteristics of an @interface.
4518 DeclContext *DC, *LexicalDC;
4519 DeclarationName Name;
4520 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004521 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004522 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4523 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004524 if (ToD)
4525 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004526
Douglas Gregor2aa53772012-01-24 17:42:07 +00004527 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004528 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004529 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004530 for (auto *FoundDecl : FoundDecls) {
4531 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004532 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004533
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004534 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004535 break;
4536 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004537
Douglas Gregor2aa53772012-01-24 17:42:07 +00004538 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004539 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004540 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004541 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4542 if (!AtBeginLocOrErr)
4543 return AtBeginLocOrErr.takeError();
4544
Gabor Marton26f72a92018-07-12 09:42:05 +00004545 if (GetImportedOrCreateDecl(
4546 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004547 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004548 /*TypeParamList=*/nullptr,
4549 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4550 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004551 ToIface->setLexicalDeclContext(LexicalDC);
4552 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004553 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004554 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004555 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004556 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004557 if (auto ToPListOrErr =
4558 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4559 ToIface->setTypeParamList(*ToPListOrErr);
4560 else
4561 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004562
Balazs Keri3b30d652018-10-19 13:32:20 +00004563 if (D->isThisDeclarationADefinition())
4564 if (Error Err = ImportDefinition(D, ToIface))
4565 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004566
Douglas Gregor98d156a2010-02-17 16:12:00 +00004567 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004568}
4569
Balazs Keri3b30d652018-10-19 13:32:20 +00004570ExpectedDecl
4571ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4572 ObjCCategoryDecl *Category;
4573 if (Error Err = importInto(Category, D->getCategoryDecl()))
4574 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004575
Douglas Gregor4da9d682010-12-07 15:32:12 +00004576 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4577 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004578 DeclContext *DC, *LexicalDC;
4579 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4580 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004581
Balazs Keri3b30d652018-10-19 13:32:20 +00004582 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4583 if (auto Imp = importSeq(
4584 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4585 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4586 else
4587 return Imp.takeError();
4588
Gabor Marton26f72a92018-07-12 09:42:05 +00004589 if (GetImportedOrCreateDecl(
4590 ToImpl, D, Importer.getToContext(), DC,
4591 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004592 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004593 return ToImpl;
4594
Balazs Keri3b30d652018-10-19 13:32:20 +00004595 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004596 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004597 Category->setImplementation(ToImpl);
4598 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004599
Gabor Marton26f72a92018-07-12 09:42:05 +00004600 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004601 if (Error Err = ImportDeclContext(D))
4602 return std::move(Err);
4603
Douglas Gregor4da9d682010-12-07 15:32:12 +00004604 return ToImpl;
4605}
4606
Balazs Keri3b30d652018-10-19 13:32:20 +00004607ExpectedDecl
4608ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004609 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004610 ObjCInterfaceDecl *Iface;
4611 if (Error Err = importInto(Iface, D->getClassInterface()))
4612 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004613
4614 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004615 ObjCInterfaceDecl *Super;
4616 if (Error Err = importInto(Super, D->getSuperClass()))
4617 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004618
4619 ObjCImplementationDecl *Impl = Iface->getImplementation();
4620 if (!Impl) {
4621 // We haven't imported an implementation yet. Create a new @implementation
4622 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004623 DeclContext *DC, *LexicalDC;
4624 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4625 return std::move(Err);
4626
4627 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4628 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4629 if (auto Imp = importSeq(
4630 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4631 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4632 std::tie(
4633 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4634 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4635 else
4636 return Imp.takeError();
4637
Gabor Marton26f72a92018-07-12 09:42:05 +00004638 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004639 DC, Iface, Super,
4640 ToLocation,
4641 ToAtStartLoc,
4642 ToSuperClassLoc,
4643 ToIvarLBraceLoc,
4644 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004645 return Impl;
4646
Balazs Keri3b30d652018-10-19 13:32:20 +00004647 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004648
Douglas Gregorda8025c2010-12-07 01:26:03 +00004649 // Associate the implementation with the class it implements.
4650 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004651 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004652 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004653 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004654
4655 // Verify that the existing @implementation has the same superclass.
4656 if ((Super && !Impl->getSuperClass()) ||
4657 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004658 (Super && Impl->getSuperClass() &&
4659 !declaresSameEntity(Super->getCanonicalDecl(),
4660 Impl->getSuperClass()))) {
4661 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004662 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004663 << Iface->getDeclName();
4664 // FIXME: It would be nice to have the location of the superclass
4665 // below.
4666 if (Impl->getSuperClass())
4667 Importer.ToDiag(Impl->getLocation(),
4668 diag::note_odr_objc_superclass)
4669 << Impl->getSuperClass()->getDeclName();
4670 else
4671 Importer.ToDiag(Impl->getLocation(),
4672 diag::note_odr_objc_missing_superclass);
4673 if (D->getSuperClass())
4674 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004675 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004676 << D->getSuperClass()->getDeclName();
4677 else
4678 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004679 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004680
4681 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004682 }
4683 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004684
Douglas Gregorda8025c2010-12-07 01:26:03 +00004685 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004686 if (Error Err = ImportDeclContext(D))
4687 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004688
4689 return Impl;
4690}
4691
Balazs Keri3b30d652018-10-19 13:32:20 +00004692ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004693 // Import the major distinguishing characteristics of an @property.
4694 DeclContext *DC, *LexicalDC;
4695 DeclarationName Name;
4696 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004697 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004698 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4699 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004700 if (ToD)
4701 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004702
4703 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004704 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004705 for (auto *FoundDecl : FoundDecls) {
4706 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004707 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004708 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004709 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004710 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004711 << Name << D->getType() << FoundProp->getType();
4712 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4713 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004714
4715 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004716 }
4717
4718 // FIXME: Check property attributes, getters, setters, etc.?
4719
4720 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004721 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004722 return FoundProp;
4723 }
4724 }
4725
Balazs Keri3b30d652018-10-19 13:32:20 +00004726 QualType ToType;
4727 TypeSourceInfo *ToTypeSourceInfo;
4728 SourceLocation ToAtLoc, ToLParenLoc;
4729 if (auto Imp = importSeq(
4730 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4731 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4732 else
4733 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004734
4735 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004736 ObjCPropertyDecl *ToProperty;
4737 if (GetImportedOrCreateDecl(
4738 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004739 Name.getAsIdentifierInfo(), ToAtLoc,
4740 ToLParenLoc, ToType,
4741 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004742 return ToProperty;
4743
Balazs Keri3b30d652018-10-19 13:32:20 +00004744 Selector ToGetterName, ToSetterName;
4745 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4746 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4747 ObjCIvarDecl *ToPropertyIvarDecl;
4748 if (auto Imp = importSeq(
4749 D->getGetterName(), D->getSetterName(),
4750 D->getGetterNameLoc(), D->getSetterNameLoc(),
4751 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4752 D->getPropertyIvarDecl()))
4753 std::tie(
4754 ToGetterName, ToSetterName,
4755 ToGetterNameLoc, ToSetterNameLoc,
4756 ToGetterMethodDecl, ToSetterMethodDecl,
4757 ToPropertyIvarDecl) = *Imp;
4758 else
4759 return Imp.takeError();
4760
Douglas Gregora11c4582010-02-17 18:02:10 +00004761 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004762 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004763
4764 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004765 ToProperty->setPropertyAttributesAsWritten(
4766 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004767 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4768 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4769 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4770 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4771 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004772 return ToProperty;
4773}
4774
Balazs Keri3b30d652018-10-19 13:32:20 +00004775ExpectedDecl
4776ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4777 ObjCPropertyDecl *Property;
4778 if (Error Err = importInto(Property, D->getPropertyDecl()))
4779 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004780
Balazs Keri3b30d652018-10-19 13:32:20 +00004781 DeclContext *DC, *LexicalDC;
4782 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4783 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004784
Balazs Keri3b30d652018-10-19 13:32:20 +00004785 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004786
4787 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004788 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004789 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4790 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004791
4792 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004793 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4794 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004795 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004796 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4797 if (auto Imp = importSeq(
4798 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4799 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4800 else
4801 return Imp.takeError();
4802
Gabor Marton26f72a92018-07-12 09:42:05 +00004803 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004804 ToBeginLoc,
4805 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004806 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004807 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004808 return ToImpl;
4809
Douglas Gregor14a49e22010-12-07 18:32:03 +00004810 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004811 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004812 } else {
4813 // Check that we have the same kind of property implementation (@synthesize
4814 // vs. @dynamic).
4815 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004816 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004817 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004818 << Property->getDeclName()
4819 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004820 == ObjCPropertyImplDecl::Dynamic);
4821 Importer.FromDiag(D->getLocation(),
4822 diag::note_odr_objc_property_impl_kind)
4823 << D->getPropertyDecl()->getDeclName()
4824 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004825
4826 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004827 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004828
4829 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004830 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4831 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004832 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004833 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004834 << Property->getDeclName()
4835 << ToImpl->getPropertyIvarDecl()->getDeclName()
4836 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004837 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004838 diag::note_odr_objc_synthesize_ivar_here)
4839 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004840
4841 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004842 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004843
Douglas Gregor14a49e22010-12-07 18:32:03 +00004844 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004845 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004846 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004847
Douglas Gregor14a49e22010-12-07 18:32:03 +00004848 return ToImpl;
4849}
4850
Balazs Keri3b30d652018-10-19 13:32:20 +00004851ExpectedDecl
4852ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004853 // For template arguments, we adopt the translation unit as our declaration
4854 // context. This context will be fixed when the actual template declaration
4855 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004856
Douglas Gregora082a492010-11-30 19:14:50 +00004857 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004858
4859 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4860 if (!BeginLocOrErr)
4861 return BeginLocOrErr.takeError();
4862
4863 ExpectedSLoc LocationOrErr = import(D->getLocation());
4864 if (!LocationOrErr)
4865 return LocationOrErr.takeError();
4866
Gabor Marton26f72a92018-07-12 09:42:05 +00004867 TemplateTypeParmDecl *ToD = nullptr;
4868 (void)GetImportedOrCreateDecl(
4869 ToD, D, Importer.getToContext(),
4870 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004871 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004872 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4873 D->wasDeclaredWithTypename(), D->isParameterPack());
4874 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004875}
4876
Balazs Keri3b30d652018-10-19 13:32:20 +00004877ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004878ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004879 DeclarationName ToDeclName;
4880 SourceLocation ToLocation, ToInnerLocStart;
4881 QualType ToType;
4882 TypeSourceInfo *ToTypeSourceInfo;
4883 if (auto Imp = importSeq(
4884 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4885 D->getInnerLocStart()))
4886 std::tie(
4887 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4888 ToInnerLocStart) = *Imp;
4889 else
4890 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004891
Douglas Gregora082a492010-11-30 19:14:50 +00004892 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004893
4894 NonTypeTemplateParmDecl *ToD = nullptr;
4895 (void)GetImportedOrCreateDecl(
4896 ToD, D, Importer.getToContext(),
4897 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004898 ToInnerLocStart, ToLocation, D->getDepth(),
4899 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4900 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004901 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004902}
4903
Balazs Keri3b30d652018-10-19 13:32:20 +00004904ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004905ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4906 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004907 auto NameOrErr = import(D->getDeclName());
4908 if (!NameOrErr)
4909 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004910
Douglas Gregora082a492010-11-30 19:14:50 +00004911 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004912 ExpectedSLoc LocationOrErr = import(D->getLocation());
4913 if (!LocationOrErr)
4914 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004915
Douglas Gregora082a492010-11-30 19:14:50 +00004916 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004917 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004918 if (!TemplateParamsOrErr)
4919 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004920
Douglas Gregora082a492010-11-30 19:14:50 +00004921 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004922
4923 TemplateTemplateParmDecl *ToD = nullptr;
4924 (void)GetImportedOrCreateDecl(
4925 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004926 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4927 D->getDepth(), D->getPosition(), D->isParameterPack(),
4928 (*NameOrErr).getAsIdentifierInfo(),
4929 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004930 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004931}
4932
Gabor Marton16d98c22019-03-07 13:01:51 +00004933// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004934// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004935template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4936 assert(D->getTemplatedDecl() && "Should be called on templates only");
4937 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004938 if (!ToTemplatedDef)
4939 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004940 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4941 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004942}
4943
Balazs Keri3b30d652018-10-19 13:32:20 +00004944ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004945 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4946
Douglas Gregora082a492010-11-30 19:14:50 +00004947 // Import the major distinguishing characteristics of this class template.
4948 DeclContext *DC, *LexicalDC;
4949 DeclarationName Name;
4950 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004951 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004952 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4953 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004954 if (ToD)
4955 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004956
Gabor Marton7df342a2018-12-17 12:42:12 +00004957 ClassTemplateDecl *FoundByLookup = nullptr;
4958
Douglas Gregora082a492010-11-30 19:14:50 +00004959 // We may already have a template of the same name; try to find and match it.
4960 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004961 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004962 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004963 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004964 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4965 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004966 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004967
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004968 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004969 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4970 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004971
Douglas Gregora082a492010-11-30 19:14:50 +00004972 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004973 ClassTemplateDecl *TemplateWithDef =
4974 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004975 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4976 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004977 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004978 FoundByLookup = FoundTemplate;
4979 break;
Gabor Marton9581c332018-05-23 13:53:36 +00004980 }
Douglas Gregora082a492010-11-30 19:14:50 +00004981 }
Gabor Marton9581c332018-05-23 13:53:36 +00004982
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004983 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004984 }
Gabor Marton9581c332018-05-23 13:53:36 +00004985
Douglas Gregora082a492010-11-30 19:14:50 +00004986 if (!ConflictingDecls.empty()) {
4987 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004988 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004989 ConflictingDecls.size());
4990 }
Gabor Marton9581c332018-05-23 13:53:36 +00004991
Douglas Gregora082a492010-11-30 19:14:50 +00004992 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00004993 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00004994 }
4995
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004996 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4997
Douglas Gregora082a492010-11-30 19:14:50 +00004998 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00004999 CXXRecordDecl *ToTemplated;
5000 if (Error Err = importInto(ToTemplated, FromTemplated))
5001 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005002
Douglas Gregora082a492010-11-30 19:14:50 +00005003 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005004 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005005 if (!TemplateParamsOrErr)
5006 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005007
Gabor Marton26f72a92018-07-12 09:42:05 +00005008 ClassTemplateDecl *D2;
5009 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005010 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005011 return D2;
5012
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005013 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005014
Douglas Gregora082a492010-11-30 19:14:50 +00005015 D2->setAccess(D->getAccess());
5016 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00005017
5018 if (D->getDeclContext()->containsDeclAndLoad(D))
5019 DC->addDeclInternal(D2);
5020 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00005021 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005022
Gabor Marton7df342a2018-12-17 12:42:12 +00005023 if (FoundByLookup) {
5024 auto *Recent =
5025 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5026
5027 // It is possible that during the import of the class template definition
5028 // we start the import of a fwd friend decl of the very same class template
5029 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5030 // had been created earlier and by that time the lookup could not find
5031 // anything existing, so it has no previous decl. Later, (still during the
5032 // import of the fwd friend decl) we start to import the definition again
5033 // and this time the lookup finds the previous fwd friend class template.
5034 // In this case we must set up the previous decl for the templated decl.
5035 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005036 assert(FoundByLookup->getTemplatedDecl() &&
5037 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005038 CXXRecordDecl *PrevTemplated =
5039 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5040 if (ToTemplated != PrevTemplated)
5041 ToTemplated->setPreviousDecl(PrevTemplated);
5042 }
5043
5044 D2->setPreviousDecl(Recent);
5045 }
5046
5047 if (LexicalDC != DC && IsFriend)
5048 DC->makeDeclVisibleInContext(D2);
5049
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005050 if (FromTemplated->isCompleteDefinition() &&
5051 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005052 // FIXME: Import definition!
5053 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005054
Douglas Gregora082a492010-11-30 19:14:50 +00005055 return D2;
5056}
5057
Balazs Keri3b30d652018-10-19 13:32:20 +00005058ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005059 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005060 ClassTemplateDecl *ClassTemplate;
5061 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5062 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005063
Douglas Gregore2e50d332010-12-01 01:36:18 +00005064 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005065 DeclContext *DC, *LexicalDC;
5066 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5067 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005068
5069 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005070 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005071 if (Error Err = ImportTemplateArguments(
5072 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5073 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005074
Douglas Gregore2e50d332010-12-01 01:36:18 +00005075 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005076 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005077 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005078 ClassTemplatePartialSpecializationDecl *PartialSpec =
5079 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5080 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005081 PrevDecl =
5082 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005083 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005084 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005085
Gabor Marton7f8c4002019-03-19 13:34:10 +00005086 if (PrevDecl) {
5087 if (IsStructuralMatch(D, PrevDecl)) {
5088 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5089 Importer.MapImported(D, PrevDecl->getDefinition());
5090 // Import those default field initializers which have been
5091 // instantiated in the "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005092 for (auto *FromField : D->fields()) {
5093 auto ToOrErr = import(FromField);
5094 if (!ToOrErr)
5095 return ToOrErr.takeError();
5096 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005097
Gabor Marton7f8c4002019-03-19 13:34:10 +00005098 // Import those methods which have been instantiated in the
5099 // "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005100 for (CXXMethodDecl *FromM : D->methods()) {
5101 auto ToOrErr = import(FromM);
5102 if (!ToOrErr)
5103 return ToOrErr.takeError();
5104 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005105
Gabor Marton7f8c4002019-03-19 13:34:10 +00005106 // TODO Import instantiated default arguments.
5107 // TODO Import instantiated exception specifications.
5108 //
5109 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5110 // what else could be fused during an AST merge.
5111 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005112 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005113 } else { // ODR violation.
5114 // FIXME HandleNameConflict
Gabor Marton303c98612019-06-25 08:00:51 +00005115 return make_error<ImportError>(ImportError::NameConflict);
Gabor Marton42e15de2018-08-22 11:52:14 +00005116 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005117 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005118
Gabor Marton7f8c4002019-03-19 13:34:10 +00005119 // Import the location of this declaration.
5120 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5121 if (!BeginLocOrErr)
5122 return BeginLocOrErr.takeError();
5123 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5124 if (!IdLocOrErr)
5125 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005126
Gabor Marton7f8c4002019-03-19 13:34:10 +00005127 // Create the specialization.
5128 ClassTemplateSpecializationDecl *D2 = nullptr;
5129 if (PartialSpec) {
5130 // Import TemplateArgumentListInfo.
5131 TemplateArgumentListInfo ToTAInfo;
5132 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5133 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5134 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005135
Gabor Marton7f8c4002019-03-19 13:34:10 +00005136 QualType CanonInjType;
5137 if (Error Err = importInto(
5138 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5139 return std::move(Err);
5140 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005141
Balazs Keridec09162019-03-20 15:42:42 +00005142 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005143 if (!ToTPListOrErr)
5144 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005145
Gabor Marton7f8c4002019-03-19 13:34:10 +00005146 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5147 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5148 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5149 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5150 ToTAInfo, CanonInjType,
5151 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5152 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005153
Gabor Marton7f8c4002019-03-19 13:34:10 +00005154 // Update InsertPos, because preceding import calls may have invalidated
5155 // it by adding new specializations.
5156 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5157 // Add this partial specialization to the class template.
5158 ClassTemplate->AddPartialSpecialization(
5159 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005160
Gabor Marton7f8c4002019-03-19 13:34:10 +00005161 } else { // Not a partial specialization.
5162 if (GetImportedOrCreateDecl(
5163 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5164 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5165 PrevDecl))
5166 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005167
Gabor Marton7f8c4002019-03-19 13:34:10 +00005168 // Update InsertPos, because preceding import calls may have invalidated
5169 // it by adding new specializations.
5170 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5171 // Add this specialization to the class template.
5172 ClassTemplate->AddSpecialization(D2, InsertPos);
5173 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005174
Gabor Marton7f8c4002019-03-19 13:34:10 +00005175 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005176
Gabor Marton7f8c4002019-03-19 13:34:10 +00005177 // Set the context of this specialization/instantiation.
5178 D2->setLexicalDeclContext(LexicalDC);
5179
5180 // Add to the DC only if it was an explicit specialization/instantiation.
5181 if (D2->isExplicitInstantiationOrSpecialization()) {
5182 LexicalDC->addDeclInternal(D2);
5183 }
5184
5185 // Import the qualifier, if any.
5186 if (auto LocOrErr = import(D->getQualifierLoc()))
5187 D2->setQualifierInfo(*LocOrErr);
5188 else
5189 return LocOrErr.takeError();
5190
5191 if (auto *TSI = D->getTypeAsWritten()) {
5192 if (auto TInfoOrErr = import(TSI))
5193 D2->setTypeAsWritten(*TInfoOrErr);
5194 else
5195 return TInfoOrErr.takeError();
5196
5197 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5198 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005199 else
5200 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005201
Gabor Marton7f8c4002019-03-19 13:34:10 +00005202 if (auto LocOrErr = import(D->getExternLoc()))
5203 D2->setExternLoc(*LocOrErr);
5204 else
5205 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005206 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005207
5208 if (D->getPointOfInstantiation().isValid()) {
5209 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5210 D2->setPointOfInstantiation(*POIOrErr);
5211 else
5212 return POIOrErr.takeError();
5213 }
5214
5215 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5216
Balazs Keri3b30d652018-10-19 13:32:20 +00005217 if (D->isCompleteDefinition())
5218 if (Error Err = ImportDefinition(D, D2))
5219 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005220
Douglas Gregore2e50d332010-12-01 01:36:18 +00005221 return D2;
5222}
5223
Balazs Keri3b30d652018-10-19 13:32:20 +00005224ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005225 // If this variable has a definition in the translation unit we're coming
5226 // from,
5227 // but this particular declaration is not that definition, import the
5228 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005229 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005230 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5231 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005232 if (ExpectedDecl ImportedDefOrErr = import(
5233 Definition->getDescribedVarTemplate()))
5234 return Importer.MapImported(D, *ImportedDefOrErr);
5235 else
5236 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005237 }
5238
5239 // Import the major distinguishing characteristics of this variable template.
5240 DeclContext *DC, *LexicalDC;
5241 DeclarationName Name;
5242 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005243 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005244 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5245 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005246 if (ToD)
5247 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005248
5249 // We may already have a template of the same name; try to find and match it.
5250 assert(!DC->isFunctionOrMethod() &&
5251 "Variable templates cannot be declared at function scope");
5252 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005253 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005254 for (auto *FoundDecl : FoundDecls) {
5255 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005256 continue;
5257
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005258 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005259 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005260 if (IsStructuralMatch(D, FoundTemplate)) {
5261 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005262 Importer.MapImported(D->getTemplatedDecl(),
5263 FoundTemplate->getTemplatedDecl());
5264 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005265 }
5266 }
5267
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005268 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005269 }
5270
5271 if (!ConflictingDecls.empty()) {
5272 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5273 ConflictingDecls.data(),
5274 ConflictingDecls.size());
5275 }
5276
5277 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005278 // FIXME: Is it possible to get other error than name conflict?
5279 // (Put this `if` into the previous `if`?)
5280 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005281
5282 VarDecl *DTemplated = D->getTemplatedDecl();
5283
5284 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005285 // FIXME: Value not used?
5286 ExpectedType TypeOrErr = import(DTemplated->getType());
5287 if (!TypeOrErr)
5288 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005289
5290 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005291 VarDecl *ToTemplated;
5292 if (Error Err = importInto(ToTemplated, DTemplated))
5293 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005294
5295 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005296 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005297 if (!TemplateParamsOrErr)
5298 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005299
Gabor Marton26f72a92018-07-12 09:42:05 +00005300 VarTemplateDecl *ToVarTD;
5301 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005302 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005303 return ToVarTD;
5304
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005305 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005306
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005307 ToVarTD->setAccess(D->getAccess());
5308 ToVarTD->setLexicalDeclContext(LexicalDC);
5309 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005310
Larisse Voufo39a1e502013-08-06 01:03:05 +00005311 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005312 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005313 // FIXME: Import definition!
5314 }
5315
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005316 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005317}
5318
Balazs Keri3b30d652018-10-19 13:32:20 +00005319ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005320 VarTemplateSpecializationDecl *D) {
5321 // If this record has a definition in the translation unit we're coming from,
5322 // but this particular declaration is not that definition, import the
5323 // definition and map to that.
5324 VarDecl *Definition = D->getDefinition();
5325 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005326 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5327 return Importer.MapImported(D, *ImportedDefOrErr);
5328 else
5329 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005330 }
5331
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00005332 VarTemplateDecl *VarTemplate = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005333 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5334 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005335
5336 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005337 DeclContext *DC, *LexicalDC;
5338 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5339 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005340
5341 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005342 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5343 if (!BeginLocOrErr)
5344 return BeginLocOrErr.takeError();
5345
5346 auto IdLocOrErr = import(D->getLocation());
5347 if (!IdLocOrErr)
5348 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005349
5350 // Import template arguments.
5351 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005352 if (Error Err = ImportTemplateArguments(
5353 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5354 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005355
5356 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005357 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005358 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005359 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005360 if (D2) {
5361 // We already have a variable template specialization with these template
5362 // arguments.
5363
5364 // FIXME: Check for specialization vs. instantiation errors.
5365
5366 if (VarDecl *FoundDef = D2->getDefinition()) {
5367 if (!D->isThisDeclarationADefinition() ||
5368 IsStructuralMatch(D, FoundDef)) {
5369 // The record types structurally match, or the "from" translation
5370 // unit only had a forward declaration anyway; call it the same
5371 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005372 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005373 }
5374 }
5375 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005376 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005377 QualType T;
5378 if (Error Err = importInto(T, D->getType()))
5379 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005380
Balazs Keri3b30d652018-10-19 13:32:20 +00005381 auto TInfoOrErr = import(D->getTypeSourceInfo());
5382 if (!TInfoOrErr)
5383 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005384
5385 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005386 if (Error Err = ImportTemplateArgumentListInfo(
5387 D->getTemplateArgsInfo(), ToTAInfo))
5388 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005389
5390 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005391 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005392 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5393 // Import TemplateArgumentListInfo
5394 TemplateArgumentListInfo ArgInfos;
5395 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5396 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005397 if (Error Err = ImportTemplateArgumentListInfo(
5398 *FromTAArgsAsWritten, ArgInfos))
5399 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005400
Balazs Keridec09162019-03-20 15:42:42 +00005401 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005402 if (!ToTPListOrErr)
5403 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005404
Gabor Marton26f72a92018-07-12 09:42:05 +00005405 PartVarSpecDecl *ToPartial;
5406 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005407 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5408 VarTemplate, T, *TInfoOrErr,
5409 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005410 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005411
Balazs Keri3b30d652018-10-19 13:32:20 +00005412 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5413 FromPartial->getInstantiatedFromMember()))
5414 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5415 else
5416 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005417
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005418 if (FromPartial->isMemberSpecialization())
5419 ToPartial->setMemberSpecialization();
5420
5421 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005422
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005423 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005424 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5425 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5426 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005427 D->getStorageClass(), TemplateArgs))
5428 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005429 }
5430
Balazs Keri3b30d652018-10-19 13:32:20 +00005431 if (D->getPointOfInstantiation().isValid()) {
5432 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5433 D2->setPointOfInstantiation(*POIOrErr);
5434 else
5435 return POIOrErr.takeError();
5436 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005437
Larisse Voufo39a1e502013-08-06 01:03:05 +00005438 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005439 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005440
5441 // Add this specialization to the class template.
5442 VarTemplate->AddSpecialization(D2, InsertPos);
5443
5444 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005445 if (auto LocOrErr = import(D->getQualifierLoc()))
5446 D2->setQualifierInfo(*LocOrErr);
5447 else
5448 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005449
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005450 if (D->isConstexpr())
5451 D2->setConstexpr(true);
5452
Larisse Voufo39a1e502013-08-06 01:03:05 +00005453 // Add the specialization to this context.
5454 D2->setLexicalDeclContext(LexicalDC);
5455 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005456
5457 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005458 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005459
Balazs Keri3b30d652018-10-19 13:32:20 +00005460 if (Error Err = ImportInitializer(D, D2))
5461 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005462
5463 return D2;
5464}
5465
Balazs Keri3b30d652018-10-19 13:32:20 +00005466ExpectedDecl
5467ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005468 DeclContext *DC, *LexicalDC;
5469 DeclarationName Name;
5470 SourceLocation Loc;
5471 NamedDecl *ToD;
5472
Balazs Keri3b30d652018-10-19 13:32:20 +00005473 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5474 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005475
5476 if (ToD)
5477 return ToD;
5478
Gabor Marton16d98c22019-03-07 13:01:51 +00005479 const FunctionTemplateDecl *FoundByLookup = nullptr;
5480
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005481 // Try to find a function in our own ("to") context with the same name, same
5482 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005483 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005484 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005485 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005486 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005487 for (auto *FoundDecl : FoundDecls) {
5488 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005489 continue;
5490
Gabor Marton16d98c22019-03-07 13:01:51 +00005491 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5492 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005493 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005494 if (IsStructuralMatch(D, FoundTemplate)) {
5495 FunctionTemplateDecl *TemplateWithDef =
5496 getTemplateDefinition(FoundTemplate);
5497 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5498 return Importer.MapImported(D, TemplateWithDef);
5499 }
5500 FoundByLookup = FoundTemplate;
5501 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005502 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005503 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005504 }
5505 }
5506 }
5507 }
5508
Balazs Keridec09162019-03-20 15:42:42 +00005509 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005510 if (!ParamsOrErr)
5511 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005512
Balazs Keri3b30d652018-10-19 13:32:20 +00005513 FunctionDecl *TemplatedFD;
5514 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5515 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005516
Gabor Marton26f72a92018-07-12 09:42:05 +00005517 FunctionTemplateDecl *ToFunc;
5518 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005519 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005520 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005521
5522 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005523
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005524 ToFunc->setAccess(D->getAccess());
5525 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005526 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005527
5528 if (FoundByLookup) {
5529 auto *Recent =
5530 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5531 if (!TemplatedFD->getPreviousDecl()) {
5532 assert(FoundByLookup->getTemplatedDecl() &&
5533 "Found decl must have its templated decl set");
5534 auto *PrevTemplated =
5535 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5536 if (TemplatedFD != PrevTemplated)
5537 TemplatedFD->setPreviousDecl(PrevTemplated);
5538 }
5539 ToFunc->setPreviousDecl(Recent);
5540 }
5541
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005542 return ToFunc;
5543}
5544
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005545//----------------------------------------------------------------------------
5546// Import Statements
5547//----------------------------------------------------------------------------
5548
Balazs Keri3b30d652018-10-19 13:32:20 +00005549ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005550 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5551 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005552 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005553}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005554
Balazs Keri3b30d652018-10-19 13:32:20 +00005555
5556ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Gabor Marton303c98612019-06-25 08:00:51 +00005557 if (Importer.returnWithErrorInTest())
5558 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005559 SmallVector<IdentifierInfo *, 4> Names;
5560 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5561 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005562 // ToII is nullptr when no symbolic name is given for output operand
5563 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005564 Names.push_back(ToII);
5565 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005566
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005567 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5568 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005569 // ToII is nullptr when no symbolic name is given for input operand
5570 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005571 Names.push_back(ToII);
5572 }
5573
5574 SmallVector<StringLiteral *, 4> Clobbers;
5575 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005576 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5577 Clobbers.push_back(*ClobberOrErr);
5578 else
5579 return ClobberOrErr.takeError();
5580
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005581 }
5582
5583 SmallVector<StringLiteral *, 4> Constraints;
5584 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005585 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5586 Constraints.push_back(*OutputOrErr);
5587 else
5588 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005589 }
5590
5591 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005592 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5593 Constraints.push_back(*InputOrErr);
5594 else
5595 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005596 }
5597
Jennifer Yub8fee672019-06-03 15:57:25 +00005598 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() +
5599 S->getNumLabels());
Balazs Keri3b30d652018-10-19 13:32:20 +00005600 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5601 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005602
Jennifer Yub8fee672019-06-03 15:57:25 +00005603 if (Error Err =
5604 ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
5605 return std::move(Err);
5606
Balazs Keri3b30d652018-10-19 13:32:20 +00005607 if (Error Err = ImportArrayChecked(
Jennifer Yub8fee672019-06-03 15:57:25 +00005608 S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs()))
Balazs Keri3b30d652018-10-19 13:32:20 +00005609 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005610
Balazs Keri3b30d652018-10-19 13:32:20 +00005611 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5612 if (!AsmLocOrErr)
5613 return AsmLocOrErr.takeError();
5614 auto AsmStrOrErr = import(S->getAsmString());
5615 if (!AsmStrOrErr)
5616 return AsmStrOrErr.takeError();
5617 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5618 if (!RParenLocOrErr)
5619 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005620
5621 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005622 Importer.getToContext(),
5623 *AsmLocOrErr,
5624 S->isSimple(),
5625 S->isVolatile(),
5626 S->getNumOutputs(),
5627 S->getNumInputs(),
5628 Names.data(),
5629 Constraints.data(),
5630 Exprs.data(),
5631 *AsmStrOrErr,
5632 S->getNumClobbers(),
5633 Clobbers.data(),
Jennifer Yub8fee672019-06-03 15:57:25 +00005634 S->getNumLabels(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005635 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005636}
5637
Balazs Keri3b30d652018-10-19 13:32:20 +00005638ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5639 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5640 if (!Imp)
5641 return Imp.takeError();
5642
5643 DeclGroupRef ToDG;
5644 SourceLocation ToBeginLoc, ToEndLoc;
5645 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5646
5647 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005648}
5649
Balazs Keri3b30d652018-10-19 13:32:20 +00005650ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5651 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5652 if (!ToSemiLocOrErr)
5653 return ToSemiLocOrErr.takeError();
5654 return new (Importer.getToContext()) NullStmt(
5655 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005656}
5657
Balazs Keri3b30d652018-10-19 13:32:20 +00005658ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005659 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005660
Balazs Keri3b30d652018-10-19 13:32:20 +00005661 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5662 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005663
Balazs Keri3b30d652018-10-19 13:32:20 +00005664 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5665 if (!ToLBracLocOrErr)
5666 return ToLBracLocOrErr.takeError();
5667
5668 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5669 if (!ToRBracLocOrErr)
5670 return ToRBracLocOrErr.takeError();
5671
5672 return CompoundStmt::Create(
5673 Importer.getToContext(), ToStmts,
5674 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005675}
5676
Balazs Keri3b30d652018-10-19 13:32:20 +00005677ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5678 auto Imp = importSeq(
5679 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5680 S->getEllipsisLoc(), S->getColonLoc());
5681 if (!Imp)
5682 return Imp.takeError();
5683
5684 Expr *ToLHS, *ToRHS;
5685 Stmt *ToSubStmt;
5686 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5687 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5688 *Imp;
5689
Bruno Ricci5b30571752018-10-28 12:30:53 +00005690 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5691 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005692 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005693
Gabor Horvath480892b2017-10-18 09:25:18 +00005694 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005695}
5696
Balazs Keri3b30d652018-10-19 13:32:20 +00005697ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5698 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5699 if (!Imp)
5700 return Imp.takeError();
5701
5702 SourceLocation ToDefaultLoc, ToColonLoc;
5703 Stmt *ToSubStmt;
5704 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5705
5706 return new (Importer.getToContext()) DefaultStmt(
5707 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005708}
5709
Balazs Keri3b30d652018-10-19 13:32:20 +00005710ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5711 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5712 if (!Imp)
5713 return Imp.takeError();
5714
5715 SourceLocation ToIdentLoc;
5716 LabelDecl *ToLabelDecl;
5717 Stmt *ToSubStmt;
5718 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5719
5720 return new (Importer.getToContext()) LabelStmt(
5721 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005722}
5723
Balazs Keri3b30d652018-10-19 13:32:20 +00005724ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5725 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5726 if (!ToAttrLocOrErr)
5727 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005728 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5729 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005730 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5731 return std::move(Err);
5732 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5733 if (!ToSubStmtOrErr)
5734 return ToSubStmtOrErr.takeError();
5735
5736 return AttributedStmt::Create(
5737 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005738}
5739
Balazs Keri3b30d652018-10-19 13:32:20 +00005740ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5741 auto Imp = importSeq(
5742 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5743 S->getThen(), S->getElseLoc(), S->getElse());
5744 if (!Imp)
5745 return Imp.takeError();
5746
5747 SourceLocation ToIfLoc, ToElseLoc;
5748 Stmt *ToInit, *ToThen, *ToElse;
5749 VarDecl *ToConditionVariable;
5750 Expr *ToCond;
5751 std::tie(
5752 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5753 *Imp;
5754
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005755 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5756 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5757 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005758}
5759
Balazs Keri3b30d652018-10-19 13:32:20 +00005760ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5761 auto Imp = importSeq(
5762 S->getInit(), S->getConditionVariable(), S->getCond(),
5763 S->getBody(), S->getSwitchLoc());
5764 if (!Imp)
5765 return Imp.takeError();
5766
5767 Stmt *ToInit, *ToBody;
5768 VarDecl *ToConditionVariable;
5769 Expr *ToCond;
5770 SourceLocation ToSwitchLoc;
5771 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5772
Bruno Riccie2806f82018-10-29 16:12:37 +00005773 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5774 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005775 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005776 ToStmt->setSwitchLoc(ToSwitchLoc);
5777
Sean Callanan59721b32015-04-28 18:41:46 +00005778 // Now we have to re-chain the cases.
5779 SwitchCase *LastChainedSwitchCase = nullptr;
5780 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5781 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005782 Expected<SwitchCase *> ToSCOrErr = import(SC);
5783 if (!ToSCOrErr)
5784 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005785 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005786 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005787 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005788 ToStmt->setSwitchCaseList(*ToSCOrErr);
5789 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005790 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005791
Sean Callanan59721b32015-04-28 18:41:46 +00005792 return ToStmt;
5793}
5794
Balazs Keri3b30d652018-10-19 13:32:20 +00005795ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5796 auto Imp = importSeq(
5797 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5798 if (!Imp)
5799 return Imp.takeError();
5800
5801 VarDecl *ToConditionVariable;
5802 Expr *ToCond;
5803 Stmt *ToBody;
5804 SourceLocation ToWhileLoc;
5805 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5806
Bruno Riccibacf7512018-10-30 13:42:41 +00005807 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5808 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005809}
5810
Balazs Keri3b30d652018-10-19 13:32:20 +00005811ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5812 auto Imp = importSeq(
5813 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5814 S->getRParenLoc());
5815 if (!Imp)
5816 return Imp.takeError();
5817
5818 Stmt *ToBody;
5819 Expr *ToCond;
5820 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5821 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5822
5823 return new (Importer.getToContext()) DoStmt(
5824 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005825}
5826
Balazs Keri3b30d652018-10-19 13:32:20 +00005827ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5828 auto Imp = importSeq(
5829 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5830 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5831 if (!Imp)
5832 return Imp.takeError();
5833
5834 Stmt *ToInit;
5835 Expr *ToCond, *ToInc;
5836 VarDecl *ToConditionVariable;
5837 Stmt *ToBody;
5838 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5839 std::tie(
5840 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5841 ToLParenLoc, ToRParenLoc) = *Imp;
5842
5843 return new (Importer.getToContext()) ForStmt(
5844 Importer.getToContext(),
5845 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5846 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005847}
5848
Balazs Keri3b30d652018-10-19 13:32:20 +00005849ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5850 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5851 if (!Imp)
5852 return Imp.takeError();
5853
5854 LabelDecl *ToLabel;
5855 SourceLocation ToGotoLoc, ToLabelLoc;
5856 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5857
5858 return new (Importer.getToContext()) GotoStmt(
5859 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005860}
5861
Balazs Keri3b30d652018-10-19 13:32:20 +00005862ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5863 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5864 if (!Imp)
5865 return Imp.takeError();
5866
5867 SourceLocation ToGotoLoc, ToStarLoc;
5868 Expr *ToTarget;
5869 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5870
5871 return new (Importer.getToContext()) IndirectGotoStmt(
5872 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005873}
5874
Balazs Keri3b30d652018-10-19 13:32:20 +00005875ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5876 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5877 if (!ToContinueLocOrErr)
5878 return ToContinueLocOrErr.takeError();
5879 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005880}
5881
Balazs Keri3b30d652018-10-19 13:32:20 +00005882ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5883 auto ToBreakLocOrErr = import(S->getBreakLoc());
5884 if (!ToBreakLocOrErr)
5885 return ToBreakLocOrErr.takeError();
5886 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005887}
5888
Balazs Keri3b30d652018-10-19 13:32:20 +00005889ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5890 auto Imp = importSeq(
5891 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5892 if (!Imp)
5893 return Imp.takeError();
5894
5895 SourceLocation ToReturnLoc;
5896 Expr *ToRetValue;
5897 const VarDecl *ToNRVOCandidate;
5898 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5899
Bruno Ricci023b1d12018-10-30 14:40:49 +00005900 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5901 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005902}
5903
Balazs Keri3b30d652018-10-19 13:32:20 +00005904ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5905 auto Imp = importSeq(
5906 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5907 if (!Imp)
5908 return Imp.takeError();
5909
5910 SourceLocation ToCatchLoc;
5911 VarDecl *ToExceptionDecl;
5912 Stmt *ToHandlerBlock;
5913 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5914
5915 return new (Importer.getToContext()) CXXCatchStmt (
5916 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005917}
5918
Balazs Keri3b30d652018-10-19 13:32:20 +00005919ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5920 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5921 if (!ToTryLocOrErr)
5922 return ToTryLocOrErr.takeError();
5923
5924 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5925 if (!ToTryBlockOrErr)
5926 return ToTryBlockOrErr.takeError();
5927
Sean Callanan59721b32015-04-28 18:41:46 +00005928 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5929 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5930 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005931 if (auto ToHandlerOrErr = import(FromHandler))
5932 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005933 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005934 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005935 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005936
5937 return CXXTryStmt::Create(
5938 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005939}
5940
Balazs Keri3b30d652018-10-19 13:32:20 +00005941ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5942 auto Imp1 = importSeq(
5943 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5944 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5945 if (!Imp1)
5946 return Imp1.takeError();
5947 auto Imp2 = importSeq(
5948 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5949 if (!Imp2)
5950 return Imp2.takeError();
5951
5952 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5953 Expr *ToCond, *ToInc;
5954 Stmt *ToInit, *ToBody;
5955 std::tie(
5956 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5957 ToBody) = *Imp1;
5958 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5959 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5960
5961 return new (Importer.getToContext()) CXXForRangeStmt(
5962 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5963 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005964}
5965
Balazs Keri3b30d652018-10-19 13:32:20 +00005966ExpectedStmt
5967ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5968 auto Imp = importSeq(
5969 S->getElement(), S->getCollection(), S->getBody(),
5970 S->getForLoc(), S->getRParenLoc());
5971 if (!Imp)
5972 return Imp.takeError();
5973
5974 Stmt *ToElement, *ToBody;
5975 Expr *ToCollection;
5976 SourceLocation ToForLoc, ToRParenLoc;
5977 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5978
5979 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
5980 ToCollection,
5981 ToBody,
5982 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00005983 ToRParenLoc);
5984}
5985
Balazs Keri3b30d652018-10-19 13:32:20 +00005986ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5987 auto Imp = importSeq(
5988 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
5989 S->getCatchBody());
5990 if (!Imp)
5991 return Imp.takeError();
5992
5993 SourceLocation ToAtCatchLoc, ToRParenLoc;
5994 VarDecl *ToCatchParamDecl;
5995 Stmt *ToCatchBody;
5996 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
5997
5998 return new (Importer.getToContext()) ObjCAtCatchStmt (
5999 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006000}
6001
Balazs Keri3b30d652018-10-19 13:32:20 +00006002ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6003 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6004 if (!ToAtFinallyLocOrErr)
6005 return ToAtFinallyLocOrErr.takeError();
6006 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6007 if (!ToAtFinallyStmtOrErr)
6008 return ToAtFinallyStmtOrErr.takeError();
6009 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6010 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006011}
6012
Balazs Keri3b30d652018-10-19 13:32:20 +00006013ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6014 auto Imp = importSeq(
6015 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6016 if (!Imp)
6017 return Imp.takeError();
6018
6019 SourceLocation ToAtTryLoc;
6020 Stmt *ToTryBody, *ToFinallyStmt;
6021 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6022
Sean Callanan59721b32015-04-28 18:41:46 +00006023 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6024 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6025 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006026 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6027 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006028 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006029 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006030 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006031
Sean Callanan59721b32015-04-28 18:41:46 +00006032 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006033 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006034 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006035 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006036}
6037
Balazs Keri3b30d652018-10-19 13:32:20 +00006038ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006039 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006040 auto Imp = importSeq(
6041 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6042 if (!Imp)
6043 return Imp.takeError();
6044
6045 SourceLocation ToAtSynchronizedLoc;
6046 Expr *ToSynchExpr;
6047 Stmt *ToSynchBody;
6048 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6049
Sean Callanan59721b32015-04-28 18:41:46 +00006050 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6051 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6052}
6053
Balazs Keri3b30d652018-10-19 13:32:20 +00006054ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6055 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6056 if (!ToThrowLocOrErr)
6057 return ToThrowLocOrErr.takeError();
6058 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6059 if (!ToThrowExprOrErr)
6060 return ToThrowExprOrErr.takeError();
6061 return new (Importer.getToContext()) ObjCAtThrowStmt(
6062 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006063}
6064
Balazs Keri3b30d652018-10-19 13:32:20 +00006065ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6066 ObjCAutoreleasePoolStmt *S) {
6067 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6068 if (!ToAtLocOrErr)
6069 return ToAtLocOrErr.takeError();
6070 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6071 if (!ToSubStmtOrErr)
6072 return ToSubStmtOrErr.takeError();
6073 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6074 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006075}
6076
6077//----------------------------------------------------------------------------
6078// Import Expressions
6079//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006080ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006081 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6082 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006083 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006084}
6085
Balazs Keri3b30d652018-10-19 13:32:20 +00006086ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6087 auto Imp = importSeq(
6088 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6089 E->getRParenLoc(), E->getType());
6090 if (!Imp)
6091 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006092
Balazs Keri3b30d652018-10-19 13:32:20 +00006093 SourceLocation ToBuiltinLoc, ToRParenLoc;
6094 Expr *ToSubExpr;
6095 TypeSourceInfo *ToWrittenTypeInfo;
6096 QualType ToType;
6097 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6098 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006099
6100 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006101 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6102 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006103}
6104
Tom Roeder521f0042019-02-26 19:26:41 +00006105ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6106 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6107 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6108 if (!Imp)
6109 return Imp.takeError();
6110
6111 Expr *ToCond;
6112 Expr *ToLHS;
6113 Expr *ToRHS;
6114 SourceLocation ToBuiltinLoc, ToRParenLoc;
6115 QualType ToType;
6116 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6117
6118 ExprValueKind VK = E->getValueKind();
6119 ExprObjectKind OK = E->getObjectKind();
6120
6121 bool TypeDependent = ToCond->isTypeDependent();
6122 bool ValueDependent = ToCond->isValueDependent();
6123
6124 // The value of CondIsTrue only matters if the value is not
6125 // condition-dependent.
6126 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6127
6128 return new (Importer.getToContext())
6129 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6130 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6131}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006132
Balazs Keri3b30d652018-10-19 13:32:20 +00006133ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6134 ExpectedType TypeOrErr = import(E->getType());
6135 if (!TypeOrErr)
6136 return TypeOrErr.takeError();
6137
6138 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6139 if (!BeginLocOrErr)
6140 return BeginLocOrErr.takeError();
6141
6142 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006143}
6144
Balazs Keri3b30d652018-10-19 13:32:20 +00006145ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6146 auto Imp = importSeq(
6147 E->getBeginLoc(), E->getType(), E->getFunctionName());
6148 if (!Imp)
6149 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006150
Balazs Keri3b30d652018-10-19 13:32:20 +00006151 SourceLocation ToBeginLoc;
6152 QualType ToType;
6153 StringLiteral *ToFunctionName;
6154 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006155
Bruno Ricci17ff0262018-10-27 19:21:19 +00006156 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6157 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006158}
6159
Balazs Keri3b30d652018-10-19 13:32:20 +00006160ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6161 auto Imp = importSeq(
6162 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6163 E->getLocation(), E->getType());
6164 if (!Imp)
6165 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006166
Balazs Keri3b30d652018-10-19 13:32:20 +00006167 NestedNameSpecifierLoc ToQualifierLoc;
6168 SourceLocation ToTemplateKeywordLoc, ToLocation;
6169 ValueDecl *ToDecl;
6170 QualType ToType;
6171 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6172 *Imp;
6173
6174 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006175 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006176 auto FoundDOrErr = import(E->getFoundDecl());
6177 if (!FoundDOrErr)
6178 return FoundDOrErr.takeError();
6179 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006180 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006181
Aleksei Sidorina693b372016-09-28 10:16:56 +00006182 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006183 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006184 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006185 if (Error Err =
6186 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6187 return std::move(Err);
6188 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006189 }
6190
Balazs Keri3b30d652018-10-19 13:32:20 +00006191 auto *ToE = DeclRefExpr::Create(
6192 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6193 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
Richard Smith715f7a12019-06-11 17:50:32 +00006194 E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse());
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006195 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006196 ToE->setHadMultipleCandidates(true);
6197 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006198}
6199
Balazs Keri3b30d652018-10-19 13:32:20 +00006200ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6201 ExpectedType TypeOrErr = import(E->getType());
6202 if (!TypeOrErr)
6203 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006204
Balazs Keri3b30d652018-10-19 13:32:20 +00006205 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006206}
6207
Balazs Keri3b30d652018-10-19 13:32:20 +00006208ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6209 ExpectedExpr ToInitOrErr = import(E->getInit());
6210 if (!ToInitOrErr)
6211 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006212
Balazs Keri3b30d652018-10-19 13:32:20 +00006213 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6214 if (!ToEqualOrColonLocOrErr)
6215 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006216
Balazs Keri3b30d652018-10-19 13:32:20 +00006217 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006218 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006219 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6220 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6221 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006222 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006223 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006224 }
6225
Balazs Keri3b30d652018-10-19 13:32:20 +00006226 SmallVector<Designator, 4> ToDesignators(E->size());
6227 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6228 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006229
6230 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006231 Importer.getToContext(), ToDesignators,
6232 ToIndexExprs, *ToEqualOrColonLocOrErr,
6233 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006234}
6235
Balazs Keri3b30d652018-10-19 13:32:20 +00006236ExpectedStmt
6237ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6238 ExpectedType ToTypeOrErr = import(E->getType());
6239 if (!ToTypeOrErr)
6240 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006241
Balazs Keri3b30d652018-10-19 13:32:20 +00006242 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6243 if (!ToLocationOrErr)
6244 return ToLocationOrErr.takeError();
6245
6246 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6247 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006248}
6249
Balazs Keri3b30d652018-10-19 13:32:20 +00006250ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6251 ExpectedType ToTypeOrErr = import(E->getType());
6252 if (!ToTypeOrErr)
6253 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006254
Balazs Keri3b30d652018-10-19 13:32:20 +00006255 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6256 if (!ToLocationOrErr)
6257 return ToLocationOrErr.takeError();
6258
6259 return IntegerLiteral::Create(
6260 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006261}
6262
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006263
Balazs Keri3b30d652018-10-19 13:32:20 +00006264ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6265 ExpectedType ToTypeOrErr = import(E->getType());
6266 if (!ToTypeOrErr)
6267 return ToTypeOrErr.takeError();
6268
6269 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6270 if (!ToLocationOrErr)
6271 return ToLocationOrErr.takeError();
6272
6273 return FloatingLiteral::Create(
6274 Importer.getToContext(), E->getValue(), E->isExact(),
6275 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006276}
6277
Balazs Keri3b30d652018-10-19 13:32:20 +00006278ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6279 auto ToTypeOrErr = import(E->getType());
6280 if (!ToTypeOrErr)
6281 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006282
Balazs Keri3b30d652018-10-19 13:32:20 +00006283 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6284 if (!ToSubExprOrErr)
6285 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006286
Balazs Keri3b30d652018-10-19 13:32:20 +00006287 return new (Importer.getToContext()) ImaginaryLiteral(
6288 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006289}
6290
Balazs Keri3b30d652018-10-19 13:32:20 +00006291ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6292 ExpectedType ToTypeOrErr = import(E->getType());
6293 if (!ToTypeOrErr)
6294 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006295
Balazs Keri3b30d652018-10-19 13:32:20 +00006296 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6297 if (!ToLocationOrErr)
6298 return ToLocationOrErr.takeError();
6299
6300 return new (Importer.getToContext()) CharacterLiteral(
6301 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006302}
6303
Balazs Keri3b30d652018-10-19 13:32:20 +00006304ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6305 ExpectedType ToTypeOrErr = import(E->getType());
6306 if (!ToTypeOrErr)
6307 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006308
Balazs Keri3b30d652018-10-19 13:32:20 +00006309 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6310 if (Error Err = ImportArrayChecked(
6311 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6312 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006313
Balazs Keri3b30d652018-10-19 13:32:20 +00006314 return StringLiteral::Create(
6315 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6316 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006317}
6318
Balazs Keri3b30d652018-10-19 13:32:20 +00006319ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6320 auto Imp = importSeq(
6321 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6322 E->getInitializer());
6323 if (!Imp)
6324 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006325
Balazs Keri3b30d652018-10-19 13:32:20 +00006326 SourceLocation ToLParenLoc;
6327 TypeSourceInfo *ToTypeSourceInfo;
6328 QualType ToType;
6329 Expr *ToInitializer;
6330 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006331
6332 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006333 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6334 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006335}
6336
Balazs Keri3b30d652018-10-19 13:32:20 +00006337ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6338 auto Imp = importSeq(
6339 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6340 if (!Imp)
6341 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006342
Balazs Keri3b30d652018-10-19 13:32:20 +00006343 SourceLocation ToBuiltinLoc, ToRParenLoc;
6344 QualType ToType;
6345 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6346
6347 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6348 if (Error Err = ImportArrayChecked(
6349 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6350 ToExprs.begin()))
6351 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006352
6353 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006354 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006355}
6356
Balazs Keri3b30d652018-10-19 13:32:20 +00006357ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6358 auto Imp = importSeq(
6359 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6360 if (!Imp)
6361 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006362
Balazs Keri3b30d652018-10-19 13:32:20 +00006363 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6364 LabelDecl *ToLabel;
6365 QualType ToType;
6366 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006367
6368 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006369 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006370}
6371
Bill Wendling8003edc2018-11-09 00:41:36 +00006372ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6373 auto Imp = importSeq(E->getSubExpr());
6374 if (!Imp)
6375 return Imp.takeError();
6376
6377 Expr *ToSubExpr;
6378 std::tie(ToSubExpr) = *Imp;
6379
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00006380 // TODO : Handle APValue::ValueKind that require importing.
6381 APValue::ValueKind Kind = E->getResultAPValueKind();
6382 if (Kind == APValue::Int || Kind == APValue::Float ||
6383 Kind == APValue::FixedPoint || Kind == APValue::ComplexFloat ||
6384 Kind == APValue::ComplexInt)
6385 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr,
6386 E->getAPValueResult());
Fangrui Song407659a2018-11-30 23:41:18 +00006387 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006388}
6389
Balazs Keri3b30d652018-10-19 13:32:20 +00006390ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6391 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6392 if (!Imp)
6393 return Imp.takeError();
6394
6395 SourceLocation ToLParen, ToRParen;
6396 Expr *ToSubExpr;
6397 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006398
Fangrui Song6907ce22018-07-30 19:24:48 +00006399 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006400 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006401}
6402
Balazs Keri3b30d652018-10-19 13:32:20 +00006403ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6404 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6405 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6406 return std::move(Err);
6407
6408 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6409 if (!ToLParenLocOrErr)
6410 return ToLParenLocOrErr.takeError();
6411
6412 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6413 if (!ToRParenLocOrErr)
6414 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006415
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006416 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6417 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006418}
6419
Balazs Keri3b30d652018-10-19 13:32:20 +00006420ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6421 auto Imp = importSeq(
6422 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6423 if (!Imp)
6424 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006425
Balazs Keri3b30d652018-10-19 13:32:20 +00006426 CompoundStmt *ToSubStmt;
6427 QualType ToType;
6428 SourceLocation ToLParenLoc, ToRParenLoc;
6429 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006430
Balazs Keri3b30d652018-10-19 13:32:20 +00006431 return new (Importer.getToContext()) StmtExpr(
6432 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006433}
6434
Balazs Keri3b30d652018-10-19 13:32:20 +00006435ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6436 auto Imp = importSeq(
6437 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6438 if (!Imp)
6439 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006440
Balazs Keri3b30d652018-10-19 13:32:20 +00006441 Expr *ToSubExpr;
6442 QualType ToType;
6443 SourceLocation ToOperatorLoc;
6444 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006445
Aaron Ballmana5038552018-01-09 13:07:03 +00006446 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006447 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6448 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006449}
6450
Balazs Keri3b30d652018-10-19 13:32:20 +00006451ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006452ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006453 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6454 if (!Imp)
6455 return Imp.takeError();
6456
6457 QualType ToType;
6458 SourceLocation ToOperatorLoc, ToRParenLoc;
6459 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006460
Douglas Gregord8552cd2010-02-19 01:24:23 +00006461 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006462 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6463 import(E->getArgumentTypeInfo());
6464 if (!ToArgumentTypeInfoOrErr)
6465 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006466
Balazs Keri3b30d652018-10-19 13:32:20 +00006467 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6468 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6469 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006470 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006471
Balazs Keri3b30d652018-10-19 13:32:20 +00006472 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6473 if (!ToArgumentExprOrErr)
6474 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006475
Balazs Keri3b30d652018-10-19 13:32:20 +00006476 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6477 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006478}
6479
Balazs Keri3b30d652018-10-19 13:32:20 +00006480ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6481 auto Imp = importSeq(
6482 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6483 if (!Imp)
6484 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006485
Balazs Keri3b30d652018-10-19 13:32:20 +00006486 Expr *ToLHS, *ToRHS;
6487 QualType ToType;
6488 SourceLocation ToOperatorLoc;
6489 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006490
Balazs Keri3b30d652018-10-19 13:32:20 +00006491 return new (Importer.getToContext()) BinaryOperator(
6492 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6493 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006494}
6495
Balazs Keri3b30d652018-10-19 13:32:20 +00006496ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6497 auto Imp = importSeq(
6498 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6499 E->getRHS(), E->getType());
6500 if (!Imp)
6501 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006502
Balazs Keri3b30d652018-10-19 13:32:20 +00006503 Expr *ToCond, *ToLHS, *ToRHS;
6504 SourceLocation ToQuestionLoc, ToColonLoc;
6505 QualType ToType;
6506 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006507
6508 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006509 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6510 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006511}
6512
Balazs Keri3b30d652018-10-19 13:32:20 +00006513ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006514 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006515 auto Imp = importSeq(
6516 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6517 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6518 if (!Imp)
6519 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006520
Balazs Keri3b30d652018-10-19 13:32:20 +00006521 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6522 OpaqueValueExpr *ToOpaqueValue;
6523 SourceLocation ToQuestionLoc, ToColonLoc;
6524 QualType ToType;
6525 std::tie(
6526 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6527 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006528
6529 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006530 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6531 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6532 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006533}
6534
Balazs Keri3b30d652018-10-19 13:32:20 +00006535ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6536 auto Imp = importSeq(
6537 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6538 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6539 if (!Imp)
6540 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006541
Balazs Keri3b30d652018-10-19 13:32:20 +00006542 SourceLocation ToBeginLoc, ToEndLoc;
6543 TypeSourceInfo *ToQueriedTypeSourceInfo;
6544 Expr *ToDimensionExpression;
6545 QualType ToType;
6546 std::tie(
6547 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6548 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006549
6550 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006551 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6552 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006553}
6554
Balazs Keri3b30d652018-10-19 13:32:20 +00006555ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6556 auto Imp = importSeq(
6557 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6558 if (!Imp)
6559 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006560
Balazs Keri3b30d652018-10-19 13:32:20 +00006561 SourceLocation ToBeginLoc, ToEndLoc;
6562 Expr *ToQueriedExpression;
6563 QualType ToType;
6564 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006565
6566 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006567 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6568 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006569}
6570
Balazs Keri3b30d652018-10-19 13:32:20 +00006571ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6572 auto Imp = importSeq(
6573 E->getLocation(), E->getType(), E->getSourceExpr());
6574 if (!Imp)
6575 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006576
Balazs Keri3b30d652018-10-19 13:32:20 +00006577 SourceLocation ToLocation;
6578 QualType ToType;
6579 Expr *ToSourceExpr;
6580 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006581
6582 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006583 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006584}
6585
Balazs Keri3b30d652018-10-19 13:32:20 +00006586ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6587 auto Imp = importSeq(
6588 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6589 if (!Imp)
6590 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006591
Balazs Keri3b30d652018-10-19 13:32:20 +00006592 Expr *ToLHS, *ToRHS;
6593 SourceLocation ToRBracketLoc;
6594 QualType ToType;
6595 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006596
6597 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006598 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6599 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006600}
6601
Balazs Keri3b30d652018-10-19 13:32:20 +00006602ExpectedStmt
6603ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6604 auto Imp = importSeq(
6605 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6606 E->getComputationResultType(), E->getOperatorLoc());
6607 if (!Imp)
6608 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006609
Balazs Keri3b30d652018-10-19 13:32:20 +00006610 Expr *ToLHS, *ToRHS;
6611 QualType ToType, ToComputationLHSType, ToComputationResultType;
6612 SourceLocation ToOperatorLoc;
6613 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6614 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006615
Balazs Keri3b30d652018-10-19 13:32:20 +00006616 return new (Importer.getToContext()) CompoundAssignOperator(
6617 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6618 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6619 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006620}
6621
Balazs Keri3b30d652018-10-19 13:32:20 +00006622Expected<CXXCastPath>
6623ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6624 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006625 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006626 if (auto SpecOrErr = import(*I))
6627 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006628 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006629 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006630 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006631 return Path;
John McCallcf142162010-08-07 06:22:56 +00006632}
6633
Balazs Keri3b30d652018-10-19 13:32:20 +00006634ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6635 ExpectedType ToTypeOrErr = import(E->getType());
6636 if (!ToTypeOrErr)
6637 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006638
Balazs Keri3b30d652018-10-19 13:32:20 +00006639 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6640 if (!ToSubExprOrErr)
6641 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006642
Balazs Keri3b30d652018-10-19 13:32:20 +00006643 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6644 if (!ToBasePathOrErr)
6645 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006646
Balazs Keri3b30d652018-10-19 13:32:20 +00006647 return ImplicitCastExpr::Create(
6648 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6649 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006650}
6651
Balazs Keri3b30d652018-10-19 13:32:20 +00006652ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6653 auto Imp1 = importSeq(
6654 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6655 if (!Imp1)
6656 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006657
Balazs Keri3b30d652018-10-19 13:32:20 +00006658 QualType ToType;
6659 Expr *ToSubExpr;
6660 TypeSourceInfo *ToTypeInfoAsWritten;
6661 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006662
Balazs Keri3b30d652018-10-19 13:32:20 +00006663 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6664 if (!ToBasePathOrErr)
6665 return ToBasePathOrErr.takeError();
6666 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006667
Aleksei Sidorina693b372016-09-28 10:16:56 +00006668 switch (E->getStmtClass()) {
6669 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006670 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006671 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6672 if (!ToLParenLocOrErr)
6673 return ToLParenLocOrErr.takeError();
6674 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6675 if (!ToRParenLocOrErr)
6676 return ToRParenLocOrErr.takeError();
6677 return CStyleCastExpr::Create(
6678 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6679 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6680 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006681 }
6682
6683 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006684 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006685 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6686 if (!ToLParenLocOrErr)
6687 return ToLParenLocOrErr.takeError();
6688 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6689 if (!ToRParenLocOrErr)
6690 return ToRParenLocOrErr.takeError();
6691 return CXXFunctionalCastExpr::Create(
6692 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6693 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6694 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006695 }
6696
6697 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006698 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6699 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6700 if (!ToLParenLocOrErr)
6701 return ToLParenLocOrErr.takeError();
6702 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6703 if (!ToBridgeKeywordLocOrErr)
6704 return ToBridgeKeywordLocOrErr.takeError();
6705 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6706 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6707 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006708 }
6709 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006710 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006711 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006712 }
6713}
6714
Balazs Keri3b30d652018-10-19 13:32:20 +00006715ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6716 SmallVector<OffsetOfNode, 4> ToNodes;
6717 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6718 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006719
Balazs Keri3b30d652018-10-19 13:32:20 +00006720 SourceLocation ToBeginLoc, ToEndLoc;
6721 if (FromNode.getKind() != OffsetOfNode::Base) {
6722 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6723 if (!Imp)
6724 return Imp.takeError();
6725 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6726 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006727
Balazs Keri3b30d652018-10-19 13:32:20 +00006728 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006730 ToNodes.push_back(
6731 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006732 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006733 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006734 auto ToBSOrErr = import(FromNode.getBase());
6735 if (!ToBSOrErr)
6736 return ToBSOrErr.takeError();
6737 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006738 break;
6739 }
6740 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006741 auto ToFieldOrErr = import(FromNode.getField());
6742 if (!ToFieldOrErr)
6743 return ToFieldOrErr.takeError();
6744 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006745 break;
6746 }
6747 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006748 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6749 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006750 break;
6751 }
6752 }
6753 }
6754
Balazs Keri3b30d652018-10-19 13:32:20 +00006755 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6756 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6757 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6758 if (!ToIndexExprOrErr)
6759 return ToIndexExprOrErr.takeError();
6760 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006761 }
6762
Balazs Keri3b30d652018-10-19 13:32:20 +00006763 auto Imp = importSeq(
6764 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6765 E->getRParenLoc());
6766 if (!Imp)
6767 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006768
Balazs Keri3b30d652018-10-19 13:32:20 +00006769 QualType ToType;
6770 TypeSourceInfo *ToTypeSourceInfo;
6771 SourceLocation ToOperatorLoc, ToRParenLoc;
6772 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6773
6774 return OffsetOfExpr::Create(
6775 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6776 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006777}
6778
Balazs Keri3b30d652018-10-19 13:32:20 +00006779ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6780 auto Imp = importSeq(
6781 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6782 if (!Imp)
6783 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006784
Balazs Keri3b30d652018-10-19 13:32:20 +00006785 QualType ToType;
6786 Expr *ToOperand;
6787 SourceLocation ToBeginLoc, ToEndLoc;
6788 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006789
Balazs Keri3b30d652018-10-19 13:32:20 +00006790 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006791 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006792 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006793 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006794 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006795
Balazs Keri3b30d652018-10-19 13:32:20 +00006796 return new (Importer.getToContext()) CXXNoexceptExpr(
6797 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006798}
6799
Balazs Keri3b30d652018-10-19 13:32:20 +00006800ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6801 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6802 if (!Imp)
6803 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006804
Balazs Keri3b30d652018-10-19 13:32:20 +00006805 Expr *ToSubExpr;
6806 QualType ToType;
6807 SourceLocation ToThrowLoc;
6808 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006809
6810 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006811 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006812}
6813
Balazs Keri3b30d652018-10-19 13:32:20 +00006814ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6815 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6816 if (!ToUsedLocOrErr)
6817 return ToUsedLocOrErr.takeError();
6818
6819 auto ToParamOrErr = import(E->getParam());
6820 if (!ToParamOrErr)
6821 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006822
Eric Fiselier708afb52019-05-16 21:04:15 +00006823 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
6824 if (!UsedContextOrErr)
6825 return UsedContextOrErr.takeError();
6826
Aleksei Sidorina693b372016-09-28 10:16:56 +00006827 return CXXDefaultArgExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00006828 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr, *UsedContextOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006829}
6830
Balazs Keri3b30d652018-10-19 13:32:20 +00006831ExpectedStmt
6832ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6833 auto Imp = importSeq(
6834 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6835 if (!Imp)
6836 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006837
Balazs Keri3b30d652018-10-19 13:32:20 +00006838 QualType ToType;
6839 TypeSourceInfo *ToTypeSourceInfo;
6840 SourceLocation ToRParenLoc;
6841 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006842
6843 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006844 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006845}
6846
Balazs Keri3b30d652018-10-19 13:32:20 +00006847ExpectedStmt
6848ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6849 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6850 if (!ToSubExprOrErr)
6851 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006852
Balazs Keri3b30d652018-10-19 13:32:20 +00006853 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6854 if (!ToDtorOrErr)
6855 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006856
6857 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006858 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6859 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006860}
6861
Balazs Keri3b30d652018-10-19 13:32:20 +00006862ExpectedStmt
6863ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6864 auto Imp = importSeq(
6865 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6866 E->getParenOrBraceRange());
6867 if (!Imp)
6868 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006869
Balazs Keri3b30d652018-10-19 13:32:20 +00006870 CXXConstructorDecl *ToConstructor;
6871 QualType ToType;
6872 TypeSourceInfo *ToTypeSourceInfo;
6873 SourceRange ToParenOrBraceRange;
6874 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006875
Balazs Keri3b30d652018-10-19 13:32:20 +00006876 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6877 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6878 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006879
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006880 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006881 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6882 ToParenOrBraceRange, E->hadMultipleCandidates(),
6883 E->isListInitialization(), E->isStdInitListInitialization(),
6884 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006885}
6886
Balazs Keri3b30d652018-10-19 13:32:20 +00006887ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006888ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006889 auto Imp = importSeq(
6890 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6891 if (!Imp)
6892 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006893
Balazs Keri3b30d652018-10-19 13:32:20 +00006894 QualType ToType;
6895 Expr *ToTemporaryExpr;
6896 const ValueDecl *ToExtendingDecl;
6897 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006898
6899 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006900 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006901
6902 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006903 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006904 return ToMTE;
6905}
6906
Balazs Keri3b30d652018-10-19 13:32:20 +00006907ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6908 auto Imp = importSeq(
6909 E->getType(), E->getPattern(), E->getEllipsisLoc());
6910 if (!Imp)
6911 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006912
Balazs Keri3b30d652018-10-19 13:32:20 +00006913 QualType ToType;
6914 Expr *ToPattern;
6915 SourceLocation ToEllipsisLoc;
6916 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006917
6918 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006919 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006920}
6921
Balazs Keri3b30d652018-10-19 13:32:20 +00006922ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6923 auto Imp = importSeq(
6924 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6925 if (!Imp)
6926 return Imp.takeError();
6927
6928 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6929 NamedDecl *ToPack;
6930 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006931
6932 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006933 if (!E->isValueDependent())
6934 Length = E->getPackLength();
6935
Balazs Keri3b30d652018-10-19 13:32:20 +00006936 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006937 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006938 if (Error Err = ImportTemplateArguments(
6939 E->getPartialArguments().data(),
6940 E->getPartialArguments().size(),
6941 ToPartialArguments))
6942 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006943 }
6944
6945 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006946 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6947 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006948}
6949
Aleksei Sidorina693b372016-09-28 10:16:56 +00006950
Balazs Keri3b30d652018-10-19 13:32:20 +00006951ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6952 auto Imp = importSeq(
6953 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6954 E->getArraySize(), E->getInitializer(), E->getType(),
6955 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6956 E->getDirectInitRange());
6957 if (!Imp)
6958 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006959
Balazs Keri3b30d652018-10-19 13:32:20 +00006960 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6961 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00006962 Optional<Expr *> ToArraySize;
6963 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00006964 QualType ToType;
6965 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6966 std::tie(
6967 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6968 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006969
Balazs Keri3b30d652018-10-19 13:32:20 +00006970 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6971 if (Error Err =
6972 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6973 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006974
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006975 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006976 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6977 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6978 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6979 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6980 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006981}
6982
Balazs Keri3b30d652018-10-19 13:32:20 +00006983ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6984 auto Imp = importSeq(
6985 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6986 if (!Imp)
6987 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006988
Balazs Keri3b30d652018-10-19 13:32:20 +00006989 QualType ToType;
6990 FunctionDecl *ToOperatorDelete;
6991 Expr *ToArgument;
6992 SourceLocation ToBeginLoc;
6993 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006994
6995 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006996 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6997 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6998 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006999}
7000
Balazs Keri3b30d652018-10-19 13:32:20 +00007001ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
7002 auto Imp = importSeq(
7003 E->getType(), E->getLocation(), E->getConstructor(),
7004 E->getParenOrBraceRange());
7005 if (!Imp)
7006 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007007
Balazs Keri3b30d652018-10-19 13:32:20 +00007008 QualType ToType;
7009 SourceLocation ToLocation;
7010 CXXConstructorDecl *ToConstructor;
7011 SourceRange ToParenOrBraceRange;
7012 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007013
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007014 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007015 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7016 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007017
Balazs Keri3b30d652018-10-19 13:32:20 +00007018 return CXXConstructExpr::Create(
7019 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7020 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7021 E->isListInitialization(), E->isStdInitListInitialization(),
7022 E->requiresZeroInitialization(), E->getConstructionKind(),
7023 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007024}
7025
Balazs Keri3b30d652018-10-19 13:32:20 +00007026ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7027 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7028 if (!ToSubExprOrErr)
7029 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007030
Balazs Keri3b30d652018-10-19 13:32:20 +00007031 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7032 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7033 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007034
Balazs Keri3b30d652018-10-19 13:32:20 +00007035 return ExprWithCleanups::Create(
7036 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7037 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007038}
7039
Balazs Keri3b30d652018-10-19 13:32:20 +00007040ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7041 auto Imp = importSeq(
7042 E->getCallee(), E->getType(), E->getRParenLoc());
7043 if (!Imp)
7044 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007045
Balazs Keri3b30d652018-10-19 13:32:20 +00007046 Expr *ToCallee;
7047 QualType ToType;
7048 SourceLocation ToRParenLoc;
7049 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007050
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007051 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007052 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7053 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007054
Bruno Riccic5885cf2018-12-21 15:20:32 +00007055 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7056 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007057}
7058
Balazs Keri3b30d652018-10-19 13:32:20 +00007059ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7060 ExpectedType ToTypeOrErr = import(E->getType());
7061 if (!ToTypeOrErr)
7062 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007063
Balazs Keri3b30d652018-10-19 13:32:20 +00007064 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7065 if (!ToLocationOrErr)
7066 return ToLocationOrErr.takeError();
7067
7068 return new (Importer.getToContext()) CXXThisExpr(
7069 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007070}
7071
Balazs Keri3b30d652018-10-19 13:32:20 +00007072ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7073 ExpectedType ToTypeOrErr = import(E->getType());
7074 if (!ToTypeOrErr)
7075 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007076
Balazs Keri3b30d652018-10-19 13:32:20 +00007077 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7078 if (!ToLocationOrErr)
7079 return ToLocationOrErr.takeError();
7080
7081 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7082 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007083}
7084
Balazs Keri3b30d652018-10-19 13:32:20 +00007085ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7086 auto Imp1 = importSeq(
7087 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7088 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7089 if (!Imp1)
7090 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007091
Balazs Keri3b30d652018-10-19 13:32:20 +00007092 Expr *ToBase;
7093 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7094 NestedNameSpecifierLoc ToQualifierLoc;
7095 ValueDecl *ToMemberDecl;
7096 QualType ToType;
7097 std::tie(
7098 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7099 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007100
Balazs Keri3b30d652018-10-19 13:32:20 +00007101 auto Imp2 = importSeq(
7102 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7103 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7104 if (!Imp2)
7105 return Imp2.takeError();
7106 NamedDecl *ToDecl;
7107 DeclarationName ToName;
7108 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7109 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007110
7111 DeclAccessPair ToFoundDecl =
7112 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007113
Balazs Keri3b30d652018-10-19 13:32:20 +00007114 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007115
Gabor Marton5caba302019-03-07 13:38:20 +00007116 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007117 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007118 if (Error Err =
7119 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7120 E->template_arguments(), ToTAInfo))
7121 return std::move(Err);
7122 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007123 }
7124
Richard Smith1bbad592019-06-11 17:50:36 +00007125 return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(),
7126 ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7127 ToMemberDecl, ToFoundDecl, ToMemberNameInfo,
7128 ResInfo, ToType, E->getValueKind(),
7129 E->getObjectKind(), E->isNonOdrUse());
Sean Callanan59721b32015-04-28 18:41:46 +00007130}
7131
Balazs Keri3b30d652018-10-19 13:32:20 +00007132ExpectedStmt
7133ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7134 auto Imp = importSeq(
7135 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7136 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7137 if (!Imp)
7138 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007139
Balazs Keri3b30d652018-10-19 13:32:20 +00007140 Expr *ToBase;
7141 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7142 NestedNameSpecifierLoc ToQualifierLoc;
7143 TypeSourceInfo *ToScopeTypeInfo;
7144 std::tie(
7145 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7146 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007147
7148 PseudoDestructorTypeStorage Storage;
7149 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7150 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007151 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7152 if (!ToDestroyedTypeLocOrErr)
7153 return ToDestroyedTypeLocOrErr.takeError();
7154 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007155 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007156 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7157 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7158 else
7159 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007160 }
7161
7162 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007163 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7164 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007165}
7166
Balazs Keri3b30d652018-10-19 13:32:20 +00007167ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007168 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007169 auto Imp = importSeq(
7170 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7171 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7172 if (!Imp)
7173 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007174
Balazs Keri3b30d652018-10-19 13:32:20 +00007175 QualType ToType;
7176 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7177 NestedNameSpecifierLoc ToQualifierLoc;
7178 NamedDecl *ToFirstQualifierFoundInScope;
7179 std::tie(
7180 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7181 ToFirstQualifierFoundInScope) = *Imp;
7182
7183 Expr *ToBase = nullptr;
7184 if (!E->isImplicitAccess()) {
7185 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7186 ToBase = *ToBaseOrErr;
7187 else
7188 return ToBaseOrErr.takeError();
7189 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007190
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007191 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007192 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007193 if (Error Err = ImportTemplateArgumentListInfo(
7194 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7195 ToTAInfo))
7196 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007197 ResInfo = &ToTAInfo;
7198 }
7199
Balazs Keri3b30d652018-10-19 13:32:20 +00007200 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7201 if (!ToMemberNameInfoOrErr)
7202 return ToMemberNameInfoOrErr.takeError();
7203 DeclarationNameInfo ToMemberNameInfo(
7204 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007205 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007206 if (Error Err = ImportDeclarationNameLoc(
7207 E->getMemberNameInfo(), ToMemberNameInfo))
7208 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007209
7210 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007211 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7212 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7213 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007214}
7215
Balazs Keri3b30d652018-10-19 13:32:20 +00007216ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007217ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007218 auto Imp = importSeq(
7219 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7220 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7221 if (!Imp)
7222 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007223
Balazs Keri3b30d652018-10-19 13:32:20 +00007224 NestedNameSpecifierLoc ToQualifierLoc;
7225 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7226 DeclarationName ToDeclName;
7227 std::tie(
7228 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7229 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007230
Balazs Keri3b30d652018-10-19 13:32:20 +00007231 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7232 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7233 return std::move(Err);
7234
7235 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007236 TemplateArgumentListInfo *ResInfo = nullptr;
7237 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007238 if (Error Err =
7239 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7240 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007241 ResInfo = &ToTAInfo;
7242 }
7243
7244 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007245 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7246 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007247}
7248
Balazs Keri3b30d652018-10-19 13:32:20 +00007249ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7250 CXXUnresolvedConstructExpr *E) {
7251 auto Imp = importSeq(
7252 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7253 if (!Imp)
7254 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007255
Balazs Keri3b30d652018-10-19 13:32:20 +00007256 SourceLocation ToLParenLoc, ToRParenLoc;
7257 TypeSourceInfo *ToTypeSourceInfo;
7258 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7259
7260 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7261 if (Error Err =
7262 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7263 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007264
7265 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007266 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7267 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007268}
7269
Balazs Keri3b30d652018-10-19 13:32:20 +00007270ExpectedStmt
7271ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7272 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7273 if (!ToNamingClassOrErr)
7274 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007275
Balazs Keri3b30d652018-10-19 13:32:20 +00007276 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7277 if (!ToQualifierLocOrErr)
7278 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007279
Balazs Keri3b30d652018-10-19 13:32:20 +00007280 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7281 if (!ToNameInfoOrErr)
7282 return ToNameInfoOrErr.takeError();
7283 DeclarationNameInfo ToNameInfo(
7284 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007285 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007286 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7287 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007288
7289 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007290 for (auto *D : E->decls())
7291 if (auto ToDOrErr = import(D))
7292 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007293 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007294 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007295
Balazs Keri3b30d652018-10-19 13:32:20 +00007296 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7297 TemplateArgumentListInfo ToTAInfo;
7298 if (Error Err = ImportTemplateArgumentListInfo(
7299 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7300 ToTAInfo))
7301 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007302
Balazs Keri3b30d652018-10-19 13:32:20 +00007303 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7304 if (!ToTemplateKeywordLocOrErr)
7305 return ToTemplateKeywordLocOrErr.takeError();
7306
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007307 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007308 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7309 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7310 ToDecls.begin(), ToDecls.end());
7311 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007312
7313 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007314 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7315 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7316 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007317}
7318
Balazs Keri3b30d652018-10-19 13:32:20 +00007319ExpectedStmt
7320ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7321 auto Imp1 = importSeq(
7322 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7323 E->getTemplateKeywordLoc());
7324 if (!Imp1)
7325 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007326
Balazs Keri3b30d652018-10-19 13:32:20 +00007327 QualType ToType;
7328 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7329 NestedNameSpecifierLoc ToQualifierLoc;
7330 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7331
7332 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7333 if (!Imp2)
7334 return Imp2.takeError();
7335 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7336 // Import additional name location/type info.
7337 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7338 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007339
7340 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007341 for (Decl *D : E->decls())
7342 if (auto ToDOrErr = import(D))
7343 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007344 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007345 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007346
7347 TemplateArgumentListInfo ToTAInfo;
7348 TemplateArgumentListInfo *ResInfo = nullptr;
7349 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007350 if (Error Err =
7351 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7352 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007353 ResInfo = &ToTAInfo;
7354 }
7355
Balazs Keri3b30d652018-10-19 13:32:20 +00007356 Expr *ToBase = nullptr;
7357 if (!E->isImplicitAccess()) {
7358 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7359 ToBase = *ToBaseOrErr;
7360 else
7361 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007362 }
7363
7364 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007365 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7366 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7367 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007368}
7369
Balazs Keri3b30d652018-10-19 13:32:20 +00007370ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7371 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7372 if (!Imp)
7373 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007374
Balazs Keri3b30d652018-10-19 13:32:20 +00007375 Expr *ToCallee;
7376 QualType ToType;
7377 SourceLocation ToRParenLoc;
7378 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007379
7380 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007381 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7382 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7383 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007384
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007385 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007386 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007387 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007388 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7389 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007390 }
7391
Bruno Riccic5885cf2018-12-21 15:20:32 +00007392 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7393 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7394 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007395}
7396
Balazs Keri3b30d652018-10-19 13:32:20 +00007397ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7398 CXXRecordDecl *FromClass = E->getLambdaClass();
7399 auto ToClassOrErr = import(FromClass);
7400 if (!ToClassOrErr)
7401 return ToClassOrErr.takeError();
7402 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007403
7404 // NOTE: lambda classes are created with BeingDefined flag set up.
7405 // It means that ImportDefinition doesn't work for them and we should fill it
7406 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007407 if (ToClass->isBeingDefined())
7408 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7409 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007410
Balazs Keri3b30d652018-10-19 13:32:20 +00007411 auto ToCallOpOrErr = import(E->getCallOperator());
7412 if (!ToCallOpOrErr)
7413 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007414
7415 ToClass->completeDefinition();
7416
Balazs Keri3b30d652018-10-19 13:32:20 +00007417 SmallVector<LambdaCapture, 8> ToCaptures;
7418 ToCaptures.reserve(E->capture_size());
7419 for (const auto &FromCapture : E->captures()) {
7420 if (auto ToCaptureOrErr = import(FromCapture))
7421 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007422 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007423 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007424 }
7425
Balazs Keri3b30d652018-10-19 13:32:20 +00007426 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7427 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7428 return std::move(Err);
7429
7430 auto Imp = importSeq(
7431 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7432 if (!Imp)
7433 return Imp.takeError();
7434
7435 SourceRange ToIntroducerRange;
7436 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7437 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007438
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007439 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007440 Importer.getToContext(), ToClass, ToIntroducerRange,
7441 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7442 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7443 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007444}
7445
Sean Callanan8bca9962016-03-28 21:43:01 +00007446
Balazs Keri3b30d652018-10-19 13:32:20 +00007447ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7448 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7449 if (!Imp)
7450 return Imp.takeError();
7451
7452 SourceLocation ToLBraceLoc, ToRBraceLoc;
7453 QualType ToType;
7454 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7455
7456 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7457 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7458 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007459
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007460 ASTContext &ToCtx = Importer.getToContext();
7461 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007462 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7463 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007464
Balazs Keri3b30d652018-10-19 13:32:20 +00007465 if (E->hasArrayFiller()) {
7466 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7467 To->setArrayFiller(*ToFillerOrErr);
7468 else
7469 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007470 }
7471
Balazs Keri3b30d652018-10-19 13:32:20 +00007472 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7473 if (auto ToFDOrErr = import(FromFD))
7474 To->setInitializedFieldInUnion(*ToFDOrErr);
7475 else
7476 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007477 }
7478
Balazs Keri3b30d652018-10-19 13:32:20 +00007479 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7480 if (auto ToSyntFormOrErr = import(SyntForm))
7481 To->setSyntacticForm(*ToSyntFormOrErr);
7482 else
7483 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007484 }
7485
Gabor Martona20ce602018-09-03 13:10:53 +00007486 // Copy InitListExprBitfields, which are not handled in the ctor of
7487 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007488 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007489
7490 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007491}
7492
Balazs Keri3b30d652018-10-19 13:32:20 +00007493ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007494 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007495 ExpectedType ToTypeOrErr = import(E->getType());
7496 if (!ToTypeOrErr)
7497 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007498
Balazs Keri3b30d652018-10-19 13:32:20 +00007499 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7500 if (!ToSubExprOrErr)
7501 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007502
Balazs Keri3b30d652018-10-19 13:32:20 +00007503 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7504 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007505}
7506
Balazs Keri3b30d652018-10-19 13:32:20 +00007507ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007508 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007509 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7510 if (!Imp)
7511 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007512
Balazs Keri3b30d652018-10-19 13:32:20 +00007513 SourceLocation ToLocation;
7514 QualType ToType;
7515 CXXConstructorDecl *ToConstructor;
7516 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007517
7518 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007519 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7520 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007521}
7522
Balazs Keri3b30d652018-10-19 13:32:20 +00007523ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7524 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7525 if (!Imp)
7526 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007527
Balazs Keri3b30d652018-10-19 13:32:20 +00007528 QualType ToType;
7529 Expr *ToCommonExpr, *ToSubExpr;
7530 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007531
Balazs Keri3b30d652018-10-19 13:32:20 +00007532 return new (Importer.getToContext()) ArrayInitLoopExpr(
7533 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007534}
7535
Balazs Keri3b30d652018-10-19 13:32:20 +00007536ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7537 ExpectedType ToTypeOrErr = import(E->getType());
7538 if (!ToTypeOrErr)
7539 return ToTypeOrErr.takeError();
7540 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007541}
7542
Balazs Keri3b30d652018-10-19 13:32:20 +00007543ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7544 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7545 if (!ToBeginLocOrErr)
7546 return ToBeginLocOrErr.takeError();
7547
7548 auto ToFieldOrErr = import(E->getField());
7549 if (!ToFieldOrErr)
7550 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007551
Eric Fiselier708afb52019-05-16 21:04:15 +00007552 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7553 if (!UsedContextOrErr)
7554 return UsedContextOrErr.takeError();
7555
Sean Callanandd2c1742016-05-16 20:48:03 +00007556 return CXXDefaultInitExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00007557 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007558}
7559
Balazs Keri3b30d652018-10-19 13:32:20 +00007560ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7561 auto Imp = importSeq(
7562 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7563 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7564 if (!Imp)
7565 return Imp.takeError();
7566
7567 QualType ToType;
7568 Expr *ToSubExpr;
7569 TypeSourceInfo *ToTypeInfoAsWritten;
7570 SourceLocation ToOperatorLoc, ToRParenLoc;
7571 SourceRange ToAngleBrackets;
7572 std::tie(
7573 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7574 ToAngleBrackets) = *Imp;
7575
Sean Callanandd2c1742016-05-16 20:48:03 +00007576 ExprValueKind VK = E->getValueKind();
7577 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007578 auto ToBasePathOrErr = ImportCastPath(E);
7579 if (!ToBasePathOrErr)
7580 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007581
Sean Callanandd2c1742016-05-16 20:48:03 +00007582 if (isa<CXXStaticCastExpr>(E)) {
7583 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007584 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7585 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007586 } else if (isa<CXXDynamicCastExpr>(E)) {
7587 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007588 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7589 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007590 } else if (isa<CXXReinterpretCastExpr>(E)) {
7591 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007592 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7593 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007594 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007595 return CXXConstCastExpr::Create(
7596 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7597 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007598 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007599 llvm_unreachable("Unknown cast type");
7600 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007601 }
7602}
7603
Balazs Keri3b30d652018-10-19 13:32:20 +00007604ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007605 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007606 auto Imp = importSeq(
7607 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7608 if (!Imp)
7609 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007610
Balazs Keri3b30d652018-10-19 13:32:20 +00007611 QualType ToType;
7612 SourceLocation ToExprLoc;
7613 NonTypeTemplateParmDecl *ToParameter;
7614 Expr *ToReplacement;
7615 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007616
7617 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007618 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007619}
7620
Balazs Keri3b30d652018-10-19 13:32:20 +00007621ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7622 auto Imp = importSeq(
7623 E->getType(), E->getBeginLoc(), E->getEndLoc());
7624 if (!Imp)
7625 return Imp.takeError();
7626
7627 QualType ToType;
7628 SourceLocation ToBeginLoc, ToEndLoc;
7629 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007630
7631 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007632 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7633 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007634
7635 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7636 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007637 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007638
7639 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007640 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7641 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007642}
7643
Balazs Keri3b30d652018-10-19 13:32:20 +00007644ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7645 ExpectedType ToTypeOrErr = import(E->getType());
7646 if (!ToTypeOrErr)
7647 return ToTypeOrErr.takeError();
7648
7649 auto ToSourceRangeOrErr = import(E->getSourceRange());
7650 if (!ToSourceRangeOrErr)
7651 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007652
7653 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007654 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7655 return new (Importer.getToContext()) CXXTypeidExpr(
7656 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7657 else
7658 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007659 }
7660
Balazs Keri3b30d652018-10-19 13:32:20 +00007661 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7662 if (!ToExprOperandOrErr)
7663 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007664
Balazs Keri3b30d652018-10-19 13:32:20 +00007665 return new (Importer.getToContext()) CXXTypeidExpr(
7666 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007667}
7668
Lang Hames19e07e12017-06-20 21:06:00 +00007669void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7670 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007671 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7672 if (auto ImportedOrErr = import(FromOverriddenMethod))
7673 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7674 (*ImportedOrErr)->getCanonicalDecl()));
7675 else
7676 consumeError(ImportedOrErr.takeError());
7677 }
Lang Hames19e07e12017-06-20 21:06:00 +00007678}
7679
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007680ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007681 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007682 bool MinimalImport,
7683 ASTImporterLookupTable *LookupTable)
7684 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007685 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7686 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007687
7688 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7689 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007690}
7691
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007692ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007693
Gabor Marton54058b52018-12-17 13:53:12 +00007694Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7695 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7696 "Try to get field index for non-field.");
7697
7698 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7699 if (!Owner)
7700 return None;
7701
7702 unsigned Index = 0;
7703 for (const auto *D : Owner->decls()) {
7704 if (D == F)
7705 return Index;
7706
7707 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7708 ++Index;
7709 }
7710
7711 llvm_unreachable("Field was not found in its parent context.");
7712
7713 return None;
7714}
7715
7716ASTImporter::FoundDeclsTy
7717ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7718 // We search in the redecl context because of transparent contexts.
7719 // E.g. a simple C language enum is a transparent context:
7720 // enum E { A, B };
7721 // Now if we had a global variable in the TU
7722 // int A;
7723 // then the enum constant 'A' and the variable 'A' violates ODR.
7724 // We can diagnose this only if we search in the redecl context.
7725 DeclContext *ReDC = DC->getRedeclContext();
7726 if (LookupTable) {
7727 ASTImporterLookupTable::LookupResult LookupResult =
7728 LookupTable->lookup(ReDC, Name);
7729 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7730 } else {
7731 // FIXME Can we remove this kind of lookup?
7732 // Or lldb really needs this C/C++ lookup?
7733 FoundDeclsTy Result;
7734 ReDC->localUncachedLookup(Name, Result);
7735 return Result;
7736 }
7737}
7738
7739void ASTImporter::AddToLookupTable(Decl *ToD) {
7740 if (LookupTable)
7741 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7742 LookupTable->add(ToND);
7743}
7744
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007745Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7746 // Import the decl using ASTNodeImporter.
7747 ASTNodeImporter Importer(*this);
7748 return Importer.Visit(FromD);
7749}
7750
7751void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7752 MapImported(FromD, ToD);
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007753}
7754
Gabor Marton5ac6d492019-05-15 10:29:48 +00007755Expected<QualType> ASTImporter::Import(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007756 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007757 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007758
Balazs Keri3b30d652018-10-19 13:32:20 +00007759 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007760
7761 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007762 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007763 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007764 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007765 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007766
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007767 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007768 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007769 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007770 if (!ToTOrErr)
7771 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007772
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007773 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007774 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007775
Balazs Keri3b30d652018-10-19 13:32:20 +00007776 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007777}
7778
Gabor Marton5ac6d492019-05-15 10:29:48 +00007779Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007780 if (!FromTSI)
7781 return FromTSI;
7782
7783 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007784 // on the type and a single location. Implement a real version of this.
Gabor Marton5ac6d492019-05-15 10:29:48 +00007785 ExpectedType TOrErr = Import(FromTSI->getType());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007786 if (!TOrErr)
7787 return TOrErr.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00007788 ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007789 if (!BeginLocOrErr)
7790 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007791
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007792 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7793}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007794
Gabor Marton5ac6d492019-05-15 10:29:48 +00007795Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007796 Attr *ToAttr = FromAttr->clone(ToContext);
Gabor Marton5ac6d492019-05-15 10:29:48 +00007797 if (auto ToRangeOrErr = Import(FromAttr->getRange()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007798 ToAttr->setRange(*ToRangeOrErr);
7799 else
7800 return ToRangeOrErr.takeError();
7801
Davide Italianofaee83d2018-11-28 19:15:23 +00007802 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007803}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007804
Gabor Martonbe77a982018-12-12 11:22:55 +00007805Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7806 auto Pos = ImportedDecls.find(FromD);
7807 if (Pos != ImportedDecls.end())
7808 return Pos->second;
7809 else
Sean Callanan59721b32015-04-28 18:41:46 +00007810 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007811}
7812
Gabor Marton458d1452019-02-14 13:07:03 +00007813TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7814 auto FromDPos = ImportedFromDecls.find(ToD);
7815 if (FromDPos == ImportedFromDecls.end())
7816 return nullptr;
7817 return FromDPos->second->getTranslationUnitDecl();
7818}
7819
Gabor Marton5ac6d492019-05-15 10:29:48 +00007820Expected<Decl *> ASTImporter::Import(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007821 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007822 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007823
Douglas Gregord451ea92011-07-29 23:31:30 +00007824
Gabor Marton303c98612019-06-25 08:00:51 +00007825 // Check whether there was a previous failed import.
7826 // If yes return the existing error.
7827 if (auto Error = getImportDeclErrorIfAny(FromD))
7828 return make_error<ImportError>(*Error);
7829
Gabor Marton26f72a92018-07-12 09:42:05 +00007830 // Check whether we've already imported this declaration.
7831 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7832 if (ToD) {
7833 // If FromD has some updated flags after last import, apply it
7834 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007835 return ToD;
7836 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007837
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007838 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007839 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Gabor Marton303c98612019-06-25 08:00:51 +00007840 if (!ToDOrErr) {
7841 // Failed to import.
7842
7843 auto Pos = ImportedDecls.find(FromD);
7844 if (Pos != ImportedDecls.end()) {
7845 // Import failed after the object was created.
7846 // Remove all references to it.
7847 auto *ToD = Pos->second;
7848 ImportedDecls.erase(Pos);
7849
7850 // ImportedDecls and ImportedFromDecls are not symmetric. It may happen
7851 // (e.g. with namespaces) that several decls from the 'from' context are
7852 // mapped to the same decl in the 'to' context. If we removed entries
7853 // from the LookupTable here then we may end up removing them multiple
7854 // times.
7855
7856 // The Lookuptable contains decls only which are in the 'to' context.
7857 // Remove from the Lookuptable only if it is *imported* into the 'to'
7858 // context (and do not remove it if it was added during the initial
7859 // traverse of the 'to' context).
7860 auto PosF = ImportedFromDecls.find(ToD);
7861 if (PosF != ImportedFromDecls.end()) {
7862 if (LookupTable)
7863 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7864 LookupTable->remove(ToND);
7865 ImportedFromDecls.erase(PosF);
7866 }
7867
7868 // FIXME: AST may contain remaining references to the failed object.
7869 }
7870
7871 // Error encountered for the first time.
7872 assert(!getImportDeclErrorIfAny(FromD) &&
7873 "Import error already set for Decl.");
7874
7875 // After takeError the error is not usable any more in ToDOrErr.
7876 // Get a copy of the error object (any more simple solution for this?).
7877 ImportError ErrOut;
7878 handleAllErrors(ToDOrErr.takeError(),
7879 [&ErrOut](const ImportError &E) { ErrOut = E; });
7880 setImportDeclError(FromD, ErrOut);
7881 // Do not return ToDOrErr, error was taken out of it.
7882 return make_error<ImportError>(ErrOut);
7883 }
7884
Balazs Keri3b30d652018-10-19 13:32:20 +00007885 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007886
Gabor Marton303c98612019-06-25 08:00:51 +00007887 // FIXME: Handle the "already imported with error" case. We can get here
7888 // nullptr only if GetImportedOrCreateDecl returned nullptr (after a
7889 // previously failed create was requested).
7890 // Later GetImportedOrCreateDecl can be updated to return the error.
Gabor Marton7f8c4002019-03-19 13:34:10 +00007891 if (!ToD) {
Gabor Marton303c98612019-06-25 08:00:51 +00007892 auto Err = getImportDeclErrorIfAny(FromD);
7893 assert(Err);
7894 return make_error<ImportError>(*Err);
Gabor Marton7f8c4002019-03-19 13:34:10 +00007895 }
7896
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007897 // Make sure that ImportImpl registered the imported decl.
7898 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
Gabor Marton26f72a92018-07-12 09:42:05 +00007899 // Notify subclasses.
7900 Imported(FromD, ToD);
7901
Gabor Martonac3a5d62018-09-17 12:04:52 +00007902 updateFlags(FromD, ToD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007903 return ToDOrErr;
7904}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007905
Balazs Keri3b30d652018-10-19 13:32:20 +00007906Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007907 if (!FromDC)
7908 return FromDC;
7909
Gabor Marton5ac6d492019-05-15 10:29:48 +00007910 ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC));
Balazs Keria1f6b102019-04-08 13:59:15 +00007911 if (!ToDCOrErr)
7912 return ToDCOrErr.takeError();
7913 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00007914
Fangrui Song6907ce22018-07-30 19:24:48 +00007915 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007916 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007917 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7918 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007919 if (ToRecord->isCompleteDefinition()) {
7920 // Do nothing.
7921 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007922 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7923 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7924 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007925 } else {
7926 CompleteDecl(ToRecord);
7927 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007928 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7929 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007930 if (ToEnum->isCompleteDefinition()) {
7931 // Do nothing.
7932 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007933 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7934 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7935 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007936 } else {
7937 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007938 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007939 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7940 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007941 if (ToClass->getDefinition()) {
7942 // Do nothing.
7943 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007944 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7945 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7946 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007947 } else {
7948 CompleteDecl(ToClass);
7949 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007950 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7951 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007952 if (ToProto->getDefinition()) {
7953 // Do nothing.
7954 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007955 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7956 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7957 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007958 } else {
7959 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007960 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007961 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007962
Douglas Gregor95d82832012-01-24 18:36:04 +00007963 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007964}
7965
Gabor Marton5ac6d492019-05-15 10:29:48 +00007966Expected<Expr *> ASTImporter::Import(Expr *FromE) {
7967 if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE)))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007968 return cast_or_null<Expr>(*ToSOrErr);
7969 else
7970 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00007971}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007972
Gabor Marton5ac6d492019-05-15 10:29:48 +00007973Expected<Stmt *> ASTImporter::Import(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007974 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007975 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007976
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007977 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007978 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7979 if (Pos != ImportedStmts.end())
7980 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007981
Balazs Keri3b30d652018-10-19 13:32:20 +00007982 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007983 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007984 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007985 if (!ToSOrErr)
7986 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007987
Balazs Keri3b30d652018-10-19 13:32:20 +00007988 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007989 auto *FromE = cast<Expr>(FromS);
7990 // Copy ExprBitfields, which may not be handled in Expr subclasses
7991 // constructors.
7992 ToE->setValueKind(FromE->getValueKind());
7993 ToE->setObjectKind(FromE->getObjectKind());
7994 ToE->setTypeDependent(FromE->isTypeDependent());
7995 ToE->setValueDependent(FromE->isValueDependent());
7996 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7997 ToE->setContainsUnexpandedParameterPack(
7998 FromE->containsUnexpandedParameterPack());
7999 }
8000
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008001 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00008002 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008003 return ToSOrErr;
8004}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008005
Balazs Keri4a3d7582018-11-27 18:36:31 +00008006Expected<NestedNameSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008007ASTImporter::Import(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008008 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00008009 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008010
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008011 NestedNameSpecifier *Prefix;
8012 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
8013 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00008014
8015 switch (FromNNS->getKind()) {
8016 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008017 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
8018 return NestedNameSpecifier::Create(ToContext, Prefix,
8019 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00008020
8021 case NestedNameSpecifier::Namespace:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008022 if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008023 return NestedNameSpecifier::Create(ToContext, Prefix,
8024 cast<NamespaceDecl>(*NSOrErr));
8025 } else
8026 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008027
8028 case NestedNameSpecifier::NamespaceAlias:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008029 if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008030 return NestedNameSpecifier::Create(ToContext, Prefix,
8031 cast<NamespaceAliasDecl>(*NSADOrErr));
8032 else
8033 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008034
8035 case NestedNameSpecifier::Global:
8036 return NestedNameSpecifier::GlobalSpecifier(ToContext);
8037
Nikola Smiljanic67860242014-09-26 00:28:20 +00008038 case NestedNameSpecifier::Super:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008039 if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008040 return NestedNameSpecifier::SuperSpecifier(ToContext,
8041 cast<CXXRecordDecl>(*RDOrErr));
8042 else
8043 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008044
Douglas Gregor90ebf252011-04-27 16:48:40 +00008045 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008046 case NestedNameSpecifier::TypeSpecWithTemplate:
8047 if (Expected<QualType> TyOrErr =
Gabor Marton5ac6d492019-05-15 10:29:48 +00008048 Import(QualType(FromNNS->getAsType(), 0u))) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008049 bool TSTemplate =
8050 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
8051 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
8052 TyOrErr->getTypePtr());
8053 } else {
8054 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008055 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008056 }
8057
8058 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008059}
8060
Balazs Keri4a3d7582018-11-27 18:36:31 +00008061Expected<NestedNameSpecifierLoc>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008062ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008063 // Copied from NestedNameSpecifier mostly.
8064 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8065 NestedNameSpecifierLoc NNS = FromNNS;
8066
8067 // Push each of the nested-name-specifiers's onto a stack for
8068 // serialization in reverse order.
8069 while (NNS) {
8070 NestedNames.push_back(NNS);
8071 NNS = NNS.getPrefix();
8072 }
8073
8074 NestedNameSpecifierLocBuilder Builder;
8075
8076 while (!NestedNames.empty()) {
8077 NNS = NestedNames.pop_back_val();
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00008078 NestedNameSpecifier *Spec = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008079 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8080 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008081
8082 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008083
8084 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8085 if (Kind != NestedNameSpecifier::Super) {
8086 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8087 return std::move(Err);
8088
8089 if (Kind != NestedNameSpecifier::Global)
8090 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8091 return std::move(Err);
8092 }
8093
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008094 switch (Kind) {
8095 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008096 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8097 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008098 break;
8099
8100 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008101 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8102 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008103 break;
8104
8105 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008106 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8107 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008108 break;
8109
8110 case NestedNameSpecifier::TypeSpec:
8111 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008112 SourceLocation ToTLoc;
8113 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8114 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008115 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008116 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008117 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8118 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008119 break;
8120 }
8121
8122 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008123 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008124 break;
8125
8126 case NestedNameSpecifier::Super: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008127 auto ToSourceRangeOrErr = Import(NNS.getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008128 if (!ToSourceRangeOrErr)
8129 return ToSourceRangeOrErr.takeError();
8130
8131 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8132 ToSourceRangeOrErr->getBegin(),
8133 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008134 }
8135 }
8136 }
8137
8138 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008139}
8140
Gabor Marton5ac6d492019-05-15 10:29:48 +00008141Expected<TemplateName> ASTImporter::Import(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008142 switch (From.getKind()) {
8143 case TemplateName::Template:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008144 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008145 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8146 else
8147 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008148
Douglas Gregore2e50d332010-12-01 01:36:18 +00008149 case TemplateName::OverloadedTemplate: {
8150 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8151 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008152 for (auto *I : *FromStorage) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008153 if (auto ToOrErr = Import(I))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008154 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008155 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008156 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008157 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008158 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008159 ToTemplates.end());
8160 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008161
Richard Smithb23c5e82019-05-09 03:31:27 +00008162 case TemplateName::AssumedTemplate: {
8163 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008164 auto DeclNameOrErr = Import(FromStorage->getDeclName());
Richard Smithb23c5e82019-05-09 03:31:27 +00008165 if (!DeclNameOrErr)
8166 return DeclNameOrErr.takeError();
8167 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8168 }
8169
Douglas Gregore2e50d332010-12-01 01:36:18 +00008170 case TemplateName::QualifiedTemplate: {
8171 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008172 auto QualifierOrErr = Import(QTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008173 if (!QualifierOrErr)
8174 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008175
Gabor Marton5ac6d492019-05-15 10:29:48 +00008176 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008177 return ToContext.getQualifiedTemplateName(
8178 *QualifierOrErr, QTN->hasTemplateKeyword(),
8179 cast<TemplateDecl>(*ToTemplateOrErr));
8180 else
8181 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008182 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008183
Douglas Gregore2e50d332010-12-01 01:36:18 +00008184 case TemplateName::DependentTemplate: {
8185 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008186 auto QualifierOrErr = Import(DTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008187 if (!QualifierOrErr)
8188 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008189
Douglas Gregore2e50d332010-12-01 01:36:18 +00008190 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008191 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008192 Import(DTN->getIdentifier()));
8193 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008194
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008195 return ToContext.getDependentTemplateName(*QualifierOrErr,
8196 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008197 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008198
8199 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008200 SubstTemplateTemplateParmStorage *Subst =
8201 From.getAsSubstTemplateTemplateParm();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008202 ExpectedDecl ParamOrErr = Import(Subst->getParameter());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008203 if (!ParamOrErr)
8204 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008205
Gabor Marton5ac6d492019-05-15 10:29:48 +00008206 auto ReplacementOrErr = Import(Subst->getReplacement());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008207 if (!ReplacementOrErr)
8208 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008209
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008210 return ToContext.getSubstTemplateTemplateParm(
8211 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008212 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008213
Douglas Gregor5590be02011-01-15 06:45:20 +00008214 case TemplateName::SubstTemplateTemplateParmPack: {
8215 SubstTemplateTemplateParmPackStorage *SubstPack
8216 = From.getAsSubstTemplateTemplateParmPack();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008217 ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008218 if (!ParamOrErr)
8219 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008220
Douglas Gregor5590be02011-01-15 06:45:20 +00008221 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008222 auto ArgPackOrErr =
8223 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8224 if (!ArgPackOrErr)
8225 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008226
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008227 return ToContext.getSubstTemplateTemplateParmPack(
8228 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008229 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008230 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008231
Douglas Gregore2e50d332010-12-01 01:36:18 +00008232 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008233}
8234
Gabor Marton5ac6d492019-05-15 10:29:48 +00008235Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008236 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008237 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008238
Douglas Gregor811663e2010-02-10 00:15:17 +00008239 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008240 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008241
Douglas Gregor811663e2010-02-10 00:15:17 +00008242 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008243 Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008244 if (!ToFileIDOrErr)
8245 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008246 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008247 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8248}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008249
Gabor Marton5ac6d492019-05-15 10:29:48 +00008250Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008251 SourceLocation ToBegin, ToEnd;
8252 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8253 return std::move(Err);
8254 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8255 return std::move(Err);
8256
8257 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008258}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008259
Gabor Marton5ac6d492019-05-15 10:29:48 +00008260Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008261 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008262 if (Pos != ImportedFileIDs.end())
8263 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008264
Douglas Gregor811663e2010-02-10 00:15:17 +00008265 SourceManager &FromSM = FromContext.getSourceManager();
8266 SourceManager &ToSM = ToContext.getSourceManager();
8267 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008268
8269 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008270 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008271 if (FromSLoc.isExpansion()) {
8272 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008273 ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008274 if (!ToSpLoc)
8275 return ToSpLoc.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008276 ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008277 if (!ToExLocS)
8278 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008279 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8280 SourceLocation MLoc;
8281 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008282 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008283 } else {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008284 if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008285 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8286 FromEx.isExpansionTokenRange());
8287 else
8288 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008289 }
8290 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008291 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008292 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008293
8294 if (!IsBuiltin) {
8295 // Include location of this file.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008296 ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008297 if (!ToIncludeLoc)
8298 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008299
8300 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8301 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8302 // disk again
8303 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8304 // than mmap the files several times.
8305 const FileEntry *Entry =
8306 ToFileManager.getFile(Cache->OrigEntry->getName());
8307 // FIXME: The filename may be a virtual name that does probably not
8308 // point to a valid file and we get no Entry here. In this case try with
8309 // the memory buffer below.
8310 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008311 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008312 FromSLoc.getFile().getFileCharacteristic());
8313 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008314 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008315
8316 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008317 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008318 bool Invalid = true;
8319 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8320 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8321 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008322 // FIXME: Use a new error kind?
8323 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008324
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008325 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8326 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8327 FromBuf->getBufferIdentifier());
8328 ToID = ToSM.createFileID(std::move(ToBuf),
8329 FromSLoc.getFile().getFileCharacteristic());
8330 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008331 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008332
Balazs Keri9cf39df2019-02-27 16:31:48 +00008333 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8334
Sebastian Redl99219f12010-09-30 01:03:06 +00008335 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008336 return ToID;
8337}
8338
Gabor Marton5ac6d492019-05-15 10:29:48 +00008339Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) {
8340 ExpectedExpr ToExprOrErr = Import(From->getInit());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008341 if (!ToExprOrErr)
8342 return ToExprOrErr.takeError();
8343
Gabor Marton5ac6d492019-05-15 10:29:48 +00008344 auto LParenLocOrErr = Import(From->getLParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008345 if (!LParenLocOrErr)
8346 return LParenLocOrErr.takeError();
8347
Gabor Marton5ac6d492019-05-15 10:29:48 +00008348 auto RParenLocOrErr = Import(From->getRParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008349 if (!RParenLocOrErr)
8350 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008351
8352 if (From->isBaseInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008353 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008354 if (!ToTInfoOrErr)
8355 return ToTInfoOrErr.takeError();
8356
8357 SourceLocation EllipsisLoc;
8358 if (From->isPackExpansion())
8359 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8360 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008361
8362 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008363 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8364 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008365 } else if (From->isMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008366 ExpectedDecl ToFieldOrErr = Import(From->getMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008367 if (!ToFieldOrErr)
8368 return ToFieldOrErr.takeError();
8369
Gabor Marton5ac6d492019-05-15 10:29:48 +00008370 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008371 if (!MemberLocOrErr)
8372 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008373
8374 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008375 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8376 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008377 } else if (From->isIndirectMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008378 ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008379 if (!ToIFieldOrErr)
8380 return ToIFieldOrErr.takeError();
8381
Gabor Marton5ac6d492019-05-15 10:29:48 +00008382 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008383 if (!MemberLocOrErr)
8384 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008385
8386 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008387 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8388 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008389 } else if (From->isDelegatingInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008390 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008391 if (!ToTInfoOrErr)
8392 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008393
8394 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008395 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8396 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008397 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008398 // FIXME: assert?
8399 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008400 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008401}
Sean Callanandd2c1742016-05-16 20:48:03 +00008402
Balazs Keri4a3d7582018-11-27 18:36:31 +00008403Expected<CXXBaseSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008404ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008405 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8406 if (Pos != ImportedCXXBaseSpecifiers.end())
8407 return Pos->second;
8408
Gabor Marton5ac6d492019-05-15 10:29:48 +00008409 Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008410 if (!ToSourceRange)
8411 return ToSourceRange.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008412 Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008413 if (!ToTSI)
8414 return ToTSI.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008415 ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008416 if (!ToEllipsisLoc)
8417 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008418 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008419 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8420 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008421 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8422 return Imported;
8423}
8424
Gabor Marton5ac6d492019-05-15 10:29:48 +00008425Error ASTImporter::ImportDefinition(Decl *From) {
8426 ExpectedDecl ToOrErr = Import(From);
8427 if (!ToOrErr)
8428 return ToOrErr.takeError();
8429 Decl *To = *ToOrErr;
Fangrui Song6907ce22018-07-30 19:24:48 +00008430
Don Hintonf170dff2019-03-19 06:14:14 +00008431 auto *FromDC = cast<DeclContext>(From);
8432 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008433
Don Hintonf170dff2019-03-19 06:14:14 +00008434 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8435 if (!ToRecord->getDefinition()) {
8436 return Importer.ImportDefinition(
8437 cast<RecordDecl>(FromDC), ToRecord,
8438 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008439 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008440 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008441
Don Hintonf170dff2019-03-19 06:14:14 +00008442 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8443 if (!ToEnum->getDefinition()) {
8444 return Importer.ImportDefinition(
8445 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8446 }
8447 }
8448
8449 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8450 if (!ToIFace->getDefinition()) {
8451 return Importer.ImportDefinition(
8452 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8453 ASTNodeImporter::IDK_Everything);
8454 }
8455 }
8456
8457 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8458 if (!ToProto->getDefinition()) {
8459 return Importer.ImportDefinition(
8460 cast<ObjCProtocolDecl>(FromDC), ToProto,
8461 ASTNodeImporter::IDK_Everything);
8462 }
8463 }
8464
8465 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008466}
8467
Gabor Marton5ac6d492019-05-15 10:29:48 +00008468Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008469 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008470 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008471
8472 switch (FromName.getNameKind()) {
8473 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008474 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008475
8476 case DeclarationName::ObjCZeroArgSelector:
8477 case DeclarationName::ObjCOneArgSelector:
8478 case DeclarationName::ObjCMultiArgSelector:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008479 if (auto ToSelOrErr = Import(FromName.getObjCSelector()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008480 return DeclarationName(*ToSelOrErr);
8481 else
8482 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008483
8484 case DeclarationName::CXXConstructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008485 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008486 return ToContext.DeclarationNames.getCXXConstructorName(
8487 ToContext.getCanonicalType(*ToTyOrErr));
8488 else
8489 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008490 }
8491
8492 case DeclarationName::CXXDestructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008493 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008494 return ToContext.DeclarationNames.getCXXDestructorName(
8495 ToContext.getCanonicalType(*ToTyOrErr));
8496 else
8497 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008498 }
8499
Richard Smith35845152017-02-07 01:37:30 +00008500 case DeclarationName::CXXDeductionGuideName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008501 if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008502 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8503 cast<TemplateDecl>(*ToTemplateOrErr));
8504 else
8505 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008506 }
8507
Douglas Gregor96e578d2010-02-05 17:54:41 +00008508 case DeclarationName::CXXConversionFunctionName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008509 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008510 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8511 ToContext.getCanonicalType(*ToTyOrErr));
8512 else
8513 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008514 }
8515
8516 case DeclarationName::CXXOperatorName:
8517 return ToContext.DeclarationNames.getCXXOperatorName(
8518 FromName.getCXXOverloadedOperator());
8519
8520 case DeclarationName::CXXLiteralOperatorName:
8521 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008522 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008523
8524 case DeclarationName::CXXUsingDirective:
8525 // FIXME: STATICS!
8526 return DeclarationName::getUsingDirectiveName();
8527 }
8528
David Blaikiee4d798f2012-01-20 21:50:17 +00008529 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008530}
8531
Douglas Gregore2e50d332010-12-01 01:36:18 +00008532IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008533 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008534 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008535
Sean Callananf94ef1d2016-05-14 06:11:19 +00008536 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8537
8538 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8539 ToId->setBuiltinID(FromId->getBuiltinID());
8540
8541 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008542}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008543
Gabor Marton5ac6d492019-05-15 10:29:48 +00008544Expected<Selector> ASTImporter::Import(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008545 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008546 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008547
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008548 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008549 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8550 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8551 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8552 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8553}
8554
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008555DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8556 DeclContext *DC,
8557 unsigned IDNS,
8558 NamedDecl **Decls,
8559 unsigned NumDecls) {
8560 return Name;
8561}
8562
8563DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008564 if (LastDiagFromFrom)
8565 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8566 FromContext.getDiagnostics());
8567 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008568 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008569}
8570
8571DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008572 if (!LastDiagFromFrom)
8573 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8574 ToContext.getDiagnostics());
8575 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008576 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008577}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008578
Douglas Gregor2e15c842012-02-01 21:00:38 +00008579void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008580 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008581 if (!ID->getDefinition())
8582 ID->startDefinition();
8583 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008584 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008585 if (!PD->getDefinition())
8586 PD->startDefinition();
8587 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008588 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008589 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8590 TD->startDefinition();
8591 TD->setCompleteDefinition(true);
8592 }
8593 }
8594 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008595 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008596 }
8597}
8598
Gabor Marton26f72a92018-07-12 09:42:05 +00008599Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8600 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8601 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8602 "Try to import an already imported Decl");
8603 if (Pos != ImportedDecls.end())
8604 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008605 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008606 // This mapping should be maintained only in this function. Therefore do not
8607 // check for additional consistency.
8608 ImportedFromDecls[To] = From;
Gabor Marton303c98612019-06-25 08:00:51 +00008609 AddToLookupTable(To);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008610 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008611}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008612
Gabor Marton303c98612019-06-25 08:00:51 +00008613llvm::Optional<ImportError>
8614ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const {
8615 auto Pos = ImportDeclErrors.find(FromD);
8616 if (Pos != ImportDeclErrors.end())
8617 return Pos->second;
8618 else
8619 return Optional<ImportError>();
8620}
8621
8622void ASTImporter::setImportDeclError(Decl *From, ImportError Error) {
8623 assert(ImportDeclErrors.find(From) == ImportDeclErrors.end() &&
8624 "Setting import error allowed only once for a Decl.");
8625 ImportDeclErrors[From] = Error;
8626}
8627
Douglas Gregordd6006f2012-07-17 21:16:27 +00008628bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8629 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008630 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8631 ImportedTypes.find(From.getTypePtr());
8632 if (Pos != ImportedTypes.end()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008633 if (ExpectedType ToFromOrErr = Import(From)) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008634 if (ToContext.hasSameType(*ToFromOrErr, To))
8635 return true;
8636 } else {
8637 llvm::consumeError(ToFromOrErr.takeError());
8638 }
8639 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008640
Douglas Gregordd6006f2012-07-17 21:16:27 +00008641 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008642 getStructuralEquivalenceKind(*this), false,
8643 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008644 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008645}