blob: 9d5dd84161dec4af2d87fe7076a1400152c67c8c [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor96e578d2010-02-05 17:54:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTImporter class which imports AST nodes from one
10// context into another context.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000013
Douglas Gregor96e578d2010-02-05 17:54:41 +000014#include "clang/AST/ASTImporter.h"
Gabor Marton2afbfb62019-07-01 15:37:07 +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"
Gabor Marton17c3eaf2019-07-01 12:44:39 +000060#include "llvm/ADT/ScopeExit.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000061#include "llvm/ADT/STLExtras.h"
62#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"
John McCalle314e272011-10-18 21:02:43 +0000962#define SHARED_SINGLETON_TYPE(Expansion)
963#define BUILTIN_TYPE(Id, SingletonId) \
964 case BuiltinType::Id: return Importer.getToContext().SingletonId;
965#include "clang/AST/BuiltinTypes.def"
966
967 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
968 // context supports C++.
969
970 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
971 // context supports ObjC.
972
Douglas Gregor96e578d2010-02-05 17:54:41 +0000973 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000974 // The context we're importing from has an unsigned 'char'. If we're
975 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000976 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000977 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000978 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000979
Douglas Gregor96e578d2010-02-05 17:54:41 +0000980 return Importer.getToContext().CharTy;
981
Douglas Gregor96e578d2010-02-05 17:54:41 +0000982 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000983 // The context we're importing from has an unsigned 'char'. If we're
984 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000985 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000986 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000988
Douglas Gregor96e578d2010-02-05 17:54:41 +0000989 return Importer.getToContext().CharTy;
990
Chris Lattnerad3467e2010-12-25 23:25:43 +0000991 case BuiltinType::WChar_S:
992 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000993 // FIXME: If not in C++, shall we translate to the C equivalent of
994 // wchar_t?
995 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000996 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000997
998 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000999}
1000
Balazs Keri3b30d652018-10-19 13:32:20 +00001001ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1002 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1003 if (!ToOriginalTypeOrErr)
1004 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001005
Balazs Keri3b30d652018-10-19 13:32:20 +00001006 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001007}
1008
Balazs Keri3b30d652018-10-19 13:32:20 +00001009ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1010 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1011 if (!ToElementTypeOrErr)
1012 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001013
Balazs Keri3b30d652018-10-19 13:32:20 +00001014 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001015}
1016
Balazs Keri3b30d652018-10-19 13:32:20 +00001017ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1018 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1019 if (!ToPointeeTypeOrErr)
1020 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001021
Balazs Keri3b30d652018-10-19 13:32:20 +00001022 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001023}
1024
Balazs Keri3b30d652018-10-19 13:32:20 +00001025ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001026 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001027 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1028 if (!ToPointeeTypeOrErr)
1029 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001030
Balazs Keri3b30d652018-10-19 13:32:20 +00001031 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001032}
1033
Balazs Keri3b30d652018-10-19 13:32:20 +00001034ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001035ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001036 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001037 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1038 if (!ToPointeeTypeOrErr)
1039 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001040
Balazs Keri3b30d652018-10-19 13:32:20 +00001041 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001042}
1043
Balazs Keri3b30d652018-10-19 13:32:20 +00001044ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001045ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001046 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001047 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1048 if (!ToPointeeTypeOrErr)
1049 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001050
Balazs Keri3b30d652018-10-19 13:32:20 +00001051 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001052}
1053
Balazs Keri3b30d652018-10-19 13:32:20 +00001054ExpectedType
1055ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001056 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001057 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1058 if (!ToPointeeTypeOrErr)
1059 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001060
Balazs Keri3b30d652018-10-19 13:32:20 +00001061 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1062 if (!ClassTypeOrErr)
1063 return ClassTypeOrErr.takeError();
1064
1065 return Importer.getToContext().getMemberPointerType(
1066 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001067}
1068
Balazs Keri3b30d652018-10-19 13:32:20 +00001069ExpectedType
1070ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1071 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1072 if (!ToElementTypeOrErr)
1073 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Balazs Keri3b30d652018-10-19 13:32:20 +00001075 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001076 T->getSize(),
1077 T->getSizeModifier(),
1078 T->getIndexTypeCVRQualifiers());
1079}
1080
Balazs Keri3b30d652018-10-19 13:32:20 +00001081ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001082ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001083 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1084 if (!ToElementTypeOrErr)
1085 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001086
Balazs Keri3b30d652018-10-19 13:32:20 +00001087 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001088 T->getSizeModifier(),
1089 T->getIndexTypeCVRQualifiers());
1090}
1091
Balazs Keri3b30d652018-10-19 13:32:20 +00001092ExpectedType
1093ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1094 QualType ToElementType;
1095 Expr *ToSizeExpr;
1096 SourceRange ToBracketsRange;
1097 if (auto Imp = importSeq(
1098 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1099 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1100 else
1101 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001102
Balazs Keri3b30d652018-10-19 13:32:20 +00001103 return Importer.getToContext().getVariableArrayType(
1104 ToElementType, ToSizeExpr, T->getSizeModifier(),
1105 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001106}
1107
Balazs Keri3b30d652018-10-19 13:32:20 +00001108ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001109 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001110 QualType ToElementType;
1111 Expr *ToSizeExpr;
1112 SourceRange ToBracketsRange;
1113 if (auto Imp = importSeq(
1114 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1115 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1116 else
1117 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001118 // SizeExpr may be null if size is not specified directly.
1119 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001120
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001121 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001122 ToElementType, ToSizeExpr, T->getSizeModifier(),
1123 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001124}
1125
Balazs Keri3b30d652018-10-19 13:32:20 +00001126ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1127 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1128 if (!ToElementTypeOrErr)
1129 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001130
Balazs Keri3b30d652018-10-19 13:32:20 +00001131 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001132 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001133 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001134}
1135
Balazs Keri3b30d652018-10-19 13:32:20 +00001136ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1137 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1138 if (!ToElementTypeOrErr)
1139 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001140
Balazs Keri3b30d652018-10-19 13:32:20 +00001141 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001142 T->getNumElements());
1143}
1144
Balazs Keri3b30d652018-10-19 13:32:20 +00001145ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001146ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001147 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001148 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001149 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1150 if (!ToReturnTypeOrErr)
1151 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001152
Balazs Keri3b30d652018-10-19 13:32:20 +00001153 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001154 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001155}
1156
Balazs Keri3b30d652018-10-19 13:32:20 +00001157ExpectedType
1158ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1159 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1160 if (!ToReturnTypeOrErr)
1161 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001162
Douglas Gregor96e578d2010-02-05 17:54:41 +00001163 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001164 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001165 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001166 ExpectedType TyOrErr = import(A);
1167 if (!TyOrErr)
1168 return TyOrErr.takeError();
1169 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001170 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001171
Douglas Gregor96e578d2010-02-05 17:54:41 +00001172 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001173 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001174 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001175 ExpectedType TyOrErr = import(E);
1176 if (!TyOrErr)
1177 return TyOrErr.takeError();
1178 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001179 }
John McCalldb40c7f2010-12-14 08:05:40 +00001180
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001181 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1182 FunctionProtoType::ExtProtoInfo ToEPI;
1183
Balazs Keri3b30d652018-10-19 13:32:20 +00001184 auto Imp = importSeq(
1185 FromEPI.ExceptionSpec.NoexceptExpr,
1186 FromEPI.ExceptionSpec.SourceDecl,
1187 FromEPI.ExceptionSpec.SourceTemplate);
1188 if (!Imp)
1189 return Imp.takeError();
1190
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001191 ToEPI.ExtInfo = FromEPI.ExtInfo;
1192 ToEPI.Variadic = FromEPI.Variadic;
1193 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1194 ToEPI.TypeQuals = FromEPI.TypeQuals;
1195 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001196 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1197 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001198 std::tie(
1199 ToEPI.ExceptionSpec.NoexceptExpr,
1200 ToEPI.ExceptionSpec.SourceDecl,
1201 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001202
Balazs Keri3b30d652018-10-19 13:32:20 +00001203 return Importer.getToContext().getFunctionType(
1204 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001205}
1206
Balazs Keri3b30d652018-10-19 13:32:20 +00001207ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001208 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001209 UnresolvedUsingTypenameDecl *ToD;
1210 Decl *ToPrevD;
1211 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1212 std::tie(ToD, ToPrevD) = *Imp;
1213 else
1214 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001215
Balazs Keri3b30d652018-10-19 13:32:20 +00001216 return Importer.getToContext().getTypeDeclType(
1217 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001218}
1219
Balazs Keri3b30d652018-10-19 13:32:20 +00001220ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1221 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1222 if (!ToInnerTypeOrErr)
1223 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001224
Balazs Keri3b30d652018-10-19 13:32:20 +00001225 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001226}
1227
Balazs Keri3b30d652018-10-19 13:32:20 +00001228ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1229 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1230 if (!ToDeclOrErr)
1231 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001232
Balazs Keri3b30d652018-10-19 13:32:20 +00001233 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001234}
1235
Balazs Keri3b30d652018-10-19 13:32:20 +00001236ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1237 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1238 if (!ToExprOrErr)
1239 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001240
Balazs Keri3b30d652018-10-19 13:32:20 +00001241 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001242}
1243
Balazs Keri3b30d652018-10-19 13:32:20 +00001244ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1245 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1246 if (!ToUnderlyingTypeOrErr)
1247 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001248
Balazs Keri3b30d652018-10-19 13:32:20 +00001249 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001250}
1251
Balazs Keri3b30d652018-10-19 13:32:20 +00001252ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001253 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001254 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1255 if (!ToExprOrErr)
1256 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001257
Balazs Keri3b30d652018-10-19 13:32:20 +00001258 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1259 if (!ToUnderlyingTypeOrErr)
1260 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262 return Importer.getToContext().getDecltypeType(
1263 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001264}
1265
Balazs Keri3b30d652018-10-19 13:32:20 +00001266ExpectedType
1267ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1268 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1269 if (!ToBaseTypeOrErr)
1270 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001271
Balazs Keri3b30d652018-10-19 13:32:20 +00001272 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1273 if (!ToUnderlyingTypeOrErr)
1274 return ToUnderlyingTypeOrErr.takeError();
1275
1276 return Importer.getToContext().getUnaryTransformType(
1277 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001278}
1279
Balazs Keri3b30d652018-10-19 13:32:20 +00001280ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001281 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001282 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1283 if (!ToDeducedTypeOrErr)
1284 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001285
Balazs Keri3b30d652018-10-19 13:32:20 +00001286 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1287 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001288 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001289}
1290
Balazs Keri3b30d652018-10-19 13:32:20 +00001291ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001292 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001293 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1294 if (!ToDeclOrErr)
1295 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001296
Balazs Keri3b30d652018-10-19 13:32:20 +00001297 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1298 if (!ToInjTypeOrErr)
1299 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001300
1301 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1302 // See comments in InjectedClassNameType definition for details
1303 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1304 enum {
1305 TypeAlignmentInBits = 4,
1306 TypeAlignment = 1 << TypeAlignmentInBits
1307 };
1308
1309 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001310 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001311}
1312
Balazs Keri3b30d652018-10-19 13:32:20 +00001313ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1314 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1315 if (!ToDeclOrErr)
1316 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001317
Balazs Keri3b30d652018-10-19 13:32:20 +00001318 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001319}
1320
Balazs Keri3b30d652018-10-19 13:32:20 +00001321ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1322 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1323 if (!ToDeclOrErr)
1324 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001325
Balazs Keri3b30d652018-10-19 13:32:20 +00001326 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001327}
1328
Balazs Keri3b30d652018-10-19 13:32:20 +00001329ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1330 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1331 if (!ToModifiedTypeOrErr)
1332 return ToModifiedTypeOrErr.takeError();
1333 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1334 if (!ToEquivalentTypeOrErr)
1335 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001336
1337 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001338 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001339}
1340
Balazs Keri3b30d652018-10-19 13:32:20 +00001341ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001342 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001343 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1344 if (!ToDeclOrErr)
1345 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001346
1347 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001348 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001349}
1350
Balazs Keri3b30d652018-10-19 13:32:20 +00001351ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001352 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001353 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1354 if (!ReplacedOrErr)
1355 return ReplacedOrErr.takeError();
1356 const TemplateTypeParmType *Replaced =
1357 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001358
Balazs Keri3b30d652018-10-19 13:32:20 +00001359 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1360 if (!ToReplacementTypeOrErr)
1361 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001362
1363 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001364 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001365}
1366
Balazs Keri3b30d652018-10-19 13:32:20 +00001367ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001368 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001369 auto ToTemplateOrErr = import(T->getTemplateName());
1370 if (!ToTemplateOrErr)
1371 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001372
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001373 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001374 if (Error Err = ImportTemplateArguments(
1375 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1376 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001377
Douglas Gregore2e50d332010-12-01 01:36:18 +00001378 QualType ToCanonType;
1379 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001380 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001381 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001382 if (ExpectedType TyOrErr = import(FromCanonType))
1383 ToCanonType = *TyOrErr;
1384 else
1385 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001386 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001387 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001388 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001389 ToCanonType);
1390}
1391
Balazs Keri3b30d652018-10-19 13:32:20 +00001392ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001393 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001394 auto ToQualifierOrErr = import(T->getQualifier());
1395 if (!ToQualifierOrErr)
1396 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001397
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1399 if (!ToNamedTypeOrErr)
1400 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001401
Balazs Keri3b30d652018-10-19 13:32:20 +00001402 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1403 if (!ToOwnedTagDeclOrErr)
1404 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001405
Abramo Bagnara6150c882010-05-11 21:36:43 +00001406 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001407 *ToQualifierOrErr,
1408 *ToNamedTypeOrErr,
1409 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001410}
1411
Balazs Keri3b30d652018-10-19 13:32:20 +00001412ExpectedType
1413ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1414 ExpectedType ToPatternOrErr = import(T->getPattern());
1415 if (!ToPatternOrErr)
1416 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001417
Balazs Keri3b30d652018-10-19 13:32:20 +00001418 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001419 T->getNumExpansions());
1420}
1421
Balazs Keri3b30d652018-10-19 13:32:20 +00001422ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001423 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001424 auto ToQualifierOrErr = import(T->getQualifier());
1425 if (!ToQualifierOrErr)
1426 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001427
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001429
1430 SmallVector<TemplateArgument, 2> ToPack;
1431 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001432 if (Error Err = ImportTemplateArguments(
1433 T->getArgs(), T->getNumArgs(), ToPack))
1434 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001435
1436 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001437 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001438}
1439
Balazs Keri3b30d652018-10-19 13:32:20 +00001440ExpectedType
1441ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1442 auto ToQualifierOrErr = import(T->getQualifier());
1443 if (!ToQualifierOrErr)
1444 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001445
1446 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001447
Balazs Keri3b30d652018-10-19 13:32:20 +00001448 QualType Canon;
1449 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1450 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1451 Canon = (*TyOrErr).getCanonicalType();
1452 else
1453 return TyOrErr.takeError();
1454 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001455
Balazs Keri3b30d652018-10-19 13:32:20 +00001456 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1457 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001458 Name, Canon);
1459}
1460
Balazs Keri3b30d652018-10-19 13:32:20 +00001461ExpectedType
1462ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1463 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1464 if (!ToDeclOrErr)
1465 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001466
Balazs Keri3b30d652018-10-19 13:32:20 +00001467 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001468}
1469
Balazs Keri3b30d652018-10-19 13:32:20 +00001470ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1471 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1472 if (!ToBaseTypeOrErr)
1473 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001474
Douglas Gregore9d95f12015-07-07 03:57:35 +00001475 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001476 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001477 if (ExpectedType TyOrErr = import(TypeArg))
1478 TypeArgs.push_back(*TyOrErr);
1479 else
1480 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001481 }
1482
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001483 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001484 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001485 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1486 Protocols.push_back(*ProtocolOrErr);
1487 else
1488 return ProtocolOrErr.takeError();
1489
Douglas Gregor96e578d2010-02-05 17:54:41 +00001490 }
1491
Balazs Keri3b30d652018-10-19 13:32:20 +00001492 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001493 Protocols,
1494 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495}
1496
Balazs Keri3b30d652018-10-19 13:32:20 +00001497ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001498ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001499 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1500 if (!ToPointeeTypeOrErr)
1501 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001502
Balazs Keri3b30d652018-10-19 13:32:20 +00001503 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001504}
1505
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001506//----------------------------------------------------------------------------
1507// Import Declarations
1508//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001509Error ASTNodeImporter::ImportDeclParts(
1510 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1511 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001512 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1513 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1514 DeclContext *OrigDC = D->getDeclContext();
1515 FunctionDecl *FunDecl;
1516 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1517 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001518 auto getLeafPointeeType = [](const Type *T) {
1519 while (T->isPointerType() || T->isArrayType()) {
1520 T = T->getPointeeOrArrayElementType();
1521 }
1522 return T;
1523 };
1524 for (const ParmVarDecl *P : FunDecl->parameters()) {
1525 const Type *LeafT =
1526 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1527 auto *RT = dyn_cast<RecordType>(LeafT);
1528 if (RT && RT->getDecl() == D) {
1529 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1530 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001531 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001532 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001533 }
1534 }
1535
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001536 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001537 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1538 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001539
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001540 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001541 if (Error Err = importInto(Name, D->getDeclName()))
1542 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001543
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001544 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001545 if (Error Err = importInto(Loc, D->getLocation()))
1546 return Err;
1547
Sean Callanan59721b32015-04-28 18:41:46 +00001548 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001549 if (ToD)
1550 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1551 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001552
1553 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001554}
1555
Balazs Keri3b30d652018-10-19 13:32:20 +00001556Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001557 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001558 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001559
Balazs Keri3b30d652018-10-19 13:32:20 +00001560 if (!ToD)
1561 if (Error Err = importInto(ToD, FromD))
1562 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001563
Balazs Keri3b30d652018-10-19 13:32:20 +00001564 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1565 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1566 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1567 !ToRecord->getDefinition()) {
1568 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1569 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001570 }
1571 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001572 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001573 }
1574
Balazs Keri3b30d652018-10-19 13:32:20 +00001575 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1576 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001577 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001578 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1579 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001580 }
1581 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001582 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001583 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001584
1585 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001586}
1587
Balazs Keri3b30d652018-10-19 13:32:20 +00001588Error
1589ASTNodeImporter::ImportDeclarationNameLoc(
1590 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001591 // NOTE: To.Name and To.Loc are already imported.
1592 // We only have to import To.LocInfo.
1593 switch (To.getName().getNameKind()) {
1594 case DeclarationName::Identifier:
1595 case DeclarationName::ObjCZeroArgSelector:
1596 case DeclarationName::ObjCOneArgSelector:
1597 case DeclarationName::ObjCMultiArgSelector:
1598 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001599 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001600 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001601
1602 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001603 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1604 To.setCXXOperatorNameRange(*ToRangeOrErr);
1605 else
1606 return ToRangeOrErr.takeError();
1607 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001608 }
1609 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001610 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1611 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1612 else
1613 return LocOrErr.takeError();
1614 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001615 }
1616 case DeclarationName::CXXConstructorName:
1617 case DeclarationName::CXXDestructorName:
1618 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001619 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1620 To.setNamedTypeInfo(*ToTInfoOrErr);
1621 else
1622 return ToTInfoOrErr.takeError();
1623 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001624 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001625 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001626 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001627}
1628
Balazs Keri3b30d652018-10-19 13:32:20 +00001629Error
1630ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001631 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001632 auto ToDCOrErr = Importer.ImportContext(FromDC);
1633 return ToDCOrErr.takeError();
1634 }
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001635
1636 // We use strict error handling in case of records and enums, but not
1637 // with e.g. namespaces.
1638 //
1639 // FIXME Clients of the ASTImporter should be able to choose an
1640 // appropriate error handling strategy for their needs. For instance,
1641 // they may not want to mark an entire namespace as erroneous merely
1642 // because there is an ODR error with two typedefs. As another example,
1643 // the client may allow EnumConstantDecls with same names but with
1644 // different values in two distinct translation units.
1645 bool AccumulateChildErrors = isa<TagDecl>(FromDC);
1646
1647 Error ChildErrors = Error::success();
Davide Italiano93a64ef2018-10-30 20:46:29 +00001648 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001649 for (auto *From : FromDC->decls()) {
1650 ExpectedDecl ImportedOrErr = import(From);
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001651 if (!ImportedOrErr) {
1652 if (AccumulateChildErrors)
1653 ChildErrors =
1654 joinErrors(std::move(ChildErrors), ImportedOrErr.takeError());
1655 else
1656 consumeError(ImportedOrErr.takeError());
1657 }
Douglas Gregor0a791672011-01-18 03:11:38 +00001658 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001659
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001660 return ChildErrors;
Douglas Gregor968d6332010-02-21 18:24:45 +00001661}
1662
Balazs Keri3b30d652018-10-19 13:32:20 +00001663Error ASTNodeImporter::ImportDeclContext(
1664 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1665 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1666 if (!ToDCOrErr)
1667 return ToDCOrErr.takeError();
1668 ToDC = *ToDCOrErr;
1669
1670 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1671 auto ToLexicalDCOrErr = Importer.ImportContext(
1672 FromD->getLexicalDeclContext());
1673 if (!ToLexicalDCOrErr)
1674 return ToLexicalDCOrErr.takeError();
1675 ToLexicalDC = *ToLexicalDCOrErr;
1676 } else
1677 ToLexicalDC = ToDC;
1678
1679 return Error::success();
1680}
1681
1682Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001683 const CXXRecordDecl *From, CXXRecordDecl *To) {
1684 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1685 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001686
Balazs Keri1d20cc22018-07-16 12:16:39 +00001687 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001688 if (FromM->isImplicit()) {
1689 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1690 if (!ToMOrErr)
1691 return ToMOrErr.takeError();
1692 }
1693
1694 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001695}
1696
Balazs Keri3b30d652018-10-19 13:32:20 +00001697static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1698 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001699 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00001700 if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef))
Balazs Keri57949eb2019-03-25 09:16:39 +00001701 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1702 else
1703 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001704 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001705 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001706}
1707
Balazs Keri3b30d652018-10-19 13:32:20 +00001708Error ASTNodeImporter::ImportDefinition(
1709 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Gabor Martonaefcf512019-07-17 13:47:46 +00001710 auto DefinitionCompleter = [To]() {
1711 // There are cases in LLDB when we first import a class without its
1712 // members. The class will have DefinitionData, but no members. Then,
1713 // importDefinition is called from LLDB, which tries to get the members, so
1714 // when we get here, the class already has the DefinitionData set, so we
1715 // must unset the CompleteDefinition here to be able to complete again the
1716 // definition.
1717 To->setCompleteDefinition(false);
1718 To->completeDefinition();
1719 };
1720
Douglas Gregor95d82832012-01-24 18:36:04 +00001721 if (To->getDefinition() || To->isBeingDefined()) {
Gabor Martone73805f2019-07-08 12:49:13 +00001722 if (Kind == IDK_Everything ||
1723 // In case of lambdas, the class already has a definition ptr set, but
1724 // the contained decls are not imported yet. Also, isBeingDefined was
1725 // set in CXXRecordDecl::CreateLambda. We must import the contained
1726 // decls here and finish the definition.
1727 (To->isLambda() && shouldForceImportDeclContext(Kind))) {
1728 Error Result = ImportDeclContext(From, /*ForceImport=*/true);
1729 // Finish the definition of the lambda, set isBeingDefined to false.
1730 if (To->isLambda())
Gabor Martonaefcf512019-07-17 13:47:46 +00001731 DefinitionCompleter();
Gabor Martone73805f2019-07-08 12:49:13 +00001732 return Result;
1733 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001734
Balazs Keri3b30d652018-10-19 13:32:20 +00001735 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001736 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001737
Douglas Gregore2e50d332010-12-01 01:36:18 +00001738 To->startDefinition();
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001739 // Complete the definition even if error is returned.
1740 // The RecordDecl may be already part of the AST so it is better to
1741 // have it in complete state even if something is wrong with it.
Gabor Martonaefcf512019-07-17 13:47:46 +00001742 auto DefinitionCompleterScopeExit =
1743 llvm::make_scope_exit(DefinitionCompleter);
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001744
Balazs Keri3b30d652018-10-19 13:32:20 +00001745 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1746 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001747
Douglas Gregore2e50d332010-12-01 01:36:18 +00001748 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001749 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1750 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1751 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001752
1753 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1754 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1755 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001756 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001757 ToData.Aggregate = FromData.Aggregate;
1758 ToData.PlainOldData = FromData.PlainOldData;
1759 ToData.Empty = FromData.Empty;
1760 ToData.Polymorphic = FromData.Polymorphic;
1761 ToData.Abstract = FromData.Abstract;
1762 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001763 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1764 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1765 ToData.HasBasesWithNonStaticDataMembers =
1766 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001767 ToData.HasPrivateFields = FromData.HasPrivateFields;
1768 ToData.HasProtectedFields = FromData.HasProtectedFields;
1769 ToData.HasPublicFields = FromData.HasPublicFields;
1770 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001771 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001772 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001773 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001774 ToData.HasUninitializedReferenceMember
1775 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001776 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001777 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1778 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001779 ToData.NeedOverloadResolutionForCopyConstructor
1780 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001781 ToData.NeedOverloadResolutionForMoveConstructor
1782 = FromData.NeedOverloadResolutionForMoveConstructor;
1783 ToData.NeedOverloadResolutionForMoveAssignment
1784 = FromData.NeedOverloadResolutionForMoveAssignment;
1785 ToData.NeedOverloadResolutionForDestructor
1786 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001787 ToData.DefaultedCopyConstructorIsDeleted
1788 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001789 ToData.DefaultedMoveConstructorIsDeleted
1790 = FromData.DefaultedMoveConstructorIsDeleted;
1791 ToData.DefaultedMoveAssignmentIsDeleted
1792 = FromData.DefaultedMoveAssignmentIsDeleted;
1793 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001794 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1795 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001796 ToData.HasConstexprNonCopyMoveConstructor
1797 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001798 ToData.HasDefaultedDefaultConstructor
1799 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001800 ToData.DefaultedDefaultConstructorIsConstexpr
1801 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001802 ToData.HasConstexprDefaultConstructor
1803 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001804 ToData.HasNonLiteralTypeFieldsOrBases
1805 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001806 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001807 ToData.UserProvidedDefaultConstructor
1808 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001809 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001810 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1811 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1812 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1813 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001814 ToData.ImplicitCopyAssignmentHasConstParam
1815 = FromData.ImplicitCopyAssignmentHasConstParam;
1816 ToData.HasDeclaredCopyConstructorWithConstParam
1817 = FromData.HasDeclaredCopyConstructorWithConstParam;
1818 ToData.HasDeclaredCopyAssignmentWithConstParam
1819 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001820
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001821 // Copy over the data stored in RecordDeclBits
1822 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1823
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001824 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001825 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001826 ExpectedType TyOrErr = import(Base1.getType());
1827 if (!TyOrErr)
1828 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001829
1830 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001831 if (Base1.isPackExpansion()) {
1832 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1833 EllipsisLoc = *LocOrErr;
1834 else
1835 return LocOrErr.takeError();
1836 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001837
1838 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001839 if (Error Err =
1840 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1841 return Err;
1842
1843 auto RangeOrErr = import(Base1.getSourceRange());
1844 if (!RangeOrErr)
1845 return RangeOrErr.takeError();
1846
1847 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1848 if (!TSIOrErr)
1849 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001850
Douglas Gregore2e50d332010-12-01 01:36:18 +00001851 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001852 new (Importer.getToContext()) CXXBaseSpecifier(
1853 *RangeOrErr,
1854 Base1.isVirtual(),
1855 Base1.isBaseOfClass(),
1856 Base1.getAccessSpecifierAsWritten(),
1857 *TSIOrErr,
1858 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001859 }
1860 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001861 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001862 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001863
Douglas Gregor2e15c842012-02-01 21:00:38 +00001864 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001865 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1866 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001867
Balazs Keri3b30d652018-10-19 13:32:20 +00001868 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001869}
1870
Balazs Keri3b30d652018-10-19 13:32:20 +00001871Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001872 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001873 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001874
Gabor Martonac3a5d62018-09-17 12:04:52 +00001875 Expr *FromInit = From->getInit();
1876 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001877 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001878
Balazs Keri3b30d652018-10-19 13:32:20 +00001879 ExpectedExpr ToInitOrErr = import(FromInit);
1880 if (!ToInitOrErr)
1881 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001882
Balazs Keri3b30d652018-10-19 13:32:20 +00001883 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001884 if (From->isInitKnownICE()) {
1885 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1886 Eval->CheckedICE = true;
1887 Eval->IsICE = From->isInitICE();
1888 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001889
1890 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001891 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001892}
1893
Balazs Keri3b30d652018-10-19 13:32:20 +00001894Error ASTNodeImporter::ImportDefinition(
1895 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001896 if (To->getDefinition() || To->isBeingDefined()) {
1897 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001898 return ImportDeclContext(From, /*ForceImport=*/true);
1899 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001900 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001901
Douglas Gregord451ea92011-07-29 23:31:30 +00001902 To->startDefinition();
1903
Balazs Keri3b30d652018-10-19 13:32:20 +00001904 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1905 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001906
Balazs Keri3b30d652018-10-19 13:32:20 +00001907 ExpectedType ToTypeOrErr =
1908 import(Importer.getFromContext().getTypeDeclType(From));
1909 if (!ToTypeOrErr)
1910 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001911
Balazs Keri3b30d652018-10-19 13:32:20 +00001912 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1913 if (!ToPromotionTypeOrErr)
1914 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001915
1916 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001917 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1918 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001919
Douglas Gregord451ea92011-07-29 23:31:30 +00001920 // FIXME: we might need to merge the number of positive or negative bits
1921 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001922 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001923 From->getNumPositiveBits(),
1924 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001925 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001926}
1927
Balazs Keri3b30d652018-10-19 13:32:20 +00001928Error ASTNodeImporter::ImportTemplateArguments(
1929 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1930 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001931 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001932 if (auto ToOrErr = import(FromArgs[I]))
1933 ToArgs.push_back(*ToOrErr);
1934 else
1935 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001936 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001937
Balazs Keri3b30d652018-10-19 13:32:20 +00001938 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001939}
1940
Balazs Keri3b30d652018-10-19 13:32:20 +00001941// FIXME: Do not forget to remove this and use only 'import'.
1942Expected<TemplateArgument>
1943ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1944 return import(From);
1945}
1946
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001947template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001948Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001949 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1950 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001951 if (auto ToLocOrErr = import(FromLoc))
1952 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001953 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001954 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001955 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001956 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001957}
1958
Gabor Marton26f72a92018-07-12 09:42:05 +00001959static StructuralEquivalenceKind
1960getStructuralEquivalenceKind(const ASTImporter &Importer) {
1961 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1962 : StructuralEquivalenceKind::Default;
1963}
1964
Gabor Marton950fb572018-07-17 12:39:27 +00001965bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1966 StructuralEquivalenceContext Ctx(
1967 Importer.getFromContext(), Importer.getToContext(),
1968 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1969 false, Complain);
1970 return Ctx.IsEquivalent(From, To);
1971}
1972
1973bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001974 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001975 // Eliminate a potential failure point where we attempt to re-import
1976 // something we're trying to import while completing ToRecord.
1977 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1978 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001979 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001980 if (ToOriginRecord)
1981 ToRecord = ToOriginRecord;
1982 }
1983
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001984 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001985 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001986 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001987 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001988 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001989 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001990}
1991
Larisse Voufo39a1e502013-08-06 01:03:05 +00001992bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1993 bool Complain) {
1994 StructuralEquivalenceContext Ctx(
1995 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001996 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1997 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001998 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001999}
2000
Douglas Gregor98c10182010-02-12 22:17:39 +00002001bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002002 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00002003 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002004 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
2005 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
2006 ToEnum = ToOriginEnum;
2007
Gabor Marton26f72a92018-07-12 09:42:05 +00002008 StructuralEquivalenceContext Ctx(
2009 Importer.getFromContext(), Importer.getToContext(),
2010 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002011 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002012}
2013
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002014bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2015 FunctionTemplateDecl *To) {
2016 StructuralEquivalenceContext Ctx(
2017 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002018 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2019 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002020 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002021}
2022
Balazs Keric7797c42018-07-11 09:37:24 +00002023bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2024 StructuralEquivalenceContext Ctx(
2025 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002026 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2027 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002028 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002029}
2030
Douglas Gregor91155082012-11-14 22:29:20 +00002031bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002032 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002033 const llvm::APSInt &FromVal = FromEC->getInitVal();
2034 const llvm::APSInt &ToVal = ToEC->getInitVal();
2035
2036 return FromVal.isSigned() == ToVal.isSigned() &&
2037 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2038 FromVal == ToVal;
2039}
2040
2041bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002042 ClassTemplateDecl *To) {
2043 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2044 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002045 Importer.getNonEquivalentDecls(),
2046 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002047 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002048}
2049
Larisse Voufo39a1e502013-08-06 01:03:05 +00002050bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2051 VarTemplateDecl *To) {
2052 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2053 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002054 Importer.getNonEquivalentDecls(),
2055 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002056 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002057}
2058
Balazs Keri3b30d652018-10-19 13:32:20 +00002059ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002060 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002061 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002062 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002063}
2064
Balazs Keri3b30d652018-10-19 13:32:20 +00002065ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2066 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2067 << D->getDeclKindName();
2068 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2069}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002070
Balazs Keri3b30d652018-10-19 13:32:20 +00002071ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2072 // Import the context of this declaration.
2073 DeclContext *DC, *LexicalDC;
2074 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2075 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002076
2077 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002078 ExpectedSLoc LocOrErr = import(D->getLocation());
2079 if (!LocOrErr)
2080 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002081
Gabor Marton26f72a92018-07-12 09:42:05 +00002082 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002083 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002084 return ToD;
2085
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002086 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002087 LexicalDC->addDeclInternal(ToD);
2088 return ToD;
2089}
2090
Balazs Keri3b30d652018-10-19 13:32:20 +00002091ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002092 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002093 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002094
Gabor Marton26f72a92018-07-12 09:42:05 +00002095 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002096
Sean Callanan65198272011-11-17 23:20:56 +00002097 return ToD;
2098}
2099
Balazs Keri3b30d652018-10-19 13:32:20 +00002100ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2101 ExpectedSLoc LocOrErr = import(D->getLocation());
2102 if (!LocOrErr)
2103 return LocOrErr.takeError();
2104 auto ColonLocOrErr = import(D->getColonLoc());
2105 if (!ColonLocOrErr)
2106 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002107
2108 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002109 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2110 if (!DCOrErr)
2111 return DCOrErr.takeError();
2112 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002113
Gabor Marton26f72a92018-07-12 09:42:05 +00002114 AccessSpecDecl *ToD;
2115 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002116 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002117 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002118
2119 // Lexical DeclContext and Semantic DeclContext
2120 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002121 ToD->setLexicalDeclContext(DC);
2122 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002123
Gabor Marton26f72a92018-07-12 09:42:05 +00002124 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002125}
2126
Balazs Keri3b30d652018-10-19 13:32:20 +00002127ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2128 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2129 if (!DCOrErr)
2130 return DCOrErr.takeError();
2131 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002132 DeclContext *LexicalDC = DC;
2133
Balazs Keri3b30d652018-10-19 13:32:20 +00002134 SourceLocation ToLocation, ToRParenLoc;
2135 Expr *ToAssertExpr;
2136 StringLiteral *ToMessage;
2137 if (auto Imp = importSeq(
2138 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2139 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2140 else
2141 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002142
Gabor Marton26f72a92018-07-12 09:42:05 +00002143 StaticAssertDecl *ToD;
2144 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002145 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2146 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002147 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002148
2149 ToD->setLexicalDeclContext(LexicalDC);
2150 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002151 return ToD;
2152}
2153
Balazs Keri3b30d652018-10-19 13:32:20 +00002154ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002155 // Import the major distinguishing characteristics of this namespace.
2156 DeclContext *DC, *LexicalDC;
2157 DeclarationName Name;
2158 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002159 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002160 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2161 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002162 if (ToD)
2163 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002164
2165 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002166 if (!Name) {
2167 // This is an anonymous namespace. Adopt an existing anonymous
2168 // namespace if we can.
2169 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002170 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002171 MergeWithNamespace = TU->getAnonymousNamespace();
2172 else
2173 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2174 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002175 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002176 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002177 for (auto *FoundDecl : FoundDecls) {
2178 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002179 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002180
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002181 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002182 MergeWithNamespace = FoundNS;
2183 ConflictingDecls.clear();
2184 break;
2185 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002186
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002187 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002188 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002189
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002190 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002191 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002192 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002193 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002194 if (!Name)
2195 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002196 }
2197 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002198
Balazs Keri3b30d652018-10-19 13:32:20 +00002199 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2200 if (!BeginLocOrErr)
2201 return BeginLocOrErr.takeError();
2202
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002203 // Create the "to" namespace, if needed.
2204 NamespaceDecl *ToNamespace = MergeWithNamespace;
2205 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002206 if (GetImportedOrCreateDecl(
2207 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002208 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002209 /*PrevDecl=*/nullptr))
2210 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002211 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002212 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002213
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002214 // If this is an anonymous namespace, register it as the anonymous
2215 // namespace within its context.
2216 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002217 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002218 TU->setAnonymousNamespace(ToNamespace);
2219 else
2220 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2221 }
2222 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002223 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002224
Balazs Keri3b30d652018-10-19 13:32:20 +00002225 if (Error Err = ImportDeclContext(D))
2226 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002227
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002228 return ToNamespace;
2229}
2230
Balazs Keri3b30d652018-10-19 13:32:20 +00002231ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002232 // Import the major distinguishing characteristics of this namespace.
2233 DeclContext *DC, *LexicalDC;
2234 DeclarationName Name;
2235 SourceLocation Loc;
2236 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002237 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2238 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002239 if (LookupD)
2240 return LookupD;
2241
2242 // NOTE: No conflict resolution is done for namespace aliases now.
2243
Balazs Keri3b30d652018-10-19 13:32:20 +00002244 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2245 NestedNameSpecifierLoc ToQualifierLoc;
2246 NamespaceDecl *ToNamespace;
2247 if (auto Imp = importSeq(
2248 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2249 D->getTargetNameLoc(), D->getNamespace()))
2250 std::tie(
2251 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2252 ToNamespace) = *Imp;
2253 else
2254 return Imp.takeError();
2255 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002256
Gabor Marton26f72a92018-07-12 09:42:05 +00002257 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002258 if (GetImportedOrCreateDecl(
2259 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2260 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002261 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002262
2263 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002264 LexicalDC->addDeclInternal(ToD);
2265
2266 return ToD;
2267}
2268
Balazs Keri3b30d652018-10-19 13:32:20 +00002269ExpectedDecl
2270ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002271 // Import the major distinguishing characteristics of this typedef.
2272 DeclContext *DC, *LexicalDC;
2273 DeclarationName Name;
2274 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002275 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002276 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2277 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002278 if (ToD)
2279 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002280
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002281 // If this typedef is not in block scope, determine whether we've
2282 // seen a typedef with the same name (that we can merge with) or any
2283 // other entity by that name (which name lookup could conflict with).
2284 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002285 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002286 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002287 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002288 for (auto *FoundDecl : FoundDecls) {
2289 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002290 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002291 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002292 QualType FromUT = D->getUnderlyingType();
2293 QualType FoundUT = FoundTypedef->getUnderlyingType();
2294 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2295 // If the "From" context has a complete underlying type but we
2296 // already have a complete underlying type then return with that.
2297 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002298 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002299 }
Gabor Martondd2b76e2019-06-11 13:35:25 +00002300 // FIXME Handle redecl chain. When you do that make consistent changes
2301 // in ASTImporterLookupTable too.
Gabor Martonb93baf62018-11-27 09:51:36 +00002302 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002303 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002304
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002305 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002306 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002307
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002308 if (!ConflictingDecls.empty()) {
2309 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002310 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002311 ConflictingDecls.size());
2312 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002313 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002314 }
2315 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002316
Balazs Keri3b30d652018-10-19 13:32:20 +00002317 QualType ToUnderlyingType;
2318 TypeSourceInfo *ToTypeSourceInfo;
2319 SourceLocation ToBeginLoc;
2320 if (auto Imp = importSeq(
2321 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2322 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2323 else
2324 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002325
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002326 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002327 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002328 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002329 if (IsAlias) {
2330 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002331 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2332 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002333 return ToTypedef;
2334 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002335 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2336 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002337 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002338
Douglas Gregordd483172010-02-22 17:42:47 +00002339 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002340 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002341
2342 // Templated declarations should not appear in DeclContext.
2343 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2344 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2345 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002346
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002347 return ToTypedef;
2348}
2349
Balazs Keri3b30d652018-10-19 13:32:20 +00002350ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002351 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2352}
2353
Balazs Keri3b30d652018-10-19 13:32:20 +00002354ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002355 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2356}
2357
Balazs Keri3b30d652018-10-19 13:32:20 +00002358ExpectedDecl
2359ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002360 // Import the major distinguishing characteristics of this typedef.
2361 DeclContext *DC, *LexicalDC;
2362 DeclarationName Name;
2363 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002364 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002365 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2366 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002367 if (FoundD)
2368 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002369
2370 // If this typedef is not in block scope, determine whether we've
2371 // seen a typedef with the same name (that we can merge with) or any
2372 // other entity by that name (which name lookup could conflict with).
2373 if (!DC->isFunctionOrMethod()) {
2374 SmallVector<NamedDecl *, 4> ConflictingDecls;
2375 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002376 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002377 for (auto *FoundDecl : FoundDecls) {
2378 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002379 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002380 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002381 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002382 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002383 }
2384
2385 if (!ConflictingDecls.empty()) {
2386 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2387 ConflictingDecls.data(),
2388 ConflictingDecls.size());
2389 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002390 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002391 }
2392 }
2393
Balazs Keri3b30d652018-10-19 13:32:20 +00002394 TemplateParameterList *ToTemplateParameters;
2395 TypeAliasDecl *ToTemplatedDecl;
2396 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2397 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2398 else
2399 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002400
Gabor Marton26f72a92018-07-12 09:42:05 +00002401 TypeAliasTemplateDecl *ToAlias;
2402 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002403 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002404 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002405
Balazs Keri3b30d652018-10-19 13:32:20 +00002406 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002407
Gabor Horvath7a91c082017-11-14 11:30:38 +00002408 ToAlias->setAccess(D->getAccess());
2409 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002410 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002411 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002412}
2413
Balazs Keri3b30d652018-10-19 13:32:20 +00002414ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002415 // Import the major distinguishing characteristics of this label.
2416 DeclContext *DC, *LexicalDC;
2417 DeclarationName Name;
2418 SourceLocation Loc;
2419 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002420 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2421 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002422 if (ToD)
2423 return ToD;
2424
2425 assert(LexicalDC->isFunctionOrMethod());
2426
Gabor Marton26f72a92018-07-12 09:42:05 +00002427 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002428 if (D->isGnuLocal()) {
2429 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2430 if (!BeginLocOrErr)
2431 return BeginLocOrErr.takeError();
2432 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2433 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2434 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002435
Balazs Keri3b30d652018-10-19 13:32:20 +00002436 } else {
2437 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2438 Name.getAsIdentifierInfo()))
2439 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002440
Balazs Keri3b30d652018-10-19 13:32:20 +00002441 }
2442
2443 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2444 if (!ToStmtOrErr)
2445 return ToStmtOrErr.takeError();
2446
2447 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002448 ToLabel->setLexicalDeclContext(LexicalDC);
2449 LexicalDC->addDeclInternal(ToLabel);
2450 return ToLabel;
2451}
2452
Balazs Keri3b30d652018-10-19 13:32:20 +00002453ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002454 // Import the major distinguishing characteristics of this enum.
2455 DeclContext *DC, *LexicalDC;
2456 DeclarationName Name;
2457 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002458 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002459 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2460 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002461 if (ToD)
2462 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002463
Douglas Gregor98c10182010-02-12 22:17:39 +00002464 // Figure out what enum name we're looking for.
2465 unsigned IDNS = Decl::IDNS_Tag;
2466 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002467 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002468 if (Error Err = importInto(
2469 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2470 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002471 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002472 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002473 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002474
Douglas Gregor98c10182010-02-12 22:17:39 +00002475 // We may already have an enum of the same name; try to find and match it.
2476 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002477 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002478 auto FoundDecls =
2479 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002480 for (auto *FoundDecl : FoundDecls) {
2481 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002482 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002483
Balazs Keri3b30d652018-10-19 13:32:20 +00002484 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002485 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002486 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002487 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002488
Balazs Keri3b30d652018-10-19 13:32:20 +00002489 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Balazs Kerieb79b252019-07-09 11:08:18 +00002490 if (!hasSameVisibilityContext(FoundEnum, D))
2491 continue;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002492 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002493 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002494 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002495
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002496 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002497 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002498
Douglas Gregor98c10182010-02-12 22:17:39 +00002499 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002500 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002501 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002502 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002503 if (!Name)
2504 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002505 }
2506 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002507
Balazs Keri3b30d652018-10-19 13:32:20 +00002508 SourceLocation ToBeginLoc;
2509 NestedNameSpecifierLoc ToQualifierLoc;
2510 QualType ToIntegerType;
2511 if (auto Imp = importSeq(
2512 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2513 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2514 else
2515 return Imp.takeError();
2516
Douglas Gregor98c10182010-02-12 22:17:39 +00002517 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002518 EnumDecl *D2;
2519 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002520 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002521 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2522 D->isScopedUsingClassTag(), D->isFixed()))
2523 return D2;
2524
Balazs Keri3b30d652018-10-19 13:32:20 +00002525 D2->setQualifierInfo(ToQualifierLoc);
2526 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002527 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002528 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002529 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002530
Douglas Gregor98c10182010-02-12 22:17:39 +00002531 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002532 if (D->isCompleteDefinition())
2533 if (Error Err = ImportDefinition(D, D2))
2534 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002535
Douglas Gregor3996e242010-02-15 22:01:00 +00002536 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002537}
2538
Balazs Keri3b30d652018-10-19 13:32:20 +00002539ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002540 bool IsFriendTemplate = false;
2541 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2542 IsFriendTemplate =
2543 DCXX->getDescribedClassTemplate() &&
2544 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2545 Decl::FOK_None;
2546 }
2547
Douglas Gregor5c73e912010-02-11 00:48:18 +00002548 // Import the major distinguishing characteristics of this record.
2549 DeclContext *DC, *LexicalDC;
2550 DeclarationName Name;
2551 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002552 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002553 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2554 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002555 if (ToD)
2556 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002557
Douglas Gregor5c73e912010-02-11 00:48:18 +00002558 // Figure out what structure name we're looking for.
2559 unsigned IDNS = Decl::IDNS_Tag;
2560 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002561 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002562 if (Error Err = importInto(
2563 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2564 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002565 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002566 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002567 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002568
2569 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002570 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002571 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002572 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002573 auto FoundDecls =
2574 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002575 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002576 // We're going to have to compare D against potentially conflicting Decls,
2577 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002578 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2579 D->getASTContext().getExternalSource()->CompleteType(D);
2580 }
2581
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002582 for (auto *FoundDecl : FoundDecls) {
2583 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002584 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002585
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002586 Decl *Found = FoundDecl;
2587 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2588 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002589 Found = Tag->getDecl();
2590 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002591
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002592 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002593 // Do not emit false positive diagnostic in case of unnamed
2594 // struct/union and in case of anonymous structs. Would be false
2595 // because there may be several anonymous/unnamed structs in a class.
2596 // E.g. these are both valid:
2597 // struct A { // unnamed structs
2598 // struct { struct A *next; } entry0;
2599 // struct { struct A *next; } entry1;
2600 // };
2601 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2602 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002603 if (!IsStructuralMatch(D, FoundRecord, false))
2604 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002605
Balazs Keric8272192019-05-27 09:36:00 +00002606 if (!hasSameVisibilityContext(FoundRecord, D))
2607 continue;
2608
Gabor Marton7df342a2018-12-17 12:42:12 +00002609 if (IsStructuralMatch(D, FoundRecord)) {
2610 RecordDecl *FoundDef = FoundRecord->getDefinition();
2611 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002612 // FIXME: Structural equivalence check should check for same
2613 // user-defined methods.
2614 Importer.MapImported(D, FoundDef);
2615 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2616 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2617 assert(FoundCXX && "Record type mismatch");
2618
Gabor Marton7df342a2018-12-17 12:42:12 +00002619 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002620 // FoundDef may not have every implicit method that D has
2621 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002622 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2623 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002624 }
Douglas Gregor25791052010-02-12 00:09:27 +00002625 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002626 PrevDecl = FoundRecord->getMostRecentDecl();
2627 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002628 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002629 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002630
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002631 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002632 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002633
Douglas Gregordd6006f2012-07-17 21:16:27 +00002634 if (!ConflictingDecls.empty() && SearchName) {
Shafik Yaghmour468724e2019-05-24 16:53:44 +00002635 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002636 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002637 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002638 if (!Name)
2639 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002640 }
2641 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002642
Balazs Keri3b30d652018-10-19 13:32:20 +00002643 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2644 if (!BeginLocOrErr)
2645 return BeginLocOrErr.takeError();
2646
Douglas Gregor5c73e912010-02-11 00:48:18 +00002647 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002648 RecordDecl *D2 = nullptr;
2649 CXXRecordDecl *D2CXX = nullptr;
2650 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2651 if (DCXX->isLambda()) {
2652 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2653 if (!TInfoOrErr)
2654 return TInfoOrErr.takeError();
2655 if (GetImportedOrCreateSpecialDecl(
2656 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2657 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2658 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2659 return D2CXX;
2660 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2661 if (!CDeclOrErr)
2662 return CDeclOrErr.takeError();
2663 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2664 } else if (DCXX->isInjectedClassName()) {
2665 // We have to be careful to do a similar dance to the one in
2666 // Sema::ActOnStartCXXMemberDeclarations
2667 const bool DelayTypeCreation = true;
2668 if (GetImportedOrCreateDecl(
2669 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2670 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2671 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2672 return D2CXX;
2673 Importer.getToContext().getTypeDeclType(
2674 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2675 } else {
2676 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2677 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2678 Name.getAsIdentifierInfo(),
2679 cast_or_null<CXXRecordDecl>(PrevDecl)))
2680 return D2CXX;
2681 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002682
Gabor Marton7df342a2018-12-17 12:42:12 +00002683 D2 = D2CXX;
2684 D2->setAccess(D->getAccess());
2685 D2->setLexicalDeclContext(LexicalDC);
2686 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2687 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002688
Gabor Marton7df342a2018-12-17 12:42:12 +00002689 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2690 DC->makeDeclVisibleInContext(D2);
2691
2692 if (ClassTemplateDecl *FromDescribed =
2693 DCXX->getDescribedClassTemplate()) {
2694 ClassTemplateDecl *ToDescribed;
2695 if (Error Err = importInto(ToDescribed, FromDescribed))
2696 return std::move(Err);
2697 D2CXX->setDescribedClassTemplate(ToDescribed);
2698 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2699 // In a record describing a template the type should be an
2700 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2701 // previously set type to the correct value here (ToDescribed is not
2702 // available at record create).
2703 // FIXME: The previous type is cleared but not removed from
2704 // ASTContext's internal storage.
2705 CXXRecordDecl *Injected = nullptr;
2706 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2707 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2708 if (Record && Record->isInjectedClassName()) {
2709 Injected = Record;
2710 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002711 }
2712 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002713 // Create an injected type for the whole redecl chain.
2714 SmallVector<Decl *, 2> Redecls =
2715 getCanonicalForwardRedeclChain(D2CXX);
2716 for (auto *R : Redecls) {
2717 auto *RI = cast<CXXRecordDecl>(R);
2718 RI->setTypeForDecl(nullptr);
2719 // Below we create a new injected type and assign that to the
2720 // canonical decl, subsequent declarations in the chain will reuse
2721 // that type.
2722 Importer.getToContext().getInjectedClassNameType(
2723 RI, ToDescribed->getInjectedClassNameSpecialization());
2724 }
2725 // Set the new type for the previous injected decl too.
2726 if (Injected) {
2727 Injected->setTypeForDecl(nullptr);
2728 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2729 }
2730 }
2731 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002732 DCXX->getMemberSpecializationInfo()) {
2733 TemplateSpecializationKind SK =
2734 MemberInfo->getTemplateSpecializationKind();
2735 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002736
2737 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2738 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2739 else
2740 return ToInstOrErr.takeError();
2741
2742 if (ExpectedSLoc POIOrErr =
2743 import(MemberInfo->getPointOfInstantiation()))
2744 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2745 *POIOrErr);
2746 else
2747 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002748 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002749
Gabor Marton7df342a2018-12-17 12:42:12 +00002750 } else {
2751 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2752 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2753 Name.getAsIdentifierInfo(), PrevDecl))
2754 return D2;
2755 D2->setLexicalDeclContext(LexicalDC);
2756 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002757 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002758
Gabor Marton7df342a2018-12-17 12:42:12 +00002759 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2760 D2->setQualifierInfo(*QualifierLocOrErr);
2761 else
2762 return QualifierLocOrErr.takeError();
2763
2764 if (D->isAnonymousStructOrUnion())
2765 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002766
Balazs Keri3b30d652018-10-19 13:32:20 +00002767 if (D->isCompleteDefinition())
2768 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2769 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002770
Douglas Gregor3996e242010-02-15 22:01:00 +00002771 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002772}
2773
Balazs Keri3b30d652018-10-19 13:32:20 +00002774ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002775 // Import the major distinguishing characteristics of this enumerator.
2776 DeclContext *DC, *LexicalDC;
2777 DeclarationName Name;
2778 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002779 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002780 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2781 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002782 if (ToD)
2783 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002784
Fangrui Song6907ce22018-07-30 19:24:48 +00002785 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002786 // in the same context.
2787 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002788 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002789 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002790 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002791 for (auto *FoundDecl : FoundDecls) {
2792 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002793 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002794
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002795 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002796 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002797 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002798 }
2799
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002800 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002801 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002802
Douglas Gregor98c10182010-02-12 22:17:39 +00002803 if (!ConflictingDecls.empty()) {
2804 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002805 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002806 ConflictingDecls.size());
2807 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002808 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002809 }
2810 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002811
Balazs Keri3b30d652018-10-19 13:32:20 +00002812 ExpectedType TypeOrErr = import(D->getType());
2813 if (!TypeOrErr)
2814 return TypeOrErr.takeError();
2815
2816 ExpectedExpr InitOrErr = import(D->getInitExpr());
2817 if (!InitOrErr)
2818 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002819
Gabor Marton26f72a92018-07-12 09:42:05 +00002820 EnumConstantDecl *ToEnumerator;
2821 if (GetImportedOrCreateDecl(
2822 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002823 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002824 return ToEnumerator;
2825
Douglas Gregordd483172010-02-22 17:42:47 +00002826 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002827 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002828 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002829 return ToEnumerator;
2830}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002831
Balazs Keri1efc9742019-05-07 10:55:11 +00002832Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2833 DeclaratorDecl *ToD) {
2834 unsigned int Num = FromD->getNumTemplateParameterLists();
2835 if (Num == 0)
2836 return Error::success();
2837 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2838 for (unsigned int I = 0; I < Num; ++I)
2839 if (Expected<TemplateParameterList *> ToTPListOrErr =
2840 import(FromD->getTemplateParameterList(I)))
2841 ToTPLists[I] = *ToTPListOrErr;
2842 else
2843 return ToTPListOrErr.takeError();
2844 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2845 return Error::success();
2846}
2847
Balazs Keri3b30d652018-10-19 13:32:20 +00002848Error ASTNodeImporter::ImportTemplateInformation(
2849 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002850 switch (FromFD->getTemplatedKind()) {
2851 case FunctionDecl::TK_NonTemplate:
2852 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002853 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002854
2855 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002856 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002857
2858 if (Expected<FunctionDecl *> InstFDOrErr =
2859 import(FromFD->getInstantiatedFromMemberFunction()))
2860 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2861 else
2862 return InstFDOrErr.takeError();
2863
2864 if (ExpectedSLoc POIOrErr = import(
2865 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2866 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2867 else
2868 return POIOrErr.takeError();
2869
2870 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002871 }
2872
2873 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002874 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002875 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002876 if (!FunctionAndArgsOrErr)
2877 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002878
2879 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002880 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002881
Gabor Marton5254e642018-06-27 13:32:50 +00002882 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002883 TemplateArgumentListInfo ToTAInfo;
2884 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002885 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002886 if (Error Err = ImportTemplateArgumentListInfo(
2887 *FromTAArgsAsWritten, ToTAInfo))
2888 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002889
Balazs Keri3b30d652018-10-19 13:32:20 +00002890 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2891 if (!POIOrErr)
2892 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002893
Balazs Keri1efc9742019-05-07 10:55:11 +00002894 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
2895 return Err;
2896
Gabor Marton5254e642018-06-27 13:32:50 +00002897 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002898 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002899 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2900 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2901 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002902 }
2903
2904 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2905 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2906 UnresolvedSet<8> TemplDecls;
2907 unsigned NumTemplates = FromInfo->getNumTemplates();
2908 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002909 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2910 import(FromInfo->getTemplate(I)))
2911 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002912 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002913 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002914 }
2915
2916 // Import TemplateArgumentListInfo.
2917 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002918 if (Error Err = ImportTemplateArgumentListInfo(
2919 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2920 llvm::makeArrayRef(
2921 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2922 ToTAInfo))
2923 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002924
2925 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2926 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002927 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002928 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002929 }
Sam McCallfdc32072018-01-26 12:06:44 +00002930 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002931}
2932
Balazs Keri3b30d652018-10-19 13:32:20 +00002933Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002934ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002935 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002936 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002937 if (!FunctionAndArgsOrErr)
2938 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002939
Balazs Keri3b30d652018-10-19 13:32:20 +00002940 FunctionTemplateDecl *Template;
2941 TemplateArgsTy ToTemplArgs;
2942 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002943 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002944 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002945 return FoundSpec;
2946}
2947
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002948Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2949 FunctionDecl *ToFD) {
2950 if (Stmt *FromBody = FromFD->getBody()) {
2951 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2952 ToFD->setBody(*ToBodyOrErr);
2953 else
2954 return ToBodyOrErr.takeError();
2955 }
2956 return Error::success();
2957}
2958
Gabor Marton458d1452019-02-14 13:07:03 +00002959template <typename T>
2960bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2961 if (From->hasExternalFormalLinkage())
2962 return Found->hasExternalFormalLinkage();
2963 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2964 return false;
2965 if (From->isInAnonymousNamespace())
2966 return Found->isInAnonymousNamespace();
2967 else
2968 return !Found->isInAnonymousNamespace() &&
2969 !Found->hasExternalFormalLinkage();
2970}
2971
Balazs Keri3b30d652018-10-19 13:32:20 +00002972ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002973
Balazs Keri3b30d652018-10-19 13:32:20 +00002974 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002975 auto RedeclIt = Redecls.begin();
2976 // Import the first part of the decl chain. I.e. import all previous
2977 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002978 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2979 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2980 if (!ToRedeclOrErr)
2981 return ToRedeclOrErr.takeError();
2982 }
Gabor Marton5254e642018-06-27 13:32:50 +00002983 assert(*RedeclIt == D);
2984
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002985 // Import the major distinguishing characteristics of this function.
2986 DeclContext *DC, *LexicalDC;
2987 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002988 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002989 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002990 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2991 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002992 if (ToD)
2993 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002994
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002995 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002996 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002997
Gabor Marton5254e642018-06-27 13:32:50 +00002998 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002999 // existing specialization in the "to" context. The lookup below will not
3000 // find any specialization, but would find the primary template; thus, we
3001 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00003002 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3003 if (D->getTemplatedKind() ==
3004 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003005 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3006 if (!FoundFunctionOrErr)
3007 return FoundFunctionOrErr.takeError();
3008 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00003009 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3010 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00003011 FoundByLookup = FoundFunction;
3012 }
3013 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003014 // Try to find a function in our own ("to") context with the same name, same
3015 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003016 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003017 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003018 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00003019 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003020 for (auto *FoundDecl : FoundDecls) {
3021 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003022 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003023
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003024 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003025 if (!hasSameVisibilityContext(FoundFunction, D))
3026 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003027
Gabor Marton458d1452019-02-14 13:07:03 +00003028 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00003029 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3030 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00003031 FoundByLookup = FoundFunction;
3032 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003033 }
Gabor Marton458d1452019-02-14 13:07:03 +00003034 // FIXME: Check for overloading more carefully, e.g., by boosting
3035 // Sema::IsOverload out to the AST library.
3036
3037 // Function overloading is okay in C++.
3038 if (Importer.getToContext().getLangOpts().CPlusPlus)
3039 continue;
3040
3041 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00003042 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003043 << Name << D->getType() << FoundFunction->getType();
3044 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
3045 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003046 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003047
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003048 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003049 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003050
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003051 if (!ConflictingDecls.empty()) {
3052 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003053 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003054 ConflictingDecls.size());
3055 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003056 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003057 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003058 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003059
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003060 // We do not allow more than one in-class declaration of a function. This is
3061 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3062 // assumes there is only one in-class declaration. Building a redecl
3063 // chain would result in more than one in-class declaration for
3064 // overrides (even if they are part of the same redecl chain inside the
3065 // derived class.)
3066 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003067 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003068 if (D->getLexicalDeclContext() == D->getDeclContext()) {
3069 if (!D->doesThisDeclarationHaveABody())
3070 return Importer.MapImported(D, FoundByLookup);
3071 else {
3072 // Let's continue and build up the redecl chain in this case.
3073 // FIXME Merge the functions into one decl.
3074 }
3075 }
3076 }
3077 }
3078
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003079 DeclarationNameInfo NameInfo(Name, Loc);
3080 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003081 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3082 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003083
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003084 QualType FromTy = D->getType();
3085 bool usedDifferentExceptionSpec = false;
3086
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003087 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003088 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3089 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3090 // FunctionDecl that we are importing the FunctionProtoType for.
3091 // To avoid an infinite recursion when importing, create the FunctionDecl
3092 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003093 if (FromEPI.ExceptionSpec.SourceDecl ||
3094 FromEPI.ExceptionSpec.SourceTemplate ||
3095 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003096 FunctionProtoType::ExtProtoInfo DefaultEPI;
3097 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003098 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003099 usedDifferentExceptionSpec = true;
3100 }
3101 }
3102
Balazs Keri3b30d652018-10-19 13:32:20 +00003103 QualType T;
3104 TypeSourceInfo *TInfo;
3105 SourceLocation ToInnerLocStart, ToEndLoc;
3106 NestedNameSpecifierLoc ToQualifierLoc;
3107 if (auto Imp = importSeq(
3108 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3109 D->getQualifierLoc(), D->getEndLoc()))
3110 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3111 else
3112 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003113
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003114 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003115 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003116 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003117 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3118 Parameters.push_back(*ToPOrErr);
3119 else
3120 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003121 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003122
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003123 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003124 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003125 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003126 Expr *ExplicitExpr = nullptr;
3127 if (FromConstructor->getExplicitSpecifier().getExpr()) {
3128 auto Imp = importSeq(FromConstructor->getExplicitSpecifier().getExpr());
3129 if (!Imp)
3130 return Imp.takeError();
3131 std::tie(ExplicitExpr) = *Imp;
3132 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003133 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Richard Smith76b90272019-05-09 03:59:21 +00003134 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3135 ToInnerLocStart, NameInfo, T, TInfo,
3136 ExplicitSpecifier(
3137 ExplicitExpr,
3138 FromConstructor->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003139 D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003140 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003141 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3142
3143 auto Imp =
3144 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3145 FromDtor->getOperatorDeleteThisArg());
3146
3147 if (!Imp)
3148 return Imp.takeError();
3149
3150 FunctionDecl *ToOperatorDelete;
3151 Expr *ToThisArg;
3152 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3153
Gabor Marton26f72a92018-07-12 09:42:05 +00003154 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003155 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3156 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3157 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003158 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003159
3160 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3161
3162 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003163 } else if (CXXConversionDecl *FromConversion =
3164 dyn_cast<CXXConversionDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003165 Expr *ExplicitExpr = nullptr;
3166 if (FromConversion->getExplicitSpecifier().getExpr()) {
3167 auto Imp = importSeq(FromConversion->getExplicitSpecifier().getExpr());
3168 if (!Imp)
3169 return Imp.takeError();
3170 std::tie(ExplicitExpr) = *Imp;
3171 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003172 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3173 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003174 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith76b90272019-05-09 03:59:21 +00003175 ExplicitSpecifier(ExplicitExpr,
3176 FromConversion->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003177 D->getConstexprKind(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003178 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003179 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003180 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3181 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003182 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003183 Method->isInlineSpecified(), D->getConstexprKind(),
3184 SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003185 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003186 } else {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003187 if (GetImportedOrCreateDecl(
3188 ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart,
3189 NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(),
3190 D->hasWrittenPrototype(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003191 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003192 }
John McCall3e11ebe2010-03-15 10:12:16 +00003193
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003194 // Connect the redecl chain.
3195 if (FoundByLookup) {
3196 auto *Recent = const_cast<FunctionDecl *>(
3197 FoundByLookup->getMostRecentDecl());
3198 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003199 // FIXME Probably we should merge exception specifications. E.g. In the
3200 // "To" context the existing function may have exception specification with
3201 // noexcept-unevaluated, while the newly imported function may have an
3202 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3203 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003204 }
3205
3206 // Import Ctor initializers.
3207 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3208 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3209 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3210 // Import first, then allocate memory and copy if there was no error.
3211 if (Error Err = ImportContainerChecked(
3212 FromConstructor->inits(), CtorInitializers))
3213 return std::move(Err);
3214 auto **Memory =
3215 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3216 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3217 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3218 ToCtor->setCtorInitializers(Memory);
3219 ToCtor->setNumCtorInitializers(NumInitializers);
3220 }
3221 }
3222
Balazs Keri3b30d652018-10-19 13:32:20 +00003223 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003224 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003225 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003226 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3227 ToFunction->setTrivial(D->isTrivial());
3228 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003229 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003230
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003231 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003232 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003233 Param->setOwningFunction(ToFunction);
3234 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003235 }
David Blaikie9c70e042011-09-21 18:16:56 +00003236 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003237
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003238 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3239 // params it refers to.
3240 if (TInfo) {
3241 if (auto ProtoLoc =
3242 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3243 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3244 ProtoLoc.setParam(I, Parameters[I]);
3245 }
3246 }
3247
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003248 if (usedDifferentExceptionSpec) {
3249 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003250 if (ExpectedType TyOrErr = import(D->getType()))
3251 ToFunction->setType(*TyOrErr);
3252 else
3253 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003254 }
3255
Balazs Keria35798d2018-07-17 09:52:41 +00003256 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003257 if (FromFT) {
3258 auto ToFTOrErr = import(FromFT);
3259 if (!ToFTOrErr)
3260 return ToFTOrErr.takeError();
3261 }
Balazs Keria35798d2018-07-17 09:52:41 +00003262
Gabor Marton5254e642018-06-27 13:32:50 +00003263 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003264 Error Err = ImportFunctionDeclBody(D, ToFunction);
3265
3266 if (Err)
3267 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003268 }
3269
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003270 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003271
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003272 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003273 if (Error Err = ImportTemplateInformation(D, ToFunction))
3274 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003275
Gabor Marton5254e642018-06-27 13:32:50 +00003276 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3277
3278 // TODO Can we generalize this approach to other AST nodes as well?
3279 if (D->getDeclContext()->containsDeclAndLoad(D))
3280 DC->addDeclInternal(ToFunction);
3281 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003282 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003283
Gabor Marton5254e642018-06-27 13:32:50 +00003284 // Friend declaration's lexical context is the befriending class, but the
3285 // semantic context is the enclosing scope of the befriending class.
3286 // We want the friend functions to be found in the semantic context by lookup.
3287 // FIXME should we handle this generically in VisitFriendDecl?
3288 // In Other cases when LexicalDC != DC we don't want it to be added,
3289 // e.g out-of-class definitions like void B::f() {} .
3290 if (LexicalDC != DC && IsFriend) {
3291 DC->makeDeclVisibleInContext(ToFunction);
3292 }
3293
Gabor Marton7a0841e2018-10-29 10:18:28 +00003294 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3295 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3296
Gabor Marton5254e642018-06-27 13:32:50 +00003297 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003298 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3299 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3300 if (!ToRedeclOrErr)
3301 return ToRedeclOrErr.takeError();
3302 }
Gabor Marton5254e642018-06-27 13:32:50 +00003303
Douglas Gregor43f54792010-02-17 02:12:47 +00003304 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003305}
3306
Balazs Keri3b30d652018-10-19 13:32:20 +00003307ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003308 return VisitFunctionDecl(D);
3309}
3310
Balazs Keri3b30d652018-10-19 13:32:20 +00003311ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003312 return VisitCXXMethodDecl(D);
3313}
3314
Balazs Keri3b30d652018-10-19 13:32:20 +00003315ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003316 return VisitCXXMethodDecl(D);
3317}
3318
Balazs Keri3b30d652018-10-19 13:32:20 +00003319ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003320 return VisitCXXMethodDecl(D);
3321}
3322
Balazs Keri3b30d652018-10-19 13:32:20 +00003323ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003324 // Import the major distinguishing characteristics of a variable.
3325 DeclContext *DC, *LexicalDC;
3326 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003327 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003328 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003329 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3330 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003331 if (ToD)
3332 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003333
Fangrui Song6907ce22018-07-30 19:24:48 +00003334 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003335 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003336 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003337 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003338 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003339 if (!Name &&
3340 ASTImporter::getFieldIndex(D) !=
3341 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003342 continue;
3343
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003344 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003345 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003346 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003347 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3348 // initializer of a FieldDecl might not had been instantiated in the
3349 // "To" context. However, the "From" context might instantiated that,
3350 // thus we have to merge that.
3351 if (Expr *FromInitializer = D->getInClassInitializer()) {
3352 // We don't have yet the initializer set.
3353 if (FoundField->hasInClassInitializer() &&
3354 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003355 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3356 FoundField->setInClassInitializer(*ToInitializerOrErr);
3357 else {
3358 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003359 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003360 // FIXME: warning message?
3361 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003362 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003363 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003364 }
3365 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003366 return FoundField;
3367 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003368
Balazs Keri3b30d652018-10-19 13:32:20 +00003369 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003370 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003371 << Name << D->getType() << FoundField->getType();
3372 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3373 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003374
3375 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003376 }
3377 }
3378
Balazs Keri3b30d652018-10-19 13:32:20 +00003379 QualType ToType;
3380 TypeSourceInfo *ToTInfo;
3381 Expr *ToBitWidth;
3382 SourceLocation ToInnerLocStart;
3383 Expr *ToInitializer;
3384 if (auto Imp = importSeq(
3385 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3386 D->getInnerLocStart(), D->getInClassInitializer()))
3387 std::tie(
3388 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3389 else
3390 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003391
Gabor Marton26f72a92018-07-12 09:42:05 +00003392 FieldDecl *ToField;
3393 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003394 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3395 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3396 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003397 return ToField;
3398
Douglas Gregordd483172010-02-22 17:42:47 +00003399 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003400 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003401 if (ToInitializer)
3402 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003403 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003404 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003405 return ToField;
3406}
3407
Balazs Keri3b30d652018-10-19 13:32:20 +00003408ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003409 // Import the major distinguishing characteristics of a variable.
3410 DeclContext *DC, *LexicalDC;
3411 DeclarationName Name;
3412 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003413 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003414 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3415 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003416 if (ToD)
3417 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003418
Fangrui Song6907ce22018-07-30 19:24:48 +00003419 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003420 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003421 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003422 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003423 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003424 if (!Name &&
3425 ASTImporter::getFieldIndex(D) !=
3426 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003427 continue;
3428
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003429 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003430 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003431 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003432 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003433 return FoundField;
3434 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003435
3436 // If there are more anonymous fields to check, continue.
3437 if (!Name && I < N-1)
3438 continue;
3439
Balazs Keri3b30d652018-10-19 13:32:20 +00003440 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003441 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003442 << Name << D->getType() << FoundField->getType();
3443 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3444 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003445
3446 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003447 }
3448 }
3449
Francois Pichet783dd6e2010-11-21 06:08:52 +00003450 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003451 auto TypeOrErr = import(D->getType());
3452 if (!TypeOrErr)
3453 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003454
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003455 auto **NamedChain =
3456 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003457
3458 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003459 for (auto *PI : D->chain())
3460 if (Expected<NamedDecl *> ToD = import(PI))
3461 NamedChain[i++] = *ToD;
3462 else
3463 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003464
Gabor Marton26f72a92018-07-12 09:42:05 +00003465 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3466 IndirectFieldDecl *ToIndirectField;
3467 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003468 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003469 // FIXME here we leak `NamedChain` which is allocated before
3470 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003471
Francois Pichet783dd6e2010-11-21 06:08:52 +00003472 ToIndirectField->setAccess(D->getAccess());
3473 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003474 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003475 return ToIndirectField;
3476}
3477
Balazs Keri3b30d652018-10-19 13:32:20 +00003478ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003479 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003480 DeclContext *DC, *LexicalDC;
3481 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3482 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003483
3484 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003485 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003486 auto *RD = cast<CXXRecordDecl>(DC);
3487 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003488
3489 while (ImportedFriend) {
3490 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003491 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3492 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003493 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003494
3495 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3496 if (Importer.IsStructurallyEquivalent(
3497 D->getFriendType()->getType(),
3498 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003499 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003500 }
3501 ImportedFriend = ImportedFriend->getNextFriend();
3502 }
3503
3504 // Not found. Create it.
3505 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003506 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003507 NamedDecl *ToFriendD;
3508 if (Error Err = importInto(ToFriendD, FriendD))
3509 return std::move(Err);
3510
3511 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003512 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3513 ToFriendD->setObjectOfFriendDecl(false);
3514
3515 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003516 } else { // The friend is a type, not a decl.
3517 if (auto TSIOrErr = import(D->getFriendType()))
3518 ToFU = *TSIOrErr;
3519 else
3520 return TSIOrErr.takeError();
3521 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003522
3523 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003524 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003525 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003526 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003527 ToTPLists[I] = *ListOrErr;
3528 else
3529 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003530 }
3531
Balazs Keri3b30d652018-10-19 13:32:20 +00003532 auto LocationOrErr = import(D->getLocation());
3533 if (!LocationOrErr)
3534 return LocationOrErr.takeError();
3535 auto FriendLocOrErr = import(D->getFriendLoc());
3536 if (!FriendLocOrErr)
3537 return FriendLocOrErr.takeError();
3538
Gabor Marton26f72a92018-07-12 09:42:05 +00003539 FriendDecl *FrD;
3540 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003541 *LocationOrErr, ToFU,
3542 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003543 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003544
3545 FrD->setAccess(D->getAccess());
3546 FrD->setLexicalDeclContext(LexicalDC);
3547 LexicalDC->addDeclInternal(FrD);
3548 return FrD;
3549}
3550
Balazs Keri3b30d652018-10-19 13:32:20 +00003551ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003552 // Import the major distinguishing characteristics of an ivar.
3553 DeclContext *DC, *LexicalDC;
3554 DeclarationName Name;
3555 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003556 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003557 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3558 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003559 if (ToD)
3560 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003561
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003562 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003563 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003564 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003565 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003566 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003567 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003568 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003569 return FoundIvar;
3570 }
3571
Gabor Marton410f32c2019-04-01 15:29:55 +00003572 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003573 << Name << D->getType() << FoundIvar->getType();
3574 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3575 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003576
3577 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003578 }
3579 }
3580
Balazs Keri3b30d652018-10-19 13:32:20 +00003581 QualType ToType;
3582 TypeSourceInfo *ToTypeSourceInfo;
3583 Expr *ToBitWidth;
3584 SourceLocation ToInnerLocStart;
3585 if (auto Imp = importSeq(
3586 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3587 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3588 else
3589 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003590
Gabor Marton26f72a92018-07-12 09:42:05 +00003591 ObjCIvarDecl *ToIvar;
3592 if (GetImportedOrCreateDecl(
3593 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003594 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3595 ToType, ToTypeSourceInfo,
3596 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003597 return ToIvar;
3598
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003599 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003600 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003601 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003602}
3603
Balazs Keri3b30d652018-10-19 13:32:20 +00003604ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003605
3606 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3607 auto RedeclIt = Redecls.begin();
3608 // Import the first part of the decl chain. I.e. import all previous
3609 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003610 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3611 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3612 if (!RedeclOrErr)
3613 return RedeclOrErr.takeError();
3614 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003615 assert(*RedeclIt == D);
3616
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003617 // Import the major distinguishing characteristics of a variable.
3618 DeclContext *DC, *LexicalDC;
3619 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003620 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003621 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003622 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3623 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003624 if (ToD)
3625 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003626
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003627 // Try to find a variable in our own ("to") context with the same name and
3628 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003629 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003630 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003631 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003632 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003633 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003634 for (auto *FoundDecl : FoundDecls) {
3635 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003636 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003637
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003638 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003639 if (!hasSameVisibilityContext(FoundVar, D))
3640 continue;
3641 if (Importer.IsStructurallyEquivalent(D->getType(),
3642 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003643
Gabor Marton458d1452019-02-14 13:07:03 +00003644 // The VarDecl in the "From" context has a definition, but in the
3645 // "To" context we already have a definition.
3646 VarDecl *FoundDef = FoundVar->getDefinition();
3647 if (D->isThisDeclarationADefinition() && FoundDef)
3648 // FIXME Check for ODR error if the two definitions have
3649 // different initializers?
3650 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003651
Gabor Marton458d1452019-02-14 13:07:03 +00003652 // The VarDecl in the "From" context has an initializer, but in the
3653 // "To" context we already have an initializer.
3654 const VarDecl *FoundDInit = nullptr;
3655 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3656 // FIXME Diagnose ODR error if the two initializers are different?
3657 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3658
3659 FoundByLookup = FoundVar;
3660 break;
3661 }
3662
3663 const ArrayType *FoundArray
3664 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3665 const ArrayType *TArray
3666 = Importer.getToContext().getAsArrayType(D->getType());
3667 if (FoundArray && TArray) {
3668 if (isa<IncompleteArrayType>(FoundArray) &&
3669 isa<ConstantArrayType>(TArray)) {
3670 // Import the type.
3671 if (auto TyOrErr = import(D->getType()))
3672 FoundVar->setType(*TyOrErr);
3673 else
3674 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003675
3676 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003677 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003678 } else if (isa<IncompleteArrayType>(TArray) &&
3679 isa<ConstantArrayType>(FoundArray)) {
3680 FoundByLookup = FoundVar;
3681 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003682 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003683 }
Gabor Marton458d1452019-02-14 13:07:03 +00003684
Gabor Marton410f32c2019-04-01 15:29:55 +00003685 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003686 << Name << D->getType() << FoundVar->getType();
3687 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3688 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003689 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003690
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003691 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003692 }
3693
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003694 if (!ConflictingDecls.empty()) {
3695 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003696 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003697 ConflictingDecls.size());
3698 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003699 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003700 }
3701 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003702
Balazs Keri3b30d652018-10-19 13:32:20 +00003703 QualType ToType;
3704 TypeSourceInfo *ToTypeSourceInfo;
3705 SourceLocation ToInnerLocStart;
3706 NestedNameSpecifierLoc ToQualifierLoc;
3707 if (auto Imp = importSeq(
3708 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3709 D->getQualifierLoc()))
3710 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3711 else
3712 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003713
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003714 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003715 VarDecl *ToVar;
3716 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003717 ToInnerLocStart, Loc,
3718 Name.getAsIdentifierInfo(),
3719 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003720 D->getStorageClass()))
3721 return ToVar;
3722
Balazs Keri3b30d652018-10-19 13:32:20 +00003723 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003724 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003725 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003726
Gabor Martonac3a5d62018-09-17 12:04:52 +00003727 if (FoundByLookup) {
3728 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3729 ToVar->setPreviousDecl(Recent);
3730 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003731
Balazs Keri3b30d652018-10-19 13:32:20 +00003732 if (Error Err = ImportInitializer(D, ToVar))
3733 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003734
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003735 if (D->isConstexpr())
3736 ToVar->setConstexpr(true);
3737
Gabor Martonac3a5d62018-09-17 12:04:52 +00003738 if (D->getDeclContext()->containsDeclAndLoad(D))
3739 DC->addDeclInternal(ToVar);
3740 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3741 LexicalDC->addDeclInternal(ToVar);
3742
3743 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003744 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3745 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3746 if (!RedeclOrErr)
3747 return RedeclOrErr.takeError();
3748 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003749
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003750 return ToVar;
3751}
3752
Balazs Keri3b30d652018-10-19 13:32:20 +00003753ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003754 // Parameters are created in the translation unit's context, then moved
3755 // into the function declaration's context afterward.
3756 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003757
Balazs Keri3b30d652018-10-19 13:32:20 +00003758 DeclarationName ToDeclName;
3759 SourceLocation ToLocation;
3760 QualType ToType;
3761 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3762 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3763 else
3764 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003765
Douglas Gregor8b228d72010-02-17 21:22:52 +00003766 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003767 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003768 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3769 ToLocation, ToDeclName.getAsIdentifierInfo(),
3770 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003771 return ToParm;
3772 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003773}
3774
Balazs Keri3b30d652018-10-19 13:32:20 +00003775ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003776 // Parameters are created in the translation unit's context, then moved
3777 // into the function declaration's context afterward.
3778 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003779
Balazs Keri3b30d652018-10-19 13:32:20 +00003780 DeclarationName ToDeclName;
3781 SourceLocation ToLocation, ToInnerLocStart;
3782 QualType ToType;
3783 TypeSourceInfo *ToTypeSourceInfo;
3784 if (auto Imp = importSeq(
3785 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3786 D->getTypeSourceInfo()))
3787 std::tie(
3788 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3789 ToTypeSourceInfo) = *Imp;
3790 else
3791 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003792
Gabor Marton26f72a92018-07-12 09:42:05 +00003793 ParmVarDecl *ToParm;
3794 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003795 ToInnerLocStart, ToLocation,
3796 ToDeclName.getAsIdentifierInfo(), ToType,
3797 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003798 /*DefaultArg*/ nullptr))
3799 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003800
3801 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003802 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003803 ToParm->setKNRPromoted(D->isKNRPromoted());
3804
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003805 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003806 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3807 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3808 else
3809 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003810 } else if (D->hasUnparsedDefaultArg()) {
3811 ToParm->setUnparsedDefaultArg();
3812 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003813 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3814 ToParm->setDefaultArg(*ToDefArgOrErr);
3815 else
3816 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003817 }
Sean Callanan59721b32015-04-28 18:41:46 +00003818
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003819 if (D->isObjCMethodParameter()) {
3820 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3821 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3822 } else {
3823 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3824 D->getFunctionScopeIndex());
3825 }
3826
Gabor Marton26f72a92018-07-12 09:42:05 +00003827 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003828}
3829
Balazs Keri3b30d652018-10-19 13:32:20 +00003830ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003831 // Import the major distinguishing characteristics of a method.
3832 DeclContext *DC, *LexicalDC;
3833 DeclarationName Name;
3834 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003835 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003836 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3837 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003838 if (ToD)
3839 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003840
Gabor Marton54058b52018-12-17 13:53:12 +00003841 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003842 for (auto *FoundDecl : FoundDecls) {
3843 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003844 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3845 continue;
3846
3847 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003848 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3849 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003850 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003851 << D->isInstanceMethod() << Name << D->getReturnType()
3852 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003853 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003854 diag::note_odr_objc_method_here)
3855 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003856
3857 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003858 }
3859
3860 // Check the number of parameters.
3861 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003862 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003863 << D->isInstanceMethod() << Name
3864 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003865 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003866 diag::note_odr_objc_method_here)
3867 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003868
3869 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003870 }
3871
3872 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003873 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003874 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3875 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003876 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003877 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003878 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003879 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003880 << D->isInstanceMethod() << Name
3881 << (*P)->getType() << (*FoundP)->getType();
3882 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3883 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003884
3885 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003886 }
3887 }
3888
3889 // Check variadic/non-variadic.
3890 // Check the number of parameters.
3891 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003892 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003893 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003894 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003895 diag::note_odr_objc_method_here)
3896 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003897
3898 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003899 }
3900
3901 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003902 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003903 }
3904 }
3905
Balazs Keri3b30d652018-10-19 13:32:20 +00003906 SourceLocation ToEndLoc;
3907 QualType ToReturnType;
3908 TypeSourceInfo *ToReturnTypeSourceInfo;
3909 if (auto Imp = importSeq(
3910 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3911 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3912 else
3913 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003914
Gabor Marton26f72a92018-07-12 09:42:05 +00003915 ObjCMethodDecl *ToMethod;
3916 if (GetImportedOrCreateDecl(
3917 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003918 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3919 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003920 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3921 D->getImplementationControl(), D->hasRelatedResultType()))
3922 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003923
3924 // FIXME: When we decide to merge method definitions, we'll need to
3925 // deal with implicit parameters.
3926
3927 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003928 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003929 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003930 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3931 ToParams.push_back(*ToPOrErr);
3932 else
3933 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003934 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003935
Douglas Gregor43f54792010-02-17 02:12:47 +00003936 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003937 for (auto *ToParam : ToParams) {
3938 ToParam->setOwningFunction(ToMethod);
3939 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003940 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003941
Balazs Keri3b30d652018-10-19 13:32:20 +00003942 SmallVector<SourceLocation, 12> FromSelLocs;
3943 D->getSelectorLocs(FromSelLocs);
3944 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3945 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3946 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003947
Balazs Keri3b30d652018-10-19 13:32:20 +00003948 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003949
3950 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003951 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003952 return ToMethod;
3953}
3954
Balazs Keri3b30d652018-10-19 13:32:20 +00003955ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003956 // Import the major distinguishing characteristics of a category.
3957 DeclContext *DC, *LexicalDC;
3958 DeclarationName Name;
3959 SourceLocation Loc;
3960 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003961 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3962 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003963 if (ToD)
3964 return ToD;
3965
Balazs Keri3b30d652018-10-19 13:32:20 +00003966 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3967 TypeSourceInfo *ToTypeSourceInfo;
3968 if (auto Imp = importSeq(
3969 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3970 D->getTypeSourceInfo()))
3971 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3972 else
3973 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003974
Gabor Marton26f72a92018-07-12 09:42:05 +00003975 ObjCTypeParamDecl *Result;
3976 if (GetImportedOrCreateDecl(
3977 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003978 ToVarianceLoc, D->getIndex(),
3979 ToLocation, Name.getAsIdentifierInfo(),
3980 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003981 return Result;
3982
Douglas Gregor85f3f952015-07-07 03:57:15 +00003983 Result->setLexicalDeclContext(LexicalDC);
3984 return Result;
3985}
3986
Balazs Keri3b30d652018-10-19 13:32:20 +00003987ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003988 // Import the major distinguishing characteristics of a category.
3989 DeclContext *DC, *LexicalDC;
3990 DeclarationName Name;
3991 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003992 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003993 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3994 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003995 if (ToD)
3996 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003997
Balazs Keri3b30d652018-10-19 13:32:20 +00003998 ObjCInterfaceDecl *ToInterface;
3999 if (Error Err = importInto(ToInterface, D->getClassInterface()))
4000 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004001
Douglas Gregor84c51c32010-02-18 01:47:50 +00004002 // Determine if we've already encountered this category.
4003 ObjCCategoryDecl *MergeWithCategory
4004 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
4005 ObjCCategoryDecl *ToCategory = MergeWithCategory;
4006 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004007 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
4008 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4009 if (auto Imp = importSeq(
4010 D->getAtStartLoc(), D->getCategoryNameLoc(),
4011 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4012 std::tie(
4013 ToAtStartLoc, ToCategoryNameLoc,
4014 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4015 else
4016 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004017
4018 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004019 ToAtStartLoc, Loc,
4020 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004021 Name.getAsIdentifierInfo(), ToInterface,
4022 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004023 ToIvarLBraceLoc,
4024 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004025 return ToCategory;
4026
Douglas Gregor84c51c32010-02-18 01:47:50 +00004027 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004028 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004029 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004030 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004031 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4032 ToCategory->setTypeParamList(*PListOrErr);
4033 else
4034 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004035
Douglas Gregor84c51c32010-02-18 01:47:50 +00004036 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004037 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4038 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004039 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4040 = D->protocol_loc_begin();
4041 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4042 FromProtoEnd = D->protocol_end();
4043 FromProto != FromProtoEnd;
4044 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004045 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4046 Protocols.push_back(*ToProtoOrErr);
4047 else
4048 return ToProtoOrErr.takeError();
4049
4050 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4051 ProtocolLocs.push_back(*ToProtoLocOrErr);
4052 else
4053 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004054 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004055
Douglas Gregor84c51c32010-02-18 01:47:50 +00004056 // FIXME: If we're merging, make sure that the protocol list is the same.
4057 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4058 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004059
Douglas Gregor84c51c32010-02-18 01:47:50 +00004060 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004061 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004062 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004063
Douglas Gregor84c51c32010-02-18 01:47:50 +00004064 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004065 if (Error Err = ImportDeclContext(D))
4066 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004067
Douglas Gregor84c51c32010-02-18 01:47:50 +00004068 // If we have an implementation, import it as well.
4069 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004070 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4071 import(D->getImplementation()))
4072 ToCategory->setImplementation(*ToImplOrErr);
4073 else
4074 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004075 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004076
Douglas Gregor84c51c32010-02-18 01:47:50 +00004077 return ToCategory;
4078}
4079
Balazs Keri3b30d652018-10-19 13:32:20 +00004080Error ASTNodeImporter::ImportDefinition(
4081 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004082 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004083 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004084 if (Error Err = ImportDeclContext(From))
4085 return Err;
4086 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004087 }
4088
4089 // Start the protocol definition
4090 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004091
Douglas Gregor2aa53772012-01-24 17:42:07 +00004092 // Import protocols
4093 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4094 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004095 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4096 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004097 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4098 FromProtoEnd = From->protocol_end();
4099 FromProto != FromProtoEnd;
4100 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004101 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4102 Protocols.push_back(*ToProtoOrErr);
4103 else
4104 return ToProtoOrErr.takeError();
4105
4106 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4107 ProtocolLocs.push_back(*ToProtoLocOrErr);
4108 else
4109 return ToProtoLocOrErr.takeError();
4110
Douglas Gregor2aa53772012-01-24 17:42:07 +00004111 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004112
Douglas Gregor2aa53772012-01-24 17:42:07 +00004113 // FIXME: If we're merging, make sure that the protocol list is the same.
4114 To->setProtocolList(Protocols.data(), Protocols.size(),
4115 ProtocolLocs.data(), Importer.getToContext());
4116
Douglas Gregor2e15c842012-02-01 21:00:38 +00004117 if (shouldForceImportDeclContext(Kind)) {
4118 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004119 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4120 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004121 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004122 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004123}
4124
Balazs Keri3b30d652018-10-19 13:32:20 +00004125ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004126 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004127 // from, but this particular declaration is not that definition, import the
4128 // definition and map to that.
4129 ObjCProtocolDecl *Definition = D->getDefinition();
4130 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004131 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4132 return Importer.MapImported(D, *ImportedDefOrErr);
4133 else
4134 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004135 }
4136
Douglas Gregor84c51c32010-02-18 01:47:50 +00004137 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004138 DeclContext *DC, *LexicalDC;
4139 DeclarationName Name;
4140 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004141 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004142 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4143 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004144 if (ToD)
4145 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004146
Craig Topper36250ad2014-05-12 05:36:57 +00004147 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004148 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004149 for (auto *FoundDecl : FoundDecls) {
4150 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004151 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004152
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004153 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004154 break;
4155 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004156
Douglas Gregor98d156a2010-02-17 16:12:00 +00004157 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004158 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004159 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4160 if (!ToAtBeginLocOrErr)
4161 return ToAtBeginLocOrErr.takeError();
4162
Gabor Marton26f72a92018-07-12 09:42:05 +00004163 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4164 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004165 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004166 /*PrevDecl=*/nullptr))
4167 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004168 ToProto->setLexicalDeclContext(LexicalDC);
4169 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004170 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004171
4172 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004173
Balazs Keri3b30d652018-10-19 13:32:20 +00004174 if (D->isThisDeclarationADefinition())
4175 if (Error Err = ImportDefinition(D, ToProto))
4176 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004177
Douglas Gregor98d156a2010-02-17 16:12:00 +00004178 return ToProto;
4179}
4180
Balazs Keri3b30d652018-10-19 13:32:20 +00004181ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4182 DeclContext *DC, *LexicalDC;
4183 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4184 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004185
Balazs Keri3b30d652018-10-19 13:32:20 +00004186 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4187 if (!ExternLocOrErr)
4188 return ExternLocOrErr.takeError();
4189
4190 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4191 if (!LangLocOrErr)
4192 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004193
4194 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004195
4196 LinkageSpecDecl *ToLinkageSpec;
4197 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004198 *ExternLocOrErr, *LangLocOrErr,
4199 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004200 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004201
4202 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004203 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4204 if (!RBraceLocOrErr)
4205 return RBraceLocOrErr.takeError();
4206 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004207 }
4208
4209 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4210 LexicalDC->addDeclInternal(ToLinkageSpec);
4211
Sean Callanan0aae0412014-12-10 00:00:37 +00004212 return ToLinkageSpec;
4213}
4214
Balazs Keri3b30d652018-10-19 13:32:20 +00004215ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004216 DeclContext *DC, *LexicalDC;
4217 DeclarationName Name;
4218 SourceLocation Loc;
4219 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004220 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4221 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004222 if (ToD)
4223 return ToD;
4224
Balazs Keri3b30d652018-10-19 13:32:20 +00004225 SourceLocation ToLoc, ToUsingLoc;
4226 NestedNameSpecifierLoc ToQualifierLoc;
4227 if (auto Imp = importSeq(
4228 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4229 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4230 else
4231 return Imp.takeError();
4232
4233 DeclarationNameInfo NameInfo(Name, ToLoc);
4234 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4235 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004236
Gabor Marton26f72a92018-07-12 09:42:05 +00004237 UsingDecl *ToUsing;
4238 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004239 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004240 D->hasTypename()))
4241 return ToUsing;
4242
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004243 ToUsing->setLexicalDeclContext(LexicalDC);
4244 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004245
4246 if (NamedDecl *FromPattern =
4247 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004248 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4249 Importer.getToContext().setInstantiatedFromUsingDecl(
4250 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004251 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004252 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004253 }
4254
Balazs Keri3b30d652018-10-19 13:32:20 +00004255 for (UsingShadowDecl *FromShadow : D->shadows()) {
4256 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4257 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004258 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004259 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004260 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004261 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004262 }
4263 return ToUsing;
4264}
4265
Balazs Keri3b30d652018-10-19 13:32:20 +00004266ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004267 DeclContext *DC, *LexicalDC;
4268 DeclarationName Name;
4269 SourceLocation Loc;
4270 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004271 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4272 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004273 if (ToD)
4274 return ToD;
4275
Balazs Keri3b30d652018-10-19 13:32:20 +00004276 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4277 if (!ToUsingOrErr)
4278 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004279
Balazs Keri3b30d652018-10-19 13:32:20 +00004280 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4281 if (!ToTargetOrErr)
4282 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004283
Gabor Marton26f72a92018-07-12 09:42:05 +00004284 UsingShadowDecl *ToShadow;
4285 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004286 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004287 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004288
4289 ToShadow->setLexicalDeclContext(LexicalDC);
4290 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004291
4292 if (UsingShadowDecl *FromPattern =
4293 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004294 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4295 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4296 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004297 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004298 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004299 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004300 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004301 }
4302
4303 LexicalDC->addDeclInternal(ToShadow);
4304
4305 return ToShadow;
4306}
4307
Balazs Keri3b30d652018-10-19 13:32:20 +00004308ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004309 DeclContext *DC, *LexicalDC;
4310 DeclarationName Name;
4311 SourceLocation Loc;
4312 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004313 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4314 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004315 if (ToD)
4316 return ToD;
4317
Balazs Keri3b30d652018-10-19 13:32:20 +00004318 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4319 if (!ToComAncestorOrErr)
4320 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004321
Balazs Keri3b30d652018-10-19 13:32:20 +00004322 NamespaceDecl *ToNominatedNamespace;
4323 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4324 NestedNameSpecifierLoc ToQualifierLoc;
4325 if (auto Imp = importSeq(
4326 D->getNominatedNamespace(), D->getUsingLoc(),
4327 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4328 D->getIdentLocation()))
4329 std::tie(
4330 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4331 ToQualifierLoc, ToIdentLocation) = *Imp;
4332 else
4333 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004334
Gabor Marton26f72a92018-07-12 09:42:05 +00004335 UsingDirectiveDecl *ToUsingDir;
4336 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004337 ToUsingLoc,
4338 ToNamespaceKeyLocation,
4339 ToQualifierLoc,
4340 ToIdentLocation,
4341 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004342 return ToUsingDir;
4343
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004344 ToUsingDir->setLexicalDeclContext(LexicalDC);
4345 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004346
4347 return ToUsingDir;
4348}
4349
Balazs Keri3b30d652018-10-19 13:32:20 +00004350ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004351 UnresolvedUsingValueDecl *D) {
4352 DeclContext *DC, *LexicalDC;
4353 DeclarationName Name;
4354 SourceLocation Loc;
4355 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004356 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4357 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004358 if (ToD)
4359 return ToD;
4360
Balazs Keri3b30d652018-10-19 13:32:20 +00004361 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4362 NestedNameSpecifierLoc ToQualifierLoc;
4363 if (auto Imp = importSeq(
4364 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4365 D->getEllipsisLoc()))
4366 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4367 else
4368 return Imp.takeError();
4369
4370 DeclarationNameInfo NameInfo(Name, ToLoc);
4371 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4372 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004373
Gabor Marton26f72a92018-07-12 09:42:05 +00004374 UnresolvedUsingValueDecl *ToUsingValue;
4375 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004376 ToUsingLoc, ToQualifierLoc, NameInfo,
4377 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004378 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004379
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004380 ToUsingValue->setAccess(D->getAccess());
4381 ToUsingValue->setLexicalDeclContext(LexicalDC);
4382 LexicalDC->addDeclInternal(ToUsingValue);
4383
4384 return ToUsingValue;
4385}
4386
Balazs Keri3b30d652018-10-19 13:32:20 +00004387ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004388 UnresolvedUsingTypenameDecl *D) {
4389 DeclContext *DC, *LexicalDC;
4390 DeclarationName Name;
4391 SourceLocation Loc;
4392 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004393 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4394 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004395 if (ToD)
4396 return ToD;
4397
Balazs Keri3b30d652018-10-19 13:32:20 +00004398 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4399 NestedNameSpecifierLoc ToQualifierLoc;
4400 if (auto Imp = importSeq(
4401 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4402 D->getEllipsisLoc()))
4403 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4404 else
4405 return Imp.takeError();
4406
Gabor Marton26f72a92018-07-12 09:42:05 +00004407 UnresolvedUsingTypenameDecl *ToUsing;
4408 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004409 ToUsingLoc, ToTypenameLoc,
4410 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004411 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004412
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004413 ToUsing->setAccess(D->getAccess());
4414 ToUsing->setLexicalDeclContext(LexicalDC);
4415 LexicalDC->addDeclInternal(ToUsing);
4416
4417 return ToUsing;
4418}
4419
Balazs Keri3b30d652018-10-19 13:32:20 +00004420
4421Error ASTNodeImporter::ImportDefinition(
4422 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004423 if (To->getDefinition()) {
4424 // Check consistency of superclass.
4425 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4426 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004427 if (auto FromSuperOrErr = import(FromSuper))
4428 FromSuper = *FromSuperOrErr;
4429 else
4430 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004431 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004432
4433 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004434 if ((bool)FromSuper != (bool)ToSuper ||
4435 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004436 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004437 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004438 << To->getDeclName();
4439 if (ToSuper)
4440 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4441 << To->getSuperClass()->getDeclName();
4442 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004443 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004444 diag::note_odr_objc_missing_superclass);
4445 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004446 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004447 diag::note_odr_objc_superclass)
4448 << From->getSuperClass()->getDeclName();
4449 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004450 Importer.FromDiag(From->getLocation(),
4451 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004452 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004453
Douglas Gregor2e15c842012-02-01 21:00:38 +00004454 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004455 if (Error Err = ImportDeclContext(From))
4456 return Err;
4457 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004458 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004459
Douglas Gregor2aa53772012-01-24 17:42:07 +00004460 // Start the definition.
4461 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004462
Douglas Gregor2aa53772012-01-24 17:42:07 +00004463 // If this class has a superclass, import it.
4464 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004465 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4466 To->setSuperClass(*SuperTInfoOrErr);
4467 else
4468 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004469 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004470
Douglas Gregor2aa53772012-01-24 17:42:07 +00004471 // Import protocols
4472 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4473 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004474 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4475 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004476
Douglas Gregor2aa53772012-01-24 17:42:07 +00004477 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4478 FromProtoEnd = From->protocol_end();
4479 FromProto != FromProtoEnd;
4480 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004481 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4482 Protocols.push_back(*ToProtoOrErr);
4483 else
4484 return ToProtoOrErr.takeError();
4485
4486 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4487 ProtocolLocs.push_back(*ToProtoLocOrErr);
4488 else
4489 return ToProtoLocOrErr.takeError();
4490
Douglas Gregor2aa53772012-01-24 17:42:07 +00004491 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004492
Douglas Gregor2aa53772012-01-24 17:42:07 +00004493 // FIXME: If we're merging, make sure that the protocol list is the same.
4494 To->setProtocolList(Protocols.data(), Protocols.size(),
4495 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004496
Douglas Gregor2aa53772012-01-24 17:42:07 +00004497 // Import categories. When the categories themselves are imported, they'll
4498 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004499 for (auto *Cat : From->known_categories()) {
4500 auto ToCatOrErr = import(Cat);
4501 if (!ToCatOrErr)
4502 return ToCatOrErr.takeError();
4503 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004504
Douglas Gregor2aa53772012-01-24 17:42:07 +00004505 // If we have an @implementation, import it as well.
4506 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004507 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4508 import(From->getImplementation()))
4509 To->setImplementation(*ToImplOrErr);
4510 else
4511 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004512 }
4513
Douglas Gregor2e15c842012-02-01 21:00:38 +00004514 if (shouldForceImportDeclContext(Kind)) {
4515 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004516 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4517 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004518 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004519 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004520}
4521
Balazs Keri3b30d652018-10-19 13:32:20 +00004522Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004523ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4524 if (!list)
4525 return nullptr;
4526
4527 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004528 for (auto *fromTypeParam : *list) {
4529 if (auto toTypeParamOrErr = import(fromTypeParam))
4530 toTypeParams.push_back(*toTypeParamOrErr);
4531 else
4532 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004533 }
4534
Balazs Keri3b30d652018-10-19 13:32:20 +00004535 auto LAngleLocOrErr = import(list->getLAngleLoc());
4536 if (!LAngleLocOrErr)
4537 return LAngleLocOrErr.takeError();
4538
4539 auto RAngleLocOrErr = import(list->getRAngleLoc());
4540 if (!RAngleLocOrErr)
4541 return RAngleLocOrErr.takeError();
4542
Douglas Gregor85f3f952015-07-07 03:57:15 +00004543 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004544 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004545 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004546 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004547}
4548
Balazs Keri3b30d652018-10-19 13:32:20 +00004549ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004550 // If this class has a definition in the translation unit we're coming from,
4551 // but this particular declaration is not that definition, import the
4552 // definition and map to that.
4553 ObjCInterfaceDecl *Definition = D->getDefinition();
4554 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004555 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4556 return Importer.MapImported(D, *ImportedDefOrErr);
4557 else
4558 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004559 }
4560
Douglas Gregor45635322010-02-16 01:20:57 +00004561 // Import the major distinguishing characteristics of an @interface.
4562 DeclContext *DC, *LexicalDC;
4563 DeclarationName Name;
4564 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004565 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004566 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4567 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004568 if (ToD)
4569 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004570
Douglas Gregor2aa53772012-01-24 17:42:07 +00004571 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004572 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004573 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004574 for (auto *FoundDecl : FoundDecls) {
4575 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004576 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004577
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004578 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004579 break;
4580 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004581
Douglas Gregor2aa53772012-01-24 17:42:07 +00004582 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004583 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004584 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004585 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4586 if (!AtBeginLocOrErr)
4587 return AtBeginLocOrErr.takeError();
4588
Gabor Marton26f72a92018-07-12 09:42:05 +00004589 if (GetImportedOrCreateDecl(
4590 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004591 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004592 /*TypeParamList=*/nullptr,
4593 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4594 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004595 ToIface->setLexicalDeclContext(LexicalDC);
4596 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004597 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004598 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004599 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004600 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004601 if (auto ToPListOrErr =
4602 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4603 ToIface->setTypeParamList(*ToPListOrErr);
4604 else
4605 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004606
Balazs Keri3b30d652018-10-19 13:32:20 +00004607 if (D->isThisDeclarationADefinition())
4608 if (Error Err = ImportDefinition(D, ToIface))
4609 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004610
Douglas Gregor98d156a2010-02-17 16:12:00 +00004611 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004612}
4613
Balazs Keri3b30d652018-10-19 13:32:20 +00004614ExpectedDecl
4615ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4616 ObjCCategoryDecl *Category;
4617 if (Error Err = importInto(Category, D->getCategoryDecl()))
4618 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004619
Douglas Gregor4da9d682010-12-07 15:32:12 +00004620 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4621 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004622 DeclContext *DC, *LexicalDC;
4623 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4624 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004625
Balazs Keri3b30d652018-10-19 13:32:20 +00004626 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4627 if (auto Imp = importSeq(
4628 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4629 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4630 else
4631 return Imp.takeError();
4632
Gabor Marton26f72a92018-07-12 09:42:05 +00004633 if (GetImportedOrCreateDecl(
4634 ToImpl, D, Importer.getToContext(), DC,
4635 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004636 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004637 return ToImpl;
4638
Balazs Keri3b30d652018-10-19 13:32:20 +00004639 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004640 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004641 Category->setImplementation(ToImpl);
4642 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004643
Gabor Marton26f72a92018-07-12 09:42:05 +00004644 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004645 if (Error Err = ImportDeclContext(D))
4646 return std::move(Err);
4647
Douglas Gregor4da9d682010-12-07 15:32:12 +00004648 return ToImpl;
4649}
4650
Balazs Keri3b30d652018-10-19 13:32:20 +00004651ExpectedDecl
4652ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004653 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004654 ObjCInterfaceDecl *Iface;
4655 if (Error Err = importInto(Iface, D->getClassInterface()))
4656 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004657
4658 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004659 ObjCInterfaceDecl *Super;
4660 if (Error Err = importInto(Super, D->getSuperClass()))
4661 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004662
4663 ObjCImplementationDecl *Impl = Iface->getImplementation();
4664 if (!Impl) {
4665 // We haven't imported an implementation yet. Create a new @implementation
4666 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004667 DeclContext *DC, *LexicalDC;
4668 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4669 return std::move(Err);
4670
4671 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4672 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4673 if (auto Imp = importSeq(
4674 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4675 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4676 std::tie(
4677 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4678 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4679 else
4680 return Imp.takeError();
4681
Gabor Marton26f72a92018-07-12 09:42:05 +00004682 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004683 DC, Iface, Super,
4684 ToLocation,
4685 ToAtStartLoc,
4686 ToSuperClassLoc,
4687 ToIvarLBraceLoc,
4688 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004689 return Impl;
4690
Balazs Keri3b30d652018-10-19 13:32:20 +00004691 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004692
Douglas Gregorda8025c2010-12-07 01:26:03 +00004693 // Associate the implementation with the class it implements.
4694 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004695 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004696 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004697 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004698
4699 // Verify that the existing @implementation has the same superclass.
4700 if ((Super && !Impl->getSuperClass()) ||
4701 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004702 (Super && Impl->getSuperClass() &&
4703 !declaresSameEntity(Super->getCanonicalDecl(),
4704 Impl->getSuperClass()))) {
4705 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004706 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004707 << Iface->getDeclName();
4708 // FIXME: It would be nice to have the location of the superclass
4709 // below.
4710 if (Impl->getSuperClass())
4711 Importer.ToDiag(Impl->getLocation(),
4712 diag::note_odr_objc_superclass)
4713 << Impl->getSuperClass()->getDeclName();
4714 else
4715 Importer.ToDiag(Impl->getLocation(),
4716 diag::note_odr_objc_missing_superclass);
4717 if (D->getSuperClass())
4718 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004719 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004720 << D->getSuperClass()->getDeclName();
4721 else
4722 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004723 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004724
4725 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004726 }
4727 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004728
Douglas Gregorda8025c2010-12-07 01:26:03 +00004729 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004730 if (Error Err = ImportDeclContext(D))
4731 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004732
4733 return Impl;
4734}
4735
Balazs Keri3b30d652018-10-19 13:32:20 +00004736ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004737 // Import the major distinguishing characteristics of an @property.
4738 DeclContext *DC, *LexicalDC;
4739 DeclarationName Name;
4740 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004741 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004742 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4743 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004744 if (ToD)
4745 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004746
4747 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004748 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004749 for (auto *FoundDecl : FoundDecls) {
4750 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004751 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004752 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004753 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004754 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004755 << Name << D->getType() << FoundProp->getType();
4756 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4757 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004758
4759 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004760 }
4761
4762 // FIXME: Check property attributes, getters, setters, etc.?
4763
4764 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004765 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004766 return FoundProp;
4767 }
4768 }
4769
Balazs Keri3b30d652018-10-19 13:32:20 +00004770 QualType ToType;
4771 TypeSourceInfo *ToTypeSourceInfo;
4772 SourceLocation ToAtLoc, ToLParenLoc;
4773 if (auto Imp = importSeq(
4774 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4775 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4776 else
4777 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004778
4779 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004780 ObjCPropertyDecl *ToProperty;
4781 if (GetImportedOrCreateDecl(
4782 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004783 Name.getAsIdentifierInfo(), ToAtLoc,
4784 ToLParenLoc, ToType,
4785 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004786 return ToProperty;
4787
Balazs Keri3b30d652018-10-19 13:32:20 +00004788 Selector ToGetterName, ToSetterName;
4789 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4790 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4791 ObjCIvarDecl *ToPropertyIvarDecl;
4792 if (auto Imp = importSeq(
4793 D->getGetterName(), D->getSetterName(),
4794 D->getGetterNameLoc(), D->getSetterNameLoc(),
4795 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4796 D->getPropertyIvarDecl()))
4797 std::tie(
4798 ToGetterName, ToSetterName,
4799 ToGetterNameLoc, ToSetterNameLoc,
4800 ToGetterMethodDecl, ToSetterMethodDecl,
4801 ToPropertyIvarDecl) = *Imp;
4802 else
4803 return Imp.takeError();
4804
Douglas Gregora11c4582010-02-17 18:02:10 +00004805 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004806 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004807
4808 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004809 ToProperty->setPropertyAttributesAsWritten(
4810 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004811 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4812 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4813 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4814 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4815 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004816 return ToProperty;
4817}
4818
Balazs Keri3b30d652018-10-19 13:32:20 +00004819ExpectedDecl
4820ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4821 ObjCPropertyDecl *Property;
4822 if (Error Err = importInto(Property, D->getPropertyDecl()))
4823 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004824
Balazs Keri3b30d652018-10-19 13:32:20 +00004825 DeclContext *DC, *LexicalDC;
4826 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4827 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004828
Balazs Keri3b30d652018-10-19 13:32:20 +00004829 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004830
4831 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004832 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004833 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4834 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004835
4836 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004837 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4838 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004839 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004840 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4841 if (auto Imp = importSeq(
4842 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4843 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4844 else
4845 return Imp.takeError();
4846
Gabor Marton26f72a92018-07-12 09:42:05 +00004847 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004848 ToBeginLoc,
4849 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004850 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004851 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004852 return ToImpl;
4853
Douglas Gregor14a49e22010-12-07 18:32:03 +00004854 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004855 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004856 } else {
4857 // Check that we have the same kind of property implementation (@synthesize
4858 // vs. @dynamic).
4859 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004860 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004861 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004862 << Property->getDeclName()
4863 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004864 == ObjCPropertyImplDecl::Dynamic);
4865 Importer.FromDiag(D->getLocation(),
4866 diag::note_odr_objc_property_impl_kind)
4867 << D->getPropertyDecl()->getDeclName()
4868 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004869
4870 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004871 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004872
4873 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004874 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4875 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004876 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004877 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004878 << Property->getDeclName()
4879 << ToImpl->getPropertyIvarDecl()->getDeclName()
4880 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004881 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004882 diag::note_odr_objc_synthesize_ivar_here)
4883 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004884
4885 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004886 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004887
Douglas Gregor14a49e22010-12-07 18:32:03 +00004888 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004889 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004890 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004891
Douglas Gregor14a49e22010-12-07 18:32:03 +00004892 return ToImpl;
4893}
4894
Balazs Keri3b30d652018-10-19 13:32:20 +00004895ExpectedDecl
4896ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004897 // For template arguments, we adopt the translation unit as our declaration
4898 // context. This context will be fixed when the actual template declaration
4899 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004900
Douglas Gregora082a492010-11-30 19:14:50 +00004901 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004902
4903 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4904 if (!BeginLocOrErr)
4905 return BeginLocOrErr.takeError();
4906
4907 ExpectedSLoc LocationOrErr = import(D->getLocation());
4908 if (!LocationOrErr)
4909 return LocationOrErr.takeError();
4910
Gabor Marton26f72a92018-07-12 09:42:05 +00004911 TemplateTypeParmDecl *ToD = nullptr;
4912 (void)GetImportedOrCreateDecl(
4913 ToD, D, Importer.getToContext(),
4914 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004915 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004916 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4917 D->wasDeclaredWithTypename(), D->isParameterPack());
4918 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004919}
4920
Balazs Keri3b30d652018-10-19 13:32:20 +00004921ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004922ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004923 DeclarationName ToDeclName;
4924 SourceLocation ToLocation, ToInnerLocStart;
4925 QualType ToType;
4926 TypeSourceInfo *ToTypeSourceInfo;
4927 if (auto Imp = importSeq(
4928 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4929 D->getInnerLocStart()))
4930 std::tie(
4931 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4932 ToInnerLocStart) = *Imp;
4933 else
4934 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004935
Douglas Gregora082a492010-11-30 19:14:50 +00004936 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004937
4938 NonTypeTemplateParmDecl *ToD = nullptr;
4939 (void)GetImportedOrCreateDecl(
4940 ToD, D, Importer.getToContext(),
4941 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004942 ToInnerLocStart, ToLocation, D->getDepth(),
4943 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4944 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004945 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004946}
4947
Balazs Keri3b30d652018-10-19 13:32:20 +00004948ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004949ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4950 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004951 auto NameOrErr = import(D->getDeclName());
4952 if (!NameOrErr)
4953 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004954
Douglas Gregora082a492010-11-30 19:14:50 +00004955 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004956 ExpectedSLoc LocationOrErr = import(D->getLocation());
4957 if (!LocationOrErr)
4958 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004959
Douglas Gregora082a492010-11-30 19:14:50 +00004960 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004961 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004962 if (!TemplateParamsOrErr)
4963 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004964
Douglas Gregora082a492010-11-30 19:14:50 +00004965 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004966
4967 TemplateTemplateParmDecl *ToD = nullptr;
4968 (void)GetImportedOrCreateDecl(
4969 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004970 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4971 D->getDepth(), D->getPosition(), D->isParameterPack(),
4972 (*NameOrErr).getAsIdentifierInfo(),
4973 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004974 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004975}
4976
Gabor Marton16d98c22019-03-07 13:01:51 +00004977// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004978// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004979template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4980 assert(D->getTemplatedDecl() && "Should be called on templates only");
4981 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004982 if (!ToTemplatedDef)
4983 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004984 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4985 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004986}
4987
Balazs Keri3b30d652018-10-19 13:32:20 +00004988ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004989 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4990
Douglas Gregora082a492010-11-30 19:14:50 +00004991 // Import the major distinguishing characteristics of this class template.
4992 DeclContext *DC, *LexicalDC;
4993 DeclarationName Name;
4994 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004995 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004996 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4997 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004998 if (ToD)
4999 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00005000
Gabor Marton7df342a2018-12-17 12:42:12 +00005001 ClassTemplateDecl *FoundByLookup = nullptr;
5002
Douglas Gregora082a492010-11-30 19:14:50 +00005003 // We may already have a template of the same name; try to find and match it.
5004 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005005 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005006 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005007 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00005008 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
5009 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00005010 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005011
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005012 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00005013 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
5014 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00005015
Douglas Gregora082a492010-11-30 19:14:50 +00005016 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005017 ClassTemplateDecl *TemplateWithDef =
5018 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00005019 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5020 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005021 }
Gabor Marton7df342a2018-12-17 12:42:12 +00005022 FoundByLookup = FoundTemplate;
5023 break;
Gabor Marton9581c332018-05-23 13:53:36 +00005024 }
Douglas Gregora082a492010-11-30 19:14:50 +00005025 }
Gabor Marton9581c332018-05-23 13:53:36 +00005026
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005027 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005028 }
Gabor Marton9581c332018-05-23 13:53:36 +00005029
Douglas Gregora082a492010-11-30 19:14:50 +00005030 if (!ConflictingDecls.empty()) {
5031 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005032 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005033 ConflictingDecls.size());
5034 }
Gabor Marton9581c332018-05-23 13:53:36 +00005035
Douglas Gregora082a492010-11-30 19:14:50 +00005036 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005037 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005038 }
5039
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005040 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5041
Douglas Gregora082a492010-11-30 19:14:50 +00005042 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005043 CXXRecordDecl *ToTemplated;
5044 if (Error Err = importInto(ToTemplated, FromTemplated))
5045 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005046
Douglas Gregora082a492010-11-30 19:14:50 +00005047 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005048 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005049 if (!TemplateParamsOrErr)
5050 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005051
Gabor Marton26f72a92018-07-12 09:42:05 +00005052 ClassTemplateDecl *D2;
5053 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005054 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005055 return D2;
5056
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005057 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005058
Douglas Gregora082a492010-11-30 19:14:50 +00005059 D2->setAccess(D->getAccess());
5060 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00005061
5062 if (D->getDeclContext()->containsDeclAndLoad(D))
5063 DC->addDeclInternal(D2);
5064 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00005065 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005066
Gabor Marton7df342a2018-12-17 12:42:12 +00005067 if (FoundByLookup) {
5068 auto *Recent =
5069 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5070
5071 // It is possible that during the import of the class template definition
5072 // we start the import of a fwd friend decl of the very same class template
5073 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5074 // had been created earlier and by that time the lookup could not find
5075 // anything existing, so it has no previous decl. Later, (still during the
5076 // import of the fwd friend decl) we start to import the definition again
5077 // and this time the lookup finds the previous fwd friend class template.
5078 // In this case we must set up the previous decl for the templated decl.
5079 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005080 assert(FoundByLookup->getTemplatedDecl() &&
5081 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005082 CXXRecordDecl *PrevTemplated =
5083 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5084 if (ToTemplated != PrevTemplated)
5085 ToTemplated->setPreviousDecl(PrevTemplated);
5086 }
5087
5088 D2->setPreviousDecl(Recent);
5089 }
5090
5091 if (LexicalDC != DC && IsFriend)
5092 DC->makeDeclVisibleInContext(D2);
5093
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005094 if (FromTemplated->isCompleteDefinition() &&
5095 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005096 // FIXME: Import definition!
5097 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005098
Douglas Gregora082a492010-11-30 19:14:50 +00005099 return D2;
5100}
5101
Balazs Keri3b30d652018-10-19 13:32:20 +00005102ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005103 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005104 ClassTemplateDecl *ClassTemplate;
5105 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5106 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005107
Douglas Gregore2e50d332010-12-01 01:36:18 +00005108 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005109 DeclContext *DC, *LexicalDC;
5110 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5111 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005112
5113 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005114 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005115 if (Error Err = ImportTemplateArguments(
5116 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5117 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005118
Douglas Gregore2e50d332010-12-01 01:36:18 +00005119 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005120 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005121 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005122 ClassTemplatePartialSpecializationDecl *PartialSpec =
5123 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5124 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005125 PrevDecl =
5126 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005127 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005128 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005129
Gabor Marton7f8c4002019-03-19 13:34:10 +00005130 if (PrevDecl) {
5131 if (IsStructuralMatch(D, PrevDecl)) {
5132 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5133 Importer.MapImported(D, PrevDecl->getDefinition());
5134 // Import those default field initializers which have been
5135 // instantiated in the "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005136 for (auto *FromField : D->fields()) {
5137 auto ToOrErr = import(FromField);
5138 if (!ToOrErr)
5139 return ToOrErr.takeError();
5140 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005141
Gabor Marton7f8c4002019-03-19 13:34:10 +00005142 // Import those methods which have been instantiated in the
5143 // "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005144 for (CXXMethodDecl *FromM : D->methods()) {
5145 auto ToOrErr = import(FromM);
5146 if (!ToOrErr)
5147 return ToOrErr.takeError();
5148 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005149
Gabor Marton7f8c4002019-03-19 13:34:10 +00005150 // TODO Import instantiated default arguments.
5151 // TODO Import instantiated exception specifications.
5152 //
5153 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5154 // what else could be fused during an AST merge.
5155 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005156 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005157 } else { // ODR violation.
5158 // FIXME HandleNameConflict
Gabor Marton303c98612019-06-25 08:00:51 +00005159 return make_error<ImportError>(ImportError::NameConflict);
Gabor Marton42e15de2018-08-22 11:52:14 +00005160 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005161 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005162
Gabor Marton7f8c4002019-03-19 13:34:10 +00005163 // Import the location of this declaration.
5164 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5165 if (!BeginLocOrErr)
5166 return BeginLocOrErr.takeError();
5167 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5168 if (!IdLocOrErr)
5169 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005170
Gabor Marton7f8c4002019-03-19 13:34:10 +00005171 // Create the specialization.
5172 ClassTemplateSpecializationDecl *D2 = nullptr;
5173 if (PartialSpec) {
5174 // Import TemplateArgumentListInfo.
5175 TemplateArgumentListInfo ToTAInfo;
5176 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5177 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5178 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005179
Gabor Marton7f8c4002019-03-19 13:34:10 +00005180 QualType CanonInjType;
5181 if (Error Err = importInto(
5182 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5183 return std::move(Err);
5184 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005185
Balazs Keridec09162019-03-20 15:42:42 +00005186 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005187 if (!ToTPListOrErr)
5188 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005189
Gabor Marton7f8c4002019-03-19 13:34:10 +00005190 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5191 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5192 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5193 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5194 ToTAInfo, CanonInjType,
5195 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5196 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005197
Gabor Marton7f8c4002019-03-19 13:34:10 +00005198 // Update InsertPos, because preceding import calls may have invalidated
5199 // it by adding new specializations.
5200 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5201 // Add this partial specialization to the class template.
5202 ClassTemplate->AddPartialSpecialization(
5203 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005204
Gabor Marton7f8c4002019-03-19 13:34:10 +00005205 } else { // Not a partial specialization.
5206 if (GetImportedOrCreateDecl(
5207 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5208 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5209 PrevDecl))
5210 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005211
Gabor Marton7f8c4002019-03-19 13:34:10 +00005212 // Update InsertPos, because preceding import calls may have invalidated
5213 // it by adding new specializations.
5214 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5215 // Add this specialization to the class template.
5216 ClassTemplate->AddSpecialization(D2, InsertPos);
5217 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005218
Gabor Marton7f8c4002019-03-19 13:34:10 +00005219 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005220
Gabor Marton7f8c4002019-03-19 13:34:10 +00005221 // Set the context of this specialization/instantiation.
5222 D2->setLexicalDeclContext(LexicalDC);
5223
5224 // Add to the DC only if it was an explicit specialization/instantiation.
5225 if (D2->isExplicitInstantiationOrSpecialization()) {
5226 LexicalDC->addDeclInternal(D2);
5227 }
5228
5229 // Import the qualifier, if any.
5230 if (auto LocOrErr = import(D->getQualifierLoc()))
5231 D2->setQualifierInfo(*LocOrErr);
5232 else
5233 return LocOrErr.takeError();
5234
5235 if (auto *TSI = D->getTypeAsWritten()) {
5236 if (auto TInfoOrErr = import(TSI))
5237 D2->setTypeAsWritten(*TInfoOrErr);
5238 else
5239 return TInfoOrErr.takeError();
5240
5241 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5242 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005243 else
5244 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005245
Gabor Marton7f8c4002019-03-19 13:34:10 +00005246 if (auto LocOrErr = import(D->getExternLoc()))
5247 D2->setExternLoc(*LocOrErr);
5248 else
5249 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005250 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005251
5252 if (D->getPointOfInstantiation().isValid()) {
5253 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5254 D2->setPointOfInstantiation(*POIOrErr);
5255 else
5256 return POIOrErr.takeError();
5257 }
5258
5259 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5260
Balazs Keri3b30d652018-10-19 13:32:20 +00005261 if (D->isCompleteDefinition())
5262 if (Error Err = ImportDefinition(D, D2))
5263 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005264
Douglas Gregore2e50d332010-12-01 01:36:18 +00005265 return D2;
5266}
5267
Balazs Keri3b30d652018-10-19 13:32:20 +00005268ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005269 // If this variable has a definition in the translation unit we're coming
5270 // from,
5271 // but this particular declaration is not that definition, import the
5272 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005273 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005274 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5275 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005276 if (ExpectedDecl ImportedDefOrErr = import(
5277 Definition->getDescribedVarTemplate()))
5278 return Importer.MapImported(D, *ImportedDefOrErr);
5279 else
5280 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005281 }
5282
5283 // Import the major distinguishing characteristics of this variable template.
5284 DeclContext *DC, *LexicalDC;
5285 DeclarationName Name;
5286 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005287 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005288 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5289 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005290 if (ToD)
5291 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005292
5293 // We may already have a template of the same name; try to find and match it.
5294 assert(!DC->isFunctionOrMethod() &&
5295 "Variable templates cannot be declared at function scope");
5296 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005297 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005298 for (auto *FoundDecl : FoundDecls) {
5299 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005300 continue;
5301
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005302 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005303 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005304 if (IsStructuralMatch(D, FoundTemplate)) {
5305 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005306 Importer.MapImported(D->getTemplatedDecl(),
5307 FoundTemplate->getTemplatedDecl());
5308 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005309 }
5310 }
5311
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005312 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005313 }
5314
5315 if (!ConflictingDecls.empty()) {
5316 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5317 ConflictingDecls.data(),
5318 ConflictingDecls.size());
5319 }
5320
5321 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005322 // FIXME: Is it possible to get other error than name conflict?
5323 // (Put this `if` into the previous `if`?)
5324 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005325
5326 VarDecl *DTemplated = D->getTemplatedDecl();
5327
5328 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005329 // FIXME: Value not used?
5330 ExpectedType TypeOrErr = import(DTemplated->getType());
5331 if (!TypeOrErr)
5332 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005333
5334 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005335 VarDecl *ToTemplated;
5336 if (Error Err = importInto(ToTemplated, DTemplated))
5337 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005338
5339 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005340 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005341 if (!TemplateParamsOrErr)
5342 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005343
Gabor Marton26f72a92018-07-12 09:42:05 +00005344 VarTemplateDecl *ToVarTD;
5345 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005346 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005347 return ToVarTD;
5348
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005349 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005350
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005351 ToVarTD->setAccess(D->getAccess());
5352 ToVarTD->setLexicalDeclContext(LexicalDC);
5353 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005354
Larisse Voufo39a1e502013-08-06 01:03:05 +00005355 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005356 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005357 // FIXME: Import definition!
5358 }
5359
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005360 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005361}
5362
Balazs Keri3b30d652018-10-19 13:32:20 +00005363ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005364 VarTemplateSpecializationDecl *D) {
5365 // If this record has a definition in the translation unit we're coming from,
5366 // but this particular declaration is not that definition, import the
5367 // definition and map to that.
5368 VarDecl *Definition = D->getDefinition();
5369 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005370 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5371 return Importer.MapImported(D, *ImportedDefOrErr);
5372 else
5373 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005374 }
5375
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00005376 VarTemplateDecl *VarTemplate = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005377 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5378 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005379
5380 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005381 DeclContext *DC, *LexicalDC;
5382 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5383 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005384
5385 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005386 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5387 if (!BeginLocOrErr)
5388 return BeginLocOrErr.takeError();
5389
5390 auto IdLocOrErr = import(D->getLocation());
5391 if (!IdLocOrErr)
5392 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005393
5394 // Import template arguments.
5395 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005396 if (Error Err = ImportTemplateArguments(
5397 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5398 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005399
5400 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005401 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005402 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005403 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005404 if (D2) {
5405 // We already have a variable template specialization with these template
5406 // arguments.
5407
5408 // FIXME: Check for specialization vs. instantiation errors.
5409
5410 if (VarDecl *FoundDef = D2->getDefinition()) {
5411 if (!D->isThisDeclarationADefinition() ||
5412 IsStructuralMatch(D, FoundDef)) {
5413 // The record types structurally match, or the "from" translation
5414 // unit only had a forward declaration anyway; call it the same
5415 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005416 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005417 }
5418 }
5419 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005420 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005421 QualType T;
5422 if (Error Err = importInto(T, D->getType()))
5423 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005424
Balazs Keri3b30d652018-10-19 13:32:20 +00005425 auto TInfoOrErr = import(D->getTypeSourceInfo());
5426 if (!TInfoOrErr)
5427 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005428
5429 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005430 if (Error Err = ImportTemplateArgumentListInfo(
5431 D->getTemplateArgsInfo(), ToTAInfo))
5432 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005433
5434 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005435 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005436 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5437 // Import TemplateArgumentListInfo
5438 TemplateArgumentListInfo ArgInfos;
5439 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5440 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005441 if (Error Err = ImportTemplateArgumentListInfo(
5442 *FromTAArgsAsWritten, ArgInfos))
5443 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005444
Balazs Keridec09162019-03-20 15:42:42 +00005445 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005446 if (!ToTPListOrErr)
5447 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005448
Gabor Marton26f72a92018-07-12 09:42:05 +00005449 PartVarSpecDecl *ToPartial;
5450 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005451 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5452 VarTemplate, T, *TInfoOrErr,
5453 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005454 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005455
Balazs Keri3b30d652018-10-19 13:32:20 +00005456 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5457 FromPartial->getInstantiatedFromMember()))
5458 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5459 else
5460 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005461
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005462 if (FromPartial->isMemberSpecialization())
5463 ToPartial->setMemberSpecialization();
5464
5465 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005466
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005467 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005468 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5469 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5470 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005471 D->getStorageClass(), TemplateArgs))
5472 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005473 }
5474
Balazs Keri3b30d652018-10-19 13:32:20 +00005475 if (D->getPointOfInstantiation().isValid()) {
5476 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5477 D2->setPointOfInstantiation(*POIOrErr);
5478 else
5479 return POIOrErr.takeError();
5480 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005481
Larisse Voufo39a1e502013-08-06 01:03:05 +00005482 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005483 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005484
5485 // Add this specialization to the class template.
5486 VarTemplate->AddSpecialization(D2, InsertPos);
5487
5488 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005489 if (auto LocOrErr = import(D->getQualifierLoc()))
5490 D2->setQualifierInfo(*LocOrErr);
5491 else
5492 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005493
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005494 if (D->isConstexpr())
5495 D2->setConstexpr(true);
5496
Larisse Voufo39a1e502013-08-06 01:03:05 +00005497 // Add the specialization to this context.
5498 D2->setLexicalDeclContext(LexicalDC);
5499 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005500
5501 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005502 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005503
Balazs Keri3b30d652018-10-19 13:32:20 +00005504 if (Error Err = ImportInitializer(D, D2))
5505 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005506
5507 return D2;
5508}
5509
Balazs Keri3b30d652018-10-19 13:32:20 +00005510ExpectedDecl
5511ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005512 DeclContext *DC, *LexicalDC;
5513 DeclarationName Name;
5514 SourceLocation Loc;
5515 NamedDecl *ToD;
5516
Balazs Keri3b30d652018-10-19 13:32:20 +00005517 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5518 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005519
5520 if (ToD)
5521 return ToD;
5522
Gabor Marton16d98c22019-03-07 13:01:51 +00005523 const FunctionTemplateDecl *FoundByLookup = nullptr;
5524
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005525 // Try to find a function in our own ("to") context with the same name, same
5526 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005527 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005528 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005529 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005530 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005531 for (auto *FoundDecl : FoundDecls) {
5532 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005533 continue;
5534
Gabor Marton16d98c22019-03-07 13:01:51 +00005535 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5536 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005537 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005538 if (IsStructuralMatch(D, FoundTemplate)) {
5539 FunctionTemplateDecl *TemplateWithDef =
5540 getTemplateDefinition(FoundTemplate);
5541 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5542 return Importer.MapImported(D, TemplateWithDef);
5543 }
5544 FoundByLookup = FoundTemplate;
5545 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005546 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005547 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005548 }
5549 }
5550 }
5551 }
5552
Balazs Keridec09162019-03-20 15:42:42 +00005553 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005554 if (!ParamsOrErr)
5555 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005556
Balazs Keri3b30d652018-10-19 13:32:20 +00005557 FunctionDecl *TemplatedFD;
5558 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5559 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005560
Gabor Marton26f72a92018-07-12 09:42:05 +00005561 FunctionTemplateDecl *ToFunc;
5562 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005563 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005564 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005565
5566 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005567
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005568 ToFunc->setAccess(D->getAccess());
5569 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005570 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005571
5572 if (FoundByLookup) {
5573 auto *Recent =
5574 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5575 if (!TemplatedFD->getPreviousDecl()) {
5576 assert(FoundByLookup->getTemplatedDecl() &&
5577 "Found decl must have its templated decl set");
5578 auto *PrevTemplated =
5579 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5580 if (TemplatedFD != PrevTemplated)
5581 TemplatedFD->setPreviousDecl(PrevTemplated);
5582 }
5583 ToFunc->setPreviousDecl(Recent);
5584 }
5585
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005586 return ToFunc;
5587}
5588
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005589//----------------------------------------------------------------------------
5590// Import Statements
5591//----------------------------------------------------------------------------
5592
Balazs Keri3b30d652018-10-19 13:32:20 +00005593ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005594 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5595 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005596 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005597}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005598
Balazs Keri3b30d652018-10-19 13:32:20 +00005599
5600ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Gabor Marton303c98612019-06-25 08:00:51 +00005601 if (Importer.returnWithErrorInTest())
5602 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005603 SmallVector<IdentifierInfo *, 4> Names;
5604 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5605 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005606 // ToII is nullptr when no symbolic name is given for output operand
5607 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005608 Names.push_back(ToII);
5609 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005610
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005611 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5612 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005613 // ToII is nullptr when no symbolic name is given for input operand
5614 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005615 Names.push_back(ToII);
5616 }
5617
5618 SmallVector<StringLiteral *, 4> Clobbers;
5619 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005620 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5621 Clobbers.push_back(*ClobberOrErr);
5622 else
5623 return ClobberOrErr.takeError();
5624
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005625 }
5626
5627 SmallVector<StringLiteral *, 4> Constraints;
5628 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005629 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5630 Constraints.push_back(*OutputOrErr);
5631 else
5632 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005633 }
5634
5635 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005636 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5637 Constraints.push_back(*InputOrErr);
5638 else
5639 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005640 }
5641
Jennifer Yub8fee672019-06-03 15:57:25 +00005642 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() +
5643 S->getNumLabels());
Balazs Keri3b30d652018-10-19 13:32:20 +00005644 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5645 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005646
Jennifer Yub8fee672019-06-03 15:57:25 +00005647 if (Error Err =
5648 ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
5649 return std::move(Err);
5650
Balazs Keri3b30d652018-10-19 13:32:20 +00005651 if (Error Err = ImportArrayChecked(
Jennifer Yub8fee672019-06-03 15:57:25 +00005652 S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs()))
Balazs Keri3b30d652018-10-19 13:32:20 +00005653 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005654
Balazs Keri3b30d652018-10-19 13:32:20 +00005655 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5656 if (!AsmLocOrErr)
5657 return AsmLocOrErr.takeError();
5658 auto AsmStrOrErr = import(S->getAsmString());
5659 if (!AsmStrOrErr)
5660 return AsmStrOrErr.takeError();
5661 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5662 if (!RParenLocOrErr)
5663 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005664
5665 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005666 Importer.getToContext(),
5667 *AsmLocOrErr,
5668 S->isSimple(),
5669 S->isVolatile(),
5670 S->getNumOutputs(),
5671 S->getNumInputs(),
5672 Names.data(),
5673 Constraints.data(),
5674 Exprs.data(),
5675 *AsmStrOrErr,
5676 S->getNumClobbers(),
5677 Clobbers.data(),
Jennifer Yub8fee672019-06-03 15:57:25 +00005678 S->getNumLabels(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005679 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005680}
5681
Balazs Keri3b30d652018-10-19 13:32:20 +00005682ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5683 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5684 if (!Imp)
5685 return Imp.takeError();
5686
5687 DeclGroupRef ToDG;
5688 SourceLocation ToBeginLoc, ToEndLoc;
5689 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5690
5691 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005692}
5693
Balazs Keri3b30d652018-10-19 13:32:20 +00005694ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5695 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5696 if (!ToSemiLocOrErr)
5697 return ToSemiLocOrErr.takeError();
5698 return new (Importer.getToContext()) NullStmt(
5699 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005700}
5701
Balazs Keri3b30d652018-10-19 13:32:20 +00005702ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005703 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005704
Balazs Keri3b30d652018-10-19 13:32:20 +00005705 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5706 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005707
Balazs Keri3b30d652018-10-19 13:32:20 +00005708 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5709 if (!ToLBracLocOrErr)
5710 return ToLBracLocOrErr.takeError();
5711
5712 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5713 if (!ToRBracLocOrErr)
5714 return ToRBracLocOrErr.takeError();
5715
5716 return CompoundStmt::Create(
5717 Importer.getToContext(), ToStmts,
5718 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005719}
5720
Balazs Keri3b30d652018-10-19 13:32:20 +00005721ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5722 auto Imp = importSeq(
5723 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5724 S->getEllipsisLoc(), S->getColonLoc());
5725 if (!Imp)
5726 return Imp.takeError();
5727
5728 Expr *ToLHS, *ToRHS;
5729 Stmt *ToSubStmt;
5730 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5731 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5732 *Imp;
5733
Bruno Ricci5b30571752018-10-28 12:30:53 +00005734 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5735 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005736 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005737
Gabor Horvath480892b2017-10-18 09:25:18 +00005738 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005739}
5740
Balazs Keri3b30d652018-10-19 13:32:20 +00005741ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5742 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5743 if (!Imp)
5744 return Imp.takeError();
5745
5746 SourceLocation ToDefaultLoc, ToColonLoc;
5747 Stmt *ToSubStmt;
5748 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5749
5750 return new (Importer.getToContext()) DefaultStmt(
5751 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005752}
5753
Balazs Keri3b30d652018-10-19 13:32:20 +00005754ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5755 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5756 if (!Imp)
5757 return Imp.takeError();
5758
5759 SourceLocation ToIdentLoc;
5760 LabelDecl *ToLabelDecl;
5761 Stmt *ToSubStmt;
5762 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5763
5764 return new (Importer.getToContext()) LabelStmt(
5765 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005766}
5767
Balazs Keri3b30d652018-10-19 13:32:20 +00005768ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5769 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5770 if (!ToAttrLocOrErr)
5771 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005772 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5773 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005774 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5775 return std::move(Err);
5776 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5777 if (!ToSubStmtOrErr)
5778 return ToSubStmtOrErr.takeError();
5779
5780 return AttributedStmt::Create(
5781 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005782}
5783
Balazs Keri3b30d652018-10-19 13:32:20 +00005784ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5785 auto Imp = importSeq(
5786 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5787 S->getThen(), S->getElseLoc(), S->getElse());
5788 if (!Imp)
5789 return Imp.takeError();
5790
5791 SourceLocation ToIfLoc, ToElseLoc;
5792 Stmt *ToInit, *ToThen, *ToElse;
5793 VarDecl *ToConditionVariable;
5794 Expr *ToCond;
5795 std::tie(
5796 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5797 *Imp;
5798
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005799 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5800 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5801 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005802}
5803
Balazs Keri3b30d652018-10-19 13:32:20 +00005804ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5805 auto Imp = importSeq(
5806 S->getInit(), S->getConditionVariable(), S->getCond(),
5807 S->getBody(), S->getSwitchLoc());
5808 if (!Imp)
5809 return Imp.takeError();
5810
5811 Stmt *ToInit, *ToBody;
5812 VarDecl *ToConditionVariable;
5813 Expr *ToCond;
5814 SourceLocation ToSwitchLoc;
5815 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5816
Bruno Riccie2806f82018-10-29 16:12:37 +00005817 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5818 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005819 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005820 ToStmt->setSwitchLoc(ToSwitchLoc);
5821
Sean Callanan59721b32015-04-28 18:41:46 +00005822 // Now we have to re-chain the cases.
5823 SwitchCase *LastChainedSwitchCase = nullptr;
5824 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5825 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005826 Expected<SwitchCase *> ToSCOrErr = import(SC);
5827 if (!ToSCOrErr)
5828 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005829 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005830 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005831 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005832 ToStmt->setSwitchCaseList(*ToSCOrErr);
5833 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005834 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005835
Sean Callanan59721b32015-04-28 18:41:46 +00005836 return ToStmt;
5837}
5838
Balazs Keri3b30d652018-10-19 13:32:20 +00005839ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5840 auto Imp = importSeq(
5841 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5842 if (!Imp)
5843 return Imp.takeError();
5844
5845 VarDecl *ToConditionVariable;
5846 Expr *ToCond;
5847 Stmt *ToBody;
5848 SourceLocation ToWhileLoc;
5849 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5850
Bruno Riccibacf7512018-10-30 13:42:41 +00005851 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5852 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005853}
5854
Balazs Keri3b30d652018-10-19 13:32:20 +00005855ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5856 auto Imp = importSeq(
5857 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5858 S->getRParenLoc());
5859 if (!Imp)
5860 return Imp.takeError();
5861
5862 Stmt *ToBody;
5863 Expr *ToCond;
5864 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5865 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5866
5867 return new (Importer.getToContext()) DoStmt(
5868 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005869}
5870
Balazs Keri3b30d652018-10-19 13:32:20 +00005871ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5872 auto Imp = importSeq(
5873 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5874 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5875 if (!Imp)
5876 return Imp.takeError();
5877
5878 Stmt *ToInit;
5879 Expr *ToCond, *ToInc;
5880 VarDecl *ToConditionVariable;
5881 Stmt *ToBody;
5882 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5883 std::tie(
5884 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5885 ToLParenLoc, ToRParenLoc) = *Imp;
5886
5887 return new (Importer.getToContext()) ForStmt(
5888 Importer.getToContext(),
5889 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5890 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005891}
5892
Balazs Keri3b30d652018-10-19 13:32:20 +00005893ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5894 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5895 if (!Imp)
5896 return Imp.takeError();
5897
5898 LabelDecl *ToLabel;
5899 SourceLocation ToGotoLoc, ToLabelLoc;
5900 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5901
5902 return new (Importer.getToContext()) GotoStmt(
5903 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005904}
5905
Balazs Keri3b30d652018-10-19 13:32:20 +00005906ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5907 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5908 if (!Imp)
5909 return Imp.takeError();
5910
5911 SourceLocation ToGotoLoc, ToStarLoc;
5912 Expr *ToTarget;
5913 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5914
5915 return new (Importer.getToContext()) IndirectGotoStmt(
5916 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005917}
5918
Balazs Keri3b30d652018-10-19 13:32:20 +00005919ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5920 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5921 if (!ToContinueLocOrErr)
5922 return ToContinueLocOrErr.takeError();
5923 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005924}
5925
Balazs Keri3b30d652018-10-19 13:32:20 +00005926ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5927 auto ToBreakLocOrErr = import(S->getBreakLoc());
5928 if (!ToBreakLocOrErr)
5929 return ToBreakLocOrErr.takeError();
5930 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005931}
5932
Balazs Keri3b30d652018-10-19 13:32:20 +00005933ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5934 auto Imp = importSeq(
5935 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5936 if (!Imp)
5937 return Imp.takeError();
5938
5939 SourceLocation ToReturnLoc;
5940 Expr *ToRetValue;
5941 const VarDecl *ToNRVOCandidate;
5942 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5943
Bruno Ricci023b1d12018-10-30 14:40:49 +00005944 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5945 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005946}
5947
Balazs Keri3b30d652018-10-19 13:32:20 +00005948ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5949 auto Imp = importSeq(
5950 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5951 if (!Imp)
5952 return Imp.takeError();
5953
5954 SourceLocation ToCatchLoc;
5955 VarDecl *ToExceptionDecl;
5956 Stmt *ToHandlerBlock;
5957 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5958
5959 return new (Importer.getToContext()) CXXCatchStmt (
5960 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005961}
5962
Balazs Keri3b30d652018-10-19 13:32:20 +00005963ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5964 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5965 if (!ToTryLocOrErr)
5966 return ToTryLocOrErr.takeError();
5967
5968 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5969 if (!ToTryBlockOrErr)
5970 return ToTryBlockOrErr.takeError();
5971
Sean Callanan59721b32015-04-28 18:41:46 +00005972 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5973 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5974 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005975 if (auto ToHandlerOrErr = import(FromHandler))
5976 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005977 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005978 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005979 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005980
5981 return CXXTryStmt::Create(
5982 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005983}
5984
Balazs Keri3b30d652018-10-19 13:32:20 +00005985ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5986 auto Imp1 = importSeq(
5987 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5988 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5989 if (!Imp1)
5990 return Imp1.takeError();
5991 auto Imp2 = importSeq(
5992 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5993 if (!Imp2)
5994 return Imp2.takeError();
5995
5996 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5997 Expr *ToCond, *ToInc;
5998 Stmt *ToInit, *ToBody;
5999 std::tie(
6000 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6001 ToBody) = *Imp1;
6002 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
6003 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
6004
6005 return new (Importer.getToContext()) CXXForRangeStmt(
6006 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6007 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006008}
6009
Balazs Keri3b30d652018-10-19 13:32:20 +00006010ExpectedStmt
6011ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
6012 auto Imp = importSeq(
6013 S->getElement(), S->getCollection(), S->getBody(),
6014 S->getForLoc(), S->getRParenLoc());
6015 if (!Imp)
6016 return Imp.takeError();
6017
6018 Stmt *ToElement, *ToBody;
6019 Expr *ToCollection;
6020 SourceLocation ToForLoc, ToRParenLoc;
6021 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6022
6023 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6024 ToCollection,
6025 ToBody,
6026 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006027 ToRParenLoc);
6028}
6029
Balazs Keri3b30d652018-10-19 13:32:20 +00006030ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6031 auto Imp = importSeq(
6032 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6033 S->getCatchBody());
6034 if (!Imp)
6035 return Imp.takeError();
6036
6037 SourceLocation ToAtCatchLoc, ToRParenLoc;
6038 VarDecl *ToCatchParamDecl;
6039 Stmt *ToCatchBody;
6040 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6041
6042 return new (Importer.getToContext()) ObjCAtCatchStmt (
6043 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006044}
6045
Balazs Keri3b30d652018-10-19 13:32:20 +00006046ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6047 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6048 if (!ToAtFinallyLocOrErr)
6049 return ToAtFinallyLocOrErr.takeError();
6050 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6051 if (!ToAtFinallyStmtOrErr)
6052 return ToAtFinallyStmtOrErr.takeError();
6053 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6054 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006055}
6056
Balazs Keri3b30d652018-10-19 13:32:20 +00006057ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6058 auto Imp = importSeq(
6059 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6060 if (!Imp)
6061 return Imp.takeError();
6062
6063 SourceLocation ToAtTryLoc;
6064 Stmt *ToTryBody, *ToFinallyStmt;
6065 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6066
Sean Callanan59721b32015-04-28 18:41:46 +00006067 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6068 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6069 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006070 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6071 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006072 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006073 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006074 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006075
Sean Callanan59721b32015-04-28 18:41:46 +00006076 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006077 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006078 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006079 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006080}
6081
Balazs Keri3b30d652018-10-19 13:32:20 +00006082ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006083 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006084 auto Imp = importSeq(
6085 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6086 if (!Imp)
6087 return Imp.takeError();
6088
6089 SourceLocation ToAtSynchronizedLoc;
6090 Expr *ToSynchExpr;
6091 Stmt *ToSynchBody;
6092 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6093
Sean Callanan59721b32015-04-28 18:41:46 +00006094 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6095 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6096}
6097
Balazs Keri3b30d652018-10-19 13:32:20 +00006098ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6099 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6100 if (!ToThrowLocOrErr)
6101 return ToThrowLocOrErr.takeError();
6102 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6103 if (!ToThrowExprOrErr)
6104 return ToThrowExprOrErr.takeError();
6105 return new (Importer.getToContext()) ObjCAtThrowStmt(
6106 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006107}
6108
Balazs Keri3b30d652018-10-19 13:32:20 +00006109ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6110 ObjCAutoreleasePoolStmt *S) {
6111 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6112 if (!ToAtLocOrErr)
6113 return ToAtLocOrErr.takeError();
6114 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6115 if (!ToSubStmtOrErr)
6116 return ToSubStmtOrErr.takeError();
6117 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6118 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006119}
6120
6121//----------------------------------------------------------------------------
6122// Import Expressions
6123//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006124ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006125 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6126 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006127 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006128}
6129
Balazs Keri3b30d652018-10-19 13:32:20 +00006130ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6131 auto Imp = importSeq(
6132 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6133 E->getRParenLoc(), E->getType());
6134 if (!Imp)
6135 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006136
Balazs Keri3b30d652018-10-19 13:32:20 +00006137 SourceLocation ToBuiltinLoc, ToRParenLoc;
6138 Expr *ToSubExpr;
6139 TypeSourceInfo *ToWrittenTypeInfo;
6140 QualType ToType;
6141 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6142 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006143
6144 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006145 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6146 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006147}
6148
Tom Roeder521f0042019-02-26 19:26:41 +00006149ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6150 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6151 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6152 if (!Imp)
6153 return Imp.takeError();
6154
6155 Expr *ToCond;
6156 Expr *ToLHS;
6157 Expr *ToRHS;
6158 SourceLocation ToBuiltinLoc, ToRParenLoc;
6159 QualType ToType;
6160 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6161
6162 ExprValueKind VK = E->getValueKind();
6163 ExprObjectKind OK = E->getObjectKind();
6164
6165 bool TypeDependent = ToCond->isTypeDependent();
6166 bool ValueDependent = ToCond->isValueDependent();
6167
6168 // The value of CondIsTrue only matters if the value is not
6169 // condition-dependent.
6170 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6171
6172 return new (Importer.getToContext())
6173 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6174 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6175}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006176
Balazs Keri3b30d652018-10-19 13:32:20 +00006177ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6178 ExpectedType TypeOrErr = import(E->getType());
6179 if (!TypeOrErr)
6180 return TypeOrErr.takeError();
6181
6182 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6183 if (!BeginLocOrErr)
6184 return BeginLocOrErr.takeError();
6185
6186 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006187}
6188
Balazs Keri3b30d652018-10-19 13:32:20 +00006189ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6190 auto Imp = importSeq(
6191 E->getBeginLoc(), E->getType(), E->getFunctionName());
6192 if (!Imp)
6193 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006194
Balazs Keri3b30d652018-10-19 13:32:20 +00006195 SourceLocation ToBeginLoc;
6196 QualType ToType;
6197 StringLiteral *ToFunctionName;
6198 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006199
Bruno Ricci17ff0262018-10-27 19:21:19 +00006200 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6201 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006202}
6203
Balazs Keri3b30d652018-10-19 13:32:20 +00006204ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6205 auto Imp = importSeq(
6206 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6207 E->getLocation(), E->getType());
6208 if (!Imp)
6209 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006210
Balazs Keri3b30d652018-10-19 13:32:20 +00006211 NestedNameSpecifierLoc ToQualifierLoc;
6212 SourceLocation ToTemplateKeywordLoc, ToLocation;
6213 ValueDecl *ToDecl;
6214 QualType ToType;
6215 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6216 *Imp;
6217
6218 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006219 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006220 auto FoundDOrErr = import(E->getFoundDecl());
6221 if (!FoundDOrErr)
6222 return FoundDOrErr.takeError();
6223 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006224 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006225
Aleksei Sidorina693b372016-09-28 10:16:56 +00006226 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006227 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006228 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006229 if (Error Err =
6230 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6231 return std::move(Err);
6232 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006233 }
6234
Balazs Keri3b30d652018-10-19 13:32:20 +00006235 auto *ToE = DeclRefExpr::Create(
6236 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6237 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
Richard Smith715f7a12019-06-11 17:50:32 +00006238 E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse());
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006239 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006240 ToE->setHadMultipleCandidates(true);
6241 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006242}
6243
Balazs Keri3b30d652018-10-19 13:32:20 +00006244ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6245 ExpectedType TypeOrErr = import(E->getType());
6246 if (!TypeOrErr)
6247 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006248
Balazs Keri3b30d652018-10-19 13:32:20 +00006249 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006250}
6251
Balazs Keri3b30d652018-10-19 13:32:20 +00006252ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6253 ExpectedExpr ToInitOrErr = import(E->getInit());
6254 if (!ToInitOrErr)
6255 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006256
Balazs Keri3b30d652018-10-19 13:32:20 +00006257 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6258 if (!ToEqualOrColonLocOrErr)
6259 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006260
Balazs Keri3b30d652018-10-19 13:32:20 +00006261 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006262 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006263 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6264 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6265 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006266 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006267 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006268 }
6269
Balazs Keri3b30d652018-10-19 13:32:20 +00006270 SmallVector<Designator, 4> ToDesignators(E->size());
6271 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6272 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006273
6274 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006275 Importer.getToContext(), ToDesignators,
6276 ToIndexExprs, *ToEqualOrColonLocOrErr,
6277 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006278}
6279
Balazs Keri3b30d652018-10-19 13:32:20 +00006280ExpectedStmt
6281ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6282 ExpectedType ToTypeOrErr = import(E->getType());
6283 if (!ToTypeOrErr)
6284 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006285
Balazs Keri3b30d652018-10-19 13:32:20 +00006286 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6287 if (!ToLocationOrErr)
6288 return ToLocationOrErr.takeError();
6289
6290 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6291 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006292}
6293
Balazs Keri3b30d652018-10-19 13:32:20 +00006294ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6295 ExpectedType ToTypeOrErr = import(E->getType());
6296 if (!ToTypeOrErr)
6297 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006298
Balazs Keri3b30d652018-10-19 13:32:20 +00006299 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6300 if (!ToLocationOrErr)
6301 return ToLocationOrErr.takeError();
6302
6303 return IntegerLiteral::Create(
6304 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006305}
6306
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006307
Balazs Keri3b30d652018-10-19 13:32:20 +00006308ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6309 ExpectedType ToTypeOrErr = import(E->getType());
6310 if (!ToTypeOrErr)
6311 return ToTypeOrErr.takeError();
6312
6313 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6314 if (!ToLocationOrErr)
6315 return ToLocationOrErr.takeError();
6316
6317 return FloatingLiteral::Create(
6318 Importer.getToContext(), E->getValue(), E->isExact(),
6319 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006320}
6321
Balazs Keri3b30d652018-10-19 13:32:20 +00006322ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6323 auto ToTypeOrErr = import(E->getType());
6324 if (!ToTypeOrErr)
6325 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006326
Balazs Keri3b30d652018-10-19 13:32:20 +00006327 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6328 if (!ToSubExprOrErr)
6329 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006330
Balazs Keri3b30d652018-10-19 13:32:20 +00006331 return new (Importer.getToContext()) ImaginaryLiteral(
6332 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006333}
6334
Balazs Keri3b30d652018-10-19 13:32:20 +00006335ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6336 ExpectedType ToTypeOrErr = import(E->getType());
6337 if (!ToTypeOrErr)
6338 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006339
Balazs Keri3b30d652018-10-19 13:32:20 +00006340 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6341 if (!ToLocationOrErr)
6342 return ToLocationOrErr.takeError();
6343
6344 return new (Importer.getToContext()) CharacterLiteral(
6345 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006346}
6347
Balazs Keri3b30d652018-10-19 13:32:20 +00006348ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6349 ExpectedType ToTypeOrErr = import(E->getType());
6350 if (!ToTypeOrErr)
6351 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006352
Balazs Keri3b30d652018-10-19 13:32:20 +00006353 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6354 if (Error Err = ImportArrayChecked(
6355 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6356 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006357
Balazs Keri3b30d652018-10-19 13:32:20 +00006358 return StringLiteral::Create(
6359 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6360 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006361}
6362
Balazs Keri3b30d652018-10-19 13:32:20 +00006363ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6364 auto Imp = importSeq(
6365 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6366 E->getInitializer());
6367 if (!Imp)
6368 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006369
Balazs Keri3b30d652018-10-19 13:32:20 +00006370 SourceLocation ToLParenLoc;
6371 TypeSourceInfo *ToTypeSourceInfo;
6372 QualType ToType;
6373 Expr *ToInitializer;
6374 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006375
6376 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006377 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6378 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006379}
6380
Balazs Keri3b30d652018-10-19 13:32:20 +00006381ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6382 auto Imp = importSeq(
6383 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6384 if (!Imp)
6385 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006386
Balazs Keri3b30d652018-10-19 13:32:20 +00006387 SourceLocation ToBuiltinLoc, ToRParenLoc;
6388 QualType ToType;
6389 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6390
6391 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6392 if (Error Err = ImportArrayChecked(
6393 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6394 ToExprs.begin()))
6395 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006396
6397 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006398 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006399}
6400
Balazs Keri3b30d652018-10-19 13:32:20 +00006401ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6402 auto Imp = importSeq(
6403 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6404 if (!Imp)
6405 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006406
Balazs Keri3b30d652018-10-19 13:32:20 +00006407 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6408 LabelDecl *ToLabel;
6409 QualType ToType;
6410 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006411
6412 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006413 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006414}
6415
Bill Wendling8003edc2018-11-09 00:41:36 +00006416ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6417 auto Imp = importSeq(E->getSubExpr());
6418 if (!Imp)
6419 return Imp.takeError();
6420
6421 Expr *ToSubExpr;
6422 std::tie(ToSubExpr) = *Imp;
6423
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00006424 // TODO : Handle APValue::ValueKind that require importing.
6425 APValue::ValueKind Kind = E->getResultAPValueKind();
6426 if (Kind == APValue::Int || Kind == APValue::Float ||
6427 Kind == APValue::FixedPoint || Kind == APValue::ComplexFloat ||
6428 Kind == APValue::ComplexInt)
6429 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr,
6430 E->getAPValueResult());
Fangrui Song407659a2018-11-30 23:41:18 +00006431 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006432}
6433
Balazs Keri3b30d652018-10-19 13:32:20 +00006434ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6435 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6436 if (!Imp)
6437 return Imp.takeError();
6438
6439 SourceLocation ToLParen, ToRParen;
6440 Expr *ToSubExpr;
6441 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006442
Fangrui Song6907ce22018-07-30 19:24:48 +00006443 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006444 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006445}
6446
Balazs Keri3b30d652018-10-19 13:32:20 +00006447ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6448 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6449 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6450 return std::move(Err);
6451
6452 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6453 if (!ToLParenLocOrErr)
6454 return ToLParenLocOrErr.takeError();
6455
6456 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6457 if (!ToRParenLocOrErr)
6458 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006459
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006460 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6461 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006462}
6463
Balazs Keri3b30d652018-10-19 13:32:20 +00006464ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6465 auto Imp = importSeq(
6466 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6467 if (!Imp)
6468 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006469
Balazs Keri3b30d652018-10-19 13:32:20 +00006470 CompoundStmt *ToSubStmt;
6471 QualType ToType;
6472 SourceLocation ToLParenLoc, ToRParenLoc;
6473 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006474
Balazs Keri3b30d652018-10-19 13:32:20 +00006475 return new (Importer.getToContext()) StmtExpr(
6476 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006477}
6478
Balazs Keri3b30d652018-10-19 13:32:20 +00006479ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6480 auto Imp = importSeq(
6481 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6482 if (!Imp)
6483 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006484
Balazs Keri3b30d652018-10-19 13:32:20 +00006485 Expr *ToSubExpr;
6486 QualType ToType;
6487 SourceLocation ToOperatorLoc;
6488 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006489
Aaron Ballmana5038552018-01-09 13:07:03 +00006490 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006491 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6492 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006493}
6494
Balazs Keri3b30d652018-10-19 13:32:20 +00006495ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006496ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006497 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6498 if (!Imp)
6499 return Imp.takeError();
6500
6501 QualType ToType;
6502 SourceLocation ToOperatorLoc, ToRParenLoc;
6503 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006504
Douglas Gregord8552cd2010-02-19 01:24:23 +00006505 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006506 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6507 import(E->getArgumentTypeInfo());
6508 if (!ToArgumentTypeInfoOrErr)
6509 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006510
Balazs Keri3b30d652018-10-19 13:32:20 +00006511 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6512 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6513 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006514 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006515
Balazs Keri3b30d652018-10-19 13:32:20 +00006516 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6517 if (!ToArgumentExprOrErr)
6518 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006519
Balazs Keri3b30d652018-10-19 13:32:20 +00006520 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6521 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006522}
6523
Balazs Keri3b30d652018-10-19 13:32:20 +00006524ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6525 auto Imp = importSeq(
6526 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6527 if (!Imp)
6528 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006529
Balazs Keri3b30d652018-10-19 13:32:20 +00006530 Expr *ToLHS, *ToRHS;
6531 QualType ToType;
6532 SourceLocation ToOperatorLoc;
6533 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006534
Balazs Keri3b30d652018-10-19 13:32:20 +00006535 return new (Importer.getToContext()) BinaryOperator(
6536 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6537 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006538}
6539
Balazs Keri3b30d652018-10-19 13:32:20 +00006540ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6541 auto Imp = importSeq(
6542 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6543 E->getRHS(), E->getType());
6544 if (!Imp)
6545 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006546
Balazs Keri3b30d652018-10-19 13:32:20 +00006547 Expr *ToCond, *ToLHS, *ToRHS;
6548 SourceLocation ToQuestionLoc, ToColonLoc;
6549 QualType ToType;
6550 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006551
6552 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006553 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6554 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006555}
6556
Balazs Keri3b30d652018-10-19 13:32:20 +00006557ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006558 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006559 auto Imp = importSeq(
6560 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6561 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6562 if (!Imp)
6563 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006564
Balazs Keri3b30d652018-10-19 13:32:20 +00006565 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6566 OpaqueValueExpr *ToOpaqueValue;
6567 SourceLocation ToQuestionLoc, ToColonLoc;
6568 QualType ToType;
6569 std::tie(
6570 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6571 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006572
6573 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006574 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6575 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6576 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006577}
6578
Balazs Keri3b30d652018-10-19 13:32:20 +00006579ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6580 auto Imp = importSeq(
6581 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6582 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6583 if (!Imp)
6584 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006585
Balazs Keri3b30d652018-10-19 13:32:20 +00006586 SourceLocation ToBeginLoc, ToEndLoc;
6587 TypeSourceInfo *ToQueriedTypeSourceInfo;
6588 Expr *ToDimensionExpression;
6589 QualType ToType;
6590 std::tie(
6591 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6592 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006593
6594 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006595 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6596 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006597}
6598
Balazs Keri3b30d652018-10-19 13:32:20 +00006599ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6600 auto Imp = importSeq(
6601 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6602 if (!Imp)
6603 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006604
Balazs Keri3b30d652018-10-19 13:32:20 +00006605 SourceLocation ToBeginLoc, ToEndLoc;
6606 Expr *ToQueriedExpression;
6607 QualType ToType;
6608 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006609
6610 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006611 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6612 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006613}
6614
Balazs Keri3b30d652018-10-19 13:32:20 +00006615ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6616 auto Imp = importSeq(
6617 E->getLocation(), E->getType(), E->getSourceExpr());
6618 if (!Imp)
6619 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006620
Balazs Keri3b30d652018-10-19 13:32:20 +00006621 SourceLocation ToLocation;
6622 QualType ToType;
6623 Expr *ToSourceExpr;
6624 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006625
6626 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006627 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006628}
6629
Balazs Keri3b30d652018-10-19 13:32:20 +00006630ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6631 auto Imp = importSeq(
6632 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6633 if (!Imp)
6634 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006635
Balazs Keri3b30d652018-10-19 13:32:20 +00006636 Expr *ToLHS, *ToRHS;
6637 SourceLocation ToRBracketLoc;
6638 QualType ToType;
6639 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006640
6641 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006642 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6643 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006644}
6645
Balazs Keri3b30d652018-10-19 13:32:20 +00006646ExpectedStmt
6647ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6648 auto Imp = importSeq(
6649 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6650 E->getComputationResultType(), E->getOperatorLoc());
6651 if (!Imp)
6652 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006653
Balazs Keri3b30d652018-10-19 13:32:20 +00006654 Expr *ToLHS, *ToRHS;
6655 QualType ToType, ToComputationLHSType, ToComputationResultType;
6656 SourceLocation ToOperatorLoc;
6657 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6658 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006659
Balazs Keri3b30d652018-10-19 13:32:20 +00006660 return new (Importer.getToContext()) CompoundAssignOperator(
6661 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6662 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6663 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006664}
6665
Balazs Keri3b30d652018-10-19 13:32:20 +00006666Expected<CXXCastPath>
6667ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6668 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006669 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006670 if (auto SpecOrErr = import(*I))
6671 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006672 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006673 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006674 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006675 return Path;
John McCallcf142162010-08-07 06:22:56 +00006676}
6677
Balazs Keri3b30d652018-10-19 13:32:20 +00006678ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6679 ExpectedType ToTypeOrErr = import(E->getType());
6680 if (!ToTypeOrErr)
6681 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006682
Balazs Keri3b30d652018-10-19 13:32:20 +00006683 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6684 if (!ToSubExprOrErr)
6685 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006686
Balazs Keri3b30d652018-10-19 13:32:20 +00006687 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6688 if (!ToBasePathOrErr)
6689 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006690
Balazs Keri3b30d652018-10-19 13:32:20 +00006691 return ImplicitCastExpr::Create(
6692 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6693 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006694}
6695
Balazs Keri3b30d652018-10-19 13:32:20 +00006696ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6697 auto Imp1 = importSeq(
6698 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6699 if (!Imp1)
6700 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006701
Balazs Keri3b30d652018-10-19 13:32:20 +00006702 QualType ToType;
6703 Expr *ToSubExpr;
6704 TypeSourceInfo *ToTypeInfoAsWritten;
6705 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006706
Balazs Keri3b30d652018-10-19 13:32:20 +00006707 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6708 if (!ToBasePathOrErr)
6709 return ToBasePathOrErr.takeError();
6710 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006711
Aleksei Sidorina693b372016-09-28 10:16:56 +00006712 switch (E->getStmtClass()) {
6713 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006714 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006715 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6716 if (!ToLParenLocOrErr)
6717 return ToLParenLocOrErr.takeError();
6718 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6719 if (!ToRParenLocOrErr)
6720 return ToRParenLocOrErr.takeError();
6721 return CStyleCastExpr::Create(
6722 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6723 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6724 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006725 }
6726
6727 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006728 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006729 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6730 if (!ToLParenLocOrErr)
6731 return ToLParenLocOrErr.takeError();
6732 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6733 if (!ToRParenLocOrErr)
6734 return ToRParenLocOrErr.takeError();
6735 return CXXFunctionalCastExpr::Create(
6736 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6737 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6738 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006739 }
6740
6741 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006742 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6743 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6744 if (!ToLParenLocOrErr)
6745 return ToLParenLocOrErr.takeError();
6746 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6747 if (!ToBridgeKeywordLocOrErr)
6748 return ToBridgeKeywordLocOrErr.takeError();
6749 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6750 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6751 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006752 }
6753 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006754 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006755 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006756 }
6757}
6758
Balazs Keri3b30d652018-10-19 13:32:20 +00006759ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6760 SmallVector<OffsetOfNode, 4> ToNodes;
6761 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6762 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006763
Balazs Keri3b30d652018-10-19 13:32:20 +00006764 SourceLocation ToBeginLoc, ToEndLoc;
6765 if (FromNode.getKind() != OffsetOfNode::Base) {
6766 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6767 if (!Imp)
6768 return Imp.takeError();
6769 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6770 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006771
Balazs Keri3b30d652018-10-19 13:32:20 +00006772 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006773 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006774 ToNodes.push_back(
6775 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006776 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006777 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006778 auto ToBSOrErr = import(FromNode.getBase());
6779 if (!ToBSOrErr)
6780 return ToBSOrErr.takeError();
6781 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006782 break;
6783 }
6784 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006785 auto ToFieldOrErr = import(FromNode.getField());
6786 if (!ToFieldOrErr)
6787 return ToFieldOrErr.takeError();
6788 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006789 break;
6790 }
6791 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006792 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6793 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006794 break;
6795 }
6796 }
6797 }
6798
Balazs Keri3b30d652018-10-19 13:32:20 +00006799 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6800 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6801 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6802 if (!ToIndexExprOrErr)
6803 return ToIndexExprOrErr.takeError();
6804 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006805 }
6806
Balazs Keri3b30d652018-10-19 13:32:20 +00006807 auto Imp = importSeq(
6808 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6809 E->getRParenLoc());
6810 if (!Imp)
6811 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006812
Balazs Keri3b30d652018-10-19 13:32:20 +00006813 QualType ToType;
6814 TypeSourceInfo *ToTypeSourceInfo;
6815 SourceLocation ToOperatorLoc, ToRParenLoc;
6816 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6817
6818 return OffsetOfExpr::Create(
6819 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6820 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006821}
6822
Balazs Keri3b30d652018-10-19 13:32:20 +00006823ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6824 auto Imp = importSeq(
6825 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6826 if (!Imp)
6827 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006828
Balazs Keri3b30d652018-10-19 13:32:20 +00006829 QualType ToType;
6830 Expr *ToOperand;
6831 SourceLocation ToBeginLoc, ToEndLoc;
6832 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006833
Balazs Keri3b30d652018-10-19 13:32:20 +00006834 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006835 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006836 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006837 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006838 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006839
Balazs Keri3b30d652018-10-19 13:32:20 +00006840 return new (Importer.getToContext()) CXXNoexceptExpr(
6841 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006842}
6843
Balazs Keri3b30d652018-10-19 13:32:20 +00006844ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6845 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6846 if (!Imp)
6847 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006848
Balazs Keri3b30d652018-10-19 13:32:20 +00006849 Expr *ToSubExpr;
6850 QualType ToType;
6851 SourceLocation ToThrowLoc;
6852 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006853
6854 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006855 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006856}
6857
Balazs Keri3b30d652018-10-19 13:32:20 +00006858ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6859 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6860 if (!ToUsedLocOrErr)
6861 return ToUsedLocOrErr.takeError();
6862
6863 auto ToParamOrErr = import(E->getParam());
6864 if (!ToParamOrErr)
6865 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006866
Eric Fiselier708afb52019-05-16 21:04:15 +00006867 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
6868 if (!UsedContextOrErr)
6869 return UsedContextOrErr.takeError();
6870
Aleksei Sidorina693b372016-09-28 10:16:56 +00006871 return CXXDefaultArgExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00006872 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr, *UsedContextOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006873}
6874
Balazs Keri3b30d652018-10-19 13:32:20 +00006875ExpectedStmt
6876ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6877 auto Imp = importSeq(
6878 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6879 if (!Imp)
6880 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006881
Balazs Keri3b30d652018-10-19 13:32:20 +00006882 QualType ToType;
6883 TypeSourceInfo *ToTypeSourceInfo;
6884 SourceLocation ToRParenLoc;
6885 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006886
6887 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006888 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006889}
6890
Balazs Keri3b30d652018-10-19 13:32:20 +00006891ExpectedStmt
6892ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6893 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6894 if (!ToSubExprOrErr)
6895 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006896
Balazs Keri3b30d652018-10-19 13:32:20 +00006897 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6898 if (!ToDtorOrErr)
6899 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006900
6901 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006902 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6903 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006904}
6905
Balazs Keri3b30d652018-10-19 13:32:20 +00006906ExpectedStmt
6907ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6908 auto Imp = importSeq(
6909 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6910 E->getParenOrBraceRange());
6911 if (!Imp)
6912 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006913
Balazs Keri3b30d652018-10-19 13:32:20 +00006914 CXXConstructorDecl *ToConstructor;
6915 QualType ToType;
6916 TypeSourceInfo *ToTypeSourceInfo;
6917 SourceRange ToParenOrBraceRange;
6918 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006919
Balazs Keri3b30d652018-10-19 13:32:20 +00006920 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6921 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6922 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006923
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006924 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006925 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6926 ToParenOrBraceRange, E->hadMultipleCandidates(),
6927 E->isListInitialization(), E->isStdInitListInitialization(),
6928 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006929}
6930
Balazs Keri3b30d652018-10-19 13:32:20 +00006931ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006932ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006933 auto Imp = importSeq(
6934 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6935 if (!Imp)
6936 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006937
Balazs Keri3b30d652018-10-19 13:32:20 +00006938 QualType ToType;
6939 Expr *ToTemporaryExpr;
6940 const ValueDecl *ToExtendingDecl;
6941 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006942
6943 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006944 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006945
6946 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006947 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006948 return ToMTE;
6949}
6950
Balazs Keri3b30d652018-10-19 13:32:20 +00006951ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6952 auto Imp = importSeq(
6953 E->getType(), E->getPattern(), E->getEllipsisLoc());
6954 if (!Imp)
6955 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006956
Balazs Keri3b30d652018-10-19 13:32:20 +00006957 QualType ToType;
6958 Expr *ToPattern;
6959 SourceLocation ToEllipsisLoc;
6960 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006961
6962 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006963 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006964}
6965
Balazs Keri3b30d652018-10-19 13:32:20 +00006966ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6967 auto Imp = importSeq(
6968 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6969 if (!Imp)
6970 return Imp.takeError();
6971
6972 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6973 NamedDecl *ToPack;
6974 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006975
6976 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006977 if (!E->isValueDependent())
6978 Length = E->getPackLength();
6979
Balazs Keri3b30d652018-10-19 13:32:20 +00006980 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006981 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006982 if (Error Err = ImportTemplateArguments(
6983 E->getPartialArguments().data(),
6984 E->getPartialArguments().size(),
6985 ToPartialArguments))
6986 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006987 }
6988
6989 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006990 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6991 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006992}
6993
Aleksei Sidorina693b372016-09-28 10:16:56 +00006994
Balazs Keri3b30d652018-10-19 13:32:20 +00006995ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6996 auto Imp = importSeq(
6997 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6998 E->getArraySize(), E->getInitializer(), E->getType(),
6999 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
7000 E->getDirectInitRange());
7001 if (!Imp)
7002 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007003
Balazs Keri3b30d652018-10-19 13:32:20 +00007004 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
7005 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00007006 Optional<Expr *> ToArraySize;
7007 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00007008 QualType ToType;
7009 TypeSourceInfo *ToAllocatedTypeSourceInfo;
7010 std::tie(
7011 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
7012 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007013
Balazs Keri3b30d652018-10-19 13:32:20 +00007014 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
7015 if (Error Err =
7016 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
7017 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007018
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00007019 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007020 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
7021 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
7022 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
7023 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
7024 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007025}
7026
Balazs Keri3b30d652018-10-19 13:32:20 +00007027ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
7028 auto Imp = importSeq(
7029 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
7030 if (!Imp)
7031 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007032
Balazs Keri3b30d652018-10-19 13:32:20 +00007033 QualType ToType;
7034 FunctionDecl *ToOperatorDelete;
7035 Expr *ToArgument;
7036 SourceLocation ToBeginLoc;
7037 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007038
7039 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007040 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
7041 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
7042 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00007043}
7044
Balazs Keri3b30d652018-10-19 13:32:20 +00007045ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
7046 auto Imp = importSeq(
7047 E->getType(), E->getLocation(), E->getConstructor(),
7048 E->getParenOrBraceRange());
7049 if (!Imp)
7050 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007051
Balazs Keri3b30d652018-10-19 13:32:20 +00007052 QualType ToType;
7053 SourceLocation ToLocation;
7054 CXXConstructorDecl *ToConstructor;
7055 SourceRange ToParenOrBraceRange;
7056 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007057
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007058 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007059 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7060 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007061
Balazs Keri3b30d652018-10-19 13:32:20 +00007062 return CXXConstructExpr::Create(
7063 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7064 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7065 E->isListInitialization(), E->isStdInitListInitialization(),
7066 E->requiresZeroInitialization(), E->getConstructionKind(),
7067 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007068}
7069
Balazs Keri3b30d652018-10-19 13:32:20 +00007070ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7071 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7072 if (!ToSubExprOrErr)
7073 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007074
Balazs Keri3b30d652018-10-19 13:32:20 +00007075 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7076 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7077 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007078
Balazs Keri3b30d652018-10-19 13:32:20 +00007079 return ExprWithCleanups::Create(
7080 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7081 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007082}
7083
Balazs Keri3b30d652018-10-19 13:32:20 +00007084ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7085 auto Imp = importSeq(
7086 E->getCallee(), E->getType(), E->getRParenLoc());
7087 if (!Imp)
7088 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007089
Balazs Keri3b30d652018-10-19 13:32:20 +00007090 Expr *ToCallee;
7091 QualType ToType;
7092 SourceLocation ToRParenLoc;
7093 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007094
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007095 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007096 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7097 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007098
Bruno Riccic5885cf2018-12-21 15:20:32 +00007099 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7100 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007101}
7102
Balazs Keri3b30d652018-10-19 13:32:20 +00007103ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7104 ExpectedType ToTypeOrErr = import(E->getType());
7105 if (!ToTypeOrErr)
7106 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007107
Balazs Keri3b30d652018-10-19 13:32:20 +00007108 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7109 if (!ToLocationOrErr)
7110 return ToLocationOrErr.takeError();
7111
7112 return new (Importer.getToContext()) CXXThisExpr(
7113 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007114}
7115
Balazs Keri3b30d652018-10-19 13:32:20 +00007116ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7117 ExpectedType ToTypeOrErr = import(E->getType());
7118 if (!ToTypeOrErr)
7119 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007120
Balazs Keri3b30d652018-10-19 13:32:20 +00007121 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7122 if (!ToLocationOrErr)
7123 return ToLocationOrErr.takeError();
7124
7125 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7126 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007127}
7128
Balazs Keri3b30d652018-10-19 13:32:20 +00007129ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7130 auto Imp1 = importSeq(
7131 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7132 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7133 if (!Imp1)
7134 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007135
Balazs Keri3b30d652018-10-19 13:32:20 +00007136 Expr *ToBase;
7137 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7138 NestedNameSpecifierLoc ToQualifierLoc;
7139 ValueDecl *ToMemberDecl;
7140 QualType ToType;
7141 std::tie(
7142 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7143 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007144
Balazs Keri3b30d652018-10-19 13:32:20 +00007145 auto Imp2 = importSeq(
7146 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7147 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7148 if (!Imp2)
7149 return Imp2.takeError();
7150 NamedDecl *ToDecl;
7151 DeclarationName ToName;
7152 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7153 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007154
7155 DeclAccessPair ToFoundDecl =
7156 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007157
Balazs Keri3b30d652018-10-19 13:32:20 +00007158 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007159
Gabor Marton5caba302019-03-07 13:38:20 +00007160 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007161 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007162 if (Error Err =
7163 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7164 E->template_arguments(), ToTAInfo))
7165 return std::move(Err);
7166 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007167 }
7168
Richard Smith1bbad592019-06-11 17:50:36 +00007169 return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(),
7170 ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7171 ToMemberDecl, ToFoundDecl, ToMemberNameInfo,
7172 ResInfo, ToType, E->getValueKind(),
7173 E->getObjectKind(), E->isNonOdrUse());
Sean Callanan59721b32015-04-28 18:41:46 +00007174}
7175
Balazs Keri3b30d652018-10-19 13:32:20 +00007176ExpectedStmt
7177ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7178 auto Imp = importSeq(
7179 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7180 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7181 if (!Imp)
7182 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007183
Balazs Keri3b30d652018-10-19 13:32:20 +00007184 Expr *ToBase;
7185 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7186 NestedNameSpecifierLoc ToQualifierLoc;
7187 TypeSourceInfo *ToScopeTypeInfo;
7188 std::tie(
7189 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7190 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007191
7192 PseudoDestructorTypeStorage Storage;
7193 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7194 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007195 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7196 if (!ToDestroyedTypeLocOrErr)
7197 return ToDestroyedTypeLocOrErr.takeError();
7198 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007199 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007200 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7201 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7202 else
7203 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007204 }
7205
7206 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007207 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7208 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007209}
7210
Balazs Keri3b30d652018-10-19 13:32:20 +00007211ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007212 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007213 auto Imp = importSeq(
7214 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7215 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7216 if (!Imp)
7217 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007218
Balazs Keri3b30d652018-10-19 13:32:20 +00007219 QualType ToType;
7220 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7221 NestedNameSpecifierLoc ToQualifierLoc;
7222 NamedDecl *ToFirstQualifierFoundInScope;
7223 std::tie(
7224 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7225 ToFirstQualifierFoundInScope) = *Imp;
7226
7227 Expr *ToBase = nullptr;
7228 if (!E->isImplicitAccess()) {
7229 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7230 ToBase = *ToBaseOrErr;
7231 else
7232 return ToBaseOrErr.takeError();
7233 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007234
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007235 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007236 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007237 if (Error Err = ImportTemplateArgumentListInfo(
7238 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7239 ToTAInfo))
7240 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007241 ResInfo = &ToTAInfo;
7242 }
7243
Balazs Keri3b30d652018-10-19 13:32:20 +00007244 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7245 if (!ToMemberNameInfoOrErr)
7246 return ToMemberNameInfoOrErr.takeError();
7247 DeclarationNameInfo ToMemberNameInfo(
7248 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007249 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007250 if (Error Err = ImportDeclarationNameLoc(
7251 E->getMemberNameInfo(), ToMemberNameInfo))
7252 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007253
7254 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007255 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7256 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7257 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007258}
7259
Balazs Keri3b30d652018-10-19 13:32:20 +00007260ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007261ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007262 auto Imp = importSeq(
7263 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7264 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7265 if (!Imp)
7266 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007267
Balazs Keri3b30d652018-10-19 13:32:20 +00007268 NestedNameSpecifierLoc ToQualifierLoc;
7269 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7270 DeclarationName ToDeclName;
7271 std::tie(
7272 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7273 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007274
Balazs Keri3b30d652018-10-19 13:32:20 +00007275 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7276 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7277 return std::move(Err);
7278
7279 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007280 TemplateArgumentListInfo *ResInfo = nullptr;
7281 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007282 if (Error Err =
7283 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7284 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007285 ResInfo = &ToTAInfo;
7286 }
7287
7288 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007289 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7290 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007291}
7292
Balazs Keri3b30d652018-10-19 13:32:20 +00007293ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7294 CXXUnresolvedConstructExpr *E) {
7295 auto Imp = importSeq(
7296 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7297 if (!Imp)
7298 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007299
Balazs Keri3b30d652018-10-19 13:32:20 +00007300 SourceLocation ToLParenLoc, ToRParenLoc;
7301 TypeSourceInfo *ToTypeSourceInfo;
7302 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7303
7304 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7305 if (Error Err =
7306 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7307 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007308
7309 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007310 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7311 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007312}
7313
Balazs Keri3b30d652018-10-19 13:32:20 +00007314ExpectedStmt
7315ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7316 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7317 if (!ToNamingClassOrErr)
7318 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007319
Balazs Keri3b30d652018-10-19 13:32:20 +00007320 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7321 if (!ToQualifierLocOrErr)
7322 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007323
Balazs Keri3b30d652018-10-19 13:32:20 +00007324 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7325 if (!ToNameInfoOrErr)
7326 return ToNameInfoOrErr.takeError();
7327 DeclarationNameInfo ToNameInfo(
7328 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007329 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007330 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7331 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007332
7333 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007334 for (auto *D : E->decls())
7335 if (auto ToDOrErr = import(D))
7336 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007337 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007338 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007339
Balazs Keri3b30d652018-10-19 13:32:20 +00007340 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7341 TemplateArgumentListInfo ToTAInfo;
7342 if (Error Err = ImportTemplateArgumentListInfo(
7343 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7344 ToTAInfo))
7345 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007346
Balazs Keri3b30d652018-10-19 13:32:20 +00007347 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7348 if (!ToTemplateKeywordLocOrErr)
7349 return ToTemplateKeywordLocOrErr.takeError();
7350
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007351 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007352 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7353 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7354 ToDecls.begin(), ToDecls.end());
7355 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007356
7357 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007358 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7359 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7360 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007361}
7362
Balazs Keri3b30d652018-10-19 13:32:20 +00007363ExpectedStmt
7364ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7365 auto Imp1 = importSeq(
7366 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7367 E->getTemplateKeywordLoc());
7368 if (!Imp1)
7369 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007370
Balazs Keri3b30d652018-10-19 13:32:20 +00007371 QualType ToType;
7372 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7373 NestedNameSpecifierLoc ToQualifierLoc;
7374 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7375
7376 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7377 if (!Imp2)
7378 return Imp2.takeError();
7379 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7380 // Import additional name location/type info.
7381 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7382 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007383
7384 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007385 for (Decl *D : E->decls())
7386 if (auto ToDOrErr = import(D))
7387 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007388 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007389 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007390
7391 TemplateArgumentListInfo ToTAInfo;
7392 TemplateArgumentListInfo *ResInfo = nullptr;
7393 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007394 if (Error Err =
7395 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7396 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007397 ResInfo = &ToTAInfo;
7398 }
7399
Balazs Keri3b30d652018-10-19 13:32:20 +00007400 Expr *ToBase = nullptr;
7401 if (!E->isImplicitAccess()) {
7402 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7403 ToBase = *ToBaseOrErr;
7404 else
7405 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007406 }
7407
7408 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007409 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7410 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7411 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007412}
7413
Balazs Keri3b30d652018-10-19 13:32:20 +00007414ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7415 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7416 if (!Imp)
7417 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007418
Balazs Keri3b30d652018-10-19 13:32:20 +00007419 Expr *ToCallee;
7420 QualType ToType;
7421 SourceLocation ToRParenLoc;
7422 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007423
7424 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007425 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7426 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7427 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007428
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007429 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007430 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007431 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007432 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7433 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007434 }
7435
Bruno Riccic5885cf2018-12-21 15:20:32 +00007436 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7437 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7438 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007439}
7440
Balazs Keri3b30d652018-10-19 13:32:20 +00007441ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7442 CXXRecordDecl *FromClass = E->getLambdaClass();
7443 auto ToClassOrErr = import(FromClass);
7444 if (!ToClassOrErr)
7445 return ToClassOrErr.takeError();
7446 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007447
Balazs Keri3b30d652018-10-19 13:32:20 +00007448 auto ToCallOpOrErr = import(E->getCallOperator());
7449 if (!ToCallOpOrErr)
7450 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007451
Balazs Keri3b30d652018-10-19 13:32:20 +00007452 SmallVector<LambdaCapture, 8> ToCaptures;
7453 ToCaptures.reserve(E->capture_size());
7454 for (const auto &FromCapture : E->captures()) {
7455 if (auto ToCaptureOrErr = import(FromCapture))
7456 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007457 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007458 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007459 }
7460
Balazs Keri3b30d652018-10-19 13:32:20 +00007461 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7462 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7463 return std::move(Err);
7464
7465 auto Imp = importSeq(
7466 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7467 if (!Imp)
7468 return Imp.takeError();
7469
7470 SourceRange ToIntroducerRange;
7471 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7472 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007473
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007474 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007475 Importer.getToContext(), ToClass, ToIntroducerRange,
7476 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7477 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7478 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007479}
7480
Sean Callanan8bca9962016-03-28 21:43:01 +00007481
Balazs Keri3b30d652018-10-19 13:32:20 +00007482ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7483 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7484 if (!Imp)
7485 return Imp.takeError();
7486
7487 SourceLocation ToLBraceLoc, ToRBraceLoc;
7488 QualType ToType;
7489 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7490
7491 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7492 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7493 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007494
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007495 ASTContext &ToCtx = Importer.getToContext();
7496 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007497 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7498 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007499
Balazs Keri3b30d652018-10-19 13:32:20 +00007500 if (E->hasArrayFiller()) {
7501 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7502 To->setArrayFiller(*ToFillerOrErr);
7503 else
7504 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007505 }
7506
Balazs Keri3b30d652018-10-19 13:32:20 +00007507 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7508 if (auto ToFDOrErr = import(FromFD))
7509 To->setInitializedFieldInUnion(*ToFDOrErr);
7510 else
7511 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007512 }
7513
Balazs Keri3b30d652018-10-19 13:32:20 +00007514 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7515 if (auto ToSyntFormOrErr = import(SyntForm))
7516 To->setSyntacticForm(*ToSyntFormOrErr);
7517 else
7518 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007519 }
7520
Gabor Martona20ce602018-09-03 13:10:53 +00007521 // Copy InitListExprBitfields, which are not handled in the ctor of
7522 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007523 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007524
7525 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007526}
7527
Balazs Keri3b30d652018-10-19 13:32:20 +00007528ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007529 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007530 ExpectedType ToTypeOrErr = import(E->getType());
7531 if (!ToTypeOrErr)
7532 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007533
Balazs Keri3b30d652018-10-19 13:32:20 +00007534 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7535 if (!ToSubExprOrErr)
7536 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007537
Balazs Keri3b30d652018-10-19 13:32:20 +00007538 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7539 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007540}
7541
Balazs Keri3b30d652018-10-19 13:32:20 +00007542ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007543 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007544 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7545 if (!Imp)
7546 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007547
Balazs Keri3b30d652018-10-19 13:32:20 +00007548 SourceLocation ToLocation;
7549 QualType ToType;
7550 CXXConstructorDecl *ToConstructor;
7551 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007552
7553 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007554 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7555 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007556}
7557
Balazs Keri3b30d652018-10-19 13:32:20 +00007558ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7559 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7560 if (!Imp)
7561 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007562
Balazs Keri3b30d652018-10-19 13:32:20 +00007563 QualType ToType;
7564 Expr *ToCommonExpr, *ToSubExpr;
7565 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007566
Balazs Keri3b30d652018-10-19 13:32:20 +00007567 return new (Importer.getToContext()) ArrayInitLoopExpr(
7568 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007569}
7570
Balazs Keri3b30d652018-10-19 13:32:20 +00007571ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7572 ExpectedType ToTypeOrErr = import(E->getType());
7573 if (!ToTypeOrErr)
7574 return ToTypeOrErr.takeError();
7575 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007576}
7577
Balazs Keri3b30d652018-10-19 13:32:20 +00007578ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7579 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7580 if (!ToBeginLocOrErr)
7581 return ToBeginLocOrErr.takeError();
7582
7583 auto ToFieldOrErr = import(E->getField());
7584 if (!ToFieldOrErr)
7585 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007586
Eric Fiselier708afb52019-05-16 21:04:15 +00007587 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7588 if (!UsedContextOrErr)
7589 return UsedContextOrErr.takeError();
7590
Sean Callanandd2c1742016-05-16 20:48:03 +00007591 return CXXDefaultInitExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00007592 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007593}
7594
Balazs Keri3b30d652018-10-19 13:32:20 +00007595ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7596 auto Imp = importSeq(
7597 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7598 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7599 if (!Imp)
7600 return Imp.takeError();
7601
7602 QualType ToType;
7603 Expr *ToSubExpr;
7604 TypeSourceInfo *ToTypeInfoAsWritten;
7605 SourceLocation ToOperatorLoc, ToRParenLoc;
7606 SourceRange ToAngleBrackets;
7607 std::tie(
7608 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7609 ToAngleBrackets) = *Imp;
7610
Sean Callanandd2c1742016-05-16 20:48:03 +00007611 ExprValueKind VK = E->getValueKind();
7612 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007613 auto ToBasePathOrErr = ImportCastPath(E);
7614 if (!ToBasePathOrErr)
7615 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007616
Sean Callanandd2c1742016-05-16 20:48:03 +00007617 if (isa<CXXStaticCastExpr>(E)) {
7618 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007619 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7620 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007621 } else if (isa<CXXDynamicCastExpr>(E)) {
7622 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007623 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7624 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007625 } else if (isa<CXXReinterpretCastExpr>(E)) {
7626 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007627 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7628 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007629 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007630 return CXXConstCastExpr::Create(
7631 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7632 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007633 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007634 llvm_unreachable("Unknown cast type");
7635 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007636 }
7637}
7638
Balazs Keri3b30d652018-10-19 13:32:20 +00007639ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007640 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007641 auto Imp = importSeq(
7642 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7643 if (!Imp)
7644 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007645
Balazs Keri3b30d652018-10-19 13:32:20 +00007646 QualType ToType;
7647 SourceLocation ToExprLoc;
7648 NonTypeTemplateParmDecl *ToParameter;
7649 Expr *ToReplacement;
7650 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007651
7652 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007653 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007654}
7655
Balazs Keri3b30d652018-10-19 13:32:20 +00007656ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7657 auto Imp = importSeq(
7658 E->getType(), E->getBeginLoc(), E->getEndLoc());
7659 if (!Imp)
7660 return Imp.takeError();
7661
7662 QualType ToType;
7663 SourceLocation ToBeginLoc, ToEndLoc;
7664 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007665
7666 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007667 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7668 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007669
7670 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7671 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007672 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007673
7674 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007675 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7676 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007677}
7678
Balazs Keri3b30d652018-10-19 13:32:20 +00007679ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7680 ExpectedType ToTypeOrErr = import(E->getType());
7681 if (!ToTypeOrErr)
7682 return ToTypeOrErr.takeError();
7683
7684 auto ToSourceRangeOrErr = import(E->getSourceRange());
7685 if (!ToSourceRangeOrErr)
7686 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007687
7688 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007689 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7690 return new (Importer.getToContext()) CXXTypeidExpr(
7691 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7692 else
7693 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007694 }
7695
Balazs Keri3b30d652018-10-19 13:32:20 +00007696 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7697 if (!ToExprOperandOrErr)
7698 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007699
Balazs Keri3b30d652018-10-19 13:32:20 +00007700 return new (Importer.getToContext()) CXXTypeidExpr(
7701 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007702}
7703
Lang Hames19e07e12017-06-20 21:06:00 +00007704void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7705 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007706 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7707 if (auto ImportedOrErr = import(FromOverriddenMethod))
7708 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7709 (*ImportedOrErr)->getCanonicalDecl()));
7710 else
7711 consumeError(ImportedOrErr.takeError());
7712 }
Lang Hames19e07e12017-06-20 21:06:00 +00007713}
7714
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007715ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007716 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007717 bool MinimalImport,
Gabor Marton2afbfb62019-07-01 15:37:07 +00007718 std::shared_ptr<ASTImporterSharedState> SharedState)
7719 : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007720 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7721 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007722
Gabor Marton2afbfb62019-07-01 15:37:07 +00007723 // Create a default state without the lookup table: LLDB case.
7724 if (!SharedState) {
7725 this->SharedState = std::make_shared<ASTImporterSharedState>();
7726 }
7727
Gabor Marton54058b52018-12-17 13:53:12 +00007728 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7729 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007730}
7731
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007732ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007733
Gabor Marton54058b52018-12-17 13:53:12 +00007734Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7735 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7736 "Try to get field index for non-field.");
7737
7738 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7739 if (!Owner)
7740 return None;
7741
7742 unsigned Index = 0;
7743 for (const auto *D : Owner->decls()) {
7744 if (D == F)
7745 return Index;
7746
7747 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7748 ++Index;
7749 }
7750
7751 llvm_unreachable("Field was not found in its parent context.");
7752
7753 return None;
7754}
7755
7756ASTImporter::FoundDeclsTy
7757ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7758 // We search in the redecl context because of transparent contexts.
7759 // E.g. a simple C language enum is a transparent context:
7760 // enum E { A, B };
7761 // Now if we had a global variable in the TU
7762 // int A;
7763 // then the enum constant 'A' and the variable 'A' violates ODR.
7764 // We can diagnose this only if we search in the redecl context.
7765 DeclContext *ReDC = DC->getRedeclContext();
Gabor Marton2afbfb62019-07-01 15:37:07 +00007766 if (SharedState->getLookupTable()) {
Gabor Marton54058b52018-12-17 13:53:12 +00007767 ASTImporterLookupTable::LookupResult LookupResult =
Gabor Marton2afbfb62019-07-01 15:37:07 +00007768 SharedState->getLookupTable()->lookup(ReDC, Name);
Gabor Marton54058b52018-12-17 13:53:12 +00007769 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7770 } else {
Gabor Martonaefcf512019-07-17 13:47:46 +00007771 DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name);
7772 FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end());
7773 // We must search by the slow case of localUncachedLookup because that is
7774 // working even if there is no LookupPtr for the DC. We could use
7775 // DC::buildLookup() to create the LookupPtr, but that would load external
7776 // decls again, we must avoid that case.
7777 // Also, even if we had the LookupPtr, we must find Decls which are not
7778 // in the LookupPtr, so we need the slow case.
7779 // These cases are handled in ASTImporterLookupTable, but we cannot use
7780 // that with LLDB since that traverses through the AST which initiates the
7781 // load of external decls again via DC::decls(). And again, we must avoid
7782 // loading external decls during the import.
7783 if (Result.empty())
7784 ReDC->localUncachedLookup(Name, Result);
Gabor Marton54058b52018-12-17 13:53:12 +00007785 return Result;
7786 }
7787}
7788
7789void ASTImporter::AddToLookupTable(Decl *ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00007790 SharedState->addDeclToLookup(ToD);
Gabor Marton54058b52018-12-17 13:53:12 +00007791}
7792
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007793Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7794 // Import the decl using ASTNodeImporter.
7795 ASTNodeImporter Importer(*this);
7796 return Importer.Visit(FromD);
7797}
7798
7799void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7800 MapImported(FromD, ToD);
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007801}
7802
Gabor Marton5ac6d492019-05-15 10:29:48 +00007803Expected<QualType> ASTImporter::Import(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007804 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007805 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007806
Balazs Keri3b30d652018-10-19 13:32:20 +00007807 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007808
7809 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007810 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007811 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007812 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007813 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007814
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007815 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007816 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007817 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007818 if (!ToTOrErr)
7819 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007820
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007821 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007822 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007823
Balazs Keri3b30d652018-10-19 13:32:20 +00007824 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007825}
7826
Gabor Marton5ac6d492019-05-15 10:29:48 +00007827Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007828 if (!FromTSI)
7829 return FromTSI;
7830
7831 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007832 // on the type and a single location. Implement a real version of this.
Gabor Marton5ac6d492019-05-15 10:29:48 +00007833 ExpectedType TOrErr = Import(FromTSI->getType());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007834 if (!TOrErr)
7835 return TOrErr.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00007836 ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007837 if (!BeginLocOrErr)
7838 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007839
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007840 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7841}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007842
Gabor Marton5ac6d492019-05-15 10:29:48 +00007843Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007844 Attr *ToAttr = FromAttr->clone(ToContext);
Gabor Marton5ac6d492019-05-15 10:29:48 +00007845 if (auto ToRangeOrErr = Import(FromAttr->getRange()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007846 ToAttr->setRange(*ToRangeOrErr);
7847 else
7848 return ToRangeOrErr.takeError();
7849
Davide Italianofaee83d2018-11-28 19:15:23 +00007850 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007851}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007852
Gabor Martonbe77a982018-12-12 11:22:55 +00007853Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7854 auto Pos = ImportedDecls.find(FromD);
7855 if (Pos != ImportedDecls.end())
7856 return Pos->second;
7857 else
Sean Callanan59721b32015-04-28 18:41:46 +00007858 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007859}
7860
Gabor Marton458d1452019-02-14 13:07:03 +00007861TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7862 auto FromDPos = ImportedFromDecls.find(ToD);
7863 if (FromDPos == ImportedFromDecls.end())
7864 return nullptr;
7865 return FromDPos->second->getTranslationUnitDecl();
7866}
7867
Gabor Marton5ac6d492019-05-15 10:29:48 +00007868Expected<Decl *> ASTImporter::Import(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007869 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007870 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007871
Gabor Marton1ad4b992019-07-01 14:19:53 +00007872 // Push FromD to the stack, and remove that when we return.
7873 ImportPath.push(FromD);
7874 auto ImportPathBuilder =
7875 llvm::make_scope_exit([this]() { ImportPath.pop(); });
Douglas Gregord451ea92011-07-29 23:31:30 +00007876
Gabor Marton303c98612019-06-25 08:00:51 +00007877 // Check whether there was a previous failed import.
7878 // If yes return the existing error.
7879 if (auto Error = getImportDeclErrorIfAny(FromD))
7880 return make_error<ImportError>(*Error);
7881
Gabor Marton26f72a92018-07-12 09:42:05 +00007882 // Check whether we've already imported this declaration.
7883 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7884 if (ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00007885 // Already imported (possibly from another TU) and with an error.
7886 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
7887 setImportDeclError(FromD, *Error);
7888 return make_error<ImportError>(*Error);
7889 }
7890
Gabor Marton26f72a92018-07-12 09:42:05 +00007891 // If FromD has some updated flags after last import, apply it
7892 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00007893 // If we encounter a cycle during an import then we save the relevant part
7894 // of the import path associated to the Decl.
7895 if (ImportPath.hasCycleAtBack())
7896 SavedImportPaths[FromD].push_back(ImportPath.copyCycleAtBack());
Douglas Gregord451ea92011-07-29 23:31:30 +00007897 return ToD;
7898 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007899
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007900 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007901 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Gabor Marton303c98612019-06-25 08:00:51 +00007902 if (!ToDOrErr) {
7903 // Failed to import.
7904
7905 auto Pos = ImportedDecls.find(FromD);
7906 if (Pos != ImportedDecls.end()) {
7907 // Import failed after the object was created.
7908 // Remove all references to it.
7909 auto *ToD = Pos->second;
7910 ImportedDecls.erase(Pos);
7911
7912 // ImportedDecls and ImportedFromDecls are not symmetric. It may happen
7913 // (e.g. with namespaces) that several decls from the 'from' context are
7914 // mapped to the same decl in the 'to' context. If we removed entries
7915 // from the LookupTable here then we may end up removing them multiple
7916 // times.
7917
7918 // The Lookuptable contains decls only which are in the 'to' context.
7919 // Remove from the Lookuptable only if it is *imported* into the 'to'
7920 // context (and do not remove it if it was added during the initial
7921 // traverse of the 'to' context).
7922 auto PosF = ImportedFromDecls.find(ToD);
7923 if (PosF != ImportedFromDecls.end()) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00007924 SharedState->removeDeclFromLookup(ToD);
Gabor Marton303c98612019-06-25 08:00:51 +00007925 ImportedFromDecls.erase(PosF);
7926 }
7927
7928 // FIXME: AST may contain remaining references to the failed object.
Gabor Marton2afbfb62019-07-01 15:37:07 +00007929 // However, the ImportDeclErrors in the shared state contains all the
7930 // failed objects together with their error.
Gabor Marton303c98612019-06-25 08:00:51 +00007931 }
7932
Gabor Marton2afbfb62019-07-01 15:37:07 +00007933 // Error encountered for the first time.
7934 // After takeError the error is not usable any more in ToDOrErr.
Gabor Marton303c98612019-06-25 08:00:51 +00007935 // Get a copy of the error object (any more simple solution for this?).
7936 ImportError ErrOut;
7937 handleAllErrors(ToDOrErr.takeError(),
7938 [&ErrOut](const ImportError &E) { ErrOut = E; });
7939 setImportDeclError(FromD, ErrOut);
Gabor Marton2afbfb62019-07-01 15:37:07 +00007940 // Set the error for the mapped to Decl, which is in the "to" context.
7941 if (Pos != ImportedDecls.end())
7942 SharedState->setImportDeclError(Pos->second, ErrOut);
Gabor Marton1ad4b992019-07-01 14:19:53 +00007943
7944 // Set the error for all nodes which have been created before we
7945 // recognized the error.
7946 for (const auto &Path : SavedImportPaths[FromD])
Gabor Marton2afbfb62019-07-01 15:37:07 +00007947 for (Decl *FromDi : Path) {
7948 setImportDeclError(FromDi, ErrOut);
7949 //FIXME Should we remove these Decls from ImportedDecls?
7950 // Set the error for the mapped to Decl, which is in the "to" context.
7951 auto Ii = ImportedDecls.find(FromDi);
7952 if (Ii != ImportedDecls.end())
7953 SharedState->setImportDeclError(Ii->second, ErrOut);
7954 // FIXME Should we remove these Decls from the LookupTable,
7955 // and from ImportedFromDecls?
7956 }
Gabor Marton1ad4b992019-07-01 14:19:53 +00007957 SavedImportPaths[FromD].clear();
7958
Gabor Marton303c98612019-06-25 08:00:51 +00007959 // Do not return ToDOrErr, error was taken out of it.
7960 return make_error<ImportError>(ErrOut);
7961 }
7962
Balazs Keri3b30d652018-10-19 13:32:20 +00007963 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007964
Gabor Marton303c98612019-06-25 08:00:51 +00007965 // FIXME: Handle the "already imported with error" case. We can get here
7966 // nullptr only if GetImportedOrCreateDecl returned nullptr (after a
7967 // previously failed create was requested).
7968 // Later GetImportedOrCreateDecl can be updated to return the error.
Gabor Marton7f8c4002019-03-19 13:34:10 +00007969 if (!ToD) {
Gabor Marton303c98612019-06-25 08:00:51 +00007970 auto Err = getImportDeclErrorIfAny(FromD);
7971 assert(Err);
7972 return make_error<ImportError>(*Err);
Gabor Marton7f8c4002019-03-19 13:34:10 +00007973 }
7974
Gabor Marton2afbfb62019-07-01 15:37:07 +00007975 // We could import from the current TU without error. But previously we
7976 // already had imported a Decl as `ToD` from another TU (with another
7977 // ASTImporter object) and with an error.
7978 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
7979 setImportDeclError(FromD, *Error);
7980 return make_error<ImportError>(*Error);
7981 }
7982
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007983 // Make sure that ImportImpl registered the imported decl.
7984 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
Gabor Marton2afbfb62019-07-01 15:37:07 +00007985
Gabor Marton26f72a92018-07-12 09:42:05 +00007986 // Notify subclasses.
7987 Imported(FromD, ToD);
7988
Gabor Martonac3a5d62018-09-17 12:04:52 +00007989 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00007990 SavedImportPaths[FromD].clear();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007991 return ToDOrErr;
7992}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007993
Balazs Keri3b30d652018-10-19 13:32:20 +00007994Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007995 if (!FromDC)
7996 return FromDC;
7997
Gabor Marton5ac6d492019-05-15 10:29:48 +00007998 ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC));
Balazs Keria1f6b102019-04-08 13:59:15 +00007999 if (!ToDCOrErr)
8000 return ToDCOrErr.takeError();
8001 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00008002
Fangrui Song6907ce22018-07-30 19:24:48 +00008003 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00008004 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008005 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
8006 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008007 if (ToRecord->isCompleteDefinition()) {
8008 // Do nothing.
8009 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008010 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8011 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
8012 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008013 } else {
8014 CompleteDecl(ToRecord);
8015 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008016 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
8017 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008018 if (ToEnum->isCompleteDefinition()) {
8019 // Do nothing.
8020 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008021 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8022 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
8023 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008024 } else {
8025 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00008026 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008027 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
8028 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008029 if (ToClass->getDefinition()) {
8030 // Do nothing.
8031 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008032 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8033 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
8034 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008035 } else {
8036 CompleteDecl(ToClass);
8037 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008038 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
8039 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008040 if (ToProto->getDefinition()) {
8041 // Do nothing.
8042 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008043 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8044 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
8045 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008046 } else {
8047 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00008048 }
Douglas Gregor95d82832012-01-24 18:36:04 +00008049 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008050
Douglas Gregor95d82832012-01-24 18:36:04 +00008051 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008052}
8053
Gabor Marton5ac6d492019-05-15 10:29:48 +00008054Expected<Expr *> ASTImporter::Import(Expr *FromE) {
8055 if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE)))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008056 return cast_or_null<Expr>(*ToSOrErr);
8057 else
8058 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00008059}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008060
Gabor Marton5ac6d492019-05-15 10:29:48 +00008061Expected<Stmt *> ASTImporter::Import(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008062 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00008063 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008064
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008065 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008066 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
8067 if (Pos != ImportedStmts.end())
8068 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00008069
Balazs Keri3b30d652018-10-19 13:32:20 +00008070 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008071 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008072 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008073 if (!ToSOrErr)
8074 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008075
Balazs Keri3b30d652018-10-19 13:32:20 +00008076 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00008077 auto *FromE = cast<Expr>(FromS);
8078 // Copy ExprBitfields, which may not be handled in Expr subclasses
8079 // constructors.
8080 ToE->setValueKind(FromE->getValueKind());
8081 ToE->setObjectKind(FromE->getObjectKind());
8082 ToE->setTypeDependent(FromE->isTypeDependent());
8083 ToE->setValueDependent(FromE->isValueDependent());
8084 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
8085 ToE->setContainsUnexpandedParameterPack(
8086 FromE->containsUnexpandedParameterPack());
8087 }
8088
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008089 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00008090 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008091 return ToSOrErr;
8092}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008093
Balazs Keri4a3d7582018-11-27 18:36:31 +00008094Expected<NestedNameSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008095ASTImporter::Import(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008096 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00008097 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008098
Simon Pilgrim130df2c2019-07-15 13:00:43 +00008099 NestedNameSpecifier *Prefix = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008100 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
8101 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00008102
8103 switch (FromNNS->getKind()) {
8104 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008105 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
8106 return NestedNameSpecifier::Create(ToContext, Prefix,
8107 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00008108
8109 case NestedNameSpecifier::Namespace:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008110 if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008111 return NestedNameSpecifier::Create(ToContext, Prefix,
8112 cast<NamespaceDecl>(*NSOrErr));
8113 } else
8114 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008115
8116 case NestedNameSpecifier::NamespaceAlias:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008117 if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008118 return NestedNameSpecifier::Create(ToContext, Prefix,
8119 cast<NamespaceAliasDecl>(*NSADOrErr));
8120 else
8121 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008122
8123 case NestedNameSpecifier::Global:
8124 return NestedNameSpecifier::GlobalSpecifier(ToContext);
8125
Nikola Smiljanic67860242014-09-26 00:28:20 +00008126 case NestedNameSpecifier::Super:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008127 if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008128 return NestedNameSpecifier::SuperSpecifier(ToContext,
8129 cast<CXXRecordDecl>(*RDOrErr));
8130 else
8131 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008132
Douglas Gregor90ebf252011-04-27 16:48:40 +00008133 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008134 case NestedNameSpecifier::TypeSpecWithTemplate:
8135 if (Expected<QualType> TyOrErr =
Gabor Marton5ac6d492019-05-15 10:29:48 +00008136 Import(QualType(FromNNS->getAsType(), 0u))) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008137 bool TSTemplate =
8138 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
8139 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
8140 TyOrErr->getTypePtr());
8141 } else {
8142 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008143 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008144 }
8145
8146 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008147}
8148
Balazs Keri4a3d7582018-11-27 18:36:31 +00008149Expected<NestedNameSpecifierLoc>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008150ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008151 // Copied from NestedNameSpecifier mostly.
8152 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8153 NestedNameSpecifierLoc NNS = FromNNS;
8154
8155 // Push each of the nested-name-specifiers's onto a stack for
8156 // serialization in reverse order.
8157 while (NNS) {
8158 NestedNames.push_back(NNS);
8159 NNS = NNS.getPrefix();
8160 }
8161
8162 NestedNameSpecifierLocBuilder Builder;
8163
8164 while (!NestedNames.empty()) {
8165 NNS = NestedNames.pop_back_val();
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00008166 NestedNameSpecifier *Spec = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008167 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8168 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008169
8170 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008171
8172 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8173 if (Kind != NestedNameSpecifier::Super) {
8174 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8175 return std::move(Err);
8176
8177 if (Kind != NestedNameSpecifier::Global)
8178 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8179 return std::move(Err);
8180 }
8181
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008182 switch (Kind) {
8183 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008184 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8185 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008186 break;
8187
8188 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008189 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8190 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008191 break;
8192
8193 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008194 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8195 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008196 break;
8197
8198 case NestedNameSpecifier::TypeSpec:
8199 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008200 SourceLocation ToTLoc;
8201 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8202 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008203 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008204 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008205 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8206 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008207 break;
8208 }
8209
8210 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008211 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008212 break;
8213
8214 case NestedNameSpecifier::Super: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008215 auto ToSourceRangeOrErr = Import(NNS.getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008216 if (!ToSourceRangeOrErr)
8217 return ToSourceRangeOrErr.takeError();
8218
8219 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8220 ToSourceRangeOrErr->getBegin(),
8221 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008222 }
8223 }
8224 }
8225
8226 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008227}
8228
Gabor Marton5ac6d492019-05-15 10:29:48 +00008229Expected<TemplateName> ASTImporter::Import(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008230 switch (From.getKind()) {
8231 case TemplateName::Template:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008232 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008233 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8234 else
8235 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008236
Douglas Gregore2e50d332010-12-01 01:36:18 +00008237 case TemplateName::OverloadedTemplate: {
8238 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8239 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008240 for (auto *I : *FromStorage) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008241 if (auto ToOrErr = Import(I))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008242 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008243 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008244 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008245 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008246 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008247 ToTemplates.end());
8248 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008249
Richard Smithb23c5e82019-05-09 03:31:27 +00008250 case TemplateName::AssumedTemplate: {
8251 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008252 auto DeclNameOrErr = Import(FromStorage->getDeclName());
Richard Smithb23c5e82019-05-09 03:31:27 +00008253 if (!DeclNameOrErr)
8254 return DeclNameOrErr.takeError();
8255 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8256 }
8257
Douglas Gregore2e50d332010-12-01 01:36:18 +00008258 case TemplateName::QualifiedTemplate: {
8259 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008260 auto QualifierOrErr = Import(QTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008261 if (!QualifierOrErr)
8262 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008263
Gabor Marton5ac6d492019-05-15 10:29:48 +00008264 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008265 return ToContext.getQualifiedTemplateName(
8266 *QualifierOrErr, QTN->hasTemplateKeyword(),
8267 cast<TemplateDecl>(*ToTemplateOrErr));
8268 else
8269 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008270 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008271
Douglas Gregore2e50d332010-12-01 01:36:18 +00008272 case TemplateName::DependentTemplate: {
8273 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008274 auto QualifierOrErr = Import(DTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008275 if (!QualifierOrErr)
8276 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008277
Douglas Gregore2e50d332010-12-01 01:36:18 +00008278 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008279 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008280 Import(DTN->getIdentifier()));
8281 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008282
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008283 return ToContext.getDependentTemplateName(*QualifierOrErr,
8284 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008285 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008286
8287 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008288 SubstTemplateTemplateParmStorage *Subst =
8289 From.getAsSubstTemplateTemplateParm();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008290 ExpectedDecl ParamOrErr = Import(Subst->getParameter());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008291 if (!ParamOrErr)
8292 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008293
Gabor Marton5ac6d492019-05-15 10:29:48 +00008294 auto ReplacementOrErr = Import(Subst->getReplacement());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008295 if (!ReplacementOrErr)
8296 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008297
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008298 return ToContext.getSubstTemplateTemplateParm(
8299 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008300 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008301
Douglas Gregor5590be02011-01-15 06:45:20 +00008302 case TemplateName::SubstTemplateTemplateParmPack: {
8303 SubstTemplateTemplateParmPackStorage *SubstPack
8304 = From.getAsSubstTemplateTemplateParmPack();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008305 ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008306 if (!ParamOrErr)
8307 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008308
Douglas Gregor5590be02011-01-15 06:45:20 +00008309 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008310 auto ArgPackOrErr =
8311 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8312 if (!ArgPackOrErr)
8313 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008314
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008315 return ToContext.getSubstTemplateTemplateParmPack(
8316 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008317 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008318 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008319
Douglas Gregore2e50d332010-12-01 01:36:18 +00008320 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008321}
8322
Gabor Marton5ac6d492019-05-15 10:29:48 +00008323Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008324 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008325 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008326
Douglas Gregor811663e2010-02-10 00:15:17 +00008327 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008328 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008329
Douglas Gregor811663e2010-02-10 00:15:17 +00008330 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008331 Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008332 if (!ToFileIDOrErr)
8333 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008334 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008335 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8336}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008337
Gabor Marton5ac6d492019-05-15 10:29:48 +00008338Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008339 SourceLocation ToBegin, ToEnd;
8340 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8341 return std::move(Err);
8342 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8343 return std::move(Err);
8344
8345 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008346}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008347
Gabor Marton5ac6d492019-05-15 10:29:48 +00008348Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008349 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008350 if (Pos != ImportedFileIDs.end())
8351 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008352
Douglas Gregor811663e2010-02-10 00:15:17 +00008353 SourceManager &FromSM = FromContext.getSourceManager();
8354 SourceManager &ToSM = ToContext.getSourceManager();
8355 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008356
8357 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008358 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008359 if (FromSLoc.isExpansion()) {
8360 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008361 ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008362 if (!ToSpLoc)
8363 return ToSpLoc.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008364 ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008365 if (!ToExLocS)
8366 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008367 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8368 SourceLocation MLoc;
8369 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008370 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008371 } else {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008372 if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008373 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8374 FromEx.isExpansionTokenRange());
8375 else
8376 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008377 }
8378 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008379 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008380 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008381
8382 if (!IsBuiltin) {
8383 // Include location of this file.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008384 ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008385 if (!ToIncludeLoc)
8386 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008387
8388 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8389 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8390 // disk again
8391 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8392 // than mmap the files several times.
8393 const FileEntry *Entry =
8394 ToFileManager.getFile(Cache->OrigEntry->getName());
8395 // FIXME: The filename may be a virtual name that does probably not
8396 // point to a valid file and we get no Entry here. In this case try with
8397 // the memory buffer below.
8398 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008399 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008400 FromSLoc.getFile().getFileCharacteristic());
8401 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008402 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008403
8404 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008405 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008406 bool Invalid = true;
8407 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8408 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8409 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008410 // FIXME: Use a new error kind?
8411 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008412
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008413 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8414 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8415 FromBuf->getBufferIdentifier());
8416 ToID = ToSM.createFileID(std::move(ToBuf),
8417 FromSLoc.getFile().getFileCharacteristic());
8418 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008419 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008420
Balazs Keri9cf39df2019-02-27 16:31:48 +00008421 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8422
Sebastian Redl99219f12010-09-30 01:03:06 +00008423 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008424 return ToID;
8425}
8426
Gabor Marton5ac6d492019-05-15 10:29:48 +00008427Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) {
8428 ExpectedExpr ToExprOrErr = Import(From->getInit());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008429 if (!ToExprOrErr)
8430 return ToExprOrErr.takeError();
8431
Gabor Marton5ac6d492019-05-15 10:29:48 +00008432 auto LParenLocOrErr = Import(From->getLParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008433 if (!LParenLocOrErr)
8434 return LParenLocOrErr.takeError();
8435
Gabor Marton5ac6d492019-05-15 10:29:48 +00008436 auto RParenLocOrErr = Import(From->getRParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008437 if (!RParenLocOrErr)
8438 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008439
8440 if (From->isBaseInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008441 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008442 if (!ToTInfoOrErr)
8443 return ToTInfoOrErr.takeError();
8444
8445 SourceLocation EllipsisLoc;
8446 if (From->isPackExpansion())
8447 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8448 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008449
8450 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008451 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8452 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008453 } else if (From->isMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008454 ExpectedDecl ToFieldOrErr = Import(From->getMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008455 if (!ToFieldOrErr)
8456 return ToFieldOrErr.takeError();
8457
Gabor Marton5ac6d492019-05-15 10:29:48 +00008458 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008459 if (!MemberLocOrErr)
8460 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008461
8462 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008463 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8464 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008465 } else if (From->isIndirectMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008466 ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008467 if (!ToIFieldOrErr)
8468 return ToIFieldOrErr.takeError();
8469
Gabor Marton5ac6d492019-05-15 10:29:48 +00008470 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008471 if (!MemberLocOrErr)
8472 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008473
8474 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008475 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8476 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008477 } else if (From->isDelegatingInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008478 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008479 if (!ToTInfoOrErr)
8480 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008481
8482 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008483 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8484 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008485 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008486 // FIXME: assert?
8487 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008488 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008489}
Sean Callanandd2c1742016-05-16 20:48:03 +00008490
Balazs Keri4a3d7582018-11-27 18:36:31 +00008491Expected<CXXBaseSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008492ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008493 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8494 if (Pos != ImportedCXXBaseSpecifiers.end())
8495 return Pos->second;
8496
Gabor Marton5ac6d492019-05-15 10:29:48 +00008497 Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008498 if (!ToSourceRange)
8499 return ToSourceRange.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008500 Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008501 if (!ToTSI)
8502 return ToTSI.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008503 ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008504 if (!ToEllipsisLoc)
8505 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008506 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008507 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8508 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008509 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8510 return Imported;
8511}
8512
Gabor Marton5ac6d492019-05-15 10:29:48 +00008513Error ASTImporter::ImportDefinition(Decl *From) {
8514 ExpectedDecl ToOrErr = Import(From);
8515 if (!ToOrErr)
8516 return ToOrErr.takeError();
8517 Decl *To = *ToOrErr;
Fangrui Song6907ce22018-07-30 19:24:48 +00008518
Don Hintonf170dff2019-03-19 06:14:14 +00008519 auto *FromDC = cast<DeclContext>(From);
8520 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008521
Don Hintonf170dff2019-03-19 06:14:14 +00008522 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8523 if (!ToRecord->getDefinition()) {
8524 return Importer.ImportDefinition(
8525 cast<RecordDecl>(FromDC), ToRecord,
8526 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008527 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008528 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008529
Don Hintonf170dff2019-03-19 06:14:14 +00008530 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8531 if (!ToEnum->getDefinition()) {
8532 return Importer.ImportDefinition(
8533 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8534 }
8535 }
8536
8537 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8538 if (!ToIFace->getDefinition()) {
8539 return Importer.ImportDefinition(
8540 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8541 ASTNodeImporter::IDK_Everything);
8542 }
8543 }
8544
8545 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8546 if (!ToProto->getDefinition()) {
8547 return Importer.ImportDefinition(
8548 cast<ObjCProtocolDecl>(FromDC), ToProto,
8549 ASTNodeImporter::IDK_Everything);
8550 }
8551 }
8552
8553 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008554}
8555
Gabor Marton5ac6d492019-05-15 10:29:48 +00008556Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008557 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008558 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008559
8560 switch (FromName.getNameKind()) {
8561 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008562 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008563
8564 case DeclarationName::ObjCZeroArgSelector:
8565 case DeclarationName::ObjCOneArgSelector:
8566 case DeclarationName::ObjCMultiArgSelector:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008567 if (auto ToSelOrErr = Import(FromName.getObjCSelector()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008568 return DeclarationName(*ToSelOrErr);
8569 else
8570 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008571
8572 case DeclarationName::CXXConstructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008573 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008574 return ToContext.DeclarationNames.getCXXConstructorName(
8575 ToContext.getCanonicalType(*ToTyOrErr));
8576 else
8577 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008578 }
8579
8580 case DeclarationName::CXXDestructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008581 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008582 return ToContext.DeclarationNames.getCXXDestructorName(
8583 ToContext.getCanonicalType(*ToTyOrErr));
8584 else
8585 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008586 }
8587
Richard Smith35845152017-02-07 01:37:30 +00008588 case DeclarationName::CXXDeductionGuideName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008589 if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008590 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8591 cast<TemplateDecl>(*ToTemplateOrErr));
8592 else
8593 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008594 }
8595
Douglas Gregor96e578d2010-02-05 17:54:41 +00008596 case DeclarationName::CXXConversionFunctionName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008597 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008598 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8599 ToContext.getCanonicalType(*ToTyOrErr));
8600 else
8601 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008602 }
8603
8604 case DeclarationName::CXXOperatorName:
8605 return ToContext.DeclarationNames.getCXXOperatorName(
8606 FromName.getCXXOverloadedOperator());
8607
8608 case DeclarationName::CXXLiteralOperatorName:
8609 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008610 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008611
8612 case DeclarationName::CXXUsingDirective:
8613 // FIXME: STATICS!
8614 return DeclarationName::getUsingDirectiveName();
8615 }
8616
David Blaikiee4d798f2012-01-20 21:50:17 +00008617 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008618}
8619
Douglas Gregore2e50d332010-12-01 01:36:18 +00008620IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008621 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008622 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008623
Sean Callananf94ef1d2016-05-14 06:11:19 +00008624 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8625
8626 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8627 ToId->setBuiltinID(FromId->getBuiltinID());
8628
8629 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008630}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008631
Gabor Marton5ac6d492019-05-15 10:29:48 +00008632Expected<Selector> ASTImporter::Import(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008633 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008634 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008635
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008636 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008637 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8638 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8639 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8640 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8641}
8642
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008643DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8644 DeclContext *DC,
8645 unsigned IDNS,
8646 NamedDecl **Decls,
8647 unsigned NumDecls) {
8648 return Name;
8649}
8650
8651DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008652 if (LastDiagFromFrom)
8653 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8654 FromContext.getDiagnostics());
8655 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008656 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008657}
8658
8659DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008660 if (!LastDiagFromFrom)
8661 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8662 ToContext.getDiagnostics());
8663 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008664 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008665}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008666
Douglas Gregor2e15c842012-02-01 21:00:38 +00008667void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008668 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008669 if (!ID->getDefinition())
8670 ID->startDefinition();
8671 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008672 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008673 if (!PD->getDefinition())
8674 PD->startDefinition();
8675 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008676 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008677 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8678 TD->startDefinition();
8679 TD->setCompleteDefinition(true);
8680 }
8681 }
8682 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008683 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008684 }
8685}
8686
Gabor Marton26f72a92018-07-12 09:42:05 +00008687Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8688 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8689 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8690 "Try to import an already imported Decl");
8691 if (Pos != ImportedDecls.end())
8692 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008693 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008694 // This mapping should be maintained only in this function. Therefore do not
8695 // check for additional consistency.
8696 ImportedFromDecls[To] = From;
Gabor Marton303c98612019-06-25 08:00:51 +00008697 AddToLookupTable(To);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008698 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008699}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008700
Gabor Marton303c98612019-06-25 08:00:51 +00008701llvm::Optional<ImportError>
8702ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const {
8703 auto Pos = ImportDeclErrors.find(FromD);
8704 if (Pos != ImportDeclErrors.end())
8705 return Pos->second;
8706 else
8707 return Optional<ImportError>();
8708}
8709
8710void ASTImporter::setImportDeclError(Decl *From, ImportError Error) {
Gabor Marton1ad4b992019-07-01 14:19:53 +00008711 auto InsertRes = ImportDeclErrors.insert({From, Error});
Benjamin Kramer4f769362019-07-01 14:33:26 +00008712 (void)InsertRes;
Gabor Marton1ad4b992019-07-01 14:19:53 +00008713 // Either we set the error for the first time, or we already had set one and
8714 // now we want to set the same error.
8715 assert(InsertRes.second || InsertRes.first->second.Error == Error.Error);
Gabor Marton303c98612019-06-25 08:00:51 +00008716}
8717
Douglas Gregordd6006f2012-07-17 21:16:27 +00008718bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8719 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008720 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8721 ImportedTypes.find(From.getTypePtr());
8722 if (Pos != ImportedTypes.end()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008723 if (ExpectedType ToFromOrErr = Import(From)) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008724 if (ToContext.hasSameType(*ToFromOrErr, To))
8725 return true;
8726 } else {
8727 llvm::consumeError(ToFromOrErr.takeError());
8728 }
8729 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008730
Douglas Gregordd6006f2012-07-17 21:16:27 +00008731 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008732 getStructuralEquivalenceKind(*this), false,
8733 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008734 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008735}