blob: 651c675649ec69905c356c207ae550cd67d6f8b2 [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"
Ilya Biryukovabc744d2019-07-18 15:43:26 +000015#include "clang/AST/ASTImporterSharedState.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"
Balazs Kerid2c57612019-07-18 15:23:10 +000060#include "llvm/ADT/ScopeExit.h"
Ilya Biryukovabc744d2019-07-18 15:43:26 +000061#include "llvm/ADT/STLExtras.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000062#include "llvm/ADT/SmallVector.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000065#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000066#include <algorithm>
67#include <cassert>
68#include <cstddef>
69#include <memory>
70#include <type_traits>
71#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000072
Douglas Gregor3c2404b2011-11-03 18:07:07 +000073namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000074
Balazs Keri3b30d652018-10-19 13:32:20 +000075 using llvm::make_error;
76 using llvm::Error;
77 using llvm::Expected;
78 using ExpectedType = llvm::Expected<QualType>;
79 using ExpectedStmt = llvm::Expected<Stmt *>;
80 using ExpectedExpr = llvm::Expected<Expr *>;
81 using ExpectedDecl = llvm::Expected<Decl *>;
82 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000083
Balazs Keri3b30d652018-10-19 13:32:20 +000084 std::string ImportError::toString() const {
85 // FIXME: Improve error texts.
86 switch (Error) {
87 case NameConflict:
88 return "NameConflict";
89 case UnsupportedConstruct:
90 return "UnsupportedConstruct";
91 case Unknown:
92 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000093 }
Balazs Keri2a13d662018-10-19 15:16:51 +000094 llvm_unreachable("Invalid error code.");
95 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000096 }
97
Balazs Keri3b30d652018-10-19 13:32:20 +000098 void ImportError::log(raw_ostream &OS) const {
99 OS << toString();
100 }
101
102 std::error_code ImportError::convertToErrorCode() const {
103 llvm_unreachable("Function not implemented.");
104 }
105
106 char ImportError::ID;
107
Gabor Marton5254e642018-06-27 13:32:50 +0000108 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000109 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000110 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000111 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000112 for (auto *R : D->getFirstDecl()->redecls()) {
113 if (R != D->getFirstDecl())
114 Redecls.push_back(R);
115 }
116 Redecls.push_back(D->getFirstDecl());
117 std::reverse(Redecls.begin(), Redecls.end());
118 return Redecls;
119 }
120
121 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000122 if (auto *FD = dyn_cast<FunctionDecl>(D))
123 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
124 if (auto *VD = dyn_cast<VarDecl>(D))
125 return getCanonicalForwardRedeclChain<VarDecl>(VD);
Gabor Marton7df342a2018-12-17 12:42:12 +0000126 if (auto *TD = dyn_cast<TagDecl>(D))
127 return getCanonicalForwardRedeclChain<TagDecl>(TD);
Gabor Martonac3a5d62018-09-17 12:04:52 +0000128 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000129 }
130
Gabor Marton26f72a92018-07-12 09:42:05 +0000131 void updateFlags(const Decl *From, Decl *To) {
132 // Check if some flags or attrs are new in 'From' and copy into 'To'.
133 // FIXME: Other flags or attrs?
134 if (From->isUsed(false) && !To->isUsed(false))
135 To->setIsUsed();
136 }
137
Balazs Keri3b30d652018-10-19 13:32:20 +0000138 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
139 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
140 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000141 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000142
Balazs Keri3b30d652018-10-19 13:32:20 +0000143 // Use this instead of Importer.importInto .
144 template <typename ImportT>
145 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
146 return Importer.importInto(To, From);
147 }
148
149 // Use this to import pointers of specific type.
150 template <typename ImportT>
151 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000152 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000153 if (ToOrErr)
154 To = cast_or_null<ImportT>(*ToOrErr);
155 return ToOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +0000156 }
157
158 // Call the import function of ASTImporter for a baseclass of type `T` and
159 // cast the return value to `T`.
160 template <typename T>
161 Expected<T *> import(T *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000162 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000163 if (!ToOrErr)
164 return ToOrErr.takeError();
165 return cast_or_null<T>(*ToOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +0000166 }
167
168 template <typename T>
169 Expected<T *> import(const T *From) {
170 return import(const_cast<T *>(From));
171 }
172
173 // Call the import function of ASTImporter for type `T`.
174 template <typename T>
175 Expected<T> import(const T &From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000176 return Importer.Import(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000177 }
178
Richard Smithb9fb1212019-05-06 03:47:15 +0000179 // Import an Optional<T> by importing the contained T, if any.
180 template<typename T>
181 Expected<Optional<T>> import(Optional<T> From) {
182 if (!From)
183 return Optional<T>();
184 return import(*From);
185 }
186
Balazs Keri3b30d652018-10-19 13:32:20 +0000187 template <class T>
188 Expected<std::tuple<T>>
189 importSeq(const T &From) {
190 Expected<T> ToOrErr = import(From);
191 if (!ToOrErr)
192 return ToOrErr.takeError();
193 return std::make_tuple<T>(std::move(*ToOrErr));
194 }
195
196 // Import multiple objects with a single function call.
197 // This should work for every type for which a variant of `import` exists.
198 // The arguments are processed from left to right and import is stopped on
199 // first error.
200 template <class THead, class... TTail>
201 Expected<std::tuple<THead, TTail...>>
202 importSeq(const THead &FromHead, const TTail &...FromTail) {
203 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
204 if (!ToHeadOrErr)
205 return ToHeadOrErr.takeError();
206 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
207 if (!ToTailOrErr)
208 return ToTailOrErr.takeError();
209 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
210 }
211
212// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000213 template <typename ToDeclT> struct CallOverloadedCreateFun {
214 template <typename... Args>
215 auto operator()(Args &&... args)
216 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
217 return ToDeclT::Create(std::forward<Args>(args)...);
218 }
219 };
220
221 // Always use these functions to create a Decl during import. There are
222 // certain tasks which must be done after the Decl was created, e.g. we
223 // must immediately register that as an imported Decl. The parameter `ToD`
224 // will be set to the newly created Decl or if had been imported before
225 // then to the already imported Decl. Returns a bool value set to true if
226 // the `FromD` had been imported before.
227 template <typename ToDeclT, typename FromDeclT, typename... Args>
228 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
229 Args &&... args) {
230 // There may be several overloads of ToDeclT::Create. We must make sure
231 // to call the one which would be chosen by the arguments, thus we use a
232 // wrapper for the overload set.
233 CallOverloadedCreateFun<ToDeclT> OC;
234 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
235 std::forward<Args>(args)...);
236 }
237 // Use this overload if a special Type is needed to be created. E.g if we
238 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
239 // then:
240 // TypedefNameDecl *ToTypedef;
241 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
242 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
243 typename... Args>
244 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
245 Args &&... args) {
246 CallOverloadedCreateFun<NewDeclT> OC;
247 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
248 std::forward<Args>(args)...);
249 }
250 // Use this version if a special create function must be
251 // used, e.g. CXXRecordDecl::CreateLambda .
252 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
253 typename... Args>
254 LLVM_NODISCARD bool
255 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
256 FromDeclT *FromD, Args &&... args) {
Gabor Marton303c98612019-06-25 08:00:51 +0000257 if (Importer.getImportDeclErrorIfAny(FromD)) {
258 ToD = nullptr;
259 return true; // Already imported but with error.
260 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
262 if (ToD)
263 return true; // Already imported.
264 ToD = CreateFun(std::forward<Args>(args)...);
Gabor Marton54058b52018-12-17 13:53:12 +0000265 // Keep track of imported Decls.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +0000266 Importer.RegisterImportedDecl(FromD, ToD);
Gabor Marton26f72a92018-07-12 09:42:05 +0000267 InitializeImportedDecl(FromD, ToD);
268 return false; // A new Decl is created.
269 }
270
271 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
Gabor Marton26f72a92018-07-12 09:42:05 +0000272 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
273 if (FromD->hasAttrs())
Balazs Keri57949eb2019-03-25 09:16:39 +0000274 for (const Attr *FromAttr : FromD->getAttrs()) {
275 // FIXME: Return of the error here is not possible until store of
276 // import errors is implemented.
277 auto ToAttrOrErr = import(FromAttr);
278 if (ToAttrOrErr)
279 ToD->addAttr(*ToAttrOrErr);
280 else
281 llvm::consumeError(ToAttrOrErr.takeError());
282 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000283 if (FromD->isUsed())
284 ToD->setIsUsed();
285 if (FromD->isImplicit())
286 ToD->setImplicit();
287 }
288
Gabor Martondd59d272019-03-19 14:04:50 +0000289 // Check if we have found an existing definition. Returns with that
290 // definition if yes, otherwise returns null.
291 Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) {
292 const FunctionDecl *Definition = nullptr;
293 if (D->doesThisDeclarationHaveABody() &&
294 FoundFunction->hasBody(Definition))
295 return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition));
296 return nullptr;
297 }
298
Douglas Gregor96e578d2010-02-05 17:54:41 +0000299 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000300 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000301
Balazs Keri3b30d652018-10-19 13:32:20 +0000302 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
303 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
304 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000305
306 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000307 ExpectedType VisitType(const Type *T);
308 ExpectedType VisitAtomicType(const AtomicType *T);
309 ExpectedType VisitBuiltinType(const BuiltinType *T);
310 ExpectedType VisitDecayedType(const DecayedType *T);
311 ExpectedType VisitComplexType(const ComplexType *T);
312 ExpectedType VisitPointerType(const PointerType *T);
313 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
314 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
315 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
316 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
317 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
318 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
319 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
320 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000321 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000322 ExpectedType VisitVectorType(const VectorType *T);
323 ExpectedType VisitExtVectorType(const ExtVectorType *T);
324 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
325 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
326 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
327 ExpectedType VisitParenType(const ParenType *T);
328 ExpectedType VisitTypedefType(const TypedefType *T);
329 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000331 ExpectedType VisitTypeOfType(const TypeOfType *T);
332 ExpectedType VisitDecltypeType(const DecltypeType *T);
333 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
334 ExpectedType VisitAutoType(const AutoType *T);
335 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000337 ExpectedType VisitRecordType(const RecordType *T);
338 ExpectedType VisitEnumType(const EnumType *T);
339 ExpectedType VisitAttributedType(const AttributedType *T);
340 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
341 ExpectedType VisitSubstTemplateTypeParmType(
342 const SubstTemplateTypeParmType *T);
343 ExpectedType VisitTemplateSpecializationType(
344 const TemplateSpecializationType *T);
345 ExpectedType VisitElaboratedType(const ElaboratedType *T);
346 ExpectedType VisitDependentNameType(const DependentNameType *T);
347 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
348 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000349 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000350 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
351 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
352 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000353
354 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000355 Error ImportDeclParts(
356 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
357 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
358 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
359 Error ImportDeclarationNameLoc(
360 const DeclarationNameInfo &From, DeclarationNameInfo &To);
361 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
362 Error ImportDeclContext(
363 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
364 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000365
Balazs Keri3b30d652018-10-19 13:32:20 +0000366 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000367
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000368 using Designator = DesignatedInitExpr::Designator;
369
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000370 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000371 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000372 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000373 /// nothing (if minimal import is set) or might be everything (if minimal
374 /// import is not set).
375 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000376 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000377 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000378 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000379 /// DeclContext.
380 IDK_Basic
381 };
382
Douglas Gregor2e15c842012-02-01 21:00:38 +0000383 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
384 return IDK == IDK_Everything ||
385 (IDK == IDK_Default && !Importer.isMinimalImport());
386 }
387
Balazs Keri3b30d652018-10-19 13:32:20 +0000388 Error ImportInitializer(VarDecl *From, VarDecl *To);
389 Error ImportDefinition(
390 RecordDecl *From, RecordDecl *To,
391 ImportDefinitionKind Kind = IDK_Default);
392 Error ImportDefinition(
393 EnumDecl *From, EnumDecl *To,
394 ImportDefinitionKind Kind = IDK_Default);
395 Error ImportDefinition(
396 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
397 ImportDefinitionKind Kind = IDK_Default);
398 Error ImportDefinition(
399 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
400 ImportDefinitionKind Kind = IDK_Default);
Balazs Keri3b30d652018-10-19 13:32:20 +0000401 Error ImportTemplateArguments(
402 const TemplateArgument *FromArgs, unsigned NumFromArgs,
403 SmallVectorImpl<TemplateArgument> &ToArgs);
404 Expected<TemplateArgument>
405 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000406
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000407 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000408 Error ImportTemplateArgumentListInfo(
409 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000410
411 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000412 Error ImportTemplateArgumentListInfo(
413 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
414 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000415
Gabor Marton5254e642018-06-27 13:32:50 +0000416 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000417 using FunctionTemplateAndArgsTy =
418 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
419 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000420 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
421 FunctionDecl *FromFD);
Balazs Keri1efc9742019-05-07 10:55:11 +0000422 Error ImportTemplateParameterLists(const DeclaratorDecl *FromD,
423 DeclaratorDecl *ToD);
Gabor Marton5254e642018-06-27 13:32:50 +0000424
Balazs Keri3b30d652018-10-19 13:32:20 +0000425 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000426
Shafik Yaghmour96b3d202019-01-28 21:55:33 +0000427 Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD);
428
Gabor Marton458d1452019-02-14 13:07:03 +0000429 template <typename T>
430 bool hasSameVisibilityContext(T *Found, T *From);
431
Gabor Marton950fb572018-07-17 12:39:27 +0000432 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000433 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
434 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000435 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
436 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000437 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000438 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000439 bool IsStructuralMatch(FunctionTemplateDecl *From,
440 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000441 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000442 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000443 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000444 ExpectedDecl VisitDecl(Decl *D);
445 ExpectedDecl VisitImportDecl(ImportDecl *D);
446 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
447 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
448 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
449 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
450 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
451 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
452 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
453 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
454 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
455 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
456 ExpectedDecl VisitLabelDecl(LabelDecl *D);
457 ExpectedDecl VisitEnumDecl(EnumDecl *D);
458 ExpectedDecl VisitRecordDecl(RecordDecl *D);
459 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
460 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
461 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
462 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
463 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
464 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
465 ExpectedDecl VisitFieldDecl(FieldDecl *D);
466 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
467 ExpectedDecl VisitFriendDecl(FriendDecl *D);
468 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
469 ExpectedDecl VisitVarDecl(VarDecl *D);
470 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
471 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
472 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
473 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
474 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
475 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
476 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
477 ExpectedDecl VisitUsingDecl(UsingDecl *D);
478 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
479 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
480 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
481 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000482
Balazs Keri3b30d652018-10-19 13:32:20 +0000483 Expected<ObjCTypeParamList *>
484 ImportObjCTypeParamList(ObjCTypeParamList *list);
485
486 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
487 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
488 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
489 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
490 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
491 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
492 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
493 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
494 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
495 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000496 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000497 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
498 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
499 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000500
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000501 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000502 ExpectedStmt VisitStmt(Stmt *S);
503 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
504 ExpectedStmt VisitDeclStmt(DeclStmt *S);
505 ExpectedStmt VisitNullStmt(NullStmt *S);
506 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
507 ExpectedStmt VisitCaseStmt(CaseStmt *S);
508 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
509 ExpectedStmt VisitLabelStmt(LabelStmt *S);
510 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
511 ExpectedStmt VisitIfStmt(IfStmt *S);
512 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
513 ExpectedStmt VisitWhileStmt(WhileStmt *S);
514 ExpectedStmt VisitDoStmt(DoStmt *S);
515 ExpectedStmt VisitForStmt(ForStmt *S);
516 ExpectedStmt VisitGotoStmt(GotoStmt *S);
517 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
518 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
519 ExpectedStmt VisitBreakStmt(BreakStmt *S);
520 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000521 // FIXME: MSAsmStmt
522 // FIXME: SEHExceptStmt
523 // FIXME: SEHFinallyStmt
524 // FIXME: SEHTryStmt
525 // FIXME: SEHLeaveStmt
526 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000527 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
528 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
529 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000530 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000531 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
532 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
533 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
534 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
535 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
536 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
537 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000538
539 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000540 ExpectedStmt VisitExpr(Expr *E);
541 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
Tom Roeder521f0042019-02-26 19:26:41 +0000542 ExpectedStmt VisitChooseExpr(ChooseExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000543 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
544 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
545 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
546 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
547 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
548 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
549 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
550 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
551 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
552 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
553 ExpectedStmt VisitStringLiteral(StringLiteral *E);
554 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
555 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
556 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000557 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000558 ExpectedStmt VisitParenExpr(ParenExpr *E);
559 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
560 ExpectedStmt VisitStmtExpr(StmtExpr *E);
561 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
562 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
563 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
564 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
565 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
566 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
567 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
568 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
569 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
570 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
571 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
572 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
573 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
574 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
575 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
576 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
577 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
578 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
579 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
580 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
581 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
582 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
583 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
584 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
585 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
586 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
587 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
588 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
589 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
590 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
591 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
592 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
593 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
594 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
595 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
596 ExpectedStmt VisitMemberExpr(MemberExpr *E);
597 ExpectedStmt VisitCallExpr(CallExpr *E);
598 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
599 ExpectedStmt VisitInitListExpr(InitListExpr *E);
600 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
601 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
602 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
603 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
604 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
605 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
606 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
607 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
608 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000609
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000610 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000611 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000612 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000613 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
614 Expected<ItemT> ToOrErr = import(*Ibegin);
615 if (!ToOrErr)
616 return ToOrErr.takeError();
617 *Obegin = *ToOrErr;
618 }
619 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000620 }
621
Balazs Keri3b30d652018-10-19 13:32:20 +0000622 // Import every item from a container structure into an output container.
623 // If error occurs, stops at first error and returns the error.
624 // The output container should have space for all needed elements (it is not
625 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000626 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000627 Error ImportContainerChecked(
628 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
629 return ImportArrayChecked(
630 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000631 }
632
633 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000634 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000635 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
636 }
Lang Hames19e07e12017-06-20 21:06:00 +0000637
Lang Hames19e07e12017-06-20 21:06:00 +0000638 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000639
Balazs Keri3b30d652018-10-19 13:32:20 +0000640 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
641 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000642 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000643
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000644template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000645Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000646 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
647 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000648 auto ToLAngleLocOrErr = import(FromLAngleLoc);
649 if (!ToLAngleLocOrErr)
650 return ToLAngleLocOrErr.takeError();
651 auto ToRAngleLocOrErr = import(FromRAngleLoc);
652 if (!ToRAngleLocOrErr)
653 return ToRAngleLocOrErr.takeError();
654
655 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
656 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
657 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000658 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000659 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000660}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000661
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000662template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000663Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000664 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
665 return ImportTemplateArgumentListInfo(
666 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
667}
668
669template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000670Error ASTNodeImporter::ImportTemplateArgumentListInfo<
671 ASTTemplateArgumentListInfo>(
672 const ASTTemplateArgumentListInfo &From,
673 TemplateArgumentListInfo &Result) {
674 return ImportTemplateArgumentListInfo(
675 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000676}
677
Balazs Keri3b30d652018-10-19 13:32:20 +0000678Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000679ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
680 FunctionDecl *FromFD) {
681 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000682 FunctionDecl::TK_FunctionTemplateSpecialization);
683
684 FunctionTemplateAndArgsTy Result;
685
Gabor Marton5254e642018-06-27 13:32:50 +0000686 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000687 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
688 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000689
690 // Import template arguments.
691 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000692 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
693 std::get<1>(Result)))
694 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000695
Balazs Keri3b30d652018-10-19 13:32:20 +0000696 return Result;
697}
698
699template <>
700Expected<TemplateParameterList *>
701ASTNodeImporter::import(TemplateParameterList *From) {
702 SmallVector<NamedDecl *, 4> To(From->size());
703 if (Error Err = ImportContainerChecked(*From, To))
704 return std::move(Err);
705
706 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
707 if (!ToRequiresClause)
708 return ToRequiresClause.takeError();
709
710 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
711 if (!ToTemplateLocOrErr)
712 return ToTemplateLocOrErr.takeError();
713 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
714 if (!ToLAngleLocOrErr)
715 return ToLAngleLocOrErr.takeError();
716 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
717 if (!ToRAngleLocOrErr)
718 return ToRAngleLocOrErr.takeError();
719
720 return TemplateParameterList::Create(
721 Importer.getToContext(),
722 *ToTemplateLocOrErr,
723 *ToLAngleLocOrErr,
724 To,
725 *ToRAngleLocOrErr,
726 *ToRequiresClause);
727}
728
729template <>
730Expected<TemplateArgument>
731ASTNodeImporter::import(const TemplateArgument &From) {
732 switch (From.getKind()) {
733 case TemplateArgument::Null:
734 return TemplateArgument();
735
736 case TemplateArgument::Type: {
737 ExpectedType ToTypeOrErr = import(From.getAsType());
738 if (!ToTypeOrErr)
739 return ToTypeOrErr.takeError();
740 return TemplateArgument(*ToTypeOrErr);
741 }
742
743 case TemplateArgument::Integral: {
744 ExpectedType ToTypeOrErr = import(From.getIntegralType());
745 if (!ToTypeOrErr)
746 return ToTypeOrErr.takeError();
747 return TemplateArgument(From, *ToTypeOrErr);
748 }
749
750 case TemplateArgument::Declaration: {
751 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
752 if (!ToOrErr)
753 return ToOrErr.takeError();
754 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
755 if (!ToTypeOrErr)
756 return ToTypeOrErr.takeError();
757 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
758 }
759
760 case TemplateArgument::NullPtr: {
761 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
762 if (!ToTypeOrErr)
763 return ToTypeOrErr.takeError();
764 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
765 }
766
767 case TemplateArgument::Template: {
768 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
769 if (!ToTemplateOrErr)
770 return ToTemplateOrErr.takeError();
771
772 return TemplateArgument(*ToTemplateOrErr);
773 }
774
775 case TemplateArgument::TemplateExpansion: {
776 Expected<TemplateName> ToTemplateOrErr =
777 import(From.getAsTemplateOrTemplatePattern());
778 if (!ToTemplateOrErr)
779 return ToTemplateOrErr.takeError();
780
781 return TemplateArgument(
782 *ToTemplateOrErr, From.getNumTemplateExpansions());
783 }
784
785 case TemplateArgument::Expression:
786 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
787 return TemplateArgument(*ToExpr);
788 else
789 return ToExpr.takeError();
790
791 case TemplateArgument::Pack: {
792 SmallVector<TemplateArgument, 2> ToPack;
793 ToPack.reserve(From.pack_size());
794 if (Error Err = ImportTemplateArguments(
795 From.pack_begin(), From.pack_size(), ToPack))
796 return std::move(Err);
797
798 return TemplateArgument(
799 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
800 }
801 }
802
803 llvm_unreachable("Invalid template argument kind");
804}
805
806template <>
807Expected<TemplateArgumentLoc>
808ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
809 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
810 if (!ArgOrErr)
811 return ArgOrErr.takeError();
812 TemplateArgument Arg = *ArgOrErr;
813
814 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
815
816 TemplateArgumentLocInfo ToInfo;
817 if (Arg.getKind() == TemplateArgument::Expression) {
818 ExpectedExpr E = import(FromInfo.getAsExpr());
819 if (!E)
820 return E.takeError();
821 ToInfo = TemplateArgumentLocInfo(*E);
822 } else if (Arg.getKind() == TemplateArgument::Type) {
823 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
824 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
825 else
826 return TSIOrErr.takeError();
827 } else {
828 auto ToTemplateQualifierLocOrErr =
829 import(FromInfo.getTemplateQualifierLoc());
830 if (!ToTemplateQualifierLocOrErr)
831 return ToTemplateQualifierLocOrErr.takeError();
832 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
833 if (!ToTemplateNameLocOrErr)
834 return ToTemplateNameLocOrErr.takeError();
835 auto ToTemplateEllipsisLocOrErr =
836 import(FromInfo.getTemplateEllipsisLoc());
837 if (!ToTemplateEllipsisLocOrErr)
838 return ToTemplateEllipsisLocOrErr.takeError();
839
840 ToInfo = TemplateArgumentLocInfo(
841 *ToTemplateQualifierLocOrErr,
842 *ToTemplateNameLocOrErr,
843 *ToTemplateEllipsisLocOrErr);
844 }
845
846 return TemplateArgumentLoc(Arg, ToInfo);
847}
848
849template <>
850Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
851 if (DG.isNull())
852 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
853 size_t NumDecls = DG.end() - DG.begin();
854 SmallVector<Decl *, 1> ToDecls;
855 ToDecls.reserve(NumDecls);
856 for (Decl *FromD : DG) {
857 if (auto ToDOrErr = import(FromD))
858 ToDecls.push_back(*ToDOrErr);
859 else
860 return ToDOrErr.takeError();
861 }
862 return DeclGroupRef::Create(Importer.getToContext(),
863 ToDecls.begin(),
864 NumDecls);
865}
866
867template <>
868Expected<ASTNodeImporter::Designator>
869ASTNodeImporter::import(const Designator &D) {
870 if (D.isFieldDesignator()) {
871 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
872
873 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
874 if (!ToDotLocOrErr)
875 return ToDotLocOrErr.takeError();
876
877 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
878 if (!ToFieldLocOrErr)
879 return ToFieldLocOrErr.takeError();
880
881 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
882 }
883
884 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
885 if (!ToLBracketLocOrErr)
886 return ToLBracketLocOrErr.takeError();
887
888 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
889 if (!ToRBracketLocOrErr)
890 return ToRBracketLocOrErr.takeError();
891
892 if (D.isArrayDesignator())
893 return Designator(D.getFirstExprIndex(),
894 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
895
896 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
897 if (!ToEllipsisLocOrErr)
898 return ToEllipsisLocOrErr.takeError();
899
900 assert(D.isArrayRangeDesignator());
901 return Designator(
902 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
903 *ToRBracketLocOrErr);
904}
905
906template <>
907Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
908 VarDecl *Var = nullptr;
909 if (From.capturesVariable()) {
910 if (auto VarOrErr = import(From.getCapturedVar()))
911 Var = *VarOrErr;
912 else
913 return VarOrErr.takeError();
914 }
915
916 auto LocationOrErr = import(From.getLocation());
917 if (!LocationOrErr)
918 return LocationOrErr.takeError();
919
920 SourceLocation EllipsisLoc;
921 if (From.isPackExpansion())
922 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
923 return std::move(Err);
924
925 return LambdaCapture(
926 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
927 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000928}
929
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000930} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000931
Douglas Gregor3996e242010-02-15 22:01:00 +0000932//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000933// Import Types
934//----------------------------------------------------------------------------
935
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000936using namespace clang;
937
Balazs Keri3b30d652018-10-19 13:32:20 +0000938ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000939 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
940 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000941 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000942}
943
Balazs Keri3b30d652018-10-19 13:32:20 +0000944ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
945 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
946 if (!UnderlyingTypeOrErr)
947 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000948
Balazs Keri3b30d652018-10-19 13:32:20 +0000949 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000950}
951
Balazs Keri3b30d652018-10-19 13:32:20 +0000952ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000953 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000954#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
955 case BuiltinType::Id: \
956 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000957#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000958#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
959 case BuiltinType::Id: \
960 return Importer.getToContext().Id##Ty;
961#include "clang/Basic/OpenCLExtensionTypes.def"
Richard Sandifordeb485fb2019-08-09 08:52:54 +0000962#define SVE_TYPE(Name, Id, SingletonId) \
963 case BuiltinType::Id: \
964 return Importer.getToContext().SingletonId;
965#include "clang/Basic/AArch64SVEACLETypes.def"
John McCalle314e272011-10-18 21:02:43 +0000966#define SHARED_SINGLETON_TYPE(Expansion)
967#define BUILTIN_TYPE(Id, SingletonId) \
968 case BuiltinType::Id: return Importer.getToContext().SingletonId;
969#include "clang/AST/BuiltinTypes.def"
970
971 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
972 // context supports C++.
973
974 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
975 // context supports ObjC.
976
Douglas Gregor96e578d2010-02-05 17:54:41 +0000977 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000978 // The context we're importing from has an unsigned 'char'. If we're
979 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000980 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000981 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000982 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000983
Douglas Gregor96e578d2010-02-05 17:54:41 +0000984 return Importer.getToContext().CharTy;
985
Douglas Gregor96e578d2010-02-05 17:54:41 +0000986 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000987 // The context we're importing from has an unsigned 'char'. If we're
988 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000989 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000990 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000991 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000992
Douglas Gregor96e578d2010-02-05 17:54:41 +0000993 return Importer.getToContext().CharTy;
994
Chris Lattnerad3467e2010-12-25 23:25:43 +0000995 case BuiltinType::WChar_S:
996 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000997 // FIXME: If not in C++, shall we translate to the C equivalent of
998 // wchar_t?
999 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001000 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001001
1002 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001003}
1004
Balazs Keri3b30d652018-10-19 13:32:20 +00001005ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1006 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1007 if (!ToOriginalTypeOrErr)
1008 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001009
Balazs Keri3b30d652018-10-19 13:32:20 +00001010 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001011}
1012
Balazs Keri3b30d652018-10-19 13:32:20 +00001013ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1014 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1015 if (!ToElementTypeOrErr)
1016 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001017
Balazs Keri3b30d652018-10-19 13:32:20 +00001018 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001019}
1020
Balazs Keri3b30d652018-10-19 13:32:20 +00001021ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1022 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1023 if (!ToPointeeTypeOrErr)
1024 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001025
Balazs Keri3b30d652018-10-19 13:32:20 +00001026 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001027}
1028
Balazs Keri3b30d652018-10-19 13:32:20 +00001029ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001030 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001031 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1032 if (!ToPointeeTypeOrErr)
1033 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001036}
1037
Balazs Keri3b30d652018-10-19 13:32:20 +00001038ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001039ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001040 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001041 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1042 if (!ToPointeeTypeOrErr)
1043 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001044
Balazs Keri3b30d652018-10-19 13:32:20 +00001045 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001046}
1047
Balazs Keri3b30d652018-10-19 13:32:20 +00001048ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001049ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001050 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001051 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1052 if (!ToPointeeTypeOrErr)
1053 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001054
Balazs Keri3b30d652018-10-19 13:32:20 +00001055 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001056}
1057
Balazs Keri3b30d652018-10-19 13:32:20 +00001058ExpectedType
1059ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001060 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001061 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1062 if (!ToPointeeTypeOrErr)
1063 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001064
Balazs Keri3b30d652018-10-19 13:32:20 +00001065 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1066 if (!ClassTypeOrErr)
1067 return ClassTypeOrErr.takeError();
1068
1069 return Importer.getToContext().getMemberPointerType(
1070 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001071}
1072
Balazs Keri3b30d652018-10-19 13:32:20 +00001073ExpectedType
1074ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1075 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1076 if (!ToElementTypeOrErr)
1077 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001078
Balazs Keri3b30d652018-10-19 13:32:20 +00001079 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001080 T->getSize(),
1081 T->getSizeModifier(),
1082 T->getIndexTypeCVRQualifiers());
1083}
1084
Balazs Keri3b30d652018-10-19 13:32:20 +00001085ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001086ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001087 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1088 if (!ToElementTypeOrErr)
1089 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001090
Balazs Keri3b30d652018-10-19 13:32:20 +00001091 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001092 T->getSizeModifier(),
1093 T->getIndexTypeCVRQualifiers());
1094}
1095
Balazs Keri3b30d652018-10-19 13:32:20 +00001096ExpectedType
1097ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1098 QualType ToElementType;
1099 Expr *ToSizeExpr;
1100 SourceRange ToBracketsRange;
1101 if (auto Imp = importSeq(
1102 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1103 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1104 else
1105 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001106
Balazs Keri3b30d652018-10-19 13:32:20 +00001107 return Importer.getToContext().getVariableArrayType(
1108 ToElementType, ToSizeExpr, T->getSizeModifier(),
1109 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001110}
1111
Balazs Keri3b30d652018-10-19 13:32:20 +00001112ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001113 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001114 QualType ToElementType;
1115 Expr *ToSizeExpr;
1116 SourceRange ToBracketsRange;
1117 if (auto Imp = importSeq(
1118 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1119 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1120 else
1121 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001122 // SizeExpr may be null if size is not specified directly.
1123 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001124
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001125 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001126 ToElementType, ToSizeExpr, T->getSizeModifier(),
1127 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001128}
1129
Balazs Keri3b30d652018-10-19 13:32:20 +00001130ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1131 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1132 if (!ToElementTypeOrErr)
1133 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001134
Balazs Keri3b30d652018-10-19 13:32:20 +00001135 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001136 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001137 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001138}
1139
Balazs Keri3b30d652018-10-19 13:32:20 +00001140ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1141 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1142 if (!ToElementTypeOrErr)
1143 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001144
Balazs Keri3b30d652018-10-19 13:32:20 +00001145 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001146 T->getNumElements());
1147}
1148
Balazs Keri3b30d652018-10-19 13:32:20 +00001149ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001150ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001151 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001152 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001153 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1154 if (!ToReturnTypeOrErr)
1155 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001156
Balazs Keri3b30d652018-10-19 13:32:20 +00001157 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001158 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001159}
1160
Balazs Keri3b30d652018-10-19 13:32:20 +00001161ExpectedType
1162ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1163 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1164 if (!ToReturnTypeOrErr)
1165 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001166
Douglas Gregor96e578d2010-02-05 17:54:41 +00001167 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001168 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001169 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001170 ExpectedType TyOrErr = import(A);
1171 if (!TyOrErr)
1172 return TyOrErr.takeError();
1173 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001174 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001175
Douglas Gregor96e578d2010-02-05 17:54:41 +00001176 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001177 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001178 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001179 ExpectedType TyOrErr = import(E);
1180 if (!TyOrErr)
1181 return TyOrErr.takeError();
1182 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001183 }
John McCalldb40c7f2010-12-14 08:05:40 +00001184
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001185 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1186 FunctionProtoType::ExtProtoInfo ToEPI;
1187
Balazs Keri3b30d652018-10-19 13:32:20 +00001188 auto Imp = importSeq(
1189 FromEPI.ExceptionSpec.NoexceptExpr,
1190 FromEPI.ExceptionSpec.SourceDecl,
1191 FromEPI.ExceptionSpec.SourceTemplate);
1192 if (!Imp)
1193 return Imp.takeError();
1194
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001195 ToEPI.ExtInfo = FromEPI.ExtInfo;
1196 ToEPI.Variadic = FromEPI.Variadic;
1197 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1198 ToEPI.TypeQuals = FromEPI.TypeQuals;
1199 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001200 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1201 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001202 std::tie(
1203 ToEPI.ExceptionSpec.NoexceptExpr,
1204 ToEPI.ExceptionSpec.SourceDecl,
1205 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001206
Balazs Keri3b30d652018-10-19 13:32:20 +00001207 return Importer.getToContext().getFunctionType(
1208 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001209}
1210
Balazs Keri3b30d652018-10-19 13:32:20 +00001211ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001212 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001213 UnresolvedUsingTypenameDecl *ToD;
1214 Decl *ToPrevD;
1215 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1216 std::tie(ToD, ToPrevD) = *Imp;
1217 else
1218 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001219
Balazs Keri3b30d652018-10-19 13:32:20 +00001220 return Importer.getToContext().getTypeDeclType(
1221 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001222}
1223
Balazs Keri3b30d652018-10-19 13:32:20 +00001224ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1225 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1226 if (!ToInnerTypeOrErr)
1227 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001228
Balazs Keri3b30d652018-10-19 13:32:20 +00001229 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001230}
1231
Balazs Keri3b30d652018-10-19 13:32:20 +00001232ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1233 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1234 if (!ToDeclOrErr)
1235 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001236
Balazs Keri3b30d652018-10-19 13:32:20 +00001237 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001238}
1239
Balazs Keri3b30d652018-10-19 13:32:20 +00001240ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1241 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1242 if (!ToExprOrErr)
1243 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001244
Balazs Keri3b30d652018-10-19 13:32:20 +00001245 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001246}
1247
Balazs Keri3b30d652018-10-19 13:32:20 +00001248ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1249 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1250 if (!ToUnderlyingTypeOrErr)
1251 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001252
Balazs Keri3b30d652018-10-19 13:32:20 +00001253 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001254}
1255
Balazs Keri3b30d652018-10-19 13:32:20 +00001256ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001257 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001258 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1259 if (!ToExprOrErr)
1260 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1263 if (!ToUnderlyingTypeOrErr)
1264 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001265
Balazs Keri3b30d652018-10-19 13:32:20 +00001266 return Importer.getToContext().getDecltypeType(
1267 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001268}
1269
Balazs Keri3b30d652018-10-19 13:32:20 +00001270ExpectedType
1271ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1272 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1273 if (!ToBaseTypeOrErr)
1274 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001275
Balazs Keri3b30d652018-10-19 13:32:20 +00001276 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1277 if (!ToUnderlyingTypeOrErr)
1278 return ToUnderlyingTypeOrErr.takeError();
1279
1280 return Importer.getToContext().getUnaryTransformType(
1281 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001282}
1283
Balazs Keri3b30d652018-10-19 13:32:20 +00001284ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001285 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001286 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1287 if (!ToDeducedTypeOrErr)
1288 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001289
Balazs Keri3b30d652018-10-19 13:32:20 +00001290 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1291 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001292 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001293}
1294
Balazs Keri3b30d652018-10-19 13:32:20 +00001295ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001296 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001297 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1298 if (!ToDeclOrErr)
1299 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001300
Balazs Keri3b30d652018-10-19 13:32:20 +00001301 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1302 if (!ToInjTypeOrErr)
1303 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001304
1305 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1306 // See comments in InjectedClassNameType definition for details
1307 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1308 enum {
1309 TypeAlignmentInBits = 4,
1310 TypeAlignment = 1 << TypeAlignmentInBits
1311 };
1312
1313 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001314 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001315}
1316
Balazs Keri3b30d652018-10-19 13:32:20 +00001317ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1318 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1319 if (!ToDeclOrErr)
1320 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001321
Balazs Keri3b30d652018-10-19 13:32:20 +00001322 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001323}
1324
Balazs Keri3b30d652018-10-19 13:32:20 +00001325ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1326 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1327 if (!ToDeclOrErr)
1328 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001329
Balazs Keri3b30d652018-10-19 13:32:20 +00001330 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001331}
1332
Balazs Keri3b30d652018-10-19 13:32:20 +00001333ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1334 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1335 if (!ToModifiedTypeOrErr)
1336 return ToModifiedTypeOrErr.takeError();
1337 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1338 if (!ToEquivalentTypeOrErr)
1339 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001340
1341 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001342 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001343}
1344
Balazs Keri3b30d652018-10-19 13:32:20 +00001345ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001346 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001347 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1348 if (!ToDeclOrErr)
1349 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001350
1351 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001352 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001353}
1354
Balazs Keri3b30d652018-10-19 13:32:20 +00001355ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001356 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001357 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1358 if (!ReplacedOrErr)
1359 return ReplacedOrErr.takeError();
1360 const TemplateTypeParmType *Replaced =
1361 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001362
Balazs Keri3b30d652018-10-19 13:32:20 +00001363 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1364 if (!ToReplacementTypeOrErr)
1365 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001366
1367 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001368 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001369}
1370
Balazs Keri3b30d652018-10-19 13:32:20 +00001371ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001372 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001373 auto ToTemplateOrErr = import(T->getTemplateName());
1374 if (!ToTemplateOrErr)
1375 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001377 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001378 if (Error Err = ImportTemplateArguments(
1379 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1380 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001381
Douglas Gregore2e50d332010-12-01 01:36:18 +00001382 QualType ToCanonType;
1383 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001384 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001385 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001386 if (ExpectedType TyOrErr = import(FromCanonType))
1387 ToCanonType = *TyOrErr;
1388 else
1389 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001390 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001391 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001392 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001393 ToCanonType);
1394}
1395
Balazs Keri3b30d652018-10-19 13:32:20 +00001396ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001397 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 auto ToQualifierOrErr = import(T->getQualifier());
1399 if (!ToQualifierOrErr)
1400 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001401
Balazs Keri3b30d652018-10-19 13:32:20 +00001402 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1403 if (!ToNamedTypeOrErr)
1404 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001405
Balazs Keri3b30d652018-10-19 13:32:20 +00001406 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1407 if (!ToOwnedTagDeclOrErr)
1408 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001409
Abramo Bagnara6150c882010-05-11 21:36:43 +00001410 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001411 *ToQualifierOrErr,
1412 *ToNamedTypeOrErr,
1413 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001414}
1415
Balazs Keri3b30d652018-10-19 13:32:20 +00001416ExpectedType
1417ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1418 ExpectedType ToPatternOrErr = import(T->getPattern());
1419 if (!ToPatternOrErr)
1420 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001421
Balazs Keri3b30d652018-10-19 13:32:20 +00001422 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001423 T->getNumExpansions());
1424}
1425
Balazs Keri3b30d652018-10-19 13:32:20 +00001426ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001427 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 auto ToQualifierOrErr = import(T->getQualifier());
1429 if (!ToQualifierOrErr)
1430 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001431
Balazs Keri3b30d652018-10-19 13:32:20 +00001432 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001433
1434 SmallVector<TemplateArgument, 2> ToPack;
1435 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 if (Error Err = ImportTemplateArguments(
1437 T->getArgs(), T->getNumArgs(), ToPack))
1438 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001439
1440 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001441 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001442}
1443
Balazs Keri3b30d652018-10-19 13:32:20 +00001444ExpectedType
1445ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1446 auto ToQualifierOrErr = import(T->getQualifier());
1447 if (!ToQualifierOrErr)
1448 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001449
1450 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001451
Balazs Keri3b30d652018-10-19 13:32:20 +00001452 QualType Canon;
1453 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1454 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1455 Canon = (*TyOrErr).getCanonicalType();
1456 else
1457 return TyOrErr.takeError();
1458 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001459
Balazs Keri3b30d652018-10-19 13:32:20 +00001460 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1461 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001462 Name, Canon);
1463}
1464
Balazs Keri3b30d652018-10-19 13:32:20 +00001465ExpectedType
1466ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1467 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1468 if (!ToDeclOrErr)
1469 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001470
Balazs Keri3b30d652018-10-19 13:32:20 +00001471 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001472}
1473
Balazs Keri3b30d652018-10-19 13:32:20 +00001474ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1475 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1476 if (!ToBaseTypeOrErr)
1477 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001478
Douglas Gregore9d95f12015-07-07 03:57:35 +00001479 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001480 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001481 if (ExpectedType TyOrErr = import(TypeArg))
1482 TypeArgs.push_back(*TyOrErr);
1483 else
1484 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001485 }
1486
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001487 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001488 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001489 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1490 Protocols.push_back(*ProtocolOrErr);
1491 else
1492 return ProtocolOrErr.takeError();
1493
Douglas Gregor96e578d2010-02-05 17:54:41 +00001494 }
1495
Balazs Keri3b30d652018-10-19 13:32:20 +00001496 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001497 Protocols,
1498 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001499}
1500
Balazs Keri3b30d652018-10-19 13:32:20 +00001501ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001502ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001503 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1504 if (!ToPointeeTypeOrErr)
1505 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001506
Balazs Keri3b30d652018-10-19 13:32:20 +00001507 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001508}
1509
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001510//----------------------------------------------------------------------------
1511// Import Declarations
1512//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001513Error ASTNodeImporter::ImportDeclParts(
1514 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1515 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001516 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1517 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1518 DeclContext *OrigDC = D->getDeclContext();
1519 FunctionDecl *FunDecl;
1520 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1521 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001522 auto getLeafPointeeType = [](const Type *T) {
1523 while (T->isPointerType() || T->isArrayType()) {
1524 T = T->getPointeeOrArrayElementType();
1525 }
1526 return T;
1527 };
1528 for (const ParmVarDecl *P : FunDecl->parameters()) {
1529 const Type *LeafT =
1530 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1531 auto *RT = dyn_cast<RecordType>(LeafT);
1532 if (RT && RT->getDecl() == D) {
1533 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1534 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001535 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001536 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001537 }
1538 }
1539
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001540 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001541 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1542 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001543
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001544 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001545 if (Error Err = importInto(Name, D->getDeclName()))
1546 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001547
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001548 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001549 if (Error Err = importInto(Loc, D->getLocation()))
1550 return Err;
1551
Sean Callanan59721b32015-04-28 18:41:46 +00001552 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001553 if (ToD)
1554 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1555 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001556
1557 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001558}
1559
Balazs Keri3b30d652018-10-19 13:32:20 +00001560Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001561 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001562 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001563
Balazs Keri3b30d652018-10-19 13:32:20 +00001564 if (!ToD)
1565 if (Error Err = importInto(ToD, FromD))
1566 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001567
Balazs Keri3b30d652018-10-19 13:32:20 +00001568 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1569 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1570 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1571 !ToRecord->getDefinition()) {
1572 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1573 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001574 }
1575 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001576 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001577 }
1578
Balazs Keri3b30d652018-10-19 13:32:20 +00001579 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1580 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001581 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001582 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1583 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001584 }
1585 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001586 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001587 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001588
1589 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001590}
1591
Balazs Keri3b30d652018-10-19 13:32:20 +00001592Error
1593ASTNodeImporter::ImportDeclarationNameLoc(
1594 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001595 // NOTE: To.Name and To.Loc are already imported.
1596 // We only have to import To.LocInfo.
1597 switch (To.getName().getNameKind()) {
1598 case DeclarationName::Identifier:
1599 case DeclarationName::ObjCZeroArgSelector:
1600 case DeclarationName::ObjCOneArgSelector:
1601 case DeclarationName::ObjCMultiArgSelector:
1602 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001603 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001604 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001605
1606 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001607 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1608 To.setCXXOperatorNameRange(*ToRangeOrErr);
1609 else
1610 return ToRangeOrErr.takeError();
1611 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001612 }
1613 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001614 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1615 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1616 else
1617 return LocOrErr.takeError();
1618 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001619 }
1620 case DeclarationName::CXXConstructorName:
1621 case DeclarationName::CXXDestructorName:
1622 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001623 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1624 To.setNamedTypeInfo(*ToTInfoOrErr);
1625 else
1626 return ToTInfoOrErr.takeError();
1627 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001628 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001629 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001630 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001631}
1632
Balazs Keri3b30d652018-10-19 13:32:20 +00001633Error
1634ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001635 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001636 auto ToDCOrErr = Importer.ImportContext(FromDC);
1637 return ToDCOrErr.takeError();
1638 }
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001639
1640 // We use strict error handling in case of records and enums, but not
1641 // with e.g. namespaces.
1642 //
1643 // FIXME Clients of the ASTImporter should be able to choose an
1644 // appropriate error handling strategy for their needs. For instance,
1645 // they may not want to mark an entire namespace as erroneous merely
1646 // because there is an ODR error with two typedefs. As another example,
1647 // the client may allow EnumConstantDecls with same names but with
1648 // different values in two distinct translation units.
1649 bool AccumulateChildErrors = isa<TagDecl>(FromDC);
1650
1651 Error ChildErrors = Error::success();
Balazs Keri3b30d652018-10-19 13:32:20 +00001652 for (auto *From : FromDC->decls()) {
1653 ExpectedDecl ImportedOrErr = import(From);
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001654 if (!ImportedOrErr) {
1655 if (AccumulateChildErrors)
1656 ChildErrors =
1657 joinErrors(std::move(ChildErrors), ImportedOrErr.takeError());
1658 else
1659 consumeError(ImportedOrErr.takeError());
1660 }
Douglas Gregor0a791672011-01-18 03:11:38 +00001661 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001662
Gabor Marton48b16e12019-07-25 09:07:17 +00001663 // We reorder declarations in RecordDecls because they may have another order
1664 // in the "to" context than they have in the "from" context. This may happen
1665 // e.g when we import a class like this:
1666 // struct declToImport {
1667 // int a = c + b;
1668 // int b = 1;
1669 // int c = 2;
1670 // };
1671 // During the import of `a` we import first the dependencies in sequence,
1672 // thus the order would be `c`, `b`, `a`. We will get the normal order by
1673 // first removing the already imported members and then adding them in the
1674 // order as they apper in the "from" context.
1675 //
1676 // Keeping field order is vital because it determines structure layout.
1677 //
1678 // Here and below, we cannot call field_begin() method and its callers on
1679 // ToDC if it has an external storage. Calling field_begin() will
1680 // automatically load all the fields by calling
1681 // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would
1682 // call ASTImporter::Import(). This is because the ExternalASTSource
1683 // interface in LLDB is implemented by the means of the ASTImporter. However,
1684 // calling an import at this point would result in an uncontrolled import, we
1685 // must avoid that.
1686 const auto *FromRD = dyn_cast<RecordDecl>(FromDC);
1687 if (!FromRD)
1688 return ChildErrors;
1689
1690 auto ToDCOrErr = Importer.ImportContext(FromDC);
1691 if (!ToDCOrErr) {
1692 consumeError(std::move(ChildErrors));
1693 return ToDCOrErr.takeError();
1694 }
1695
1696 DeclContext *ToDC = *ToDCOrErr;
1697 // Remove all declarations, which may be in wrong order in the
1698 // lexical DeclContext and then add them in the proper order.
1699 for (auto *D : FromRD->decls()) {
1700 if (isa<FieldDecl>(D) || isa<FriendDecl>(D)) {
1701 assert(D && "DC contains a null decl");
1702 Decl *ToD = Importer.GetAlreadyImportedOrNull(D);
1703 // Remove only the decls which we successfully imported.
1704 if (ToD) {
1705 assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD));
1706 // Remove the decl from its wrong place in the linked list.
1707 ToDC->removeDecl(ToD);
1708 // Add the decl to the end of the linked list.
1709 // This time it will be at the proper place because the enclosing for
1710 // loop iterates in the original (good) order of the decls.
1711 ToDC->addDeclInternal(ToD);
1712 }
1713 }
1714 }
1715
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001716 return ChildErrors;
Douglas Gregor968d6332010-02-21 18:24:45 +00001717}
1718
Balazs Keri3b30d652018-10-19 13:32:20 +00001719Error ASTNodeImporter::ImportDeclContext(
1720 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1721 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1722 if (!ToDCOrErr)
1723 return ToDCOrErr.takeError();
1724 ToDC = *ToDCOrErr;
1725
1726 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1727 auto ToLexicalDCOrErr = Importer.ImportContext(
1728 FromD->getLexicalDeclContext());
1729 if (!ToLexicalDCOrErr)
1730 return ToLexicalDCOrErr.takeError();
1731 ToLexicalDC = *ToLexicalDCOrErr;
1732 } else
1733 ToLexicalDC = ToDC;
1734
1735 return Error::success();
1736}
1737
1738Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001739 const CXXRecordDecl *From, CXXRecordDecl *To) {
1740 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1741 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001742
Balazs Keri1d20cc22018-07-16 12:16:39 +00001743 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001744 if (FromM->isImplicit()) {
1745 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1746 if (!ToMOrErr)
1747 return ToMOrErr.takeError();
1748 }
1749
1750 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001751}
1752
Balazs Keri3b30d652018-10-19 13:32:20 +00001753static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1754 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001755 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00001756 if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef))
Balazs Keri57949eb2019-03-25 09:16:39 +00001757 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1758 else
1759 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001760 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001761 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001762}
1763
Balazs Keri3b30d652018-10-19 13:32:20 +00001764Error ASTNodeImporter::ImportDefinition(
1765 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Gabor Martonaefcf512019-07-17 13:47:46 +00001766 auto DefinitionCompleter = [To]() {
1767 // There are cases in LLDB when we first import a class without its
1768 // members. The class will have DefinitionData, but no members. Then,
1769 // importDefinition is called from LLDB, which tries to get the members, so
1770 // when we get here, the class already has the DefinitionData set, so we
1771 // must unset the CompleteDefinition here to be able to complete again the
1772 // definition.
1773 To->setCompleteDefinition(false);
1774 To->completeDefinition();
1775 };
1776
Douglas Gregor95d82832012-01-24 18:36:04 +00001777 if (To->getDefinition() || To->isBeingDefined()) {
Gabor Martone73805f2019-07-08 12:49:13 +00001778 if (Kind == IDK_Everything ||
1779 // In case of lambdas, the class already has a definition ptr set, but
1780 // the contained decls are not imported yet. Also, isBeingDefined was
1781 // set in CXXRecordDecl::CreateLambda. We must import the contained
1782 // decls here and finish the definition.
1783 (To->isLambda() && shouldForceImportDeclContext(Kind))) {
1784 Error Result = ImportDeclContext(From, /*ForceImport=*/true);
1785 // Finish the definition of the lambda, set isBeingDefined to false.
1786 if (To->isLambda())
Gabor Martonaefcf512019-07-17 13:47:46 +00001787 DefinitionCompleter();
Gabor Martone73805f2019-07-08 12:49:13 +00001788 return Result;
1789 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001790
Balazs Keri3b30d652018-10-19 13:32:20 +00001791 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001792 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001793
Douglas Gregore2e50d332010-12-01 01:36:18 +00001794 To->startDefinition();
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001795 // Complete the definition even if error is returned.
1796 // The RecordDecl may be already part of the AST so it is better to
1797 // have it in complete state even if something is wrong with it.
Gabor Martonaefcf512019-07-17 13:47:46 +00001798 auto DefinitionCompleterScopeExit =
1799 llvm::make_scope_exit(DefinitionCompleter);
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001800
Balazs Keri3b30d652018-10-19 13:32:20 +00001801 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1802 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001803
Douglas Gregore2e50d332010-12-01 01:36:18 +00001804 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001805 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1806 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1807 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001808
1809 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1810 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1811 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001812 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001813 ToData.Aggregate = FromData.Aggregate;
1814 ToData.PlainOldData = FromData.PlainOldData;
1815 ToData.Empty = FromData.Empty;
1816 ToData.Polymorphic = FromData.Polymorphic;
1817 ToData.Abstract = FromData.Abstract;
1818 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001819 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1820 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1821 ToData.HasBasesWithNonStaticDataMembers =
1822 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001823 ToData.HasPrivateFields = FromData.HasPrivateFields;
1824 ToData.HasProtectedFields = FromData.HasProtectedFields;
1825 ToData.HasPublicFields = FromData.HasPublicFields;
1826 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001827 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001828 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001829 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001830 ToData.HasUninitializedReferenceMember
1831 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001832 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001833 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1834 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001835 ToData.NeedOverloadResolutionForCopyConstructor
1836 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001837 ToData.NeedOverloadResolutionForMoveConstructor
1838 = FromData.NeedOverloadResolutionForMoveConstructor;
1839 ToData.NeedOverloadResolutionForMoveAssignment
1840 = FromData.NeedOverloadResolutionForMoveAssignment;
1841 ToData.NeedOverloadResolutionForDestructor
1842 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001843 ToData.DefaultedCopyConstructorIsDeleted
1844 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001845 ToData.DefaultedMoveConstructorIsDeleted
1846 = FromData.DefaultedMoveConstructorIsDeleted;
1847 ToData.DefaultedMoveAssignmentIsDeleted
1848 = FromData.DefaultedMoveAssignmentIsDeleted;
1849 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001850 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1851 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001852 ToData.HasConstexprNonCopyMoveConstructor
1853 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001854 ToData.HasDefaultedDefaultConstructor
1855 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001856 ToData.DefaultedDefaultConstructorIsConstexpr
1857 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001858 ToData.HasConstexprDefaultConstructor
1859 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001860 ToData.HasNonLiteralTypeFieldsOrBases
1861 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001862 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001863 ToData.UserProvidedDefaultConstructor
1864 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001865 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001866 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1867 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1868 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1869 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001870 ToData.ImplicitCopyAssignmentHasConstParam
1871 = FromData.ImplicitCopyAssignmentHasConstParam;
1872 ToData.HasDeclaredCopyConstructorWithConstParam
1873 = FromData.HasDeclaredCopyConstructorWithConstParam;
1874 ToData.HasDeclaredCopyAssignmentWithConstParam
1875 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001876
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001877 // Copy over the data stored in RecordDeclBits
1878 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1879
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001880 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001881 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001882 ExpectedType TyOrErr = import(Base1.getType());
1883 if (!TyOrErr)
1884 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001885
1886 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001887 if (Base1.isPackExpansion()) {
1888 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1889 EllipsisLoc = *LocOrErr;
1890 else
1891 return LocOrErr.takeError();
1892 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001893
1894 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001895 if (Error Err =
1896 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1897 return Err;
1898
1899 auto RangeOrErr = import(Base1.getSourceRange());
1900 if (!RangeOrErr)
1901 return RangeOrErr.takeError();
1902
1903 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1904 if (!TSIOrErr)
1905 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001906
Douglas Gregore2e50d332010-12-01 01:36:18 +00001907 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001908 new (Importer.getToContext()) CXXBaseSpecifier(
1909 *RangeOrErr,
1910 Base1.isVirtual(),
1911 Base1.isBaseOfClass(),
1912 Base1.getAccessSpecifierAsWritten(),
1913 *TSIOrErr,
1914 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001915 }
1916 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001917 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001918 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001919
Douglas Gregor2e15c842012-02-01 21:00:38 +00001920 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001921 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1922 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001923
Balazs Keri3b30d652018-10-19 13:32:20 +00001924 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001925}
1926
Balazs Keri3b30d652018-10-19 13:32:20 +00001927Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001928 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001929 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001930
Gabor Martonac3a5d62018-09-17 12:04:52 +00001931 Expr *FromInit = From->getInit();
1932 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001933 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001934
Balazs Keri3b30d652018-10-19 13:32:20 +00001935 ExpectedExpr ToInitOrErr = import(FromInit);
1936 if (!ToInitOrErr)
1937 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001938
Balazs Keri3b30d652018-10-19 13:32:20 +00001939 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001940 if (From->isInitKnownICE()) {
1941 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1942 Eval->CheckedICE = true;
1943 Eval->IsICE = From->isInitICE();
1944 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001945
1946 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001947 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001948}
1949
Balazs Keri3b30d652018-10-19 13:32:20 +00001950Error ASTNodeImporter::ImportDefinition(
1951 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001952 if (To->getDefinition() || To->isBeingDefined()) {
1953 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001954 return ImportDeclContext(From, /*ForceImport=*/true);
1955 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001956 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001957
Douglas Gregord451ea92011-07-29 23:31:30 +00001958 To->startDefinition();
1959
Balazs Keri3b30d652018-10-19 13:32:20 +00001960 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1961 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001962
Balazs Keri3b30d652018-10-19 13:32:20 +00001963 ExpectedType ToTypeOrErr =
1964 import(Importer.getFromContext().getTypeDeclType(From));
1965 if (!ToTypeOrErr)
1966 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001967
Balazs Keri3b30d652018-10-19 13:32:20 +00001968 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1969 if (!ToPromotionTypeOrErr)
1970 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001971
1972 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001973 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1974 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001975
Douglas Gregord451ea92011-07-29 23:31:30 +00001976 // FIXME: we might need to merge the number of positive or negative bits
1977 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001978 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001979 From->getNumPositiveBits(),
1980 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001981 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001982}
1983
Balazs Keri3b30d652018-10-19 13:32:20 +00001984Error ASTNodeImporter::ImportTemplateArguments(
1985 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1986 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001987 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001988 if (auto ToOrErr = import(FromArgs[I]))
1989 ToArgs.push_back(*ToOrErr);
1990 else
1991 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001992 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001993
Balazs Keri3b30d652018-10-19 13:32:20 +00001994 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001995}
1996
Balazs Keri3b30d652018-10-19 13:32:20 +00001997// FIXME: Do not forget to remove this and use only 'import'.
1998Expected<TemplateArgument>
1999ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2000 return import(From);
2001}
2002
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002003template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00002004Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002005 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
2006 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002007 if (auto ToLocOrErr = import(FromLoc))
2008 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002009 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002010 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002011 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002012 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002013}
2014
Gabor Marton26f72a92018-07-12 09:42:05 +00002015static StructuralEquivalenceKind
2016getStructuralEquivalenceKind(const ASTImporter &Importer) {
2017 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
2018 : StructuralEquivalenceKind::Default;
2019}
2020
Gabor Marton950fb572018-07-17 12:39:27 +00002021bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
2022 StructuralEquivalenceContext Ctx(
2023 Importer.getFromContext(), Importer.getToContext(),
2024 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2025 false, Complain);
2026 return Ctx.IsEquivalent(From, To);
2027}
2028
2029bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002030 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002031 // Eliminate a potential failure point where we attempt to re-import
2032 // something we're trying to import while completing ToRecord.
2033 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2034 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002035 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002036 if (ToOriginRecord)
2037 ToRecord = ToOriginRecord;
2038 }
2039
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002040 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002041 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002042 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002043 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002044 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002045 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002046}
2047
Larisse Voufo39a1e502013-08-06 01:03:05 +00002048bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2049 bool Complain) {
2050 StructuralEquivalenceContext Ctx(
2051 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002052 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2053 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002054 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002055}
2056
Douglas Gregor98c10182010-02-12 22:17:39 +00002057bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002058 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00002059 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002060 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
2061 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
2062 ToEnum = ToOriginEnum;
2063
Gabor Marton26f72a92018-07-12 09:42:05 +00002064 StructuralEquivalenceContext Ctx(
2065 Importer.getFromContext(), Importer.getToContext(),
2066 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002067 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002068}
2069
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002070bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2071 FunctionTemplateDecl *To) {
2072 StructuralEquivalenceContext Ctx(
2073 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002074 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2075 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002076 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002077}
2078
Balazs Keric7797c42018-07-11 09:37:24 +00002079bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2080 StructuralEquivalenceContext Ctx(
2081 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002082 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2083 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002084 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002085}
2086
Douglas Gregor91155082012-11-14 22:29:20 +00002087bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002088 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002089 const llvm::APSInt &FromVal = FromEC->getInitVal();
2090 const llvm::APSInt &ToVal = ToEC->getInitVal();
2091
2092 return FromVal.isSigned() == ToVal.isSigned() &&
2093 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2094 FromVal == ToVal;
2095}
2096
2097bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002098 ClassTemplateDecl *To) {
2099 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2100 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002101 Importer.getNonEquivalentDecls(),
2102 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002103 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002104}
2105
Larisse Voufo39a1e502013-08-06 01:03:05 +00002106bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2107 VarTemplateDecl *To) {
2108 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2109 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002110 Importer.getNonEquivalentDecls(),
2111 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002112 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002113}
2114
Balazs Keri3b30d652018-10-19 13:32:20 +00002115ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002116 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002117 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002118 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002119}
2120
Balazs Keri3b30d652018-10-19 13:32:20 +00002121ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2122 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2123 << D->getDeclKindName();
2124 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2125}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002126
Balazs Keri3b30d652018-10-19 13:32:20 +00002127ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2128 // Import the context of this declaration.
2129 DeclContext *DC, *LexicalDC;
2130 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2131 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002132
2133 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002134 ExpectedSLoc LocOrErr = import(D->getLocation());
2135 if (!LocOrErr)
2136 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002137
Gabor Marton26f72a92018-07-12 09:42:05 +00002138 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002139 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002140 return ToD;
2141
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002142 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002143 LexicalDC->addDeclInternal(ToD);
2144 return ToD;
2145}
2146
Balazs Keri3b30d652018-10-19 13:32:20 +00002147ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002148 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002149 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002150
Gabor Marton26f72a92018-07-12 09:42:05 +00002151 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002152
Sean Callanan65198272011-11-17 23:20:56 +00002153 return ToD;
2154}
2155
Balazs Keri3b30d652018-10-19 13:32:20 +00002156ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2157 ExpectedSLoc LocOrErr = import(D->getLocation());
2158 if (!LocOrErr)
2159 return LocOrErr.takeError();
2160 auto ColonLocOrErr = import(D->getColonLoc());
2161 if (!ColonLocOrErr)
2162 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002163
2164 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002165 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2166 if (!DCOrErr)
2167 return DCOrErr.takeError();
2168 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002169
Gabor Marton26f72a92018-07-12 09:42:05 +00002170 AccessSpecDecl *ToD;
2171 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002172 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002173 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002174
2175 // Lexical DeclContext and Semantic DeclContext
2176 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002177 ToD->setLexicalDeclContext(DC);
2178 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002179
Gabor Marton26f72a92018-07-12 09:42:05 +00002180 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002181}
2182
Balazs Keri3b30d652018-10-19 13:32:20 +00002183ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2184 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2185 if (!DCOrErr)
2186 return DCOrErr.takeError();
2187 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002188 DeclContext *LexicalDC = DC;
2189
Balazs Keri3b30d652018-10-19 13:32:20 +00002190 SourceLocation ToLocation, ToRParenLoc;
2191 Expr *ToAssertExpr;
2192 StringLiteral *ToMessage;
2193 if (auto Imp = importSeq(
2194 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2195 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2196 else
2197 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002198
Gabor Marton26f72a92018-07-12 09:42:05 +00002199 StaticAssertDecl *ToD;
2200 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002201 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2202 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002203 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002204
2205 ToD->setLexicalDeclContext(LexicalDC);
2206 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002207 return ToD;
2208}
2209
Balazs Keri3b30d652018-10-19 13:32:20 +00002210ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002211 // Import the major distinguishing characteristics of this namespace.
2212 DeclContext *DC, *LexicalDC;
2213 DeclarationName Name;
2214 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002215 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002216 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2217 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002218 if (ToD)
2219 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002220
2221 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002222 if (!Name) {
2223 // This is an anonymous namespace. Adopt an existing anonymous
2224 // namespace if we can.
2225 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002226 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002227 MergeWithNamespace = TU->getAnonymousNamespace();
2228 else
2229 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2230 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002231 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002232 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002233 for (auto *FoundDecl : FoundDecls) {
2234 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002235 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002236
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002237 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002238 MergeWithNamespace = FoundNS;
2239 ConflictingDecls.clear();
2240 break;
2241 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002242
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002243 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002244 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002245
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002246 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002247 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002248 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002249 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002250 if (!Name)
2251 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002252 }
2253 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002254
Balazs Keri3b30d652018-10-19 13:32:20 +00002255 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2256 if (!BeginLocOrErr)
2257 return BeginLocOrErr.takeError();
2258
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002259 // Create the "to" namespace, if needed.
2260 NamespaceDecl *ToNamespace = MergeWithNamespace;
2261 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002262 if (GetImportedOrCreateDecl(
2263 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002264 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002265 /*PrevDecl=*/nullptr))
2266 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002267 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002268 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002269
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002270 // If this is an anonymous namespace, register it as the anonymous
2271 // namespace within its context.
2272 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002273 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002274 TU->setAnonymousNamespace(ToNamespace);
2275 else
2276 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2277 }
2278 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002279 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002280
Balazs Keri3b30d652018-10-19 13:32:20 +00002281 if (Error Err = ImportDeclContext(D))
2282 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002283
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002284 return ToNamespace;
2285}
2286
Balazs Keri3b30d652018-10-19 13:32:20 +00002287ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002288 // Import the major distinguishing characteristics of this namespace.
2289 DeclContext *DC, *LexicalDC;
2290 DeclarationName Name;
2291 SourceLocation Loc;
2292 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002293 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2294 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002295 if (LookupD)
2296 return LookupD;
2297
2298 // NOTE: No conflict resolution is done for namespace aliases now.
2299
Balazs Keri3b30d652018-10-19 13:32:20 +00002300 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2301 NestedNameSpecifierLoc ToQualifierLoc;
2302 NamespaceDecl *ToNamespace;
2303 if (auto Imp = importSeq(
2304 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2305 D->getTargetNameLoc(), D->getNamespace()))
2306 std::tie(
2307 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2308 ToNamespace) = *Imp;
2309 else
2310 return Imp.takeError();
2311 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002312
Gabor Marton26f72a92018-07-12 09:42:05 +00002313 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002314 if (GetImportedOrCreateDecl(
2315 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2316 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002317 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002318
2319 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002320 LexicalDC->addDeclInternal(ToD);
2321
2322 return ToD;
2323}
2324
Balazs Keri3b30d652018-10-19 13:32:20 +00002325ExpectedDecl
2326ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002327 // Import the major distinguishing characteristics of this typedef.
2328 DeclContext *DC, *LexicalDC;
2329 DeclarationName Name;
2330 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002331 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002332 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2333 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002334 if (ToD)
2335 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002336
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002337 // If this typedef is not in block scope, determine whether we've
2338 // seen a typedef with the same name (that we can merge with) or any
2339 // other entity by that name (which name lookup could conflict with).
2340 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002341 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002342 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002343 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002344 for (auto *FoundDecl : FoundDecls) {
2345 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002346 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002347 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002348 QualType FromUT = D->getUnderlyingType();
2349 QualType FoundUT = FoundTypedef->getUnderlyingType();
2350 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2351 // If the "From" context has a complete underlying type but we
2352 // already have a complete underlying type then return with that.
2353 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002354 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002355 }
Gabor Martondd2b76e2019-06-11 13:35:25 +00002356 // FIXME Handle redecl chain. When you do that make consistent changes
2357 // in ASTImporterLookupTable too.
Gabor Martonb93baf62018-11-27 09:51:36 +00002358 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002359 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002360
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002361 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002362 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002363
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002364 if (!ConflictingDecls.empty()) {
2365 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002366 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002367 ConflictingDecls.size());
2368 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002369 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002370 }
2371 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002372
Balazs Keri3b30d652018-10-19 13:32:20 +00002373 QualType ToUnderlyingType;
2374 TypeSourceInfo *ToTypeSourceInfo;
2375 SourceLocation ToBeginLoc;
2376 if (auto Imp = importSeq(
2377 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2378 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2379 else
2380 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002381
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002382 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002383 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002384 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002385 if (IsAlias) {
2386 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002387 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2388 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002389 return ToTypedef;
2390 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002391 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2392 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002393 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002394
Douglas Gregordd483172010-02-22 17:42:47 +00002395 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002396 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002397
2398 // Templated declarations should not appear in DeclContext.
2399 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2400 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2401 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002402
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002403 return ToTypedef;
2404}
2405
Balazs Keri3b30d652018-10-19 13:32:20 +00002406ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002407 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2408}
2409
Balazs Keri3b30d652018-10-19 13:32:20 +00002410ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002411 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2412}
2413
Balazs Keri3b30d652018-10-19 13:32:20 +00002414ExpectedDecl
2415ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002416 // Import the major distinguishing characteristics of this typedef.
2417 DeclContext *DC, *LexicalDC;
2418 DeclarationName Name;
2419 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002420 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002421 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2422 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002423 if (FoundD)
2424 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002425
2426 // If this typedef is not in block scope, determine whether we've
2427 // seen a typedef with the same name (that we can merge with) or any
2428 // other entity by that name (which name lookup could conflict with).
2429 if (!DC->isFunctionOrMethod()) {
2430 SmallVector<NamedDecl *, 4> ConflictingDecls;
2431 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002432 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002433 for (auto *FoundDecl : FoundDecls) {
2434 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002435 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002436 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002437 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002438 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002439 }
2440
2441 if (!ConflictingDecls.empty()) {
2442 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2443 ConflictingDecls.data(),
2444 ConflictingDecls.size());
2445 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002446 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002447 }
2448 }
2449
Balazs Keri3b30d652018-10-19 13:32:20 +00002450 TemplateParameterList *ToTemplateParameters;
2451 TypeAliasDecl *ToTemplatedDecl;
2452 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2453 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2454 else
2455 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002456
Gabor Marton26f72a92018-07-12 09:42:05 +00002457 TypeAliasTemplateDecl *ToAlias;
2458 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002459 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002460 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002461
Balazs Keri3b30d652018-10-19 13:32:20 +00002462 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002463
Gabor Horvath7a91c082017-11-14 11:30:38 +00002464 ToAlias->setAccess(D->getAccess());
2465 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002466 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002467 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002468}
2469
Balazs Keri3b30d652018-10-19 13:32:20 +00002470ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002471 // Import the major distinguishing characteristics of this label.
2472 DeclContext *DC, *LexicalDC;
2473 DeclarationName Name;
2474 SourceLocation Loc;
2475 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002476 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2477 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002478 if (ToD)
2479 return ToD;
2480
2481 assert(LexicalDC->isFunctionOrMethod());
2482
Gabor Marton26f72a92018-07-12 09:42:05 +00002483 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002484 if (D->isGnuLocal()) {
2485 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2486 if (!BeginLocOrErr)
2487 return BeginLocOrErr.takeError();
2488 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2489 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2490 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002491
Balazs Keri3b30d652018-10-19 13:32:20 +00002492 } else {
2493 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2494 Name.getAsIdentifierInfo()))
2495 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002496
Balazs Keri3b30d652018-10-19 13:32:20 +00002497 }
2498
2499 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2500 if (!ToStmtOrErr)
2501 return ToStmtOrErr.takeError();
2502
2503 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002504 ToLabel->setLexicalDeclContext(LexicalDC);
2505 LexicalDC->addDeclInternal(ToLabel);
2506 return ToLabel;
2507}
2508
Balazs Keri3b30d652018-10-19 13:32:20 +00002509ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002510 // Import the major distinguishing characteristics of this enum.
2511 DeclContext *DC, *LexicalDC;
2512 DeclarationName Name;
2513 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002514 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002515 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2516 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002517 if (ToD)
2518 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002519
Douglas Gregor98c10182010-02-12 22:17:39 +00002520 // Figure out what enum name we're looking for.
2521 unsigned IDNS = Decl::IDNS_Tag;
2522 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002523 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002524 if (Error Err = importInto(
2525 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2526 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002527 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002528 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002529 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002530
Douglas Gregor98c10182010-02-12 22:17:39 +00002531 // We may already have an enum of the same name; try to find and match it.
2532 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002533 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002534 auto FoundDecls =
2535 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002536 for (auto *FoundDecl : FoundDecls) {
2537 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002538 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002539
Balazs Keri3b30d652018-10-19 13:32:20 +00002540 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002541 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002542 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002543 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002544
Balazs Keri3b30d652018-10-19 13:32:20 +00002545 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Balazs Kerieb79b252019-07-09 11:08:18 +00002546 if (!hasSameVisibilityContext(FoundEnum, D))
2547 continue;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002548 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002549 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002550 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002551
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002552 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002553 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002554
Douglas Gregor98c10182010-02-12 22:17:39 +00002555 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002556 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002557 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002558 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002559 if (!Name)
2560 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002561 }
2562 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002563
Balazs Keri3b30d652018-10-19 13:32:20 +00002564 SourceLocation ToBeginLoc;
2565 NestedNameSpecifierLoc ToQualifierLoc;
2566 QualType ToIntegerType;
2567 if (auto Imp = importSeq(
2568 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2569 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2570 else
2571 return Imp.takeError();
2572
Douglas Gregor98c10182010-02-12 22:17:39 +00002573 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002574 EnumDecl *D2;
2575 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002576 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002577 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2578 D->isScopedUsingClassTag(), D->isFixed()))
2579 return D2;
2580
Balazs Keri3b30d652018-10-19 13:32:20 +00002581 D2->setQualifierInfo(ToQualifierLoc);
2582 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002583 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002584 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002585 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002586
Douglas Gregor98c10182010-02-12 22:17:39 +00002587 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002588 if (D->isCompleteDefinition())
2589 if (Error Err = ImportDefinition(D, D2))
2590 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002591
Douglas Gregor3996e242010-02-15 22:01:00 +00002592 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002593}
2594
Balazs Keri3b30d652018-10-19 13:32:20 +00002595ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002596 bool IsFriendTemplate = false;
2597 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2598 IsFriendTemplate =
2599 DCXX->getDescribedClassTemplate() &&
2600 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2601 Decl::FOK_None;
2602 }
2603
Douglas Gregor5c73e912010-02-11 00:48:18 +00002604 // Import the major distinguishing characteristics of this record.
2605 DeclContext *DC, *LexicalDC;
2606 DeclarationName Name;
2607 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002608 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002609 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2610 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002611 if (ToD)
2612 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002613
Douglas Gregor5c73e912010-02-11 00:48:18 +00002614 // Figure out what structure name we're looking for.
2615 unsigned IDNS = Decl::IDNS_Tag;
2616 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002617 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002618 if (Error Err = importInto(
2619 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2620 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002621 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002622 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002623 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002624
2625 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002626 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002627 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002628 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002629 auto FoundDecls =
2630 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002631 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002632 // We're going to have to compare D against potentially conflicting Decls,
2633 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002634 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2635 D->getASTContext().getExternalSource()->CompleteType(D);
2636 }
2637
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002638 for (auto *FoundDecl : FoundDecls) {
2639 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002640 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002641
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002642 Decl *Found = FoundDecl;
2643 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2644 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002645 Found = Tag->getDecl();
2646 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002647
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002648 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002649 // Do not emit false positive diagnostic in case of unnamed
2650 // struct/union and in case of anonymous structs. Would be false
2651 // because there may be several anonymous/unnamed structs in a class.
2652 // E.g. these are both valid:
2653 // struct A { // unnamed structs
2654 // struct { struct A *next; } entry0;
2655 // struct { struct A *next; } entry1;
2656 // };
2657 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2658 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002659 if (!IsStructuralMatch(D, FoundRecord, false))
2660 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002661
Balazs Keric8272192019-05-27 09:36:00 +00002662 if (!hasSameVisibilityContext(FoundRecord, D))
2663 continue;
2664
Gabor Marton7df342a2018-12-17 12:42:12 +00002665 if (IsStructuralMatch(D, FoundRecord)) {
2666 RecordDecl *FoundDef = FoundRecord->getDefinition();
2667 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002668 // FIXME: Structural equivalence check should check for same
2669 // user-defined methods.
2670 Importer.MapImported(D, FoundDef);
2671 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2672 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2673 assert(FoundCXX && "Record type mismatch");
2674
Gabor Marton7df342a2018-12-17 12:42:12 +00002675 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002676 // FoundDef may not have every implicit method that D has
2677 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002678 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2679 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002680 }
Douglas Gregor25791052010-02-12 00:09:27 +00002681 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002682 PrevDecl = FoundRecord->getMostRecentDecl();
2683 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002684 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002685 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002686
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002687 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002688 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002689
Douglas Gregordd6006f2012-07-17 21:16:27 +00002690 if (!ConflictingDecls.empty() && SearchName) {
Shafik Yaghmour468724e2019-05-24 16:53:44 +00002691 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002692 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002693 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002694 if (!Name)
2695 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002696 }
2697 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002698
Balazs Keri3b30d652018-10-19 13:32:20 +00002699 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2700 if (!BeginLocOrErr)
2701 return BeginLocOrErr.takeError();
2702
Douglas Gregor5c73e912010-02-11 00:48:18 +00002703 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002704 RecordDecl *D2 = nullptr;
2705 CXXRecordDecl *D2CXX = nullptr;
2706 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2707 if (DCXX->isLambda()) {
2708 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2709 if (!TInfoOrErr)
2710 return TInfoOrErr.takeError();
2711 if (GetImportedOrCreateSpecialDecl(
2712 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2713 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2714 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2715 return D2CXX;
2716 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2717 if (!CDeclOrErr)
2718 return CDeclOrErr.takeError();
2719 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2720 } else if (DCXX->isInjectedClassName()) {
2721 // We have to be careful to do a similar dance to the one in
2722 // Sema::ActOnStartCXXMemberDeclarations
2723 const bool DelayTypeCreation = true;
2724 if (GetImportedOrCreateDecl(
2725 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2726 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2727 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2728 return D2CXX;
2729 Importer.getToContext().getTypeDeclType(
2730 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2731 } else {
2732 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2733 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2734 Name.getAsIdentifierInfo(),
2735 cast_or_null<CXXRecordDecl>(PrevDecl)))
2736 return D2CXX;
2737 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002738
Gabor Marton7df342a2018-12-17 12:42:12 +00002739 D2 = D2CXX;
2740 D2->setAccess(D->getAccess());
2741 D2->setLexicalDeclContext(LexicalDC);
2742 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2743 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002744
Gabor Marton7df342a2018-12-17 12:42:12 +00002745 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2746 DC->makeDeclVisibleInContext(D2);
2747
2748 if (ClassTemplateDecl *FromDescribed =
2749 DCXX->getDescribedClassTemplate()) {
2750 ClassTemplateDecl *ToDescribed;
2751 if (Error Err = importInto(ToDescribed, FromDescribed))
2752 return std::move(Err);
2753 D2CXX->setDescribedClassTemplate(ToDescribed);
2754 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2755 // In a record describing a template the type should be an
2756 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2757 // previously set type to the correct value here (ToDescribed is not
2758 // available at record create).
2759 // FIXME: The previous type is cleared but not removed from
2760 // ASTContext's internal storage.
2761 CXXRecordDecl *Injected = nullptr;
2762 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2763 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2764 if (Record && Record->isInjectedClassName()) {
2765 Injected = Record;
2766 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002767 }
2768 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002769 // Create an injected type for the whole redecl chain.
2770 SmallVector<Decl *, 2> Redecls =
2771 getCanonicalForwardRedeclChain(D2CXX);
2772 for (auto *R : Redecls) {
2773 auto *RI = cast<CXXRecordDecl>(R);
2774 RI->setTypeForDecl(nullptr);
2775 // Below we create a new injected type and assign that to the
2776 // canonical decl, subsequent declarations in the chain will reuse
2777 // that type.
2778 Importer.getToContext().getInjectedClassNameType(
2779 RI, ToDescribed->getInjectedClassNameSpecialization());
2780 }
2781 // Set the new type for the previous injected decl too.
2782 if (Injected) {
2783 Injected->setTypeForDecl(nullptr);
2784 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2785 }
2786 }
2787 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002788 DCXX->getMemberSpecializationInfo()) {
2789 TemplateSpecializationKind SK =
2790 MemberInfo->getTemplateSpecializationKind();
2791 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002792
2793 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2794 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2795 else
2796 return ToInstOrErr.takeError();
2797
2798 if (ExpectedSLoc POIOrErr =
2799 import(MemberInfo->getPointOfInstantiation()))
2800 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2801 *POIOrErr);
2802 else
2803 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002804 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002805
Gabor Marton7df342a2018-12-17 12:42:12 +00002806 } else {
2807 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2808 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2809 Name.getAsIdentifierInfo(), PrevDecl))
2810 return D2;
2811 D2->setLexicalDeclContext(LexicalDC);
2812 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002813 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002814
Gabor Marton7df342a2018-12-17 12:42:12 +00002815 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2816 D2->setQualifierInfo(*QualifierLocOrErr);
2817 else
2818 return QualifierLocOrErr.takeError();
2819
2820 if (D->isAnonymousStructOrUnion())
2821 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002822
Balazs Keri3b30d652018-10-19 13:32:20 +00002823 if (D->isCompleteDefinition())
2824 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2825 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002826
Douglas Gregor3996e242010-02-15 22:01:00 +00002827 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002828}
2829
Balazs Keri3b30d652018-10-19 13:32:20 +00002830ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002831 // Import the major distinguishing characteristics of this enumerator.
2832 DeclContext *DC, *LexicalDC;
2833 DeclarationName Name;
2834 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002835 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002836 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2837 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002838 if (ToD)
2839 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002840
Fangrui Song6907ce22018-07-30 19:24:48 +00002841 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002842 // in the same context.
2843 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002844 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002845 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002846 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002847 for (auto *FoundDecl : FoundDecls) {
2848 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002849 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002850
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002851 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002852 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002853 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002854 }
2855
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002856 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002857 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002858
Douglas Gregor98c10182010-02-12 22:17:39 +00002859 if (!ConflictingDecls.empty()) {
2860 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002861 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002862 ConflictingDecls.size());
2863 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002864 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002865 }
2866 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002867
Balazs Keri3b30d652018-10-19 13:32:20 +00002868 ExpectedType TypeOrErr = import(D->getType());
2869 if (!TypeOrErr)
2870 return TypeOrErr.takeError();
2871
2872 ExpectedExpr InitOrErr = import(D->getInitExpr());
2873 if (!InitOrErr)
2874 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002875
Gabor Marton26f72a92018-07-12 09:42:05 +00002876 EnumConstantDecl *ToEnumerator;
2877 if (GetImportedOrCreateDecl(
2878 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002879 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002880 return ToEnumerator;
2881
Douglas Gregordd483172010-02-22 17:42:47 +00002882 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002883 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002884 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002885 return ToEnumerator;
2886}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002887
Balazs Keri1efc9742019-05-07 10:55:11 +00002888Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2889 DeclaratorDecl *ToD) {
2890 unsigned int Num = FromD->getNumTemplateParameterLists();
2891 if (Num == 0)
2892 return Error::success();
2893 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2894 for (unsigned int I = 0; I < Num; ++I)
2895 if (Expected<TemplateParameterList *> ToTPListOrErr =
2896 import(FromD->getTemplateParameterList(I)))
2897 ToTPLists[I] = *ToTPListOrErr;
2898 else
2899 return ToTPListOrErr.takeError();
2900 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2901 return Error::success();
2902}
2903
Balazs Keri3b30d652018-10-19 13:32:20 +00002904Error ASTNodeImporter::ImportTemplateInformation(
2905 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002906 switch (FromFD->getTemplatedKind()) {
2907 case FunctionDecl::TK_NonTemplate:
2908 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002909 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002910
2911 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002912 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002913
2914 if (Expected<FunctionDecl *> InstFDOrErr =
2915 import(FromFD->getInstantiatedFromMemberFunction()))
2916 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2917 else
2918 return InstFDOrErr.takeError();
2919
2920 if (ExpectedSLoc POIOrErr = import(
2921 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2922 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2923 else
2924 return POIOrErr.takeError();
2925
2926 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002927 }
2928
2929 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002930 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002931 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002932 if (!FunctionAndArgsOrErr)
2933 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002934
2935 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002936 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002937
Gabor Marton5254e642018-06-27 13:32:50 +00002938 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002939 TemplateArgumentListInfo ToTAInfo;
2940 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002941 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002942 if (Error Err = ImportTemplateArgumentListInfo(
2943 *FromTAArgsAsWritten, ToTAInfo))
2944 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002945
Balazs Keri3b30d652018-10-19 13:32:20 +00002946 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2947 if (!POIOrErr)
2948 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002949
Balazs Keri1efc9742019-05-07 10:55:11 +00002950 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
2951 return Err;
2952
Gabor Marton5254e642018-06-27 13:32:50 +00002953 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002954 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002955 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2956 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2957 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002958 }
2959
2960 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2961 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2962 UnresolvedSet<8> TemplDecls;
2963 unsigned NumTemplates = FromInfo->getNumTemplates();
2964 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002965 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2966 import(FromInfo->getTemplate(I)))
2967 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002968 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002969 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002970 }
2971
2972 // Import TemplateArgumentListInfo.
2973 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002974 if (Error Err = ImportTemplateArgumentListInfo(
2975 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2976 llvm::makeArrayRef(
2977 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2978 ToTAInfo))
2979 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002980
2981 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2982 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002983 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002984 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002985 }
Sam McCallfdc32072018-01-26 12:06:44 +00002986 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002987}
2988
Balazs Keri3b30d652018-10-19 13:32:20 +00002989Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002990ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002991 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002992 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002993 if (!FunctionAndArgsOrErr)
2994 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002995
Balazs Keri3b30d652018-10-19 13:32:20 +00002996 FunctionTemplateDecl *Template;
2997 TemplateArgsTy ToTemplArgs;
2998 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002999 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003000 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00003001 return FoundSpec;
3002}
3003
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003004Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
3005 FunctionDecl *ToFD) {
3006 if (Stmt *FromBody = FromFD->getBody()) {
3007 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3008 ToFD->setBody(*ToBodyOrErr);
3009 else
3010 return ToBodyOrErr.takeError();
3011 }
3012 return Error::success();
3013}
3014
Gabor Marton458d1452019-02-14 13:07:03 +00003015template <typename T>
3016bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
3017 if (From->hasExternalFormalLinkage())
3018 return Found->hasExternalFormalLinkage();
3019 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
3020 return false;
3021 if (From->isInAnonymousNamespace())
3022 return Found->isInAnonymousNamespace();
3023 else
3024 return !Found->isInAnonymousNamespace() &&
3025 !Found->hasExternalFormalLinkage();
3026}
3027
Balazs Keri3b30d652018-10-19 13:32:20 +00003028ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00003029
Balazs Keri3b30d652018-10-19 13:32:20 +00003030 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00003031 auto RedeclIt = Redecls.begin();
3032 // Import the first part of the decl chain. I.e. import all previous
3033 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003034 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3035 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3036 if (!ToRedeclOrErr)
3037 return ToRedeclOrErr.takeError();
3038 }
Gabor Marton5254e642018-06-27 13:32:50 +00003039 assert(*RedeclIt == D);
3040
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003041 // Import the major distinguishing characteristics of this function.
3042 DeclContext *DC, *LexicalDC;
3043 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003044 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003045 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003046 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3047 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003048 if (ToD)
3049 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003050
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003051 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003052 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003053
Gabor Marton5254e642018-06-27 13:32:50 +00003054 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00003055 // existing specialization in the "to" context. The lookup below will not
3056 // find any specialization, but would find the primary template; thus, we
3057 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00003058 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3059 if (D->getTemplatedKind() ==
3060 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003061 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3062 if (!FoundFunctionOrErr)
3063 return FoundFunctionOrErr.takeError();
3064 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00003065 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3066 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00003067 FoundByLookup = FoundFunction;
3068 }
3069 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003070 // Try to find a function in our own ("to") context with the same name, same
3071 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003072 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003073 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003074 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00003075 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003076 for (auto *FoundDecl : FoundDecls) {
3077 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003078 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003079
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003080 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003081 if (!hasSameVisibilityContext(FoundFunction, D))
3082 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003083
Gabor Marton458d1452019-02-14 13:07:03 +00003084 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00003085 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3086 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00003087 FoundByLookup = FoundFunction;
3088 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003089 }
Gabor Marton458d1452019-02-14 13:07:03 +00003090 // FIXME: Check for overloading more carefully, e.g., by boosting
3091 // Sema::IsOverload out to the AST library.
3092
3093 // Function overloading is okay in C++.
3094 if (Importer.getToContext().getLangOpts().CPlusPlus)
3095 continue;
3096
3097 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00003098 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003099 << Name << D->getType() << FoundFunction->getType();
3100 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
3101 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003102 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003103
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003104 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003105 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003106
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003107 if (!ConflictingDecls.empty()) {
3108 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003109 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003110 ConflictingDecls.size());
3111 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003112 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003113 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003114 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003115
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003116 // We do not allow more than one in-class declaration of a function. This is
3117 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3118 // assumes there is only one in-class declaration. Building a redecl
3119 // chain would result in more than one in-class declaration for
3120 // overrides (even if they are part of the same redecl chain inside the
3121 // derived class.)
3122 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003123 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003124 if (D->getLexicalDeclContext() == D->getDeclContext()) {
Balazs Kerie9719f92019-08-07 12:40:17 +00003125 if (!D->doesThisDeclarationHaveABody()) {
3126 if (FunctionTemplateDecl *DescribedD =
3127 D->getDescribedFunctionTemplate()) {
3128 // Handle a "templated" function together with its described
3129 // template. This avoids need for a similar check at import of the
3130 // described template.
3131 assert(FoundByLookup->getDescribedFunctionTemplate() &&
3132 "Templated function mapped to non-templated?");
3133 Importer.MapImported(DescribedD,
3134 FoundByLookup->getDescribedFunctionTemplate());
3135 }
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003136 return Importer.MapImported(D, FoundByLookup);
Balazs Kerie9719f92019-08-07 12:40:17 +00003137 } else {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003138 // Let's continue and build up the redecl chain in this case.
3139 // FIXME Merge the functions into one decl.
3140 }
3141 }
3142 }
3143 }
3144
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003145 DeclarationNameInfo NameInfo(Name, Loc);
3146 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003147 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3148 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003149
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003150 QualType FromTy = D->getType();
3151 bool usedDifferentExceptionSpec = false;
3152
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003153 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003154 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3155 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3156 // FunctionDecl that we are importing the FunctionProtoType for.
3157 // To avoid an infinite recursion when importing, create the FunctionDecl
3158 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003159 if (FromEPI.ExceptionSpec.SourceDecl ||
3160 FromEPI.ExceptionSpec.SourceTemplate ||
3161 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003162 FunctionProtoType::ExtProtoInfo DefaultEPI;
3163 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003164 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003165 usedDifferentExceptionSpec = true;
3166 }
3167 }
3168
Balazs Keri3b30d652018-10-19 13:32:20 +00003169 QualType T;
3170 TypeSourceInfo *TInfo;
3171 SourceLocation ToInnerLocStart, ToEndLoc;
3172 NestedNameSpecifierLoc ToQualifierLoc;
3173 if (auto Imp = importSeq(
3174 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3175 D->getQualifierLoc(), D->getEndLoc()))
3176 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3177 else
3178 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003179
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003180 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003181 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003182 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003183 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3184 Parameters.push_back(*ToPOrErr);
3185 else
3186 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003187 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003188
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003189 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003190 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003191 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003192 Expr *ExplicitExpr = nullptr;
3193 if (FromConstructor->getExplicitSpecifier().getExpr()) {
3194 auto Imp = importSeq(FromConstructor->getExplicitSpecifier().getExpr());
3195 if (!Imp)
3196 return Imp.takeError();
3197 std::tie(ExplicitExpr) = *Imp;
3198 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003199 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Richard Smith76b90272019-05-09 03:59:21 +00003200 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3201 ToInnerLocStart, NameInfo, T, TInfo,
3202 ExplicitSpecifier(
3203 ExplicitExpr,
3204 FromConstructor->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003205 D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003206 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003207 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3208
3209 auto Imp =
3210 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3211 FromDtor->getOperatorDeleteThisArg());
3212
3213 if (!Imp)
3214 return Imp.takeError();
3215
3216 FunctionDecl *ToOperatorDelete;
3217 Expr *ToThisArg;
3218 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3219
Gabor Marton26f72a92018-07-12 09:42:05 +00003220 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003221 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3222 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3223 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003224 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003225
3226 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3227
3228 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003229 } else if (CXXConversionDecl *FromConversion =
3230 dyn_cast<CXXConversionDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003231 Expr *ExplicitExpr = nullptr;
3232 if (FromConversion->getExplicitSpecifier().getExpr()) {
3233 auto Imp = importSeq(FromConversion->getExplicitSpecifier().getExpr());
3234 if (!Imp)
3235 return Imp.takeError();
3236 std::tie(ExplicitExpr) = *Imp;
3237 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003238 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3239 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003240 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith76b90272019-05-09 03:59:21 +00003241 ExplicitSpecifier(ExplicitExpr,
3242 FromConversion->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003243 D->getConstexprKind(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003244 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003245 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003246 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3247 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003248 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003249 Method->isInlineSpecified(), D->getConstexprKind(),
3250 SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003251 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003252 } else {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003253 if (GetImportedOrCreateDecl(
3254 ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart,
3255 NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(),
3256 D->hasWrittenPrototype(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003257 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003258 }
John McCall3e11ebe2010-03-15 10:12:16 +00003259
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003260 // Connect the redecl chain.
3261 if (FoundByLookup) {
3262 auto *Recent = const_cast<FunctionDecl *>(
3263 FoundByLookup->getMostRecentDecl());
3264 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003265 // FIXME Probably we should merge exception specifications. E.g. In the
3266 // "To" context the existing function may have exception specification with
3267 // noexcept-unevaluated, while the newly imported function may have an
3268 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3269 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003270 }
3271
3272 // Import Ctor initializers.
3273 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3274 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3275 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3276 // Import first, then allocate memory and copy if there was no error.
3277 if (Error Err = ImportContainerChecked(
3278 FromConstructor->inits(), CtorInitializers))
3279 return std::move(Err);
3280 auto **Memory =
3281 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3282 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3283 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3284 ToCtor->setCtorInitializers(Memory);
3285 ToCtor->setNumCtorInitializers(NumInitializers);
3286 }
3287 }
3288
Balazs Keri3b30d652018-10-19 13:32:20 +00003289 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003290 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003291 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003292 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3293 ToFunction->setTrivial(D->isTrivial());
3294 ToFunction->setPure(D->isPure());
Balazs Kerib427c062019-08-13 08:04:06 +00003295 ToFunction->setDefaulted(D->isDefaulted());
3296 ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted());
3297 ToFunction->setDeletedAsWritten(D->isDeletedAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00003298 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003299
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003300 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003301 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003302 Param->setOwningFunction(ToFunction);
3303 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003304 }
David Blaikie9c70e042011-09-21 18:16:56 +00003305 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003306
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003307 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3308 // params it refers to.
3309 if (TInfo) {
3310 if (auto ProtoLoc =
3311 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3312 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3313 ProtoLoc.setParam(I, Parameters[I]);
3314 }
3315 }
3316
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003317 if (usedDifferentExceptionSpec) {
3318 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003319 if (ExpectedType TyOrErr = import(D->getType()))
3320 ToFunction->setType(*TyOrErr);
3321 else
3322 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003323 }
3324
Balazs Keria35798d2018-07-17 09:52:41 +00003325 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003326 if (FromFT) {
3327 auto ToFTOrErr = import(FromFT);
3328 if (!ToFTOrErr)
3329 return ToFTOrErr.takeError();
3330 }
Balazs Keria35798d2018-07-17 09:52:41 +00003331
Gabor Marton5254e642018-06-27 13:32:50 +00003332 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003333 Error Err = ImportFunctionDeclBody(D, ToFunction);
3334
3335 if (Err)
3336 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003337 }
3338
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003339 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003340
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003341 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003342 if (Error Err = ImportTemplateInformation(D, ToFunction))
3343 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003344
Gabor Marton5254e642018-06-27 13:32:50 +00003345 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3346
3347 // TODO Can we generalize this approach to other AST nodes as well?
3348 if (D->getDeclContext()->containsDeclAndLoad(D))
3349 DC->addDeclInternal(ToFunction);
3350 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003351 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003352
Gabor Marton5254e642018-06-27 13:32:50 +00003353 // Friend declaration's lexical context is the befriending class, but the
3354 // semantic context is the enclosing scope of the befriending class.
3355 // We want the friend functions to be found in the semantic context by lookup.
3356 // FIXME should we handle this generically in VisitFriendDecl?
3357 // In Other cases when LexicalDC != DC we don't want it to be added,
3358 // e.g out-of-class definitions like void B::f() {} .
3359 if (LexicalDC != DC && IsFriend) {
3360 DC->makeDeclVisibleInContext(ToFunction);
3361 }
3362
Gabor Marton7a0841e2018-10-29 10:18:28 +00003363 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3364 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3365
Gabor Marton5254e642018-06-27 13:32:50 +00003366 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003367 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3368 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3369 if (!ToRedeclOrErr)
3370 return ToRedeclOrErr.takeError();
3371 }
Gabor Marton5254e642018-06-27 13:32:50 +00003372
Douglas Gregor43f54792010-02-17 02:12:47 +00003373 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003374}
3375
Balazs Keri3b30d652018-10-19 13:32:20 +00003376ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003377 return VisitFunctionDecl(D);
3378}
3379
Balazs Keri3b30d652018-10-19 13:32:20 +00003380ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003381 return VisitCXXMethodDecl(D);
3382}
3383
Balazs Keri3b30d652018-10-19 13:32:20 +00003384ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003385 return VisitCXXMethodDecl(D);
3386}
3387
Balazs Keri3b30d652018-10-19 13:32:20 +00003388ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003389 return VisitCXXMethodDecl(D);
3390}
3391
Balazs Keri3b30d652018-10-19 13:32:20 +00003392ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003393 // Import the major distinguishing characteristics of a variable.
3394 DeclContext *DC, *LexicalDC;
3395 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003396 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003397 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003398 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3399 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003400 if (ToD)
3401 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003402
Fangrui Song6907ce22018-07-30 19:24:48 +00003403 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003404 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003405 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003406 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003407 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003408 if (!Name &&
3409 ASTImporter::getFieldIndex(D) !=
3410 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003411 continue;
3412
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003413 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003414 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003415 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003416 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3417 // initializer of a FieldDecl might not had been instantiated in the
3418 // "To" context. However, the "From" context might instantiated that,
3419 // thus we have to merge that.
3420 if (Expr *FromInitializer = D->getInClassInitializer()) {
3421 // We don't have yet the initializer set.
3422 if (FoundField->hasInClassInitializer() &&
3423 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003424 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3425 FoundField->setInClassInitializer(*ToInitializerOrErr);
3426 else {
3427 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003428 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003429 // FIXME: warning message?
3430 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003431 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003432 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003433 }
3434 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003435 return FoundField;
3436 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003437
Balazs Keri3b30d652018-10-19 13:32:20 +00003438 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003439 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003440 << Name << D->getType() << FoundField->getType();
3441 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3442 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003443
3444 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003445 }
3446 }
3447
Balazs Keri3b30d652018-10-19 13:32:20 +00003448 QualType ToType;
3449 TypeSourceInfo *ToTInfo;
3450 Expr *ToBitWidth;
3451 SourceLocation ToInnerLocStart;
3452 Expr *ToInitializer;
3453 if (auto Imp = importSeq(
3454 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3455 D->getInnerLocStart(), D->getInClassInitializer()))
3456 std::tie(
3457 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3458 else
3459 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003460
Gabor Marton26f72a92018-07-12 09:42:05 +00003461 FieldDecl *ToField;
3462 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003463 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3464 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3465 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003466 return ToField;
3467
Douglas Gregordd483172010-02-22 17:42:47 +00003468 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003469 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003470 if (ToInitializer)
3471 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003472 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003473 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003474 return ToField;
3475}
3476
Balazs Keri3b30d652018-10-19 13:32:20 +00003477ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003478 // Import the major distinguishing characteristics of a variable.
3479 DeclContext *DC, *LexicalDC;
3480 DeclarationName Name;
3481 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003482 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003483 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3484 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003485 if (ToD)
3486 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003487
Fangrui Song6907ce22018-07-30 19:24:48 +00003488 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003489 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003490 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003491 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003492 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003493 if (!Name &&
3494 ASTImporter::getFieldIndex(D) !=
3495 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003496 continue;
3497
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003498 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003499 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003500 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003501 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003502 return FoundField;
3503 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003504
3505 // If there are more anonymous fields to check, continue.
3506 if (!Name && I < N-1)
3507 continue;
3508
Balazs Keri3b30d652018-10-19 13:32:20 +00003509 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003510 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003511 << Name << D->getType() << FoundField->getType();
3512 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3513 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003514
3515 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003516 }
3517 }
3518
Francois Pichet783dd6e2010-11-21 06:08:52 +00003519 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003520 auto TypeOrErr = import(D->getType());
3521 if (!TypeOrErr)
3522 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003523
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003524 auto **NamedChain =
3525 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003526
3527 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003528 for (auto *PI : D->chain())
3529 if (Expected<NamedDecl *> ToD = import(PI))
3530 NamedChain[i++] = *ToD;
3531 else
3532 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003533
Gabor Marton26f72a92018-07-12 09:42:05 +00003534 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3535 IndirectFieldDecl *ToIndirectField;
3536 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003537 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003538 // FIXME here we leak `NamedChain` which is allocated before
3539 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003540
Francois Pichet783dd6e2010-11-21 06:08:52 +00003541 ToIndirectField->setAccess(D->getAccess());
3542 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003543 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003544 return ToIndirectField;
3545}
3546
Balazs Keri3b30d652018-10-19 13:32:20 +00003547ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003548 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003549 DeclContext *DC, *LexicalDC;
3550 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3551 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003552
3553 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003554 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003555 auto *RD = cast<CXXRecordDecl>(DC);
3556 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003557
3558 while (ImportedFriend) {
3559 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003560 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3561 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003562 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003563
3564 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3565 if (Importer.IsStructurallyEquivalent(
3566 D->getFriendType()->getType(),
3567 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003568 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003569 }
3570 ImportedFriend = ImportedFriend->getNextFriend();
3571 }
3572
3573 // Not found. Create it.
3574 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003575 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003576 NamedDecl *ToFriendD;
3577 if (Error Err = importInto(ToFriendD, FriendD))
3578 return std::move(Err);
3579
3580 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003581 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3582 ToFriendD->setObjectOfFriendDecl(false);
3583
3584 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003585 } else { // The friend is a type, not a decl.
3586 if (auto TSIOrErr = import(D->getFriendType()))
3587 ToFU = *TSIOrErr;
3588 else
3589 return TSIOrErr.takeError();
3590 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003591
3592 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003593 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003594 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003595 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003596 ToTPLists[I] = *ListOrErr;
3597 else
3598 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003599 }
3600
Balazs Keri3b30d652018-10-19 13:32:20 +00003601 auto LocationOrErr = import(D->getLocation());
3602 if (!LocationOrErr)
3603 return LocationOrErr.takeError();
3604 auto FriendLocOrErr = import(D->getFriendLoc());
3605 if (!FriendLocOrErr)
3606 return FriendLocOrErr.takeError();
3607
Gabor Marton26f72a92018-07-12 09:42:05 +00003608 FriendDecl *FrD;
3609 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003610 *LocationOrErr, ToFU,
3611 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003612 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003613
3614 FrD->setAccess(D->getAccess());
3615 FrD->setLexicalDeclContext(LexicalDC);
3616 LexicalDC->addDeclInternal(FrD);
3617 return FrD;
3618}
3619
Balazs Keri3b30d652018-10-19 13:32:20 +00003620ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003621 // Import the major distinguishing characteristics of an ivar.
3622 DeclContext *DC, *LexicalDC;
3623 DeclarationName Name;
3624 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003625 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003626 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3627 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003628 if (ToD)
3629 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003630
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003631 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003632 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003633 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003634 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003635 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003636 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003637 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003638 return FoundIvar;
3639 }
3640
Gabor Marton410f32c2019-04-01 15:29:55 +00003641 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003642 << Name << D->getType() << FoundIvar->getType();
3643 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3644 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003645
3646 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003647 }
3648 }
3649
Balazs Keri3b30d652018-10-19 13:32:20 +00003650 QualType ToType;
3651 TypeSourceInfo *ToTypeSourceInfo;
3652 Expr *ToBitWidth;
3653 SourceLocation ToInnerLocStart;
3654 if (auto Imp = importSeq(
3655 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3656 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3657 else
3658 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003659
Gabor Marton26f72a92018-07-12 09:42:05 +00003660 ObjCIvarDecl *ToIvar;
3661 if (GetImportedOrCreateDecl(
3662 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003663 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3664 ToType, ToTypeSourceInfo,
3665 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003666 return ToIvar;
3667
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003668 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003669 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003670 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003671}
3672
Balazs Keri3b30d652018-10-19 13:32:20 +00003673ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003674
3675 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3676 auto RedeclIt = Redecls.begin();
3677 // Import the first part of the decl chain. I.e. import all previous
3678 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003679 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3680 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3681 if (!RedeclOrErr)
3682 return RedeclOrErr.takeError();
3683 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003684 assert(*RedeclIt == D);
3685
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003686 // Import the major distinguishing characteristics of a variable.
3687 DeclContext *DC, *LexicalDC;
3688 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003689 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003690 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003691 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3692 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003693 if (ToD)
3694 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003695
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003696 // Try to find a variable in our own ("to") context with the same name and
3697 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003698 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003699 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003700 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003701 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003702 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003703 for (auto *FoundDecl : FoundDecls) {
3704 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003705 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003706
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003707 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003708 if (!hasSameVisibilityContext(FoundVar, D))
3709 continue;
3710 if (Importer.IsStructurallyEquivalent(D->getType(),
3711 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003712
Gabor Marton458d1452019-02-14 13:07:03 +00003713 // The VarDecl in the "From" context has a definition, but in the
3714 // "To" context we already have a definition.
3715 VarDecl *FoundDef = FoundVar->getDefinition();
3716 if (D->isThisDeclarationADefinition() && FoundDef)
3717 // FIXME Check for ODR error if the two definitions have
3718 // different initializers?
3719 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003720
Gabor Marton458d1452019-02-14 13:07:03 +00003721 // The VarDecl in the "From" context has an initializer, but in the
3722 // "To" context we already have an initializer.
3723 const VarDecl *FoundDInit = nullptr;
3724 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3725 // FIXME Diagnose ODR error if the two initializers are different?
3726 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3727
3728 FoundByLookup = FoundVar;
3729 break;
3730 }
3731
3732 const ArrayType *FoundArray
3733 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3734 const ArrayType *TArray
3735 = Importer.getToContext().getAsArrayType(D->getType());
3736 if (FoundArray && TArray) {
3737 if (isa<IncompleteArrayType>(FoundArray) &&
3738 isa<ConstantArrayType>(TArray)) {
3739 // Import the type.
3740 if (auto TyOrErr = import(D->getType()))
3741 FoundVar->setType(*TyOrErr);
3742 else
3743 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003744
3745 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003746 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003747 } else if (isa<IncompleteArrayType>(TArray) &&
3748 isa<ConstantArrayType>(FoundArray)) {
3749 FoundByLookup = FoundVar;
3750 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003751 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003752 }
Gabor Marton458d1452019-02-14 13:07:03 +00003753
Gabor Marton410f32c2019-04-01 15:29:55 +00003754 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003755 << Name << D->getType() << FoundVar->getType();
3756 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3757 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003758 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003759
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003760 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003761 }
3762
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003763 if (!ConflictingDecls.empty()) {
3764 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003765 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003766 ConflictingDecls.size());
3767 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003768 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003769 }
3770 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003771
Balazs Keri3b30d652018-10-19 13:32:20 +00003772 QualType ToType;
3773 TypeSourceInfo *ToTypeSourceInfo;
3774 SourceLocation ToInnerLocStart;
3775 NestedNameSpecifierLoc ToQualifierLoc;
3776 if (auto Imp = importSeq(
3777 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3778 D->getQualifierLoc()))
3779 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3780 else
3781 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003782
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003783 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003784 VarDecl *ToVar;
3785 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003786 ToInnerLocStart, Loc,
3787 Name.getAsIdentifierInfo(),
3788 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003789 D->getStorageClass()))
3790 return ToVar;
3791
Balazs Keri3b30d652018-10-19 13:32:20 +00003792 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003793 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003794 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003795
Gabor Martonac3a5d62018-09-17 12:04:52 +00003796 if (FoundByLookup) {
3797 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3798 ToVar->setPreviousDecl(Recent);
3799 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003800
Balazs Keri3b30d652018-10-19 13:32:20 +00003801 if (Error Err = ImportInitializer(D, ToVar))
3802 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003803
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003804 if (D->isConstexpr())
3805 ToVar->setConstexpr(true);
3806
Gabor Martonac3a5d62018-09-17 12:04:52 +00003807 if (D->getDeclContext()->containsDeclAndLoad(D))
3808 DC->addDeclInternal(ToVar);
3809 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3810 LexicalDC->addDeclInternal(ToVar);
3811
3812 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003813 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3814 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3815 if (!RedeclOrErr)
3816 return RedeclOrErr.takeError();
3817 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003818
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003819 return ToVar;
3820}
3821
Balazs Keri3b30d652018-10-19 13:32:20 +00003822ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003823 // Parameters are created in the translation unit's context, then moved
3824 // into the function declaration's context afterward.
3825 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003826
Balazs Keri3b30d652018-10-19 13:32:20 +00003827 DeclarationName ToDeclName;
3828 SourceLocation ToLocation;
3829 QualType ToType;
3830 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3831 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3832 else
3833 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003834
Douglas Gregor8b228d72010-02-17 21:22:52 +00003835 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003836 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003837 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3838 ToLocation, ToDeclName.getAsIdentifierInfo(),
3839 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003840 return ToParm;
3841 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003842}
3843
Balazs Keri3b30d652018-10-19 13:32:20 +00003844ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003845 // Parameters are created in the translation unit's context, then moved
3846 // into the function declaration's context afterward.
3847 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003848
Balazs Keri3b30d652018-10-19 13:32:20 +00003849 DeclarationName ToDeclName;
3850 SourceLocation ToLocation, ToInnerLocStart;
3851 QualType ToType;
3852 TypeSourceInfo *ToTypeSourceInfo;
3853 if (auto Imp = importSeq(
3854 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3855 D->getTypeSourceInfo()))
3856 std::tie(
3857 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3858 ToTypeSourceInfo) = *Imp;
3859 else
3860 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003861
Gabor Marton26f72a92018-07-12 09:42:05 +00003862 ParmVarDecl *ToParm;
3863 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003864 ToInnerLocStart, ToLocation,
3865 ToDeclName.getAsIdentifierInfo(), ToType,
3866 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003867 /*DefaultArg*/ nullptr))
3868 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003869
3870 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003871 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003872 ToParm->setKNRPromoted(D->isKNRPromoted());
3873
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003874 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003875 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3876 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3877 else
3878 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003879 } else if (D->hasUnparsedDefaultArg()) {
3880 ToParm->setUnparsedDefaultArg();
3881 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003882 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3883 ToParm->setDefaultArg(*ToDefArgOrErr);
3884 else
3885 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003886 }
Sean Callanan59721b32015-04-28 18:41:46 +00003887
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003888 if (D->isObjCMethodParameter()) {
3889 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3890 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3891 } else {
3892 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3893 D->getFunctionScopeIndex());
3894 }
3895
Gabor Marton26f72a92018-07-12 09:42:05 +00003896 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003897}
3898
Balazs Keri3b30d652018-10-19 13:32:20 +00003899ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003900 // Import the major distinguishing characteristics of a method.
3901 DeclContext *DC, *LexicalDC;
3902 DeclarationName Name;
3903 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003904 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003905 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3906 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003907 if (ToD)
3908 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003909
Gabor Marton54058b52018-12-17 13:53:12 +00003910 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003911 for (auto *FoundDecl : FoundDecls) {
3912 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003913 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3914 continue;
3915
3916 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003917 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3918 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003919 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003920 << D->isInstanceMethod() << Name << D->getReturnType()
3921 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003922 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003923 diag::note_odr_objc_method_here)
3924 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003925
3926 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003927 }
3928
3929 // Check the number of parameters.
3930 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003931 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003932 << D->isInstanceMethod() << Name
3933 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003934 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003935 diag::note_odr_objc_method_here)
3936 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003937
3938 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003939 }
3940
3941 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003942 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003943 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3944 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003945 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003946 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003947 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003948 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003949 << D->isInstanceMethod() << Name
3950 << (*P)->getType() << (*FoundP)->getType();
3951 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3952 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003953
3954 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003955 }
3956 }
3957
3958 // Check variadic/non-variadic.
3959 // Check the number of parameters.
3960 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003961 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003962 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003963 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003964 diag::note_odr_objc_method_here)
3965 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003966
3967 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003968 }
3969
3970 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003971 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003972 }
3973 }
3974
Balazs Keri3b30d652018-10-19 13:32:20 +00003975 SourceLocation ToEndLoc;
3976 QualType ToReturnType;
3977 TypeSourceInfo *ToReturnTypeSourceInfo;
3978 if (auto Imp = importSeq(
3979 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3980 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3981 else
3982 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003983
Gabor Marton26f72a92018-07-12 09:42:05 +00003984 ObjCMethodDecl *ToMethod;
3985 if (GetImportedOrCreateDecl(
3986 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003987 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3988 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003989 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3990 D->getImplementationControl(), D->hasRelatedResultType()))
3991 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003992
3993 // FIXME: When we decide to merge method definitions, we'll need to
3994 // deal with implicit parameters.
3995
3996 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003997 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003998 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003999 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
4000 ToParams.push_back(*ToPOrErr);
4001 else
4002 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00004003 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004004
Douglas Gregor43f54792010-02-17 02:12:47 +00004005 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004006 for (auto *ToParam : ToParams) {
4007 ToParam->setOwningFunction(ToMethod);
4008 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00004009 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00004010
Balazs Keri3b30d652018-10-19 13:32:20 +00004011 SmallVector<SourceLocation, 12> FromSelLocs;
4012 D->getSelectorLocs(FromSelLocs);
4013 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
4014 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
4015 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00004016
Balazs Keri3b30d652018-10-19 13:32:20 +00004017 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00004018
4019 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004020 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00004021 return ToMethod;
4022}
4023
Balazs Keri3b30d652018-10-19 13:32:20 +00004024ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00004025 // Import the major distinguishing characteristics of a category.
4026 DeclContext *DC, *LexicalDC;
4027 DeclarationName Name;
4028 SourceLocation Loc;
4029 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004030 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4031 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004032 if (ToD)
4033 return ToD;
4034
Balazs Keri3b30d652018-10-19 13:32:20 +00004035 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
4036 TypeSourceInfo *ToTypeSourceInfo;
4037 if (auto Imp = importSeq(
4038 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
4039 D->getTypeSourceInfo()))
4040 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
4041 else
4042 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004043
Gabor Marton26f72a92018-07-12 09:42:05 +00004044 ObjCTypeParamDecl *Result;
4045 if (GetImportedOrCreateDecl(
4046 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004047 ToVarianceLoc, D->getIndex(),
4048 ToLocation, Name.getAsIdentifierInfo(),
4049 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00004050 return Result;
4051
Douglas Gregor85f3f952015-07-07 03:57:15 +00004052 Result->setLexicalDeclContext(LexicalDC);
4053 return Result;
4054}
4055
Balazs Keri3b30d652018-10-19 13:32:20 +00004056ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00004057 // Import the major distinguishing characteristics of a category.
4058 DeclContext *DC, *LexicalDC;
4059 DeclarationName Name;
4060 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004061 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004062 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4063 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004064 if (ToD)
4065 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004066
Balazs Keri3b30d652018-10-19 13:32:20 +00004067 ObjCInterfaceDecl *ToInterface;
4068 if (Error Err = importInto(ToInterface, D->getClassInterface()))
4069 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004070
Douglas Gregor84c51c32010-02-18 01:47:50 +00004071 // Determine if we've already encountered this category.
4072 ObjCCategoryDecl *MergeWithCategory
4073 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
4074 ObjCCategoryDecl *ToCategory = MergeWithCategory;
4075 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004076 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
4077 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4078 if (auto Imp = importSeq(
4079 D->getAtStartLoc(), D->getCategoryNameLoc(),
4080 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4081 std::tie(
4082 ToAtStartLoc, ToCategoryNameLoc,
4083 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4084 else
4085 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004086
4087 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004088 ToAtStartLoc, Loc,
4089 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004090 Name.getAsIdentifierInfo(), ToInterface,
4091 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004092 ToIvarLBraceLoc,
4093 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004094 return ToCategory;
4095
Douglas Gregor84c51c32010-02-18 01:47:50 +00004096 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004097 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004098 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004099 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004100 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4101 ToCategory->setTypeParamList(*PListOrErr);
4102 else
4103 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004104
Douglas Gregor84c51c32010-02-18 01:47:50 +00004105 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004106 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4107 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004108 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4109 = D->protocol_loc_begin();
4110 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4111 FromProtoEnd = D->protocol_end();
4112 FromProto != FromProtoEnd;
4113 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004114 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4115 Protocols.push_back(*ToProtoOrErr);
4116 else
4117 return ToProtoOrErr.takeError();
4118
4119 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4120 ProtocolLocs.push_back(*ToProtoLocOrErr);
4121 else
4122 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004123 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004124
Douglas Gregor84c51c32010-02-18 01:47:50 +00004125 // FIXME: If we're merging, make sure that the protocol list is the same.
4126 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4127 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004128
Douglas Gregor84c51c32010-02-18 01:47:50 +00004129 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004130 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004131 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004132
Douglas Gregor84c51c32010-02-18 01:47:50 +00004133 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004134 if (Error Err = ImportDeclContext(D))
4135 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004136
Douglas Gregor84c51c32010-02-18 01:47:50 +00004137 // If we have an implementation, import it as well.
4138 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004139 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4140 import(D->getImplementation()))
4141 ToCategory->setImplementation(*ToImplOrErr);
4142 else
4143 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004144 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004145
Douglas Gregor84c51c32010-02-18 01:47:50 +00004146 return ToCategory;
4147}
4148
Balazs Keri3b30d652018-10-19 13:32:20 +00004149Error ASTNodeImporter::ImportDefinition(
4150 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004151 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004152 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004153 if (Error Err = ImportDeclContext(From))
4154 return Err;
4155 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004156 }
4157
4158 // Start the protocol definition
4159 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004160
Douglas Gregor2aa53772012-01-24 17:42:07 +00004161 // Import protocols
4162 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4163 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004164 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4165 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004166 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4167 FromProtoEnd = From->protocol_end();
4168 FromProto != FromProtoEnd;
4169 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004170 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4171 Protocols.push_back(*ToProtoOrErr);
4172 else
4173 return ToProtoOrErr.takeError();
4174
4175 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4176 ProtocolLocs.push_back(*ToProtoLocOrErr);
4177 else
4178 return ToProtoLocOrErr.takeError();
4179
Douglas Gregor2aa53772012-01-24 17:42:07 +00004180 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004181
Douglas Gregor2aa53772012-01-24 17:42:07 +00004182 // FIXME: If we're merging, make sure that the protocol list is the same.
4183 To->setProtocolList(Protocols.data(), Protocols.size(),
4184 ProtocolLocs.data(), Importer.getToContext());
4185
Douglas Gregor2e15c842012-02-01 21:00:38 +00004186 if (shouldForceImportDeclContext(Kind)) {
4187 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004188 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4189 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004190 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004191 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004192}
4193
Balazs Keri3b30d652018-10-19 13:32:20 +00004194ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004195 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004196 // from, but this particular declaration is not that definition, import the
4197 // definition and map to that.
4198 ObjCProtocolDecl *Definition = D->getDefinition();
4199 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004200 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4201 return Importer.MapImported(D, *ImportedDefOrErr);
4202 else
4203 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004204 }
4205
Douglas Gregor84c51c32010-02-18 01:47:50 +00004206 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004207 DeclContext *DC, *LexicalDC;
4208 DeclarationName Name;
4209 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004210 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004211 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4212 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004213 if (ToD)
4214 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004215
Craig Topper36250ad2014-05-12 05:36:57 +00004216 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004217 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004218 for (auto *FoundDecl : FoundDecls) {
4219 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004220 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004221
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004222 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004223 break;
4224 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004225
Douglas Gregor98d156a2010-02-17 16:12:00 +00004226 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004227 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004228 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4229 if (!ToAtBeginLocOrErr)
4230 return ToAtBeginLocOrErr.takeError();
4231
Gabor Marton26f72a92018-07-12 09:42:05 +00004232 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4233 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004234 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004235 /*PrevDecl=*/nullptr))
4236 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004237 ToProto->setLexicalDeclContext(LexicalDC);
4238 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004239 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004240
4241 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004242
Balazs Keri3b30d652018-10-19 13:32:20 +00004243 if (D->isThisDeclarationADefinition())
4244 if (Error Err = ImportDefinition(D, ToProto))
4245 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004246
Douglas Gregor98d156a2010-02-17 16:12:00 +00004247 return ToProto;
4248}
4249
Balazs Keri3b30d652018-10-19 13:32:20 +00004250ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4251 DeclContext *DC, *LexicalDC;
4252 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4253 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004254
Balazs Keri3b30d652018-10-19 13:32:20 +00004255 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4256 if (!ExternLocOrErr)
4257 return ExternLocOrErr.takeError();
4258
4259 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4260 if (!LangLocOrErr)
4261 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004262
4263 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004264
4265 LinkageSpecDecl *ToLinkageSpec;
4266 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004267 *ExternLocOrErr, *LangLocOrErr,
4268 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004269 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004270
4271 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004272 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4273 if (!RBraceLocOrErr)
4274 return RBraceLocOrErr.takeError();
4275 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004276 }
4277
4278 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4279 LexicalDC->addDeclInternal(ToLinkageSpec);
4280
Sean Callanan0aae0412014-12-10 00:00:37 +00004281 return ToLinkageSpec;
4282}
4283
Balazs Keri3b30d652018-10-19 13:32:20 +00004284ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004285 DeclContext *DC, *LexicalDC;
4286 DeclarationName Name;
4287 SourceLocation Loc;
4288 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004289 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4290 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004291 if (ToD)
4292 return ToD;
4293
Balazs Keri3b30d652018-10-19 13:32:20 +00004294 SourceLocation ToLoc, ToUsingLoc;
4295 NestedNameSpecifierLoc ToQualifierLoc;
4296 if (auto Imp = importSeq(
4297 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4298 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4299 else
4300 return Imp.takeError();
4301
4302 DeclarationNameInfo NameInfo(Name, ToLoc);
4303 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4304 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004305
Gabor Marton26f72a92018-07-12 09:42:05 +00004306 UsingDecl *ToUsing;
4307 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004308 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004309 D->hasTypename()))
4310 return ToUsing;
4311
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004312 ToUsing->setLexicalDeclContext(LexicalDC);
4313 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004314
4315 if (NamedDecl *FromPattern =
4316 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004317 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4318 Importer.getToContext().setInstantiatedFromUsingDecl(
4319 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004320 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004321 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004322 }
4323
Balazs Keri3b30d652018-10-19 13:32:20 +00004324 for (UsingShadowDecl *FromShadow : D->shadows()) {
4325 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4326 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004327 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004328 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004329 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004330 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004331 }
4332 return ToUsing;
4333}
4334
Balazs Keri3b30d652018-10-19 13:32:20 +00004335ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004336 DeclContext *DC, *LexicalDC;
4337 DeclarationName Name;
4338 SourceLocation Loc;
4339 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004340 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4341 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004342 if (ToD)
4343 return ToD;
4344
Balazs Keri3b30d652018-10-19 13:32:20 +00004345 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4346 if (!ToUsingOrErr)
4347 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004348
Balazs Keri3b30d652018-10-19 13:32:20 +00004349 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4350 if (!ToTargetOrErr)
4351 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004352
Gabor Marton26f72a92018-07-12 09:42:05 +00004353 UsingShadowDecl *ToShadow;
4354 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004355 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004356 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004357
4358 ToShadow->setLexicalDeclContext(LexicalDC);
4359 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004360
4361 if (UsingShadowDecl *FromPattern =
4362 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004363 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4364 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4365 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004366 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004367 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004368 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004369 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004370 }
4371
4372 LexicalDC->addDeclInternal(ToShadow);
4373
4374 return ToShadow;
4375}
4376
Balazs Keri3b30d652018-10-19 13:32:20 +00004377ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004378 DeclContext *DC, *LexicalDC;
4379 DeclarationName Name;
4380 SourceLocation Loc;
4381 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004382 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4383 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004384 if (ToD)
4385 return ToD;
4386
Balazs Keri3b30d652018-10-19 13:32:20 +00004387 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4388 if (!ToComAncestorOrErr)
4389 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004390
Balazs Keri3b30d652018-10-19 13:32:20 +00004391 NamespaceDecl *ToNominatedNamespace;
4392 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4393 NestedNameSpecifierLoc ToQualifierLoc;
4394 if (auto Imp = importSeq(
4395 D->getNominatedNamespace(), D->getUsingLoc(),
4396 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4397 D->getIdentLocation()))
4398 std::tie(
4399 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4400 ToQualifierLoc, ToIdentLocation) = *Imp;
4401 else
4402 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004403
Gabor Marton26f72a92018-07-12 09:42:05 +00004404 UsingDirectiveDecl *ToUsingDir;
4405 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004406 ToUsingLoc,
4407 ToNamespaceKeyLocation,
4408 ToQualifierLoc,
4409 ToIdentLocation,
4410 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004411 return ToUsingDir;
4412
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004413 ToUsingDir->setLexicalDeclContext(LexicalDC);
4414 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004415
4416 return ToUsingDir;
4417}
4418
Balazs Keri3b30d652018-10-19 13:32:20 +00004419ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004420 UnresolvedUsingValueDecl *D) {
4421 DeclContext *DC, *LexicalDC;
4422 DeclarationName Name;
4423 SourceLocation Loc;
4424 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004425 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4426 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004427 if (ToD)
4428 return ToD;
4429
Balazs Keri3b30d652018-10-19 13:32:20 +00004430 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4431 NestedNameSpecifierLoc ToQualifierLoc;
4432 if (auto Imp = importSeq(
4433 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4434 D->getEllipsisLoc()))
4435 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4436 else
4437 return Imp.takeError();
4438
4439 DeclarationNameInfo NameInfo(Name, ToLoc);
4440 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4441 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004442
Gabor Marton26f72a92018-07-12 09:42:05 +00004443 UnresolvedUsingValueDecl *ToUsingValue;
4444 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004445 ToUsingLoc, ToQualifierLoc, NameInfo,
4446 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004447 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004448
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004449 ToUsingValue->setAccess(D->getAccess());
4450 ToUsingValue->setLexicalDeclContext(LexicalDC);
4451 LexicalDC->addDeclInternal(ToUsingValue);
4452
4453 return ToUsingValue;
4454}
4455
Balazs Keri3b30d652018-10-19 13:32:20 +00004456ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004457 UnresolvedUsingTypenameDecl *D) {
4458 DeclContext *DC, *LexicalDC;
4459 DeclarationName Name;
4460 SourceLocation Loc;
4461 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004462 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4463 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004464 if (ToD)
4465 return ToD;
4466
Balazs Keri3b30d652018-10-19 13:32:20 +00004467 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4468 NestedNameSpecifierLoc ToQualifierLoc;
4469 if (auto Imp = importSeq(
4470 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4471 D->getEllipsisLoc()))
4472 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4473 else
4474 return Imp.takeError();
4475
Gabor Marton26f72a92018-07-12 09:42:05 +00004476 UnresolvedUsingTypenameDecl *ToUsing;
4477 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004478 ToUsingLoc, ToTypenameLoc,
4479 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004480 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004481
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004482 ToUsing->setAccess(D->getAccess());
4483 ToUsing->setLexicalDeclContext(LexicalDC);
4484 LexicalDC->addDeclInternal(ToUsing);
4485
4486 return ToUsing;
4487}
4488
Balazs Keri3b30d652018-10-19 13:32:20 +00004489
4490Error ASTNodeImporter::ImportDefinition(
4491 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004492 if (To->getDefinition()) {
4493 // Check consistency of superclass.
4494 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4495 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004496 if (auto FromSuperOrErr = import(FromSuper))
4497 FromSuper = *FromSuperOrErr;
4498 else
4499 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004500 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004501
4502 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004503 if ((bool)FromSuper != (bool)ToSuper ||
4504 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004505 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004506 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004507 << To->getDeclName();
4508 if (ToSuper)
4509 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4510 << To->getSuperClass()->getDeclName();
4511 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004512 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004513 diag::note_odr_objc_missing_superclass);
4514 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004515 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004516 diag::note_odr_objc_superclass)
4517 << From->getSuperClass()->getDeclName();
4518 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004519 Importer.FromDiag(From->getLocation(),
4520 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004521 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004522
Douglas Gregor2e15c842012-02-01 21:00:38 +00004523 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004524 if (Error Err = ImportDeclContext(From))
4525 return Err;
4526 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004527 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004528
Douglas Gregor2aa53772012-01-24 17:42:07 +00004529 // Start the definition.
4530 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004531
Douglas Gregor2aa53772012-01-24 17:42:07 +00004532 // If this class has a superclass, import it.
4533 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004534 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4535 To->setSuperClass(*SuperTInfoOrErr);
4536 else
4537 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004538 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004539
Douglas Gregor2aa53772012-01-24 17:42:07 +00004540 // Import protocols
4541 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4542 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004543 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4544 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004545
Douglas Gregor2aa53772012-01-24 17:42:07 +00004546 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4547 FromProtoEnd = From->protocol_end();
4548 FromProto != FromProtoEnd;
4549 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004550 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4551 Protocols.push_back(*ToProtoOrErr);
4552 else
4553 return ToProtoOrErr.takeError();
4554
4555 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4556 ProtocolLocs.push_back(*ToProtoLocOrErr);
4557 else
4558 return ToProtoLocOrErr.takeError();
4559
Douglas Gregor2aa53772012-01-24 17:42:07 +00004560 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004561
Douglas Gregor2aa53772012-01-24 17:42:07 +00004562 // FIXME: If we're merging, make sure that the protocol list is the same.
4563 To->setProtocolList(Protocols.data(), Protocols.size(),
4564 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004565
Douglas Gregor2aa53772012-01-24 17:42:07 +00004566 // Import categories. When the categories themselves are imported, they'll
4567 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004568 for (auto *Cat : From->known_categories()) {
4569 auto ToCatOrErr = import(Cat);
4570 if (!ToCatOrErr)
4571 return ToCatOrErr.takeError();
4572 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004573
Douglas Gregor2aa53772012-01-24 17:42:07 +00004574 // If we have an @implementation, import it as well.
4575 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004576 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4577 import(From->getImplementation()))
4578 To->setImplementation(*ToImplOrErr);
4579 else
4580 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004581 }
4582
Douglas Gregor2e15c842012-02-01 21:00:38 +00004583 if (shouldForceImportDeclContext(Kind)) {
4584 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004585 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4586 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004587 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004588 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004589}
4590
Balazs Keri3b30d652018-10-19 13:32:20 +00004591Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004592ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4593 if (!list)
4594 return nullptr;
4595
4596 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004597 for (auto *fromTypeParam : *list) {
4598 if (auto toTypeParamOrErr = import(fromTypeParam))
4599 toTypeParams.push_back(*toTypeParamOrErr);
4600 else
4601 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004602 }
4603
Balazs Keri3b30d652018-10-19 13:32:20 +00004604 auto LAngleLocOrErr = import(list->getLAngleLoc());
4605 if (!LAngleLocOrErr)
4606 return LAngleLocOrErr.takeError();
4607
4608 auto RAngleLocOrErr = import(list->getRAngleLoc());
4609 if (!RAngleLocOrErr)
4610 return RAngleLocOrErr.takeError();
4611
Douglas Gregor85f3f952015-07-07 03:57:15 +00004612 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004613 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004614 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004615 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004616}
4617
Balazs Keri3b30d652018-10-19 13:32:20 +00004618ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004619 // If this class has a definition in the translation unit we're coming from,
4620 // but this particular declaration is not that definition, import the
4621 // definition and map to that.
4622 ObjCInterfaceDecl *Definition = D->getDefinition();
4623 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004624 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4625 return Importer.MapImported(D, *ImportedDefOrErr);
4626 else
4627 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004628 }
4629
Douglas Gregor45635322010-02-16 01:20:57 +00004630 // Import the major distinguishing characteristics of an @interface.
4631 DeclContext *DC, *LexicalDC;
4632 DeclarationName Name;
4633 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004634 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004635 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4636 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004637 if (ToD)
4638 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004639
Douglas Gregor2aa53772012-01-24 17:42:07 +00004640 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004641 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004642 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004643 for (auto *FoundDecl : FoundDecls) {
4644 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004645 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004646
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004647 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004648 break;
4649 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004650
Douglas Gregor2aa53772012-01-24 17:42:07 +00004651 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004652 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004653 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004654 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4655 if (!AtBeginLocOrErr)
4656 return AtBeginLocOrErr.takeError();
4657
Gabor Marton26f72a92018-07-12 09:42:05 +00004658 if (GetImportedOrCreateDecl(
4659 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004660 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004661 /*TypeParamList=*/nullptr,
4662 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4663 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004664 ToIface->setLexicalDeclContext(LexicalDC);
4665 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004666 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004667 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004668 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004669 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004670 if (auto ToPListOrErr =
4671 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4672 ToIface->setTypeParamList(*ToPListOrErr);
4673 else
4674 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004675
Balazs Keri3b30d652018-10-19 13:32:20 +00004676 if (D->isThisDeclarationADefinition())
4677 if (Error Err = ImportDefinition(D, ToIface))
4678 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004679
Douglas Gregor98d156a2010-02-17 16:12:00 +00004680 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004681}
4682
Balazs Keri3b30d652018-10-19 13:32:20 +00004683ExpectedDecl
4684ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4685 ObjCCategoryDecl *Category;
4686 if (Error Err = importInto(Category, D->getCategoryDecl()))
4687 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004688
Douglas Gregor4da9d682010-12-07 15:32:12 +00004689 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4690 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004691 DeclContext *DC, *LexicalDC;
4692 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4693 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004694
Balazs Keri3b30d652018-10-19 13:32:20 +00004695 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4696 if (auto Imp = importSeq(
4697 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4698 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4699 else
4700 return Imp.takeError();
4701
Gabor Marton26f72a92018-07-12 09:42:05 +00004702 if (GetImportedOrCreateDecl(
4703 ToImpl, D, Importer.getToContext(), DC,
4704 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004705 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004706 return ToImpl;
4707
Balazs Keri3b30d652018-10-19 13:32:20 +00004708 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004709 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004710 Category->setImplementation(ToImpl);
4711 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004712
Gabor Marton26f72a92018-07-12 09:42:05 +00004713 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004714 if (Error Err = ImportDeclContext(D))
4715 return std::move(Err);
4716
Douglas Gregor4da9d682010-12-07 15:32:12 +00004717 return ToImpl;
4718}
4719
Balazs Keri3b30d652018-10-19 13:32:20 +00004720ExpectedDecl
4721ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004722 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004723 ObjCInterfaceDecl *Iface;
4724 if (Error Err = importInto(Iface, D->getClassInterface()))
4725 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004726
4727 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004728 ObjCInterfaceDecl *Super;
4729 if (Error Err = importInto(Super, D->getSuperClass()))
4730 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004731
4732 ObjCImplementationDecl *Impl = Iface->getImplementation();
4733 if (!Impl) {
4734 // We haven't imported an implementation yet. Create a new @implementation
4735 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004736 DeclContext *DC, *LexicalDC;
4737 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4738 return std::move(Err);
4739
4740 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4741 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4742 if (auto Imp = importSeq(
4743 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4744 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4745 std::tie(
4746 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4747 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4748 else
4749 return Imp.takeError();
4750
Gabor Marton26f72a92018-07-12 09:42:05 +00004751 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004752 DC, Iface, Super,
4753 ToLocation,
4754 ToAtStartLoc,
4755 ToSuperClassLoc,
4756 ToIvarLBraceLoc,
4757 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004758 return Impl;
4759
Balazs Keri3b30d652018-10-19 13:32:20 +00004760 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004761
Douglas Gregorda8025c2010-12-07 01:26:03 +00004762 // Associate the implementation with the class it implements.
4763 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004764 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004765 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004766 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004767
4768 // Verify that the existing @implementation has the same superclass.
4769 if ((Super && !Impl->getSuperClass()) ||
4770 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004771 (Super && Impl->getSuperClass() &&
4772 !declaresSameEntity(Super->getCanonicalDecl(),
4773 Impl->getSuperClass()))) {
4774 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004775 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004776 << Iface->getDeclName();
4777 // FIXME: It would be nice to have the location of the superclass
4778 // below.
4779 if (Impl->getSuperClass())
4780 Importer.ToDiag(Impl->getLocation(),
4781 diag::note_odr_objc_superclass)
4782 << Impl->getSuperClass()->getDeclName();
4783 else
4784 Importer.ToDiag(Impl->getLocation(),
4785 diag::note_odr_objc_missing_superclass);
4786 if (D->getSuperClass())
4787 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004788 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004789 << D->getSuperClass()->getDeclName();
4790 else
4791 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004792 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004793
4794 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004795 }
4796 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004797
Douglas Gregorda8025c2010-12-07 01:26:03 +00004798 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004799 if (Error Err = ImportDeclContext(D))
4800 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004801
4802 return Impl;
4803}
4804
Balazs Keri3b30d652018-10-19 13:32:20 +00004805ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004806 // Import the major distinguishing characteristics of an @property.
4807 DeclContext *DC, *LexicalDC;
4808 DeclarationName Name;
4809 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004810 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004811 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4812 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004813 if (ToD)
4814 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004815
4816 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004817 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004818 for (auto *FoundDecl : FoundDecls) {
4819 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004820 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004821 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004822 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004823 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004824 << Name << D->getType() << FoundProp->getType();
4825 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4826 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004827
4828 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004829 }
4830
4831 // FIXME: Check property attributes, getters, setters, etc.?
4832
4833 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004834 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004835 return FoundProp;
4836 }
4837 }
4838
Balazs Keri3b30d652018-10-19 13:32:20 +00004839 QualType ToType;
4840 TypeSourceInfo *ToTypeSourceInfo;
4841 SourceLocation ToAtLoc, ToLParenLoc;
4842 if (auto Imp = importSeq(
4843 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4844 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4845 else
4846 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004847
4848 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004849 ObjCPropertyDecl *ToProperty;
4850 if (GetImportedOrCreateDecl(
4851 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004852 Name.getAsIdentifierInfo(), ToAtLoc,
4853 ToLParenLoc, ToType,
4854 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004855 return ToProperty;
4856
Balazs Keri3b30d652018-10-19 13:32:20 +00004857 Selector ToGetterName, ToSetterName;
4858 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4859 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4860 ObjCIvarDecl *ToPropertyIvarDecl;
4861 if (auto Imp = importSeq(
4862 D->getGetterName(), D->getSetterName(),
4863 D->getGetterNameLoc(), D->getSetterNameLoc(),
4864 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4865 D->getPropertyIvarDecl()))
4866 std::tie(
4867 ToGetterName, ToSetterName,
4868 ToGetterNameLoc, ToSetterNameLoc,
4869 ToGetterMethodDecl, ToSetterMethodDecl,
4870 ToPropertyIvarDecl) = *Imp;
4871 else
4872 return Imp.takeError();
4873
Douglas Gregora11c4582010-02-17 18:02:10 +00004874 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004875 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004876
4877 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004878 ToProperty->setPropertyAttributesAsWritten(
4879 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004880 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4881 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4882 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4883 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4884 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004885 return ToProperty;
4886}
4887
Balazs Keri3b30d652018-10-19 13:32:20 +00004888ExpectedDecl
4889ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4890 ObjCPropertyDecl *Property;
4891 if (Error Err = importInto(Property, D->getPropertyDecl()))
4892 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004893
Balazs Keri3b30d652018-10-19 13:32:20 +00004894 DeclContext *DC, *LexicalDC;
4895 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4896 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004897
Balazs Keri3b30d652018-10-19 13:32:20 +00004898 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004899
4900 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004901 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004902 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4903 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004904
4905 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004906 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4907 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004908 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004909 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4910 if (auto Imp = importSeq(
4911 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4912 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4913 else
4914 return Imp.takeError();
4915
Gabor Marton26f72a92018-07-12 09:42:05 +00004916 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004917 ToBeginLoc,
4918 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004919 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004920 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004921 return ToImpl;
4922
Douglas Gregor14a49e22010-12-07 18:32:03 +00004923 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004924 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004925 } else {
4926 // Check that we have the same kind of property implementation (@synthesize
4927 // vs. @dynamic).
4928 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004929 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004930 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004931 << Property->getDeclName()
4932 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004933 == ObjCPropertyImplDecl::Dynamic);
4934 Importer.FromDiag(D->getLocation(),
4935 diag::note_odr_objc_property_impl_kind)
4936 << D->getPropertyDecl()->getDeclName()
4937 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004938
4939 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004940 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004941
4942 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004943 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4944 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004945 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004946 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004947 << Property->getDeclName()
4948 << ToImpl->getPropertyIvarDecl()->getDeclName()
4949 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004950 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004951 diag::note_odr_objc_synthesize_ivar_here)
4952 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004953
4954 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004955 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004956
Douglas Gregor14a49e22010-12-07 18:32:03 +00004957 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004958 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004959 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004960
Douglas Gregor14a49e22010-12-07 18:32:03 +00004961 return ToImpl;
4962}
4963
Balazs Keri3b30d652018-10-19 13:32:20 +00004964ExpectedDecl
4965ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004966 // For template arguments, we adopt the translation unit as our declaration
4967 // context. This context will be fixed when the actual template declaration
4968 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004969
Douglas Gregora082a492010-11-30 19:14:50 +00004970 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004971
4972 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4973 if (!BeginLocOrErr)
4974 return BeginLocOrErr.takeError();
4975
4976 ExpectedSLoc LocationOrErr = import(D->getLocation());
4977 if (!LocationOrErr)
4978 return LocationOrErr.takeError();
4979
Gabor Marton26f72a92018-07-12 09:42:05 +00004980 TemplateTypeParmDecl *ToD = nullptr;
4981 (void)GetImportedOrCreateDecl(
4982 ToD, D, Importer.getToContext(),
4983 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004984 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004985 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4986 D->wasDeclaredWithTypename(), D->isParameterPack());
4987 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004988}
4989
Balazs Keri3b30d652018-10-19 13:32:20 +00004990ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004991ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004992 DeclarationName ToDeclName;
4993 SourceLocation ToLocation, ToInnerLocStart;
4994 QualType ToType;
4995 TypeSourceInfo *ToTypeSourceInfo;
4996 if (auto Imp = importSeq(
4997 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4998 D->getInnerLocStart()))
4999 std::tie(
5000 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
5001 ToInnerLocStart) = *Imp;
5002 else
5003 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005004
Douglas Gregora082a492010-11-30 19:14:50 +00005005 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00005006
5007 NonTypeTemplateParmDecl *ToD = nullptr;
5008 (void)GetImportedOrCreateDecl(
5009 ToD, D, Importer.getToContext(),
5010 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005011 ToInnerLocStart, ToLocation, D->getDepth(),
5012 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
5013 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00005014 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005015}
5016
Balazs Keri3b30d652018-10-19 13:32:20 +00005017ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00005018ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
5019 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005020 auto NameOrErr = import(D->getDeclName());
5021 if (!NameOrErr)
5022 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005023
Douglas Gregora082a492010-11-30 19:14:50 +00005024 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005025 ExpectedSLoc LocationOrErr = import(D->getLocation());
5026 if (!LocationOrErr)
5027 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00005028
Douglas Gregora082a492010-11-30 19:14:50 +00005029 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00005030 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005031 if (!TemplateParamsOrErr)
5032 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005033
Douglas Gregora082a492010-11-30 19:14:50 +00005034 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00005035
5036 TemplateTemplateParmDecl *ToD = nullptr;
5037 (void)GetImportedOrCreateDecl(
5038 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005039 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
5040 D->getDepth(), D->getPosition(), D->isParameterPack(),
5041 (*NameOrErr).getAsIdentifierInfo(),
5042 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00005043 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005044}
5045
Gabor Marton16d98c22019-03-07 13:01:51 +00005046// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00005047// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00005048template <typename T> static auto getTemplateDefinition(T *D) -> T * {
5049 assert(D->getTemplatedDecl() && "Should be called on templates only");
5050 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00005051 if (!ToTemplatedDef)
5052 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00005053 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
5054 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00005055}
5056
Balazs Keri3b30d652018-10-19 13:32:20 +00005057ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005058 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
5059
Douglas Gregora082a492010-11-30 19:14:50 +00005060 // Import the major distinguishing characteristics of this class template.
5061 DeclContext *DC, *LexicalDC;
5062 DeclarationName Name;
5063 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005064 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005065 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5066 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005067 if (ToD)
5068 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00005069
Gabor Marton7df342a2018-12-17 12:42:12 +00005070 ClassTemplateDecl *FoundByLookup = nullptr;
5071
Douglas Gregora082a492010-11-30 19:14:50 +00005072 // We may already have a template of the same name; try to find and match it.
5073 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005074 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005075 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005076 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00005077 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
5078 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00005079 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005080
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005081 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00005082 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
5083 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00005084
Douglas Gregora082a492010-11-30 19:14:50 +00005085 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005086 ClassTemplateDecl *TemplateWithDef =
5087 getTemplateDefinition(FoundTemplate);
Balazs Keri2e160602019-08-12 10:07:38 +00005088 if (D->isThisDeclarationADefinition() && TemplateWithDef)
Gabor Marton7df342a2018-12-17 12:42:12 +00005089 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri2e160602019-08-12 10:07:38 +00005090 if (!FoundByLookup)
5091 FoundByLookup = FoundTemplate;
5092 // Search in all matches because there may be multiple decl chains,
5093 // see ASTTests test ImportExistingFriendClassTemplateDef.
5094 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005095 }
Douglas Gregora082a492010-11-30 19:14:50 +00005096 }
Gabor Marton9581c332018-05-23 13:53:36 +00005097
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005098 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005099 }
Gabor Marton9581c332018-05-23 13:53:36 +00005100
Douglas Gregora082a492010-11-30 19:14:50 +00005101 if (!ConflictingDecls.empty()) {
5102 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005103 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005104 ConflictingDecls.size());
5105 }
Gabor Marton9581c332018-05-23 13:53:36 +00005106
Douglas Gregora082a492010-11-30 19:14:50 +00005107 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005108 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005109 }
5110
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005111 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5112
Douglas Gregora082a492010-11-30 19:14:50 +00005113 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005114 CXXRecordDecl *ToTemplated;
5115 if (Error Err = importInto(ToTemplated, FromTemplated))
5116 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005117
Douglas Gregora082a492010-11-30 19:14:50 +00005118 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005119 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005120 if (!TemplateParamsOrErr)
5121 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005122
Gabor Marton26f72a92018-07-12 09:42:05 +00005123 ClassTemplateDecl *D2;
5124 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005125 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005126 return D2;
5127
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005128 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005129
Douglas Gregora082a492010-11-30 19:14:50 +00005130 D2->setAccess(D->getAccess());
5131 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00005132
5133 if (D->getDeclContext()->containsDeclAndLoad(D))
5134 DC->addDeclInternal(D2);
5135 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00005136 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005137
Gabor Marton7df342a2018-12-17 12:42:12 +00005138 if (FoundByLookup) {
5139 auto *Recent =
5140 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5141
5142 // It is possible that during the import of the class template definition
5143 // we start the import of a fwd friend decl of the very same class template
5144 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5145 // had been created earlier and by that time the lookup could not find
5146 // anything existing, so it has no previous decl. Later, (still during the
5147 // import of the fwd friend decl) we start to import the definition again
5148 // and this time the lookup finds the previous fwd friend class template.
5149 // In this case we must set up the previous decl for the templated decl.
5150 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005151 assert(FoundByLookup->getTemplatedDecl() &&
5152 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005153 CXXRecordDecl *PrevTemplated =
5154 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5155 if (ToTemplated != PrevTemplated)
5156 ToTemplated->setPreviousDecl(PrevTemplated);
5157 }
5158
5159 D2->setPreviousDecl(Recent);
5160 }
5161
5162 if (LexicalDC != DC && IsFriend)
5163 DC->makeDeclVisibleInContext(D2);
5164
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005165 if (FromTemplated->isCompleteDefinition() &&
5166 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005167 // FIXME: Import definition!
5168 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005169
Douglas Gregora082a492010-11-30 19:14:50 +00005170 return D2;
5171}
5172
Balazs Keri3b30d652018-10-19 13:32:20 +00005173ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005174 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005175 ClassTemplateDecl *ClassTemplate;
5176 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5177 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005178
Douglas Gregore2e50d332010-12-01 01:36:18 +00005179 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005180 DeclContext *DC, *LexicalDC;
5181 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5182 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005183
5184 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005185 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005186 if (Error Err = ImportTemplateArguments(
5187 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5188 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005189
Douglas Gregore2e50d332010-12-01 01:36:18 +00005190 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005191 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005192 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005193 ClassTemplatePartialSpecializationDecl *PartialSpec =
5194 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5195 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005196 PrevDecl =
5197 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005198 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005199 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005200
Gabor Marton7f8c4002019-03-19 13:34:10 +00005201 if (PrevDecl) {
5202 if (IsStructuralMatch(D, PrevDecl)) {
5203 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5204 Importer.MapImported(D, PrevDecl->getDefinition());
5205 // Import those default field initializers which have been
5206 // instantiated in the "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005207 for (auto *FromField : D->fields()) {
5208 auto ToOrErr = import(FromField);
5209 if (!ToOrErr)
5210 return ToOrErr.takeError();
5211 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005212
Gabor Marton7f8c4002019-03-19 13:34:10 +00005213 // Import those methods which have been instantiated in the
5214 // "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005215 for (CXXMethodDecl *FromM : D->methods()) {
5216 auto ToOrErr = import(FromM);
5217 if (!ToOrErr)
5218 return ToOrErr.takeError();
5219 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005220
Gabor Marton7f8c4002019-03-19 13:34:10 +00005221 // TODO Import instantiated default arguments.
5222 // TODO Import instantiated exception specifications.
5223 //
5224 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5225 // what else could be fused during an AST merge.
5226 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005227 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005228 } else { // ODR violation.
5229 // FIXME HandleNameConflict
Gabor Marton303c98612019-06-25 08:00:51 +00005230 return make_error<ImportError>(ImportError::NameConflict);
Gabor Marton42e15de2018-08-22 11:52:14 +00005231 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005232 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005233
Gabor Marton7f8c4002019-03-19 13:34:10 +00005234 // Import the location of this declaration.
5235 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5236 if (!BeginLocOrErr)
5237 return BeginLocOrErr.takeError();
5238 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5239 if (!IdLocOrErr)
5240 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005241
Gabor Marton7f8c4002019-03-19 13:34:10 +00005242 // Create the specialization.
5243 ClassTemplateSpecializationDecl *D2 = nullptr;
5244 if (PartialSpec) {
5245 // Import TemplateArgumentListInfo.
5246 TemplateArgumentListInfo ToTAInfo;
5247 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5248 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5249 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005250
Gabor Marton7f8c4002019-03-19 13:34:10 +00005251 QualType CanonInjType;
5252 if (Error Err = importInto(
5253 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5254 return std::move(Err);
5255 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005256
Balazs Keridec09162019-03-20 15:42:42 +00005257 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005258 if (!ToTPListOrErr)
5259 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005260
Gabor Marton7f8c4002019-03-19 13:34:10 +00005261 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5262 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5263 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5264 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5265 ToTAInfo, CanonInjType,
5266 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5267 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005268
Gabor Marton7f8c4002019-03-19 13:34:10 +00005269 // Update InsertPos, because preceding import calls may have invalidated
5270 // it by adding new specializations.
5271 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5272 // Add this partial specialization to the class template.
5273 ClassTemplate->AddPartialSpecialization(
5274 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005275
Gabor Marton7f8c4002019-03-19 13:34:10 +00005276 } else { // Not a partial specialization.
5277 if (GetImportedOrCreateDecl(
5278 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5279 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5280 PrevDecl))
5281 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005282
Gabor Marton7f8c4002019-03-19 13:34:10 +00005283 // Update InsertPos, because preceding import calls may have invalidated
5284 // it by adding new specializations.
5285 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5286 // Add this specialization to the class template.
5287 ClassTemplate->AddSpecialization(D2, InsertPos);
5288 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005289
Gabor Marton7f8c4002019-03-19 13:34:10 +00005290 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005291
Gabor Marton7f8c4002019-03-19 13:34:10 +00005292 // Set the context of this specialization/instantiation.
5293 D2->setLexicalDeclContext(LexicalDC);
5294
5295 // Add to the DC only if it was an explicit specialization/instantiation.
5296 if (D2->isExplicitInstantiationOrSpecialization()) {
5297 LexicalDC->addDeclInternal(D2);
5298 }
5299
5300 // Import the qualifier, if any.
5301 if (auto LocOrErr = import(D->getQualifierLoc()))
5302 D2->setQualifierInfo(*LocOrErr);
5303 else
5304 return LocOrErr.takeError();
5305
5306 if (auto *TSI = D->getTypeAsWritten()) {
5307 if (auto TInfoOrErr = import(TSI))
5308 D2->setTypeAsWritten(*TInfoOrErr);
5309 else
5310 return TInfoOrErr.takeError();
5311
5312 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5313 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005314 else
5315 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005316
Gabor Marton7f8c4002019-03-19 13:34:10 +00005317 if (auto LocOrErr = import(D->getExternLoc()))
5318 D2->setExternLoc(*LocOrErr);
5319 else
5320 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005321 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005322
5323 if (D->getPointOfInstantiation().isValid()) {
5324 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5325 D2->setPointOfInstantiation(*POIOrErr);
5326 else
5327 return POIOrErr.takeError();
5328 }
5329
5330 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5331
Balazs Keri3b30d652018-10-19 13:32:20 +00005332 if (D->isCompleteDefinition())
5333 if (Error Err = ImportDefinition(D, D2))
5334 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005335
Douglas Gregore2e50d332010-12-01 01:36:18 +00005336 return D2;
5337}
5338
Balazs Keri3b30d652018-10-19 13:32:20 +00005339ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005340 // If this variable has a definition in the translation unit we're coming
5341 // from,
5342 // but this particular declaration is not that definition, import the
5343 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005344 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005345 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5346 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005347 if (ExpectedDecl ImportedDefOrErr = import(
5348 Definition->getDescribedVarTemplate()))
5349 return Importer.MapImported(D, *ImportedDefOrErr);
5350 else
5351 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005352 }
5353
5354 // Import the major distinguishing characteristics of this variable template.
5355 DeclContext *DC, *LexicalDC;
5356 DeclarationName Name;
5357 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005358 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005359 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5360 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005361 if (ToD)
5362 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005363
5364 // We may already have a template of the same name; try to find and match it.
5365 assert(!DC->isFunctionOrMethod() &&
5366 "Variable templates cannot be declared at function scope");
5367 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005368 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005369 for (auto *FoundDecl : FoundDecls) {
5370 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005371 continue;
5372
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005373 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005374 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005375 if (IsStructuralMatch(D, FoundTemplate)) {
5376 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005377 Importer.MapImported(D->getTemplatedDecl(),
5378 FoundTemplate->getTemplatedDecl());
5379 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005380 }
5381 }
5382
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005383 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005384 }
5385
5386 if (!ConflictingDecls.empty()) {
5387 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5388 ConflictingDecls.data(),
5389 ConflictingDecls.size());
5390 }
5391
5392 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005393 // FIXME: Is it possible to get other error than name conflict?
5394 // (Put this `if` into the previous `if`?)
5395 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005396
5397 VarDecl *DTemplated = D->getTemplatedDecl();
5398
5399 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005400 // FIXME: Value not used?
5401 ExpectedType TypeOrErr = import(DTemplated->getType());
5402 if (!TypeOrErr)
5403 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005404
5405 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005406 VarDecl *ToTemplated;
5407 if (Error Err = importInto(ToTemplated, DTemplated))
5408 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005409
5410 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005411 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005412 if (!TemplateParamsOrErr)
5413 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005414
Gabor Marton26f72a92018-07-12 09:42:05 +00005415 VarTemplateDecl *ToVarTD;
5416 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005417 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005418 return ToVarTD;
5419
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005420 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005421
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005422 ToVarTD->setAccess(D->getAccess());
5423 ToVarTD->setLexicalDeclContext(LexicalDC);
5424 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005425
Larisse Voufo39a1e502013-08-06 01:03:05 +00005426 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005427 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005428 // FIXME: Import definition!
5429 }
5430
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005431 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005432}
5433
Balazs Keri3b30d652018-10-19 13:32:20 +00005434ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005435 VarTemplateSpecializationDecl *D) {
5436 // If this record has a definition in the translation unit we're coming from,
5437 // but this particular declaration is not that definition, import the
5438 // definition and map to that.
5439 VarDecl *Definition = D->getDefinition();
5440 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005441 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5442 return Importer.MapImported(D, *ImportedDefOrErr);
5443 else
5444 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005445 }
5446
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00005447 VarTemplateDecl *VarTemplate = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005448 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5449 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005450
5451 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005452 DeclContext *DC, *LexicalDC;
5453 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5454 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005455
5456 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005457 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5458 if (!BeginLocOrErr)
5459 return BeginLocOrErr.takeError();
5460
5461 auto IdLocOrErr = import(D->getLocation());
5462 if (!IdLocOrErr)
5463 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005464
5465 // Import template arguments.
5466 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005467 if (Error Err = ImportTemplateArguments(
5468 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5469 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005470
5471 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005472 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005473 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005474 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005475 if (D2) {
5476 // We already have a variable template specialization with these template
5477 // arguments.
5478
5479 // FIXME: Check for specialization vs. instantiation errors.
5480
5481 if (VarDecl *FoundDef = D2->getDefinition()) {
5482 if (!D->isThisDeclarationADefinition() ||
5483 IsStructuralMatch(D, FoundDef)) {
5484 // The record types structurally match, or the "from" translation
5485 // unit only had a forward declaration anyway; call it the same
5486 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005487 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005488 }
5489 }
5490 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005491 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005492 QualType T;
5493 if (Error Err = importInto(T, D->getType()))
5494 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005495
Balazs Keri3b30d652018-10-19 13:32:20 +00005496 auto TInfoOrErr = import(D->getTypeSourceInfo());
5497 if (!TInfoOrErr)
5498 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005499
5500 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005501 if (Error Err = ImportTemplateArgumentListInfo(
5502 D->getTemplateArgsInfo(), ToTAInfo))
5503 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005504
5505 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005506 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005507 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5508 // Import TemplateArgumentListInfo
5509 TemplateArgumentListInfo ArgInfos;
5510 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5511 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005512 if (Error Err = ImportTemplateArgumentListInfo(
5513 *FromTAArgsAsWritten, ArgInfos))
5514 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005515
Balazs Keridec09162019-03-20 15:42:42 +00005516 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005517 if (!ToTPListOrErr)
5518 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005519
Gabor Marton26f72a92018-07-12 09:42:05 +00005520 PartVarSpecDecl *ToPartial;
5521 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005522 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5523 VarTemplate, T, *TInfoOrErr,
5524 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005525 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005526
Balazs Keri3b30d652018-10-19 13:32:20 +00005527 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5528 FromPartial->getInstantiatedFromMember()))
5529 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5530 else
5531 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005532
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005533 if (FromPartial->isMemberSpecialization())
5534 ToPartial->setMemberSpecialization();
5535
5536 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005537
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005538 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005539 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5540 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5541 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005542 D->getStorageClass(), TemplateArgs))
5543 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005544 }
5545
Balazs Keri3b30d652018-10-19 13:32:20 +00005546 if (D->getPointOfInstantiation().isValid()) {
5547 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5548 D2->setPointOfInstantiation(*POIOrErr);
5549 else
5550 return POIOrErr.takeError();
5551 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005552
Larisse Voufo39a1e502013-08-06 01:03:05 +00005553 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005554 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005555
5556 // Add this specialization to the class template.
5557 VarTemplate->AddSpecialization(D2, InsertPos);
5558
5559 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005560 if (auto LocOrErr = import(D->getQualifierLoc()))
5561 D2->setQualifierInfo(*LocOrErr);
5562 else
5563 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005564
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005565 if (D->isConstexpr())
5566 D2->setConstexpr(true);
5567
Larisse Voufo39a1e502013-08-06 01:03:05 +00005568 // Add the specialization to this context.
5569 D2->setLexicalDeclContext(LexicalDC);
5570 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005571
5572 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005573 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005574
Balazs Keri3b30d652018-10-19 13:32:20 +00005575 if (Error Err = ImportInitializer(D, D2))
5576 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005577
5578 return D2;
5579}
5580
Balazs Keri3b30d652018-10-19 13:32:20 +00005581ExpectedDecl
5582ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005583 DeclContext *DC, *LexicalDC;
5584 DeclarationName Name;
5585 SourceLocation Loc;
5586 NamedDecl *ToD;
5587
Balazs Keri3b30d652018-10-19 13:32:20 +00005588 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5589 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005590
5591 if (ToD)
5592 return ToD;
5593
Gabor Marton16d98c22019-03-07 13:01:51 +00005594 const FunctionTemplateDecl *FoundByLookup = nullptr;
5595
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005596 // Try to find a function in our own ("to") context with the same name, same
5597 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005598 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005599 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005600 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005601 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005602 for (auto *FoundDecl : FoundDecls) {
5603 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005604 continue;
5605
Gabor Marton16d98c22019-03-07 13:01:51 +00005606 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5607 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005608 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005609 if (IsStructuralMatch(D, FoundTemplate)) {
5610 FunctionTemplateDecl *TemplateWithDef =
5611 getTemplateDefinition(FoundTemplate);
5612 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5613 return Importer.MapImported(D, TemplateWithDef);
5614 }
5615 FoundByLookup = FoundTemplate;
5616 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005617 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005618 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005619 }
5620 }
5621 }
5622 }
5623
Balazs Keridec09162019-03-20 15:42:42 +00005624 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005625 if (!ParamsOrErr)
5626 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005627
Balazs Keri3b30d652018-10-19 13:32:20 +00005628 FunctionDecl *TemplatedFD;
5629 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5630 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005631
Gabor Marton26f72a92018-07-12 09:42:05 +00005632 FunctionTemplateDecl *ToFunc;
5633 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005634 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005635 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005636
5637 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005638
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005639 ToFunc->setAccess(D->getAccess());
5640 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005641 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005642
5643 if (FoundByLookup) {
5644 auto *Recent =
5645 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5646 if (!TemplatedFD->getPreviousDecl()) {
5647 assert(FoundByLookup->getTemplatedDecl() &&
5648 "Found decl must have its templated decl set");
5649 auto *PrevTemplated =
5650 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5651 if (TemplatedFD != PrevTemplated)
5652 TemplatedFD->setPreviousDecl(PrevTemplated);
5653 }
5654 ToFunc->setPreviousDecl(Recent);
5655 }
5656
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005657 return ToFunc;
5658}
5659
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005660//----------------------------------------------------------------------------
5661// Import Statements
5662//----------------------------------------------------------------------------
5663
Balazs Keri3b30d652018-10-19 13:32:20 +00005664ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005665 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5666 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005667 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005668}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005669
Balazs Keri3b30d652018-10-19 13:32:20 +00005670
5671ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Gabor Marton303c98612019-06-25 08:00:51 +00005672 if (Importer.returnWithErrorInTest())
5673 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005674 SmallVector<IdentifierInfo *, 4> Names;
5675 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5676 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005677 // ToII is nullptr when no symbolic name is given for output operand
5678 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005679 Names.push_back(ToII);
5680 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005681
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005682 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5683 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005684 // ToII is nullptr when no symbolic name is given for input operand
5685 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005686 Names.push_back(ToII);
5687 }
5688
5689 SmallVector<StringLiteral *, 4> Clobbers;
5690 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005691 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5692 Clobbers.push_back(*ClobberOrErr);
5693 else
5694 return ClobberOrErr.takeError();
5695
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005696 }
5697
5698 SmallVector<StringLiteral *, 4> Constraints;
5699 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005700 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5701 Constraints.push_back(*OutputOrErr);
5702 else
5703 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005704 }
5705
5706 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005707 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5708 Constraints.push_back(*InputOrErr);
5709 else
5710 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005711 }
5712
Jennifer Yub8fee672019-06-03 15:57:25 +00005713 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() +
5714 S->getNumLabels());
Balazs Keri3b30d652018-10-19 13:32:20 +00005715 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5716 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005717
Jennifer Yub8fee672019-06-03 15:57:25 +00005718 if (Error Err =
5719 ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
5720 return std::move(Err);
5721
Balazs Keri3b30d652018-10-19 13:32:20 +00005722 if (Error Err = ImportArrayChecked(
Jennifer Yub8fee672019-06-03 15:57:25 +00005723 S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs()))
Balazs Keri3b30d652018-10-19 13:32:20 +00005724 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005725
Balazs Keri3b30d652018-10-19 13:32:20 +00005726 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5727 if (!AsmLocOrErr)
5728 return AsmLocOrErr.takeError();
5729 auto AsmStrOrErr = import(S->getAsmString());
5730 if (!AsmStrOrErr)
5731 return AsmStrOrErr.takeError();
5732 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5733 if (!RParenLocOrErr)
5734 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005735
5736 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005737 Importer.getToContext(),
5738 *AsmLocOrErr,
5739 S->isSimple(),
5740 S->isVolatile(),
5741 S->getNumOutputs(),
5742 S->getNumInputs(),
5743 Names.data(),
5744 Constraints.data(),
5745 Exprs.data(),
5746 *AsmStrOrErr,
5747 S->getNumClobbers(),
5748 Clobbers.data(),
Jennifer Yub8fee672019-06-03 15:57:25 +00005749 S->getNumLabels(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005750 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005751}
5752
Balazs Keri3b30d652018-10-19 13:32:20 +00005753ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5754 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5755 if (!Imp)
5756 return Imp.takeError();
5757
5758 DeclGroupRef ToDG;
5759 SourceLocation ToBeginLoc, ToEndLoc;
5760 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5761
5762 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005763}
5764
Balazs Keri3b30d652018-10-19 13:32:20 +00005765ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5766 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5767 if (!ToSemiLocOrErr)
5768 return ToSemiLocOrErr.takeError();
5769 return new (Importer.getToContext()) NullStmt(
5770 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005771}
5772
Balazs Keri3b30d652018-10-19 13:32:20 +00005773ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005774 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005775
Balazs Keri3b30d652018-10-19 13:32:20 +00005776 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5777 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005778
Balazs Keri3b30d652018-10-19 13:32:20 +00005779 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5780 if (!ToLBracLocOrErr)
5781 return ToLBracLocOrErr.takeError();
5782
5783 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5784 if (!ToRBracLocOrErr)
5785 return ToRBracLocOrErr.takeError();
5786
5787 return CompoundStmt::Create(
5788 Importer.getToContext(), ToStmts,
5789 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005790}
5791
Balazs Keri3b30d652018-10-19 13:32:20 +00005792ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5793 auto Imp = importSeq(
5794 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5795 S->getEllipsisLoc(), S->getColonLoc());
5796 if (!Imp)
5797 return Imp.takeError();
5798
5799 Expr *ToLHS, *ToRHS;
5800 Stmt *ToSubStmt;
5801 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5802 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5803 *Imp;
5804
Bruno Ricci5b30571752018-10-28 12:30:53 +00005805 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5806 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005807 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005808
Gabor Horvath480892b2017-10-18 09:25:18 +00005809 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005810}
5811
Balazs Keri3b30d652018-10-19 13:32:20 +00005812ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5813 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5814 if (!Imp)
5815 return Imp.takeError();
5816
5817 SourceLocation ToDefaultLoc, ToColonLoc;
5818 Stmt *ToSubStmt;
5819 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5820
5821 return new (Importer.getToContext()) DefaultStmt(
5822 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005823}
5824
Balazs Keri3b30d652018-10-19 13:32:20 +00005825ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5826 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5827 if (!Imp)
5828 return Imp.takeError();
5829
5830 SourceLocation ToIdentLoc;
5831 LabelDecl *ToLabelDecl;
5832 Stmt *ToSubStmt;
5833 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5834
5835 return new (Importer.getToContext()) LabelStmt(
5836 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005837}
5838
Balazs Keri3b30d652018-10-19 13:32:20 +00005839ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5840 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5841 if (!ToAttrLocOrErr)
5842 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005843 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5844 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005845 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5846 return std::move(Err);
5847 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5848 if (!ToSubStmtOrErr)
5849 return ToSubStmtOrErr.takeError();
5850
5851 return AttributedStmt::Create(
5852 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005853}
5854
Balazs Keri3b30d652018-10-19 13:32:20 +00005855ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5856 auto Imp = importSeq(
5857 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5858 S->getThen(), S->getElseLoc(), S->getElse());
5859 if (!Imp)
5860 return Imp.takeError();
5861
5862 SourceLocation ToIfLoc, ToElseLoc;
5863 Stmt *ToInit, *ToThen, *ToElse;
5864 VarDecl *ToConditionVariable;
5865 Expr *ToCond;
5866 std::tie(
5867 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5868 *Imp;
5869
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005870 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5871 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5872 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005873}
5874
Balazs Keri3b30d652018-10-19 13:32:20 +00005875ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5876 auto Imp = importSeq(
5877 S->getInit(), S->getConditionVariable(), S->getCond(),
5878 S->getBody(), S->getSwitchLoc());
5879 if (!Imp)
5880 return Imp.takeError();
5881
5882 Stmt *ToInit, *ToBody;
5883 VarDecl *ToConditionVariable;
5884 Expr *ToCond;
5885 SourceLocation ToSwitchLoc;
5886 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5887
Bruno Riccie2806f82018-10-29 16:12:37 +00005888 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5889 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005890 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005891 ToStmt->setSwitchLoc(ToSwitchLoc);
5892
Sean Callanan59721b32015-04-28 18:41:46 +00005893 // Now we have to re-chain the cases.
5894 SwitchCase *LastChainedSwitchCase = nullptr;
5895 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5896 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005897 Expected<SwitchCase *> ToSCOrErr = import(SC);
5898 if (!ToSCOrErr)
5899 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005900 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005901 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005902 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005903 ToStmt->setSwitchCaseList(*ToSCOrErr);
5904 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005905 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005906
Sean Callanan59721b32015-04-28 18:41:46 +00005907 return ToStmt;
5908}
5909
Balazs Keri3b30d652018-10-19 13:32:20 +00005910ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5911 auto Imp = importSeq(
5912 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5913 if (!Imp)
5914 return Imp.takeError();
5915
5916 VarDecl *ToConditionVariable;
5917 Expr *ToCond;
5918 Stmt *ToBody;
5919 SourceLocation ToWhileLoc;
5920 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5921
Bruno Riccibacf7512018-10-30 13:42:41 +00005922 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5923 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005924}
5925
Balazs Keri3b30d652018-10-19 13:32:20 +00005926ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5927 auto Imp = importSeq(
5928 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5929 S->getRParenLoc());
5930 if (!Imp)
5931 return Imp.takeError();
5932
5933 Stmt *ToBody;
5934 Expr *ToCond;
5935 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5936 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5937
5938 return new (Importer.getToContext()) DoStmt(
5939 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005940}
5941
Balazs Keri3b30d652018-10-19 13:32:20 +00005942ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5943 auto Imp = importSeq(
5944 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5945 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5946 if (!Imp)
5947 return Imp.takeError();
5948
5949 Stmt *ToInit;
5950 Expr *ToCond, *ToInc;
5951 VarDecl *ToConditionVariable;
5952 Stmt *ToBody;
5953 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5954 std::tie(
5955 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5956 ToLParenLoc, ToRParenLoc) = *Imp;
5957
5958 return new (Importer.getToContext()) ForStmt(
5959 Importer.getToContext(),
5960 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5961 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005962}
5963
Balazs Keri3b30d652018-10-19 13:32:20 +00005964ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5965 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5966 if (!Imp)
5967 return Imp.takeError();
5968
5969 LabelDecl *ToLabel;
5970 SourceLocation ToGotoLoc, ToLabelLoc;
5971 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5972
5973 return new (Importer.getToContext()) GotoStmt(
5974 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005975}
5976
Balazs Keri3b30d652018-10-19 13:32:20 +00005977ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5978 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5979 if (!Imp)
5980 return Imp.takeError();
5981
5982 SourceLocation ToGotoLoc, ToStarLoc;
5983 Expr *ToTarget;
5984 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5985
5986 return new (Importer.getToContext()) IndirectGotoStmt(
5987 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005988}
5989
Balazs Keri3b30d652018-10-19 13:32:20 +00005990ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5991 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5992 if (!ToContinueLocOrErr)
5993 return ToContinueLocOrErr.takeError();
5994 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005995}
5996
Balazs Keri3b30d652018-10-19 13:32:20 +00005997ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5998 auto ToBreakLocOrErr = import(S->getBreakLoc());
5999 if (!ToBreakLocOrErr)
6000 return ToBreakLocOrErr.takeError();
6001 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006002}
6003
Balazs Keri3b30d652018-10-19 13:32:20 +00006004ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
6005 auto Imp = importSeq(
6006 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
6007 if (!Imp)
6008 return Imp.takeError();
6009
6010 SourceLocation ToReturnLoc;
6011 Expr *ToRetValue;
6012 const VarDecl *ToNRVOCandidate;
6013 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
6014
Bruno Ricci023b1d12018-10-30 14:40:49 +00006015 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
6016 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00006017}
6018
Balazs Keri3b30d652018-10-19 13:32:20 +00006019ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
6020 auto Imp = importSeq(
6021 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
6022 if (!Imp)
6023 return Imp.takeError();
6024
6025 SourceLocation ToCatchLoc;
6026 VarDecl *ToExceptionDecl;
6027 Stmt *ToHandlerBlock;
6028 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
6029
6030 return new (Importer.getToContext()) CXXCatchStmt (
6031 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00006032}
6033
Balazs Keri3b30d652018-10-19 13:32:20 +00006034ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
6035 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
6036 if (!ToTryLocOrErr)
6037 return ToTryLocOrErr.takeError();
6038
6039 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
6040 if (!ToTryBlockOrErr)
6041 return ToTryBlockOrErr.takeError();
6042
Sean Callanan59721b32015-04-28 18:41:46 +00006043 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
6044 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
6045 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006046 if (auto ToHandlerOrErr = import(FromHandler))
6047 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006048 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006049 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006050 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006051
6052 return CXXTryStmt::Create(
6053 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00006054}
6055
Balazs Keri3b30d652018-10-19 13:32:20 +00006056ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
6057 auto Imp1 = importSeq(
6058 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
6059 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
6060 if (!Imp1)
6061 return Imp1.takeError();
6062 auto Imp2 = importSeq(
6063 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
6064 if (!Imp2)
6065 return Imp2.takeError();
6066
6067 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
6068 Expr *ToCond, *ToInc;
6069 Stmt *ToInit, *ToBody;
6070 std::tie(
6071 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6072 ToBody) = *Imp1;
6073 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
6074 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
6075
6076 return new (Importer.getToContext()) CXXForRangeStmt(
6077 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6078 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006079}
6080
Balazs Keri3b30d652018-10-19 13:32:20 +00006081ExpectedStmt
6082ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
6083 auto Imp = importSeq(
6084 S->getElement(), S->getCollection(), S->getBody(),
6085 S->getForLoc(), S->getRParenLoc());
6086 if (!Imp)
6087 return Imp.takeError();
6088
6089 Stmt *ToElement, *ToBody;
6090 Expr *ToCollection;
6091 SourceLocation ToForLoc, ToRParenLoc;
6092 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6093
6094 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6095 ToCollection,
6096 ToBody,
6097 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006098 ToRParenLoc);
6099}
6100
Balazs Keri3b30d652018-10-19 13:32:20 +00006101ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6102 auto Imp = importSeq(
6103 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6104 S->getCatchBody());
6105 if (!Imp)
6106 return Imp.takeError();
6107
6108 SourceLocation ToAtCatchLoc, ToRParenLoc;
6109 VarDecl *ToCatchParamDecl;
6110 Stmt *ToCatchBody;
6111 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6112
6113 return new (Importer.getToContext()) ObjCAtCatchStmt (
6114 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006115}
6116
Balazs Keri3b30d652018-10-19 13:32:20 +00006117ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6118 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6119 if (!ToAtFinallyLocOrErr)
6120 return ToAtFinallyLocOrErr.takeError();
6121 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6122 if (!ToAtFinallyStmtOrErr)
6123 return ToAtFinallyStmtOrErr.takeError();
6124 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6125 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006126}
6127
Balazs Keri3b30d652018-10-19 13:32:20 +00006128ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6129 auto Imp = importSeq(
6130 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6131 if (!Imp)
6132 return Imp.takeError();
6133
6134 SourceLocation ToAtTryLoc;
6135 Stmt *ToTryBody, *ToFinallyStmt;
6136 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6137
Sean Callanan59721b32015-04-28 18:41:46 +00006138 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6139 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6140 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006141 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6142 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006143 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006144 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006145 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006146
Sean Callanan59721b32015-04-28 18:41:46 +00006147 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006148 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006149 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006150 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006151}
6152
Balazs Keri3b30d652018-10-19 13:32:20 +00006153ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006154 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006155 auto Imp = importSeq(
6156 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6157 if (!Imp)
6158 return Imp.takeError();
6159
6160 SourceLocation ToAtSynchronizedLoc;
6161 Expr *ToSynchExpr;
6162 Stmt *ToSynchBody;
6163 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6164
Sean Callanan59721b32015-04-28 18:41:46 +00006165 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6166 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6167}
6168
Balazs Keri3b30d652018-10-19 13:32:20 +00006169ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6170 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6171 if (!ToThrowLocOrErr)
6172 return ToThrowLocOrErr.takeError();
6173 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6174 if (!ToThrowExprOrErr)
6175 return ToThrowExprOrErr.takeError();
6176 return new (Importer.getToContext()) ObjCAtThrowStmt(
6177 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006178}
6179
Balazs Keri3b30d652018-10-19 13:32:20 +00006180ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6181 ObjCAutoreleasePoolStmt *S) {
6182 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6183 if (!ToAtLocOrErr)
6184 return ToAtLocOrErr.takeError();
6185 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6186 if (!ToSubStmtOrErr)
6187 return ToSubStmtOrErr.takeError();
6188 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6189 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006190}
6191
6192//----------------------------------------------------------------------------
6193// Import Expressions
6194//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006195ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006196 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6197 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006198 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006199}
6200
Balazs Keri3b30d652018-10-19 13:32:20 +00006201ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6202 auto Imp = importSeq(
6203 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6204 E->getRParenLoc(), E->getType());
6205 if (!Imp)
6206 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006207
Balazs Keri3b30d652018-10-19 13:32:20 +00006208 SourceLocation ToBuiltinLoc, ToRParenLoc;
6209 Expr *ToSubExpr;
6210 TypeSourceInfo *ToWrittenTypeInfo;
6211 QualType ToType;
6212 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6213 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006214
6215 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006216 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6217 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006218}
6219
Tom Roeder521f0042019-02-26 19:26:41 +00006220ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6221 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6222 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6223 if (!Imp)
6224 return Imp.takeError();
6225
6226 Expr *ToCond;
6227 Expr *ToLHS;
6228 Expr *ToRHS;
6229 SourceLocation ToBuiltinLoc, ToRParenLoc;
6230 QualType ToType;
6231 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6232
6233 ExprValueKind VK = E->getValueKind();
6234 ExprObjectKind OK = E->getObjectKind();
6235
6236 bool TypeDependent = ToCond->isTypeDependent();
6237 bool ValueDependent = ToCond->isValueDependent();
6238
6239 // The value of CondIsTrue only matters if the value is not
6240 // condition-dependent.
6241 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6242
6243 return new (Importer.getToContext())
6244 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6245 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6246}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006247
Balazs Keri3b30d652018-10-19 13:32:20 +00006248ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6249 ExpectedType TypeOrErr = import(E->getType());
6250 if (!TypeOrErr)
6251 return TypeOrErr.takeError();
6252
6253 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6254 if (!BeginLocOrErr)
6255 return BeginLocOrErr.takeError();
6256
6257 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006258}
6259
Balazs Keri3b30d652018-10-19 13:32:20 +00006260ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6261 auto Imp = importSeq(
6262 E->getBeginLoc(), E->getType(), E->getFunctionName());
6263 if (!Imp)
6264 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006265
Balazs Keri3b30d652018-10-19 13:32:20 +00006266 SourceLocation ToBeginLoc;
6267 QualType ToType;
6268 StringLiteral *ToFunctionName;
6269 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006270
Bruno Ricci17ff0262018-10-27 19:21:19 +00006271 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6272 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006273}
6274
Balazs Keri3b30d652018-10-19 13:32:20 +00006275ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6276 auto Imp = importSeq(
6277 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6278 E->getLocation(), E->getType());
6279 if (!Imp)
6280 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006281
Balazs Keri3b30d652018-10-19 13:32:20 +00006282 NestedNameSpecifierLoc ToQualifierLoc;
6283 SourceLocation ToTemplateKeywordLoc, ToLocation;
6284 ValueDecl *ToDecl;
6285 QualType ToType;
6286 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6287 *Imp;
6288
6289 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006290 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006291 auto FoundDOrErr = import(E->getFoundDecl());
6292 if (!FoundDOrErr)
6293 return FoundDOrErr.takeError();
6294 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006295 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006296
Aleksei Sidorina693b372016-09-28 10:16:56 +00006297 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006298 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006299 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006300 if (Error Err =
6301 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6302 return std::move(Err);
6303 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006304 }
6305
Balazs Keri3b30d652018-10-19 13:32:20 +00006306 auto *ToE = DeclRefExpr::Create(
6307 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6308 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
Richard Smith715f7a12019-06-11 17:50:32 +00006309 E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse());
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006310 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006311 ToE->setHadMultipleCandidates(true);
6312 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006313}
6314
Balazs Keri3b30d652018-10-19 13:32:20 +00006315ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6316 ExpectedType TypeOrErr = import(E->getType());
6317 if (!TypeOrErr)
6318 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006319
Balazs Keri3b30d652018-10-19 13:32:20 +00006320 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006321}
6322
Balazs Keri3b30d652018-10-19 13:32:20 +00006323ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6324 ExpectedExpr ToInitOrErr = import(E->getInit());
6325 if (!ToInitOrErr)
6326 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006327
Balazs Keri3b30d652018-10-19 13:32:20 +00006328 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6329 if (!ToEqualOrColonLocOrErr)
6330 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006331
Balazs Keri3b30d652018-10-19 13:32:20 +00006332 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006333 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006334 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6335 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6336 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006337 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006338 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006339 }
6340
Balazs Keri3b30d652018-10-19 13:32:20 +00006341 SmallVector<Designator, 4> ToDesignators(E->size());
6342 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6343 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006344
6345 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006346 Importer.getToContext(), ToDesignators,
6347 ToIndexExprs, *ToEqualOrColonLocOrErr,
6348 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006349}
6350
Balazs Keri3b30d652018-10-19 13:32:20 +00006351ExpectedStmt
6352ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6353 ExpectedType ToTypeOrErr = import(E->getType());
6354 if (!ToTypeOrErr)
6355 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006356
Balazs Keri3b30d652018-10-19 13:32:20 +00006357 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6358 if (!ToLocationOrErr)
6359 return ToLocationOrErr.takeError();
6360
6361 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6362 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006363}
6364
Balazs Keri3b30d652018-10-19 13:32:20 +00006365ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6366 ExpectedType ToTypeOrErr = import(E->getType());
6367 if (!ToTypeOrErr)
6368 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006369
Balazs Keri3b30d652018-10-19 13:32:20 +00006370 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6371 if (!ToLocationOrErr)
6372 return ToLocationOrErr.takeError();
6373
6374 return IntegerLiteral::Create(
6375 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006376}
6377
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006378
Balazs Keri3b30d652018-10-19 13:32:20 +00006379ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6380 ExpectedType ToTypeOrErr = import(E->getType());
6381 if (!ToTypeOrErr)
6382 return ToTypeOrErr.takeError();
6383
6384 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6385 if (!ToLocationOrErr)
6386 return ToLocationOrErr.takeError();
6387
6388 return FloatingLiteral::Create(
6389 Importer.getToContext(), E->getValue(), E->isExact(),
6390 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006391}
6392
Balazs Keri3b30d652018-10-19 13:32:20 +00006393ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6394 auto ToTypeOrErr = import(E->getType());
6395 if (!ToTypeOrErr)
6396 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006397
Balazs Keri3b30d652018-10-19 13:32:20 +00006398 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6399 if (!ToSubExprOrErr)
6400 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006401
Balazs Keri3b30d652018-10-19 13:32:20 +00006402 return new (Importer.getToContext()) ImaginaryLiteral(
6403 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006404}
6405
Balazs Keri3b30d652018-10-19 13:32:20 +00006406ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6407 ExpectedType ToTypeOrErr = import(E->getType());
6408 if (!ToTypeOrErr)
6409 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006410
Balazs Keri3b30d652018-10-19 13:32:20 +00006411 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6412 if (!ToLocationOrErr)
6413 return ToLocationOrErr.takeError();
6414
6415 return new (Importer.getToContext()) CharacterLiteral(
6416 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006417}
6418
Balazs Keri3b30d652018-10-19 13:32:20 +00006419ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6420 ExpectedType ToTypeOrErr = import(E->getType());
6421 if (!ToTypeOrErr)
6422 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006423
Balazs Keri3b30d652018-10-19 13:32:20 +00006424 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6425 if (Error Err = ImportArrayChecked(
6426 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6427 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006428
Balazs Keri3b30d652018-10-19 13:32:20 +00006429 return StringLiteral::Create(
6430 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6431 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006432}
6433
Balazs Keri3b30d652018-10-19 13:32:20 +00006434ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6435 auto Imp = importSeq(
6436 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6437 E->getInitializer());
6438 if (!Imp)
6439 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006440
Balazs Keri3b30d652018-10-19 13:32:20 +00006441 SourceLocation ToLParenLoc;
6442 TypeSourceInfo *ToTypeSourceInfo;
6443 QualType ToType;
6444 Expr *ToInitializer;
6445 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006446
6447 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006448 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6449 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006450}
6451
Balazs Keri3b30d652018-10-19 13:32:20 +00006452ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6453 auto Imp = importSeq(
6454 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6455 if (!Imp)
6456 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006457
Balazs Keri3b30d652018-10-19 13:32:20 +00006458 SourceLocation ToBuiltinLoc, ToRParenLoc;
6459 QualType ToType;
6460 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6461
6462 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6463 if (Error Err = ImportArrayChecked(
6464 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6465 ToExprs.begin()))
6466 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006467
6468 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006469 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006470}
6471
Balazs Keri3b30d652018-10-19 13:32:20 +00006472ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6473 auto Imp = importSeq(
6474 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6475 if (!Imp)
6476 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006477
Balazs Keri3b30d652018-10-19 13:32:20 +00006478 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6479 LabelDecl *ToLabel;
6480 QualType ToType;
6481 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006482
6483 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006484 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006485}
6486
Bill Wendling8003edc2018-11-09 00:41:36 +00006487ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6488 auto Imp = importSeq(E->getSubExpr());
6489 if (!Imp)
6490 return Imp.takeError();
6491
6492 Expr *ToSubExpr;
6493 std::tie(ToSubExpr) = *Imp;
6494
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00006495 // TODO : Handle APValue::ValueKind that require importing.
6496 APValue::ValueKind Kind = E->getResultAPValueKind();
6497 if (Kind == APValue::Int || Kind == APValue::Float ||
6498 Kind == APValue::FixedPoint || Kind == APValue::ComplexFloat ||
6499 Kind == APValue::ComplexInt)
6500 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr,
6501 E->getAPValueResult());
Fangrui Song407659a2018-11-30 23:41:18 +00006502 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006503}
6504
Balazs Keri3b30d652018-10-19 13:32:20 +00006505ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6506 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6507 if (!Imp)
6508 return Imp.takeError();
6509
6510 SourceLocation ToLParen, ToRParen;
6511 Expr *ToSubExpr;
6512 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006513
Fangrui Song6907ce22018-07-30 19:24:48 +00006514 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006515 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006516}
6517
Balazs Keri3b30d652018-10-19 13:32:20 +00006518ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6519 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6520 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6521 return std::move(Err);
6522
6523 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6524 if (!ToLParenLocOrErr)
6525 return ToLParenLocOrErr.takeError();
6526
6527 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6528 if (!ToRParenLocOrErr)
6529 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006530
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006531 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6532 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006533}
6534
Balazs Keri3b30d652018-10-19 13:32:20 +00006535ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6536 auto Imp = importSeq(
6537 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6538 if (!Imp)
6539 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006540
Balazs Keri3b30d652018-10-19 13:32:20 +00006541 CompoundStmt *ToSubStmt;
6542 QualType ToType;
6543 SourceLocation ToLParenLoc, ToRParenLoc;
6544 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006545
Balazs Keri3b30d652018-10-19 13:32:20 +00006546 return new (Importer.getToContext()) StmtExpr(
6547 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006548}
6549
Balazs Keri3b30d652018-10-19 13:32:20 +00006550ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6551 auto Imp = importSeq(
6552 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6553 if (!Imp)
6554 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006555
Balazs Keri3b30d652018-10-19 13:32:20 +00006556 Expr *ToSubExpr;
6557 QualType ToType;
6558 SourceLocation ToOperatorLoc;
6559 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006560
Aaron Ballmana5038552018-01-09 13:07:03 +00006561 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006562 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6563 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006564}
6565
Balazs Keri3b30d652018-10-19 13:32:20 +00006566ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006567ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006568 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6569 if (!Imp)
6570 return Imp.takeError();
6571
6572 QualType ToType;
6573 SourceLocation ToOperatorLoc, ToRParenLoc;
6574 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006575
Douglas Gregord8552cd2010-02-19 01:24:23 +00006576 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006577 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6578 import(E->getArgumentTypeInfo());
6579 if (!ToArgumentTypeInfoOrErr)
6580 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006581
Balazs Keri3b30d652018-10-19 13:32:20 +00006582 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6583 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6584 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006585 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006586
Balazs Keri3b30d652018-10-19 13:32:20 +00006587 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6588 if (!ToArgumentExprOrErr)
6589 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006590
Balazs Keri3b30d652018-10-19 13:32:20 +00006591 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6592 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006593}
6594
Balazs Keri3b30d652018-10-19 13:32:20 +00006595ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6596 auto Imp = importSeq(
6597 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6598 if (!Imp)
6599 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006600
Balazs Keri3b30d652018-10-19 13:32:20 +00006601 Expr *ToLHS, *ToRHS;
6602 QualType ToType;
6603 SourceLocation ToOperatorLoc;
6604 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006605
Balazs Keri3b30d652018-10-19 13:32:20 +00006606 return new (Importer.getToContext()) BinaryOperator(
6607 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6608 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006609}
6610
Balazs Keri3b30d652018-10-19 13:32:20 +00006611ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6612 auto Imp = importSeq(
6613 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6614 E->getRHS(), E->getType());
6615 if (!Imp)
6616 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006617
Balazs Keri3b30d652018-10-19 13:32:20 +00006618 Expr *ToCond, *ToLHS, *ToRHS;
6619 SourceLocation ToQuestionLoc, ToColonLoc;
6620 QualType ToType;
6621 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006622
6623 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006624 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6625 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006626}
6627
Balazs Keri3b30d652018-10-19 13:32:20 +00006628ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006629 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006630 auto Imp = importSeq(
6631 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6632 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6633 if (!Imp)
6634 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006635
Balazs Keri3b30d652018-10-19 13:32:20 +00006636 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6637 OpaqueValueExpr *ToOpaqueValue;
6638 SourceLocation ToQuestionLoc, ToColonLoc;
6639 QualType ToType;
6640 std::tie(
6641 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6642 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006643
6644 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006645 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6646 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6647 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006648}
6649
Balazs Keri3b30d652018-10-19 13:32:20 +00006650ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6651 auto Imp = importSeq(
6652 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6653 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6654 if (!Imp)
6655 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006656
Balazs Keri3b30d652018-10-19 13:32:20 +00006657 SourceLocation ToBeginLoc, ToEndLoc;
6658 TypeSourceInfo *ToQueriedTypeSourceInfo;
6659 Expr *ToDimensionExpression;
6660 QualType ToType;
6661 std::tie(
6662 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6663 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006664
6665 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006666 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6667 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006668}
6669
Balazs Keri3b30d652018-10-19 13:32:20 +00006670ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6671 auto Imp = importSeq(
6672 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6673 if (!Imp)
6674 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006675
Balazs Keri3b30d652018-10-19 13:32:20 +00006676 SourceLocation ToBeginLoc, ToEndLoc;
6677 Expr *ToQueriedExpression;
6678 QualType ToType;
6679 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006680
6681 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006682 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6683 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006684}
6685
Balazs Keri3b30d652018-10-19 13:32:20 +00006686ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6687 auto Imp = importSeq(
6688 E->getLocation(), E->getType(), E->getSourceExpr());
6689 if (!Imp)
6690 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006691
Balazs Keri3b30d652018-10-19 13:32:20 +00006692 SourceLocation ToLocation;
6693 QualType ToType;
6694 Expr *ToSourceExpr;
6695 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006696
6697 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006698 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006699}
6700
Balazs Keri3b30d652018-10-19 13:32:20 +00006701ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6702 auto Imp = importSeq(
6703 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6704 if (!Imp)
6705 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006706
Balazs Keri3b30d652018-10-19 13:32:20 +00006707 Expr *ToLHS, *ToRHS;
6708 SourceLocation ToRBracketLoc;
6709 QualType ToType;
6710 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006711
6712 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006713 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6714 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006715}
6716
Balazs Keri3b30d652018-10-19 13:32:20 +00006717ExpectedStmt
6718ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6719 auto Imp = importSeq(
6720 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6721 E->getComputationResultType(), E->getOperatorLoc());
6722 if (!Imp)
6723 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006724
Balazs Keri3b30d652018-10-19 13:32:20 +00006725 Expr *ToLHS, *ToRHS;
6726 QualType ToType, ToComputationLHSType, ToComputationResultType;
6727 SourceLocation ToOperatorLoc;
6728 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6729 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006730
Balazs Keri3b30d652018-10-19 13:32:20 +00006731 return new (Importer.getToContext()) CompoundAssignOperator(
6732 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6733 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6734 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006735}
6736
Balazs Keri3b30d652018-10-19 13:32:20 +00006737Expected<CXXCastPath>
6738ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6739 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006740 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006741 if (auto SpecOrErr = import(*I))
6742 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006743 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006744 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006745 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006746 return Path;
John McCallcf142162010-08-07 06:22:56 +00006747}
6748
Balazs Keri3b30d652018-10-19 13:32:20 +00006749ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6750 ExpectedType ToTypeOrErr = import(E->getType());
6751 if (!ToTypeOrErr)
6752 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006753
Balazs Keri3b30d652018-10-19 13:32:20 +00006754 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6755 if (!ToSubExprOrErr)
6756 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006757
Balazs Keri3b30d652018-10-19 13:32:20 +00006758 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6759 if (!ToBasePathOrErr)
6760 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006761
Balazs Keri3b30d652018-10-19 13:32:20 +00006762 return ImplicitCastExpr::Create(
6763 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6764 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006765}
6766
Balazs Keri3b30d652018-10-19 13:32:20 +00006767ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6768 auto Imp1 = importSeq(
6769 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6770 if (!Imp1)
6771 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006772
Balazs Keri3b30d652018-10-19 13:32:20 +00006773 QualType ToType;
6774 Expr *ToSubExpr;
6775 TypeSourceInfo *ToTypeInfoAsWritten;
6776 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006777
Balazs Keri3b30d652018-10-19 13:32:20 +00006778 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6779 if (!ToBasePathOrErr)
6780 return ToBasePathOrErr.takeError();
6781 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006782
Aleksei Sidorina693b372016-09-28 10:16:56 +00006783 switch (E->getStmtClass()) {
6784 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006785 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006786 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6787 if (!ToLParenLocOrErr)
6788 return ToLParenLocOrErr.takeError();
6789 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6790 if (!ToRParenLocOrErr)
6791 return ToRParenLocOrErr.takeError();
6792 return CStyleCastExpr::Create(
6793 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6794 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6795 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006796 }
6797
6798 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006799 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006800 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6801 if (!ToLParenLocOrErr)
6802 return ToLParenLocOrErr.takeError();
6803 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6804 if (!ToRParenLocOrErr)
6805 return ToRParenLocOrErr.takeError();
6806 return CXXFunctionalCastExpr::Create(
6807 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6808 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6809 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006810 }
6811
6812 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006813 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6814 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6815 if (!ToLParenLocOrErr)
6816 return ToLParenLocOrErr.takeError();
6817 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6818 if (!ToBridgeKeywordLocOrErr)
6819 return ToBridgeKeywordLocOrErr.takeError();
6820 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6821 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6822 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006823 }
6824 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006825 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006826 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006827 }
6828}
6829
Balazs Keri3b30d652018-10-19 13:32:20 +00006830ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6831 SmallVector<OffsetOfNode, 4> ToNodes;
6832 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6833 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006834
Balazs Keri3b30d652018-10-19 13:32:20 +00006835 SourceLocation ToBeginLoc, ToEndLoc;
6836 if (FromNode.getKind() != OffsetOfNode::Base) {
6837 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6838 if (!Imp)
6839 return Imp.takeError();
6840 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6841 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006842
Balazs Keri3b30d652018-10-19 13:32:20 +00006843 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006844 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006845 ToNodes.push_back(
6846 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006847 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006848 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006849 auto ToBSOrErr = import(FromNode.getBase());
6850 if (!ToBSOrErr)
6851 return ToBSOrErr.takeError();
6852 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006853 break;
6854 }
6855 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006856 auto ToFieldOrErr = import(FromNode.getField());
6857 if (!ToFieldOrErr)
6858 return ToFieldOrErr.takeError();
6859 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006860 break;
6861 }
6862 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006863 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6864 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006865 break;
6866 }
6867 }
6868 }
6869
Balazs Keri3b30d652018-10-19 13:32:20 +00006870 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6871 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6872 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6873 if (!ToIndexExprOrErr)
6874 return ToIndexExprOrErr.takeError();
6875 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006876 }
6877
Balazs Keri3b30d652018-10-19 13:32:20 +00006878 auto Imp = importSeq(
6879 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6880 E->getRParenLoc());
6881 if (!Imp)
6882 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006883
Balazs Keri3b30d652018-10-19 13:32:20 +00006884 QualType ToType;
6885 TypeSourceInfo *ToTypeSourceInfo;
6886 SourceLocation ToOperatorLoc, ToRParenLoc;
6887 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6888
6889 return OffsetOfExpr::Create(
6890 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6891 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006892}
6893
Balazs Keri3b30d652018-10-19 13:32:20 +00006894ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6895 auto Imp = importSeq(
6896 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6897 if (!Imp)
6898 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006899
Balazs Keri3b30d652018-10-19 13:32:20 +00006900 QualType ToType;
6901 Expr *ToOperand;
6902 SourceLocation ToBeginLoc, ToEndLoc;
6903 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006904
Balazs Keri3b30d652018-10-19 13:32:20 +00006905 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006906 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006907 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006908 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006909 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006910
Balazs Keri3b30d652018-10-19 13:32:20 +00006911 return new (Importer.getToContext()) CXXNoexceptExpr(
6912 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006913}
6914
Balazs Keri3b30d652018-10-19 13:32:20 +00006915ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6916 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6917 if (!Imp)
6918 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006919
Balazs Keri3b30d652018-10-19 13:32:20 +00006920 Expr *ToSubExpr;
6921 QualType ToType;
6922 SourceLocation ToThrowLoc;
6923 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006924
6925 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006926 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006927}
6928
Balazs Keri3b30d652018-10-19 13:32:20 +00006929ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6930 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6931 if (!ToUsedLocOrErr)
6932 return ToUsedLocOrErr.takeError();
6933
6934 auto ToParamOrErr = import(E->getParam());
6935 if (!ToParamOrErr)
6936 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006937
Eric Fiselier708afb52019-05-16 21:04:15 +00006938 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
6939 if (!UsedContextOrErr)
6940 return UsedContextOrErr.takeError();
6941
Aleksei Sidorina693b372016-09-28 10:16:56 +00006942 return CXXDefaultArgExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00006943 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr, *UsedContextOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006944}
6945
Balazs Keri3b30d652018-10-19 13:32:20 +00006946ExpectedStmt
6947ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6948 auto Imp = importSeq(
6949 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6950 if (!Imp)
6951 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006952
Balazs Keri3b30d652018-10-19 13:32:20 +00006953 QualType ToType;
6954 TypeSourceInfo *ToTypeSourceInfo;
6955 SourceLocation ToRParenLoc;
6956 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006957
6958 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006959 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006960}
6961
Balazs Keri3b30d652018-10-19 13:32:20 +00006962ExpectedStmt
6963ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6964 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6965 if (!ToSubExprOrErr)
6966 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006967
Balazs Keri3b30d652018-10-19 13:32:20 +00006968 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6969 if (!ToDtorOrErr)
6970 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006971
6972 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006973 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6974 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006975}
6976
Balazs Keri3b30d652018-10-19 13:32:20 +00006977ExpectedStmt
6978ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6979 auto Imp = importSeq(
6980 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6981 E->getParenOrBraceRange());
6982 if (!Imp)
6983 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006984
Balazs Keri3b30d652018-10-19 13:32:20 +00006985 CXXConstructorDecl *ToConstructor;
6986 QualType ToType;
6987 TypeSourceInfo *ToTypeSourceInfo;
6988 SourceRange ToParenOrBraceRange;
6989 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006990
Balazs Keri3b30d652018-10-19 13:32:20 +00006991 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6992 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6993 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006994
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006995 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006996 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6997 ToParenOrBraceRange, E->hadMultipleCandidates(),
6998 E->isListInitialization(), E->isStdInitListInitialization(),
6999 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007000}
7001
Balazs Keri3b30d652018-10-19 13:32:20 +00007002ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00007003ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007004 auto Imp = importSeq(
7005 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
7006 if (!Imp)
7007 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007008
Balazs Keri3b30d652018-10-19 13:32:20 +00007009 QualType ToType;
7010 Expr *ToTemporaryExpr;
7011 const ValueDecl *ToExtendingDecl;
7012 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007013
7014 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007015 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007016
7017 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00007018 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007019 return ToMTE;
7020}
7021
Balazs Keri3b30d652018-10-19 13:32:20 +00007022ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
7023 auto Imp = importSeq(
7024 E->getType(), E->getPattern(), E->getEllipsisLoc());
7025 if (!Imp)
7026 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00007027
Balazs Keri3b30d652018-10-19 13:32:20 +00007028 QualType ToType;
7029 Expr *ToPattern;
7030 SourceLocation ToEllipsisLoc;
7031 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00007032
7033 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007034 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00007035}
7036
Balazs Keri3b30d652018-10-19 13:32:20 +00007037ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
7038 auto Imp = importSeq(
7039 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
7040 if (!Imp)
7041 return Imp.takeError();
7042
7043 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
7044 NamedDecl *ToPack;
7045 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007046
7047 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007048 if (!E->isValueDependent())
7049 Length = E->getPackLength();
7050
Balazs Keri3b30d652018-10-19 13:32:20 +00007051 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007052 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007053 if (Error Err = ImportTemplateArguments(
7054 E->getPartialArguments().data(),
7055 E->getPartialArguments().size(),
7056 ToPartialArguments))
7057 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007058 }
7059
7060 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007061 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
7062 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007063}
7064
Aleksei Sidorina693b372016-09-28 10:16:56 +00007065
Balazs Keri3b30d652018-10-19 13:32:20 +00007066ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
7067 auto Imp = importSeq(
7068 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
7069 E->getArraySize(), E->getInitializer(), E->getType(),
7070 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
7071 E->getDirectInitRange());
7072 if (!Imp)
7073 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007074
Balazs Keri3b30d652018-10-19 13:32:20 +00007075 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
7076 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00007077 Optional<Expr *> ToArraySize;
7078 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00007079 QualType ToType;
7080 TypeSourceInfo *ToAllocatedTypeSourceInfo;
7081 std::tie(
7082 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
7083 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007084
Balazs Keri3b30d652018-10-19 13:32:20 +00007085 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
7086 if (Error Err =
7087 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
7088 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007089
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00007090 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007091 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
7092 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
7093 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
7094 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
7095 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007096}
7097
Balazs Keri3b30d652018-10-19 13:32:20 +00007098ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
7099 auto Imp = importSeq(
7100 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
7101 if (!Imp)
7102 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007103
Balazs Keri3b30d652018-10-19 13:32:20 +00007104 QualType ToType;
7105 FunctionDecl *ToOperatorDelete;
7106 Expr *ToArgument;
7107 SourceLocation ToBeginLoc;
7108 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007109
7110 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007111 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
7112 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
7113 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00007114}
7115
Balazs Keri3b30d652018-10-19 13:32:20 +00007116ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
7117 auto Imp = importSeq(
7118 E->getType(), E->getLocation(), E->getConstructor(),
7119 E->getParenOrBraceRange());
7120 if (!Imp)
7121 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007122
Balazs Keri3b30d652018-10-19 13:32:20 +00007123 QualType ToType;
7124 SourceLocation ToLocation;
7125 CXXConstructorDecl *ToConstructor;
7126 SourceRange ToParenOrBraceRange;
7127 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007128
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007129 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007130 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7131 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007132
Balazs Keri3b30d652018-10-19 13:32:20 +00007133 return CXXConstructExpr::Create(
7134 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7135 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7136 E->isListInitialization(), E->isStdInitListInitialization(),
7137 E->requiresZeroInitialization(), E->getConstructionKind(),
7138 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007139}
7140
Balazs Keri3b30d652018-10-19 13:32:20 +00007141ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7142 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7143 if (!ToSubExprOrErr)
7144 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007145
Balazs Keri3b30d652018-10-19 13:32:20 +00007146 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7147 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7148 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007149
Balazs Keri3b30d652018-10-19 13:32:20 +00007150 return ExprWithCleanups::Create(
7151 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7152 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007153}
7154
Balazs Keri3b30d652018-10-19 13:32:20 +00007155ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7156 auto Imp = importSeq(
7157 E->getCallee(), E->getType(), E->getRParenLoc());
7158 if (!Imp)
7159 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007160
Balazs Keri3b30d652018-10-19 13:32:20 +00007161 Expr *ToCallee;
7162 QualType ToType;
7163 SourceLocation ToRParenLoc;
7164 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007165
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007166 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007167 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7168 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007169
Bruno Riccic5885cf2018-12-21 15:20:32 +00007170 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7171 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007172}
7173
Balazs Keri3b30d652018-10-19 13:32:20 +00007174ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7175 ExpectedType ToTypeOrErr = import(E->getType());
7176 if (!ToTypeOrErr)
7177 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007178
Balazs Keri3b30d652018-10-19 13:32:20 +00007179 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7180 if (!ToLocationOrErr)
7181 return ToLocationOrErr.takeError();
7182
7183 return new (Importer.getToContext()) CXXThisExpr(
7184 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007185}
7186
Balazs Keri3b30d652018-10-19 13:32:20 +00007187ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7188 ExpectedType ToTypeOrErr = import(E->getType());
7189 if (!ToTypeOrErr)
7190 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007191
Balazs Keri3b30d652018-10-19 13:32:20 +00007192 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7193 if (!ToLocationOrErr)
7194 return ToLocationOrErr.takeError();
7195
7196 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7197 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007198}
7199
Balazs Keri3b30d652018-10-19 13:32:20 +00007200ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7201 auto Imp1 = importSeq(
7202 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7203 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7204 if (!Imp1)
7205 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007206
Balazs Keri3b30d652018-10-19 13:32:20 +00007207 Expr *ToBase;
7208 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7209 NestedNameSpecifierLoc ToQualifierLoc;
7210 ValueDecl *ToMemberDecl;
7211 QualType ToType;
7212 std::tie(
7213 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7214 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007215
Balazs Keri3b30d652018-10-19 13:32:20 +00007216 auto Imp2 = importSeq(
7217 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7218 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7219 if (!Imp2)
7220 return Imp2.takeError();
7221 NamedDecl *ToDecl;
7222 DeclarationName ToName;
7223 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7224 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007225
7226 DeclAccessPair ToFoundDecl =
7227 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007228
Balazs Keri3b30d652018-10-19 13:32:20 +00007229 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007230
Gabor Marton5caba302019-03-07 13:38:20 +00007231 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007232 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007233 if (Error Err =
7234 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7235 E->template_arguments(), ToTAInfo))
7236 return std::move(Err);
7237 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007238 }
7239
Richard Smith1bbad592019-06-11 17:50:36 +00007240 return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(),
7241 ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7242 ToMemberDecl, ToFoundDecl, ToMemberNameInfo,
7243 ResInfo, ToType, E->getValueKind(),
7244 E->getObjectKind(), E->isNonOdrUse());
Sean Callanan59721b32015-04-28 18:41:46 +00007245}
7246
Balazs Keri3b30d652018-10-19 13:32:20 +00007247ExpectedStmt
7248ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7249 auto Imp = importSeq(
7250 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7251 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7252 if (!Imp)
7253 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007254
Balazs Keri3b30d652018-10-19 13:32:20 +00007255 Expr *ToBase;
7256 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7257 NestedNameSpecifierLoc ToQualifierLoc;
7258 TypeSourceInfo *ToScopeTypeInfo;
7259 std::tie(
7260 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7261 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007262
7263 PseudoDestructorTypeStorage Storage;
7264 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7265 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007266 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7267 if (!ToDestroyedTypeLocOrErr)
7268 return ToDestroyedTypeLocOrErr.takeError();
7269 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007270 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007271 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7272 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7273 else
7274 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007275 }
7276
7277 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007278 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7279 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007280}
7281
Balazs Keri3b30d652018-10-19 13:32:20 +00007282ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007283 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007284 auto Imp = importSeq(
7285 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7286 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7287 if (!Imp)
7288 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007289
Balazs Keri3b30d652018-10-19 13:32:20 +00007290 QualType ToType;
7291 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7292 NestedNameSpecifierLoc ToQualifierLoc;
7293 NamedDecl *ToFirstQualifierFoundInScope;
7294 std::tie(
7295 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7296 ToFirstQualifierFoundInScope) = *Imp;
7297
7298 Expr *ToBase = nullptr;
7299 if (!E->isImplicitAccess()) {
7300 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7301 ToBase = *ToBaseOrErr;
7302 else
7303 return ToBaseOrErr.takeError();
7304 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007305
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007306 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007307 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007308 if (Error Err = ImportTemplateArgumentListInfo(
7309 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7310 ToTAInfo))
7311 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007312 ResInfo = &ToTAInfo;
7313 }
7314
Balazs Keri3b30d652018-10-19 13:32:20 +00007315 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7316 if (!ToMemberNameInfoOrErr)
7317 return ToMemberNameInfoOrErr.takeError();
7318 DeclarationNameInfo ToMemberNameInfo(
7319 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007320 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007321 if (Error Err = ImportDeclarationNameLoc(
7322 E->getMemberNameInfo(), ToMemberNameInfo))
7323 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007324
7325 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007326 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7327 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7328 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007329}
7330
Balazs Keri3b30d652018-10-19 13:32:20 +00007331ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007332ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007333 auto Imp = importSeq(
7334 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7335 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7336 if (!Imp)
7337 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007338
Balazs Keri3b30d652018-10-19 13:32:20 +00007339 NestedNameSpecifierLoc ToQualifierLoc;
7340 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7341 DeclarationName ToDeclName;
7342 std::tie(
7343 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7344 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007345
Balazs Keri3b30d652018-10-19 13:32:20 +00007346 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7347 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7348 return std::move(Err);
7349
7350 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007351 TemplateArgumentListInfo *ResInfo = nullptr;
7352 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007353 if (Error Err =
7354 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7355 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007356 ResInfo = &ToTAInfo;
7357 }
7358
7359 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007360 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7361 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007362}
7363
Balazs Keri3b30d652018-10-19 13:32:20 +00007364ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7365 CXXUnresolvedConstructExpr *E) {
7366 auto Imp = importSeq(
7367 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7368 if (!Imp)
7369 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007370
Balazs Keri3b30d652018-10-19 13:32:20 +00007371 SourceLocation ToLParenLoc, ToRParenLoc;
7372 TypeSourceInfo *ToTypeSourceInfo;
7373 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7374
7375 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7376 if (Error Err =
7377 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7378 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007379
7380 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007381 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7382 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007383}
7384
Balazs Keri3b30d652018-10-19 13:32:20 +00007385ExpectedStmt
7386ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7387 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7388 if (!ToNamingClassOrErr)
7389 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007390
Balazs Keri3b30d652018-10-19 13:32:20 +00007391 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7392 if (!ToQualifierLocOrErr)
7393 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007394
Balazs Keri3b30d652018-10-19 13:32:20 +00007395 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7396 if (!ToNameInfoOrErr)
7397 return ToNameInfoOrErr.takeError();
7398 DeclarationNameInfo ToNameInfo(
7399 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007400 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007401 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7402 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007403
7404 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007405 for (auto *D : E->decls())
7406 if (auto ToDOrErr = import(D))
7407 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007408 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007409 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007410
Balazs Keri3b30d652018-10-19 13:32:20 +00007411 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7412 TemplateArgumentListInfo ToTAInfo;
7413 if (Error Err = ImportTemplateArgumentListInfo(
7414 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7415 ToTAInfo))
7416 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007417
Balazs Keri3b30d652018-10-19 13:32:20 +00007418 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7419 if (!ToTemplateKeywordLocOrErr)
7420 return ToTemplateKeywordLocOrErr.takeError();
7421
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007422 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007423 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7424 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7425 ToDecls.begin(), ToDecls.end());
7426 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007427
7428 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007429 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7430 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7431 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007432}
7433
Balazs Keri3b30d652018-10-19 13:32:20 +00007434ExpectedStmt
7435ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7436 auto Imp1 = importSeq(
7437 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7438 E->getTemplateKeywordLoc());
7439 if (!Imp1)
7440 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007441
Balazs Keri3b30d652018-10-19 13:32:20 +00007442 QualType ToType;
7443 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7444 NestedNameSpecifierLoc ToQualifierLoc;
7445 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7446
7447 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7448 if (!Imp2)
7449 return Imp2.takeError();
7450 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7451 // Import additional name location/type info.
7452 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7453 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007454
7455 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007456 for (Decl *D : E->decls())
7457 if (auto ToDOrErr = import(D))
7458 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007459 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007460 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007461
7462 TemplateArgumentListInfo ToTAInfo;
7463 TemplateArgumentListInfo *ResInfo = nullptr;
7464 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007465 if (Error Err =
7466 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7467 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007468 ResInfo = &ToTAInfo;
7469 }
7470
Balazs Keri3b30d652018-10-19 13:32:20 +00007471 Expr *ToBase = nullptr;
7472 if (!E->isImplicitAccess()) {
7473 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7474 ToBase = *ToBaseOrErr;
7475 else
7476 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007477 }
7478
7479 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007480 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7481 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7482 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007483}
7484
Balazs Keri3b30d652018-10-19 13:32:20 +00007485ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7486 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7487 if (!Imp)
7488 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007489
Balazs Keri3b30d652018-10-19 13:32:20 +00007490 Expr *ToCallee;
7491 QualType ToType;
7492 SourceLocation ToRParenLoc;
7493 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007494
7495 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007496 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7497 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7498 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007499
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007500 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007501 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007502 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007503 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7504 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007505 }
7506
Bruno Riccic5885cf2018-12-21 15:20:32 +00007507 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7508 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7509 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007510}
7511
Balazs Keri3b30d652018-10-19 13:32:20 +00007512ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7513 CXXRecordDecl *FromClass = E->getLambdaClass();
7514 auto ToClassOrErr = import(FromClass);
7515 if (!ToClassOrErr)
7516 return ToClassOrErr.takeError();
7517 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007518
Balazs Keri3b30d652018-10-19 13:32:20 +00007519 auto ToCallOpOrErr = import(E->getCallOperator());
7520 if (!ToCallOpOrErr)
7521 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007522
Balazs Keri3b30d652018-10-19 13:32:20 +00007523 SmallVector<LambdaCapture, 8> ToCaptures;
7524 ToCaptures.reserve(E->capture_size());
7525 for (const auto &FromCapture : E->captures()) {
7526 if (auto ToCaptureOrErr = import(FromCapture))
7527 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007528 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007529 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007530 }
7531
Balazs Keri3b30d652018-10-19 13:32:20 +00007532 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7533 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7534 return std::move(Err);
7535
7536 auto Imp = importSeq(
7537 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7538 if (!Imp)
7539 return Imp.takeError();
7540
7541 SourceRange ToIntroducerRange;
7542 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7543 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007544
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007545 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007546 Importer.getToContext(), ToClass, ToIntroducerRange,
7547 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7548 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7549 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007550}
7551
Sean Callanan8bca9962016-03-28 21:43:01 +00007552
Balazs Keri3b30d652018-10-19 13:32:20 +00007553ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7554 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7555 if (!Imp)
7556 return Imp.takeError();
7557
7558 SourceLocation ToLBraceLoc, ToRBraceLoc;
7559 QualType ToType;
7560 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7561
7562 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7563 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7564 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007565
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007566 ASTContext &ToCtx = Importer.getToContext();
7567 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007568 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7569 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007570
Balazs Keri3b30d652018-10-19 13:32:20 +00007571 if (E->hasArrayFiller()) {
7572 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7573 To->setArrayFiller(*ToFillerOrErr);
7574 else
7575 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007576 }
7577
Balazs Keri3b30d652018-10-19 13:32:20 +00007578 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7579 if (auto ToFDOrErr = import(FromFD))
7580 To->setInitializedFieldInUnion(*ToFDOrErr);
7581 else
7582 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007583 }
7584
Balazs Keri3b30d652018-10-19 13:32:20 +00007585 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7586 if (auto ToSyntFormOrErr = import(SyntForm))
7587 To->setSyntacticForm(*ToSyntFormOrErr);
7588 else
7589 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007590 }
7591
Gabor Martona20ce602018-09-03 13:10:53 +00007592 // Copy InitListExprBitfields, which are not handled in the ctor of
7593 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007594 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007595
7596 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007597}
7598
Balazs Keri3b30d652018-10-19 13:32:20 +00007599ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007600 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007601 ExpectedType ToTypeOrErr = import(E->getType());
7602 if (!ToTypeOrErr)
7603 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007604
Balazs Keri3b30d652018-10-19 13:32:20 +00007605 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7606 if (!ToSubExprOrErr)
7607 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007608
Balazs Keri3b30d652018-10-19 13:32:20 +00007609 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7610 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007611}
7612
Balazs Keri3b30d652018-10-19 13:32:20 +00007613ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007614 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007615 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7616 if (!Imp)
7617 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007618
Balazs Keri3b30d652018-10-19 13:32:20 +00007619 SourceLocation ToLocation;
7620 QualType ToType;
7621 CXXConstructorDecl *ToConstructor;
7622 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007623
7624 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007625 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7626 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007627}
7628
Balazs Keri3b30d652018-10-19 13:32:20 +00007629ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7630 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7631 if (!Imp)
7632 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007633
Balazs Keri3b30d652018-10-19 13:32:20 +00007634 QualType ToType;
7635 Expr *ToCommonExpr, *ToSubExpr;
7636 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007637
Balazs Keri3b30d652018-10-19 13:32:20 +00007638 return new (Importer.getToContext()) ArrayInitLoopExpr(
7639 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007640}
7641
Balazs Keri3b30d652018-10-19 13:32:20 +00007642ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7643 ExpectedType ToTypeOrErr = import(E->getType());
7644 if (!ToTypeOrErr)
7645 return ToTypeOrErr.takeError();
7646 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007647}
7648
Balazs Keri3b30d652018-10-19 13:32:20 +00007649ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7650 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7651 if (!ToBeginLocOrErr)
7652 return ToBeginLocOrErr.takeError();
7653
7654 auto ToFieldOrErr = import(E->getField());
7655 if (!ToFieldOrErr)
7656 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007657
Eric Fiselier708afb52019-05-16 21:04:15 +00007658 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7659 if (!UsedContextOrErr)
7660 return UsedContextOrErr.takeError();
7661
Sean Callanandd2c1742016-05-16 20:48:03 +00007662 return CXXDefaultInitExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00007663 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007664}
7665
Balazs Keri3b30d652018-10-19 13:32:20 +00007666ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7667 auto Imp = importSeq(
7668 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7669 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7670 if (!Imp)
7671 return Imp.takeError();
7672
7673 QualType ToType;
7674 Expr *ToSubExpr;
7675 TypeSourceInfo *ToTypeInfoAsWritten;
7676 SourceLocation ToOperatorLoc, ToRParenLoc;
7677 SourceRange ToAngleBrackets;
7678 std::tie(
7679 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7680 ToAngleBrackets) = *Imp;
7681
Sean Callanandd2c1742016-05-16 20:48:03 +00007682 ExprValueKind VK = E->getValueKind();
7683 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007684 auto ToBasePathOrErr = ImportCastPath(E);
7685 if (!ToBasePathOrErr)
7686 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007687
Sean Callanandd2c1742016-05-16 20:48:03 +00007688 if (isa<CXXStaticCastExpr>(E)) {
7689 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007690 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7691 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007692 } else if (isa<CXXDynamicCastExpr>(E)) {
7693 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007694 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7695 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007696 } else if (isa<CXXReinterpretCastExpr>(E)) {
7697 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007698 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7699 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007700 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007701 return CXXConstCastExpr::Create(
7702 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7703 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007704 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007705 llvm_unreachable("Unknown cast type");
7706 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007707 }
7708}
7709
Balazs Keri3b30d652018-10-19 13:32:20 +00007710ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007711 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007712 auto Imp = importSeq(
7713 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7714 if (!Imp)
7715 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007716
Balazs Keri3b30d652018-10-19 13:32:20 +00007717 QualType ToType;
7718 SourceLocation ToExprLoc;
7719 NonTypeTemplateParmDecl *ToParameter;
7720 Expr *ToReplacement;
7721 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007722
7723 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007724 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007725}
7726
Balazs Keri3b30d652018-10-19 13:32:20 +00007727ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7728 auto Imp = importSeq(
7729 E->getType(), E->getBeginLoc(), E->getEndLoc());
7730 if (!Imp)
7731 return Imp.takeError();
7732
7733 QualType ToType;
7734 SourceLocation ToBeginLoc, ToEndLoc;
7735 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007736
7737 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007738 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7739 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007740
7741 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7742 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007743 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007744
7745 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007746 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7747 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007748}
7749
Balazs Keri3b30d652018-10-19 13:32:20 +00007750ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7751 ExpectedType ToTypeOrErr = import(E->getType());
7752 if (!ToTypeOrErr)
7753 return ToTypeOrErr.takeError();
7754
7755 auto ToSourceRangeOrErr = import(E->getSourceRange());
7756 if (!ToSourceRangeOrErr)
7757 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007758
7759 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007760 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7761 return new (Importer.getToContext()) CXXTypeidExpr(
7762 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7763 else
7764 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007765 }
7766
Balazs Keri3b30d652018-10-19 13:32:20 +00007767 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7768 if (!ToExprOperandOrErr)
7769 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007770
Balazs Keri3b30d652018-10-19 13:32:20 +00007771 return new (Importer.getToContext()) CXXTypeidExpr(
7772 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007773}
7774
Lang Hames19e07e12017-06-20 21:06:00 +00007775void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7776 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007777 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7778 if (auto ImportedOrErr = import(FromOverriddenMethod))
7779 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7780 (*ImportedOrErr)->getCanonicalDecl()));
7781 else
7782 consumeError(ImportedOrErr.takeError());
7783 }
Lang Hames19e07e12017-06-20 21:06:00 +00007784}
7785
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007786ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007787 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007788 bool MinimalImport,
Gabor Marton2afbfb62019-07-01 15:37:07 +00007789 std::shared_ptr<ASTImporterSharedState> SharedState)
7790 : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007791 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Ilya Biryukovabc744d2019-07-18 15:43:26 +00007792 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007793
Gabor Marton2afbfb62019-07-01 15:37:07 +00007794 // Create a default state without the lookup table: LLDB case.
7795 if (!SharedState) {
7796 this->SharedState = std::make_shared<ASTImporterSharedState>();
7797 }
7798
Gabor Marton54058b52018-12-17 13:53:12 +00007799 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7800 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007801}
7802
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007803ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007804
Gabor Marton54058b52018-12-17 13:53:12 +00007805Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7806 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7807 "Try to get field index for non-field.");
7808
7809 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7810 if (!Owner)
7811 return None;
7812
7813 unsigned Index = 0;
7814 for (const auto *D : Owner->decls()) {
7815 if (D == F)
7816 return Index;
7817
7818 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7819 ++Index;
7820 }
7821
7822 llvm_unreachable("Field was not found in its parent context.");
7823
7824 return None;
7825}
7826
7827ASTImporter::FoundDeclsTy
7828ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7829 // We search in the redecl context because of transparent contexts.
7830 // E.g. a simple C language enum is a transparent context:
7831 // enum E { A, B };
7832 // Now if we had a global variable in the TU
7833 // int A;
7834 // then the enum constant 'A' and the variable 'A' violates ODR.
7835 // We can diagnose this only if we search in the redecl context.
7836 DeclContext *ReDC = DC->getRedeclContext();
Gabor Marton2afbfb62019-07-01 15:37:07 +00007837 if (SharedState->getLookupTable()) {
Gabor Marton54058b52018-12-17 13:53:12 +00007838 ASTImporterLookupTable::LookupResult LookupResult =
Gabor Marton2afbfb62019-07-01 15:37:07 +00007839 SharedState->getLookupTable()->lookup(ReDC, Name);
Gabor Marton54058b52018-12-17 13:53:12 +00007840 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7841 } else {
Gabor Martonaefcf512019-07-17 13:47:46 +00007842 DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name);
7843 FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end());
7844 // We must search by the slow case of localUncachedLookup because that is
7845 // working even if there is no LookupPtr for the DC. We could use
7846 // DC::buildLookup() to create the LookupPtr, but that would load external
7847 // decls again, we must avoid that case.
7848 // Also, even if we had the LookupPtr, we must find Decls which are not
7849 // in the LookupPtr, so we need the slow case.
7850 // These cases are handled in ASTImporterLookupTable, but we cannot use
7851 // that with LLDB since that traverses through the AST which initiates the
7852 // load of external decls again via DC::decls(). And again, we must avoid
7853 // loading external decls during the import.
7854 if (Result.empty())
7855 ReDC->localUncachedLookup(Name, Result);
Gabor Marton54058b52018-12-17 13:53:12 +00007856 return Result;
7857 }
7858}
7859
7860void ASTImporter::AddToLookupTable(Decl *ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00007861 SharedState->addDeclToLookup(ToD);
Gabor Marton54058b52018-12-17 13:53:12 +00007862}
7863
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007864Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7865 // Import the decl using ASTNodeImporter.
7866 ASTNodeImporter Importer(*this);
7867 return Importer.Visit(FromD);
7868}
7869
7870void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7871 MapImported(FromD, ToD);
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007872}
7873
Gabor Marton5ac6d492019-05-15 10:29:48 +00007874Expected<QualType> ASTImporter::Import(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007875 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007876 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007877
Balazs Keri3b30d652018-10-19 13:32:20 +00007878 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007879
7880 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007881 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007882 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007883 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007884 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007885
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007886 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007887 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007888 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007889 if (!ToTOrErr)
7890 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007891
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007892 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007893 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007894
Balazs Keri3b30d652018-10-19 13:32:20 +00007895 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007896}
7897
Gabor Marton5ac6d492019-05-15 10:29:48 +00007898Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007899 if (!FromTSI)
7900 return FromTSI;
7901
7902 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007903 // on the type and a single location. Implement a real version of this.
Gabor Marton5ac6d492019-05-15 10:29:48 +00007904 ExpectedType TOrErr = Import(FromTSI->getType());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007905 if (!TOrErr)
7906 return TOrErr.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00007907 ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007908 if (!BeginLocOrErr)
7909 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007910
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007911 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7912}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007913
Gabor Marton5ac6d492019-05-15 10:29:48 +00007914Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007915 Attr *ToAttr = FromAttr->clone(ToContext);
Gabor Marton5ac6d492019-05-15 10:29:48 +00007916 if (auto ToRangeOrErr = Import(FromAttr->getRange()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007917 ToAttr->setRange(*ToRangeOrErr);
7918 else
7919 return ToRangeOrErr.takeError();
7920
Davide Italianofaee83d2018-11-28 19:15:23 +00007921 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007922}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007923
Gabor Martonbe77a982018-12-12 11:22:55 +00007924Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7925 auto Pos = ImportedDecls.find(FromD);
7926 if (Pos != ImportedDecls.end())
7927 return Pos->second;
7928 else
Sean Callanan59721b32015-04-28 18:41:46 +00007929 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007930}
7931
Gabor Marton458d1452019-02-14 13:07:03 +00007932TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7933 auto FromDPos = ImportedFromDecls.find(ToD);
7934 if (FromDPos == ImportedFromDecls.end())
7935 return nullptr;
7936 return FromDPos->second->getTranslationUnitDecl();
7937}
7938
Gabor Marton5ac6d492019-05-15 10:29:48 +00007939Expected<Decl *> ASTImporter::Import(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007940 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007941 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007942
Gabor Marton1ad4b992019-07-01 14:19:53 +00007943 // Push FromD to the stack, and remove that when we return.
7944 ImportPath.push(FromD);
7945 auto ImportPathBuilder =
7946 llvm::make_scope_exit([this]() { ImportPath.pop(); });
Douglas Gregord451ea92011-07-29 23:31:30 +00007947
Gabor Marton303c98612019-06-25 08:00:51 +00007948 // Check whether there was a previous failed import.
7949 // If yes return the existing error.
7950 if (auto Error = getImportDeclErrorIfAny(FromD))
7951 return make_error<ImportError>(*Error);
7952
Gabor Marton26f72a92018-07-12 09:42:05 +00007953 // Check whether we've already imported this declaration.
7954 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7955 if (ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00007956 // Already imported (possibly from another TU) and with an error.
7957 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
7958 setImportDeclError(FromD, *Error);
7959 return make_error<ImportError>(*Error);
7960 }
7961
Gabor Marton26f72a92018-07-12 09:42:05 +00007962 // If FromD has some updated flags after last import, apply it
7963 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00007964 // If we encounter a cycle during an import then we save the relevant part
7965 // of the import path associated to the Decl.
7966 if (ImportPath.hasCycleAtBack())
7967 SavedImportPaths[FromD].push_back(ImportPath.copyCycleAtBack());
Douglas Gregord451ea92011-07-29 23:31:30 +00007968 return ToD;
7969 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007970
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007971 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007972 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Gabor Marton303c98612019-06-25 08:00:51 +00007973 if (!ToDOrErr) {
7974 // Failed to import.
7975
7976 auto Pos = ImportedDecls.find(FromD);
7977 if (Pos != ImportedDecls.end()) {
7978 // Import failed after the object was created.
7979 // Remove all references to it.
7980 auto *ToD = Pos->second;
7981 ImportedDecls.erase(Pos);
7982
7983 // ImportedDecls and ImportedFromDecls are not symmetric. It may happen
7984 // (e.g. with namespaces) that several decls from the 'from' context are
7985 // mapped to the same decl in the 'to' context. If we removed entries
7986 // from the LookupTable here then we may end up removing them multiple
7987 // times.
7988
7989 // The Lookuptable contains decls only which are in the 'to' context.
7990 // Remove from the Lookuptable only if it is *imported* into the 'to'
7991 // context (and do not remove it if it was added during the initial
7992 // traverse of the 'to' context).
7993 auto PosF = ImportedFromDecls.find(ToD);
7994 if (PosF != ImportedFromDecls.end()) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00007995 SharedState->removeDeclFromLookup(ToD);
Gabor Marton303c98612019-06-25 08:00:51 +00007996 ImportedFromDecls.erase(PosF);
7997 }
7998
7999 // FIXME: AST may contain remaining references to the failed object.
Gabor Marton2afbfb62019-07-01 15:37:07 +00008000 // However, the ImportDeclErrors in the shared state contains all the
8001 // failed objects together with their error.
Gabor Marton303c98612019-06-25 08:00:51 +00008002 }
8003
Gabor Marton2afbfb62019-07-01 15:37:07 +00008004 // Error encountered for the first time.
8005 // After takeError the error is not usable any more in ToDOrErr.
Gabor Marton303c98612019-06-25 08:00:51 +00008006 // Get a copy of the error object (any more simple solution for this?).
8007 ImportError ErrOut;
8008 handleAllErrors(ToDOrErr.takeError(),
8009 [&ErrOut](const ImportError &E) { ErrOut = E; });
8010 setImportDeclError(FromD, ErrOut);
Gabor Marton2afbfb62019-07-01 15:37:07 +00008011 // Set the error for the mapped to Decl, which is in the "to" context.
8012 if (Pos != ImportedDecls.end())
8013 SharedState->setImportDeclError(Pos->second, ErrOut);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008014
8015 // Set the error for all nodes which have been created before we
8016 // recognized the error.
8017 for (const auto &Path : SavedImportPaths[FromD])
Gabor Marton2afbfb62019-07-01 15:37:07 +00008018 for (Decl *FromDi : Path) {
8019 setImportDeclError(FromDi, ErrOut);
8020 //FIXME Should we remove these Decls from ImportedDecls?
8021 // Set the error for the mapped to Decl, which is in the "to" context.
8022 auto Ii = ImportedDecls.find(FromDi);
8023 if (Ii != ImportedDecls.end())
8024 SharedState->setImportDeclError(Ii->second, ErrOut);
8025 // FIXME Should we remove these Decls from the LookupTable,
8026 // and from ImportedFromDecls?
8027 }
Gabor Marton1ad4b992019-07-01 14:19:53 +00008028 SavedImportPaths[FromD].clear();
8029
Gabor Marton303c98612019-06-25 08:00:51 +00008030 // Do not return ToDOrErr, error was taken out of it.
8031 return make_error<ImportError>(ErrOut);
8032 }
8033
Balazs Keri3b30d652018-10-19 13:32:20 +00008034 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008035
Gabor Marton303c98612019-06-25 08:00:51 +00008036 // FIXME: Handle the "already imported with error" case. We can get here
8037 // nullptr only if GetImportedOrCreateDecl returned nullptr (after a
8038 // previously failed create was requested).
8039 // Later GetImportedOrCreateDecl can be updated to return the error.
Gabor Marton7f8c4002019-03-19 13:34:10 +00008040 if (!ToD) {
Gabor Marton303c98612019-06-25 08:00:51 +00008041 auto Err = getImportDeclErrorIfAny(FromD);
8042 assert(Err);
8043 return make_error<ImportError>(*Err);
Gabor Marton7f8c4002019-03-19 13:34:10 +00008044 }
8045
Gabor Marton2afbfb62019-07-01 15:37:07 +00008046 // We could import from the current TU without error. But previously we
8047 // already had imported a Decl as `ToD` from another TU (with another
8048 // ASTImporter object) and with an error.
8049 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
8050 setImportDeclError(FromD, *Error);
8051 return make_error<ImportError>(*Error);
8052 }
8053
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008054 // Make sure that ImportImpl registered the imported decl.
8055 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
Gabor Marton2afbfb62019-07-01 15:37:07 +00008056
Gabor Marton26f72a92018-07-12 09:42:05 +00008057 // Notify subclasses.
8058 Imported(FromD, ToD);
8059
Gabor Martonac3a5d62018-09-17 12:04:52 +00008060 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008061 SavedImportPaths[FromD].clear();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008062 return ToDOrErr;
8063}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008064
Balazs Keri3b30d652018-10-19 13:32:20 +00008065Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008066 if (!FromDC)
8067 return FromDC;
8068
Gabor Marton5ac6d492019-05-15 10:29:48 +00008069 ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC));
Balazs Keria1f6b102019-04-08 13:59:15 +00008070 if (!ToDCOrErr)
8071 return ToDCOrErr.takeError();
8072 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00008073
Fangrui Song6907ce22018-07-30 19:24:48 +00008074 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00008075 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008076 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
8077 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008078 if (ToRecord->isCompleteDefinition()) {
8079 // Do nothing.
8080 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008081 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8082 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
8083 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008084 } else {
8085 CompleteDecl(ToRecord);
8086 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008087 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
8088 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008089 if (ToEnum->isCompleteDefinition()) {
8090 // Do nothing.
8091 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008092 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8093 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
8094 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008095 } else {
8096 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00008097 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008098 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
8099 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008100 if (ToClass->getDefinition()) {
8101 // Do nothing.
8102 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008103 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8104 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
8105 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008106 } else {
8107 CompleteDecl(ToClass);
8108 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008109 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
8110 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008111 if (ToProto->getDefinition()) {
8112 // Do nothing.
8113 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008114 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8115 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
8116 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008117 } else {
8118 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00008119 }
Douglas Gregor95d82832012-01-24 18:36:04 +00008120 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008121
Douglas Gregor95d82832012-01-24 18:36:04 +00008122 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008123}
8124
Gabor Marton5ac6d492019-05-15 10:29:48 +00008125Expected<Expr *> ASTImporter::Import(Expr *FromE) {
8126 if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE)))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008127 return cast_or_null<Expr>(*ToSOrErr);
8128 else
8129 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00008130}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008131
Gabor Marton5ac6d492019-05-15 10:29:48 +00008132Expected<Stmt *> ASTImporter::Import(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008133 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00008134 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008135
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008136 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008137 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
8138 if (Pos != ImportedStmts.end())
8139 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00008140
Balazs Keri3b30d652018-10-19 13:32:20 +00008141 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008142 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008143 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008144 if (!ToSOrErr)
8145 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008146
Balazs Keri3b30d652018-10-19 13:32:20 +00008147 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00008148 auto *FromE = cast<Expr>(FromS);
8149 // Copy ExprBitfields, which may not be handled in Expr subclasses
8150 // constructors.
8151 ToE->setValueKind(FromE->getValueKind());
8152 ToE->setObjectKind(FromE->getObjectKind());
8153 ToE->setTypeDependent(FromE->isTypeDependent());
8154 ToE->setValueDependent(FromE->isValueDependent());
8155 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
8156 ToE->setContainsUnexpandedParameterPack(
8157 FromE->containsUnexpandedParameterPack());
8158 }
8159
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008160 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00008161 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008162 return ToSOrErr;
8163}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008164
Balazs Keri4a3d7582018-11-27 18:36:31 +00008165Expected<NestedNameSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008166ASTImporter::Import(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008167 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00008168 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008169
Simon Pilgrim130df2c2019-07-15 13:00:43 +00008170 NestedNameSpecifier *Prefix = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008171 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
8172 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00008173
8174 switch (FromNNS->getKind()) {
8175 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008176 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
8177 return NestedNameSpecifier::Create(ToContext, Prefix,
8178 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00008179
8180 case NestedNameSpecifier::Namespace:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008181 if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008182 return NestedNameSpecifier::Create(ToContext, Prefix,
8183 cast<NamespaceDecl>(*NSOrErr));
8184 } else
8185 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008186
8187 case NestedNameSpecifier::NamespaceAlias:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008188 if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008189 return NestedNameSpecifier::Create(ToContext, Prefix,
8190 cast<NamespaceAliasDecl>(*NSADOrErr));
8191 else
8192 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008193
8194 case NestedNameSpecifier::Global:
8195 return NestedNameSpecifier::GlobalSpecifier(ToContext);
8196
Nikola Smiljanic67860242014-09-26 00:28:20 +00008197 case NestedNameSpecifier::Super:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008198 if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008199 return NestedNameSpecifier::SuperSpecifier(ToContext,
8200 cast<CXXRecordDecl>(*RDOrErr));
8201 else
8202 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008203
Douglas Gregor90ebf252011-04-27 16:48:40 +00008204 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008205 case NestedNameSpecifier::TypeSpecWithTemplate:
8206 if (Expected<QualType> TyOrErr =
Gabor Marton5ac6d492019-05-15 10:29:48 +00008207 Import(QualType(FromNNS->getAsType(), 0u))) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008208 bool TSTemplate =
8209 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
8210 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
8211 TyOrErr->getTypePtr());
8212 } else {
8213 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008214 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008215 }
8216
8217 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008218}
8219
Balazs Keri4a3d7582018-11-27 18:36:31 +00008220Expected<NestedNameSpecifierLoc>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008221ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008222 // Copied from NestedNameSpecifier mostly.
8223 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8224 NestedNameSpecifierLoc NNS = FromNNS;
8225
8226 // Push each of the nested-name-specifiers's onto a stack for
8227 // serialization in reverse order.
8228 while (NNS) {
8229 NestedNames.push_back(NNS);
8230 NNS = NNS.getPrefix();
8231 }
8232
8233 NestedNameSpecifierLocBuilder Builder;
8234
8235 while (!NestedNames.empty()) {
8236 NNS = NestedNames.pop_back_val();
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00008237 NestedNameSpecifier *Spec = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008238 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8239 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008240
8241 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008242
8243 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8244 if (Kind != NestedNameSpecifier::Super) {
8245 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8246 return std::move(Err);
8247
8248 if (Kind != NestedNameSpecifier::Global)
8249 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8250 return std::move(Err);
8251 }
8252
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008253 switch (Kind) {
8254 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008255 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8256 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008257 break;
8258
8259 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008260 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8261 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008262 break;
8263
8264 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008265 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8266 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008267 break;
8268
8269 case NestedNameSpecifier::TypeSpec:
8270 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008271 SourceLocation ToTLoc;
8272 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8273 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008274 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008275 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008276 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8277 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008278 break;
8279 }
8280
8281 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008282 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008283 break;
8284
8285 case NestedNameSpecifier::Super: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008286 auto ToSourceRangeOrErr = Import(NNS.getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008287 if (!ToSourceRangeOrErr)
8288 return ToSourceRangeOrErr.takeError();
8289
8290 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8291 ToSourceRangeOrErr->getBegin(),
8292 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008293 }
8294 }
8295 }
8296
8297 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008298}
8299
Gabor Marton5ac6d492019-05-15 10:29:48 +00008300Expected<TemplateName> ASTImporter::Import(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008301 switch (From.getKind()) {
8302 case TemplateName::Template:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008303 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008304 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8305 else
8306 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008307
Douglas Gregore2e50d332010-12-01 01:36:18 +00008308 case TemplateName::OverloadedTemplate: {
8309 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8310 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008311 for (auto *I : *FromStorage) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008312 if (auto ToOrErr = Import(I))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008313 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008314 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008315 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008316 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008317 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008318 ToTemplates.end());
8319 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008320
Richard Smithb23c5e82019-05-09 03:31:27 +00008321 case TemplateName::AssumedTemplate: {
8322 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008323 auto DeclNameOrErr = Import(FromStorage->getDeclName());
Richard Smithb23c5e82019-05-09 03:31:27 +00008324 if (!DeclNameOrErr)
8325 return DeclNameOrErr.takeError();
8326 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8327 }
8328
Douglas Gregore2e50d332010-12-01 01:36:18 +00008329 case TemplateName::QualifiedTemplate: {
8330 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008331 auto QualifierOrErr = Import(QTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008332 if (!QualifierOrErr)
8333 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008334
Gabor Marton5ac6d492019-05-15 10:29:48 +00008335 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008336 return ToContext.getQualifiedTemplateName(
8337 *QualifierOrErr, QTN->hasTemplateKeyword(),
8338 cast<TemplateDecl>(*ToTemplateOrErr));
8339 else
8340 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008341 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008342
Douglas Gregore2e50d332010-12-01 01:36:18 +00008343 case TemplateName::DependentTemplate: {
8344 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008345 auto QualifierOrErr = Import(DTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008346 if (!QualifierOrErr)
8347 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008348
Douglas Gregore2e50d332010-12-01 01:36:18 +00008349 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008350 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008351 Import(DTN->getIdentifier()));
8352 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008353
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008354 return ToContext.getDependentTemplateName(*QualifierOrErr,
8355 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008356 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008357
8358 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008359 SubstTemplateTemplateParmStorage *Subst =
8360 From.getAsSubstTemplateTemplateParm();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008361 ExpectedDecl ParamOrErr = Import(Subst->getParameter());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008362 if (!ParamOrErr)
8363 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008364
Gabor Marton5ac6d492019-05-15 10:29:48 +00008365 auto ReplacementOrErr = Import(Subst->getReplacement());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008366 if (!ReplacementOrErr)
8367 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008368
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008369 return ToContext.getSubstTemplateTemplateParm(
8370 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008371 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008372
Douglas Gregor5590be02011-01-15 06:45:20 +00008373 case TemplateName::SubstTemplateTemplateParmPack: {
8374 SubstTemplateTemplateParmPackStorage *SubstPack
8375 = From.getAsSubstTemplateTemplateParmPack();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008376 ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008377 if (!ParamOrErr)
8378 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008379
Douglas Gregor5590be02011-01-15 06:45:20 +00008380 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008381 auto ArgPackOrErr =
8382 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8383 if (!ArgPackOrErr)
8384 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008385
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008386 return ToContext.getSubstTemplateTemplateParmPack(
8387 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008388 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008389 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008390
Douglas Gregore2e50d332010-12-01 01:36:18 +00008391 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008392}
8393
Gabor Marton5ac6d492019-05-15 10:29:48 +00008394Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008395 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008396 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008397
Douglas Gregor811663e2010-02-10 00:15:17 +00008398 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008399 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008400
Douglas Gregor811663e2010-02-10 00:15:17 +00008401 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008402 Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008403 if (!ToFileIDOrErr)
8404 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008405 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008406 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8407}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008408
Gabor Marton5ac6d492019-05-15 10:29:48 +00008409Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008410 SourceLocation ToBegin, ToEnd;
8411 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8412 return std::move(Err);
8413 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8414 return std::move(Err);
8415
8416 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008417}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008418
Gabor Marton5ac6d492019-05-15 10:29:48 +00008419Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008420 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008421 if (Pos != ImportedFileIDs.end())
8422 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008423
Douglas Gregor811663e2010-02-10 00:15:17 +00008424 SourceManager &FromSM = FromContext.getSourceManager();
8425 SourceManager &ToSM = ToContext.getSourceManager();
8426 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008427
8428 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008429 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008430 if (FromSLoc.isExpansion()) {
8431 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008432 ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008433 if (!ToSpLoc)
8434 return ToSpLoc.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008435 ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008436 if (!ToExLocS)
8437 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008438 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8439 SourceLocation MLoc;
8440 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008441 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008442 } else {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008443 if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008444 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8445 FromEx.isExpansionTokenRange());
8446 else
8447 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008448 }
8449 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008450 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008451 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008452
8453 if (!IsBuiltin) {
8454 // Include location of this file.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008455 ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008456 if (!ToIncludeLoc)
8457 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008458
8459 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8460 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8461 // disk again
8462 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8463 // than mmap the files several times.
Harlan Haskins8d323d12019-08-01 21:31:56 +00008464 auto Entry =
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008465 ToFileManager.getFile(Cache->OrigEntry->getName());
8466 // FIXME: The filename may be a virtual name that does probably not
8467 // point to a valid file and we get no Entry here. In this case try with
8468 // the memory buffer below.
8469 if (Entry)
Harlan Haskins8d323d12019-08-01 21:31:56 +00008470 ToID = ToSM.createFileID(*Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008471 FromSLoc.getFile().getFileCharacteristic());
8472 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008473 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008474
8475 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008476 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008477 bool Invalid = true;
8478 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8479 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8480 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008481 // FIXME: Use a new error kind?
8482 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008483
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008484 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8485 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8486 FromBuf->getBufferIdentifier());
8487 ToID = ToSM.createFileID(std::move(ToBuf),
8488 FromSLoc.getFile().getFileCharacteristic());
8489 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008490 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008491
Balazs Keri9cf39df2019-02-27 16:31:48 +00008492 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8493
Sebastian Redl99219f12010-09-30 01:03:06 +00008494 ImportedFileIDs[FromID] = ToID;
Balazs Kerid22f8772019-07-24 10:16:37 +00008495
8496 if (FileIDImportHandler)
8497 FileIDImportHandler(ToID, FromID);
8498
Douglas Gregor811663e2010-02-10 00:15:17 +00008499 return ToID;
8500}
8501
Gabor Marton5ac6d492019-05-15 10:29:48 +00008502Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) {
8503 ExpectedExpr ToExprOrErr = Import(From->getInit());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008504 if (!ToExprOrErr)
8505 return ToExprOrErr.takeError();
8506
Gabor Marton5ac6d492019-05-15 10:29:48 +00008507 auto LParenLocOrErr = Import(From->getLParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008508 if (!LParenLocOrErr)
8509 return LParenLocOrErr.takeError();
8510
Gabor Marton5ac6d492019-05-15 10:29:48 +00008511 auto RParenLocOrErr = Import(From->getRParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008512 if (!RParenLocOrErr)
8513 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008514
8515 if (From->isBaseInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008516 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008517 if (!ToTInfoOrErr)
8518 return ToTInfoOrErr.takeError();
8519
8520 SourceLocation EllipsisLoc;
8521 if (From->isPackExpansion())
8522 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8523 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008524
8525 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008526 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8527 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008528 } else if (From->isMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008529 ExpectedDecl ToFieldOrErr = Import(From->getMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008530 if (!ToFieldOrErr)
8531 return ToFieldOrErr.takeError();
8532
Gabor Marton5ac6d492019-05-15 10:29:48 +00008533 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008534 if (!MemberLocOrErr)
8535 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008536
8537 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008538 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8539 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008540 } else if (From->isIndirectMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008541 ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008542 if (!ToIFieldOrErr)
8543 return ToIFieldOrErr.takeError();
8544
Gabor Marton5ac6d492019-05-15 10:29:48 +00008545 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008546 if (!MemberLocOrErr)
8547 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008548
8549 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008550 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8551 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008552 } else if (From->isDelegatingInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008553 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008554 if (!ToTInfoOrErr)
8555 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008556
8557 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008558 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8559 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008560 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008561 // FIXME: assert?
8562 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008563 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008564}
Sean Callanandd2c1742016-05-16 20:48:03 +00008565
Balazs Keri4a3d7582018-11-27 18:36:31 +00008566Expected<CXXBaseSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008567ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008568 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8569 if (Pos != ImportedCXXBaseSpecifiers.end())
8570 return Pos->second;
8571
Gabor Marton5ac6d492019-05-15 10:29:48 +00008572 Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008573 if (!ToSourceRange)
8574 return ToSourceRange.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008575 Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008576 if (!ToTSI)
8577 return ToTSI.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008578 ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008579 if (!ToEllipsisLoc)
8580 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008581 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008582 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8583 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008584 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8585 return Imported;
8586}
8587
Gabor Marton5ac6d492019-05-15 10:29:48 +00008588Error ASTImporter::ImportDefinition(Decl *From) {
8589 ExpectedDecl ToOrErr = Import(From);
8590 if (!ToOrErr)
8591 return ToOrErr.takeError();
8592 Decl *To = *ToOrErr;
Fangrui Song6907ce22018-07-30 19:24:48 +00008593
Don Hintonf170dff2019-03-19 06:14:14 +00008594 auto *FromDC = cast<DeclContext>(From);
8595 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008596
Don Hintonf170dff2019-03-19 06:14:14 +00008597 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8598 if (!ToRecord->getDefinition()) {
8599 return Importer.ImportDefinition(
8600 cast<RecordDecl>(FromDC), ToRecord,
8601 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008602 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008603 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008604
Don Hintonf170dff2019-03-19 06:14:14 +00008605 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8606 if (!ToEnum->getDefinition()) {
8607 return Importer.ImportDefinition(
8608 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8609 }
8610 }
8611
8612 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8613 if (!ToIFace->getDefinition()) {
8614 return Importer.ImportDefinition(
8615 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8616 ASTNodeImporter::IDK_Everything);
8617 }
8618 }
8619
8620 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8621 if (!ToProto->getDefinition()) {
8622 return Importer.ImportDefinition(
8623 cast<ObjCProtocolDecl>(FromDC), ToProto,
8624 ASTNodeImporter::IDK_Everything);
8625 }
8626 }
8627
8628 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008629}
8630
Gabor Marton5ac6d492019-05-15 10:29:48 +00008631Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008632 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008633 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008634
8635 switch (FromName.getNameKind()) {
8636 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008637 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008638
8639 case DeclarationName::ObjCZeroArgSelector:
8640 case DeclarationName::ObjCOneArgSelector:
8641 case DeclarationName::ObjCMultiArgSelector:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008642 if (auto ToSelOrErr = Import(FromName.getObjCSelector()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008643 return DeclarationName(*ToSelOrErr);
8644 else
8645 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008646
8647 case DeclarationName::CXXConstructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008648 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008649 return ToContext.DeclarationNames.getCXXConstructorName(
8650 ToContext.getCanonicalType(*ToTyOrErr));
8651 else
8652 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008653 }
8654
8655 case DeclarationName::CXXDestructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008656 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008657 return ToContext.DeclarationNames.getCXXDestructorName(
8658 ToContext.getCanonicalType(*ToTyOrErr));
8659 else
8660 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008661 }
8662
Richard Smith35845152017-02-07 01:37:30 +00008663 case DeclarationName::CXXDeductionGuideName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008664 if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008665 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8666 cast<TemplateDecl>(*ToTemplateOrErr));
8667 else
8668 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008669 }
8670
Douglas Gregor96e578d2010-02-05 17:54:41 +00008671 case DeclarationName::CXXConversionFunctionName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008672 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008673 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8674 ToContext.getCanonicalType(*ToTyOrErr));
8675 else
8676 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008677 }
8678
8679 case DeclarationName::CXXOperatorName:
8680 return ToContext.DeclarationNames.getCXXOperatorName(
8681 FromName.getCXXOverloadedOperator());
8682
8683 case DeclarationName::CXXLiteralOperatorName:
8684 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008685 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008686
8687 case DeclarationName::CXXUsingDirective:
8688 // FIXME: STATICS!
8689 return DeclarationName::getUsingDirectiveName();
8690 }
8691
David Blaikiee4d798f2012-01-20 21:50:17 +00008692 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008693}
8694
Douglas Gregore2e50d332010-12-01 01:36:18 +00008695IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008696 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008697 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008698
Sean Callananf94ef1d2016-05-14 06:11:19 +00008699 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8700
8701 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8702 ToId->setBuiltinID(FromId->getBuiltinID());
8703
8704 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008705}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008706
Gabor Marton5ac6d492019-05-15 10:29:48 +00008707Expected<Selector> ASTImporter::Import(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008708 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008709 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008710
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008711 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008712 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8713 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8714 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8715 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8716}
8717
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008718DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8719 DeclContext *DC,
8720 unsigned IDNS,
8721 NamedDecl **Decls,
8722 unsigned NumDecls) {
8723 return Name;
8724}
8725
8726DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008727 if (LastDiagFromFrom)
8728 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8729 FromContext.getDiagnostics());
8730 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008731 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008732}
8733
8734DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008735 if (!LastDiagFromFrom)
8736 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8737 ToContext.getDiagnostics());
8738 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008739 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008740}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008741
Douglas Gregor2e15c842012-02-01 21:00:38 +00008742void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008743 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008744 if (!ID->getDefinition())
8745 ID->startDefinition();
8746 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008747 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008748 if (!PD->getDefinition())
8749 PD->startDefinition();
8750 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008751 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008752 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8753 TD->startDefinition();
8754 TD->setCompleteDefinition(true);
8755 }
8756 }
8757 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008758 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008759 }
8760}
8761
Gabor Marton26f72a92018-07-12 09:42:05 +00008762Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8763 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8764 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8765 "Try to import an already imported Decl");
8766 if (Pos != ImportedDecls.end())
8767 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008768 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008769 // This mapping should be maintained only in this function. Therefore do not
8770 // check for additional consistency.
8771 ImportedFromDecls[To] = From;
Gabor Marton303c98612019-06-25 08:00:51 +00008772 AddToLookupTable(To);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008773 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008774}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008775
Gabor Marton303c98612019-06-25 08:00:51 +00008776llvm::Optional<ImportError>
8777ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const {
8778 auto Pos = ImportDeclErrors.find(FromD);
8779 if (Pos != ImportDeclErrors.end())
8780 return Pos->second;
8781 else
8782 return Optional<ImportError>();
8783}
8784
8785void ASTImporter::setImportDeclError(Decl *From, ImportError Error) {
Gabor Marton1ad4b992019-07-01 14:19:53 +00008786 auto InsertRes = ImportDeclErrors.insert({From, Error});
Benjamin Kramer4f769362019-07-01 14:33:26 +00008787 (void)InsertRes;
Gabor Marton1ad4b992019-07-01 14:19:53 +00008788 // Either we set the error for the first time, or we already had set one and
8789 // now we want to set the same error.
8790 assert(InsertRes.second || InsertRes.first->second.Error == Error.Error);
Gabor Marton303c98612019-06-25 08:00:51 +00008791}
8792
Douglas Gregordd6006f2012-07-17 21:16:27 +00008793bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8794 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008795 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8796 ImportedTypes.find(From.getTypePtr());
8797 if (Pos != ImportedTypes.end()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008798 if (ExpectedType ToFromOrErr = Import(From)) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008799 if (ToContext.hasSameType(*ToFromOrErr, To))
8800 return true;
8801 } else {
8802 llvm::consumeError(ToFromOrErr.takeError());
8803 }
8804 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008805
Douglas Gregordd6006f2012-07-17 21:16:27 +00008806 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008807 getStructuralEquivalenceKind(*this), false,
8808 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008809 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008810}