blob: d10db32e872570cdea5f2f1ab4bbc8bbc6f6a2e2 [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor96e578d2010-02-05 17:54:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTImporter class which imports AST nodes from one
10// context into another context.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000013
Douglas Gregor96e578d2010-02-05 17:54:41 +000014#include "clang/AST/ASTImporter.h"
Gabor Marton54058b52018-12-17 13:53:12 +000015#include "clang/AST/ASTImporterLookupTable.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Balazs Keri3b30d652018-10-19 13:32:20 +000074 using llvm::make_error;
75 using llvm::Error;
76 using llvm::Expected;
77 using ExpectedType = llvm::Expected<QualType>;
78 using ExpectedStmt = llvm::Expected<Stmt *>;
79 using ExpectedExpr = llvm::Expected<Expr *>;
80 using ExpectedDecl = llvm::Expected<Decl *>;
81 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000082
Balazs Keri3b30d652018-10-19 13:32:20 +000083 std::string ImportError::toString() const {
84 // FIXME: Improve error texts.
85 switch (Error) {
86 case NameConflict:
87 return "NameConflict";
88 case UnsupportedConstruct:
89 return "UnsupportedConstruct";
90 case Unknown:
91 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000092 }
Balazs Keri2a13d662018-10-19 15:16:51 +000093 llvm_unreachable("Invalid error code.");
94 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
Gabor Marton7df342a2018-12-17 12:42:12 +0000125 if (auto *TD = dyn_cast<TagDecl>(D))
126 return getCanonicalForwardRedeclChain<TagDecl>(TD);
Gabor Martonac3a5d62018-09-17 12:04:52 +0000127 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000128 }
129
Gabor Marton26f72a92018-07-12 09:42:05 +0000130 void updateFlags(const Decl *From, Decl *To) {
131 // Check if some flags or attrs are new in 'From' and copy into 'To'.
132 // FIXME: Other flags or attrs?
133 if (From->isUsed(false) && !To->isUsed(false))
134 To->setIsUsed();
135 }
136
Balazs Keri3b30d652018-10-19 13:32:20 +0000137 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
138 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
139 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000140 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000141
Balazs Keri3b30d652018-10-19 13:32:20 +0000142 // Use this instead of Importer.importInto .
143 template <typename ImportT>
144 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
145 return Importer.importInto(To, From);
146 }
147
148 // Use this to import pointers of specific type.
149 template <typename ImportT>
150 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000151 auto ToOrErr = Importer.Import_New(From);
152 if (ToOrErr)
153 To = cast_or_null<ImportT>(*ToOrErr);
154 return ToOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +0000155 }
156
157 // Call the import function of ASTImporter for a baseclass of type `T` and
158 // cast the return value to `T`.
159 template <typename T>
160 Expected<T *> import(T *From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000161 auto ToOrErr = Importer.Import_New(From);
162 if (!ToOrErr)
163 return ToOrErr.takeError();
164 return cast_or_null<T>(*ToOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +0000165 }
166
167 template <typename T>
168 Expected<T *> import(const T *From) {
169 return import(const_cast<T *>(From));
170 }
171
172 // Call the import function of ASTImporter for type `T`.
173 template <typename T>
174 Expected<T> import(const T &From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000175 return Importer.Import_New(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000176 }
177
Richard Smithb9fb1212019-05-06 03:47:15 +0000178 // Import an Optional<T> by importing the contained T, if any.
179 template<typename T>
180 Expected<Optional<T>> import(Optional<T> From) {
181 if (!From)
182 return Optional<T>();
183 return import(*From);
184 }
185
Balazs Keri3b30d652018-10-19 13:32:20 +0000186 template <class T>
187 Expected<std::tuple<T>>
188 importSeq(const T &From) {
189 Expected<T> ToOrErr = import(From);
190 if (!ToOrErr)
191 return ToOrErr.takeError();
192 return std::make_tuple<T>(std::move(*ToOrErr));
193 }
194
195 // Import multiple objects with a single function call.
196 // This should work for every type for which a variant of `import` exists.
197 // The arguments are processed from left to right and import is stopped on
198 // first error.
199 template <class THead, class... TTail>
200 Expected<std::tuple<THead, TTail...>>
201 importSeq(const THead &FromHead, const TTail &...FromTail) {
202 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
203 if (!ToHeadOrErr)
204 return ToHeadOrErr.takeError();
205 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
206 if (!ToTailOrErr)
207 return ToTailOrErr.takeError();
208 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
209 }
210
211// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000212 template <typename ToDeclT> struct CallOverloadedCreateFun {
213 template <typename... Args>
214 auto operator()(Args &&... args)
215 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
216 return ToDeclT::Create(std::forward<Args>(args)...);
217 }
218 };
219
220 // Always use these functions to create a Decl during import. There are
221 // certain tasks which must be done after the Decl was created, e.g. we
222 // must immediately register that as an imported Decl. The parameter `ToD`
223 // will be set to the newly created Decl or if had been imported before
224 // then to the already imported Decl. Returns a bool value set to true if
225 // the `FromD` had been imported before.
226 template <typename ToDeclT, typename FromDeclT, typename... Args>
227 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
228 Args &&... args) {
229 // There may be several overloads of ToDeclT::Create. We must make sure
230 // to call the one which would be chosen by the arguments, thus we use a
231 // wrapper for the overload set.
232 CallOverloadedCreateFun<ToDeclT> OC;
233 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
234 std::forward<Args>(args)...);
235 }
236 // Use this overload if a special Type is needed to be created. E.g if we
237 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
238 // then:
239 // TypedefNameDecl *ToTypedef;
240 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
241 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
242 typename... Args>
243 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
244 Args &&... args) {
245 CallOverloadedCreateFun<NewDeclT> OC;
246 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
247 std::forward<Args>(args)...);
248 }
249 // Use this version if a special create function must be
250 // used, e.g. CXXRecordDecl::CreateLambda .
251 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
252 typename... Args>
253 LLVM_NODISCARD bool
254 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
255 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000256 // FIXME: This code is needed later.
257 //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 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001635 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001636 for (auto *From : FromDC->decls()) {
1637 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001638 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001639 // Ignore the error, continue with next Decl.
1640 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001641 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001642 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001643
Balazs Keri3b30d652018-10-19 13:32:20 +00001644 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001645}
1646
Balazs Keri3b30d652018-10-19 13:32:20 +00001647Error ASTNodeImporter::ImportDeclContext(
1648 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1649 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1650 if (!ToDCOrErr)
1651 return ToDCOrErr.takeError();
1652 ToDC = *ToDCOrErr;
1653
1654 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1655 auto ToLexicalDCOrErr = Importer.ImportContext(
1656 FromD->getLexicalDeclContext());
1657 if (!ToLexicalDCOrErr)
1658 return ToLexicalDCOrErr.takeError();
1659 ToLexicalDC = *ToLexicalDCOrErr;
1660 } else
1661 ToLexicalDC = ToDC;
1662
1663 return Error::success();
1664}
1665
1666Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001667 const CXXRecordDecl *From, CXXRecordDecl *To) {
1668 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1669 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001670
Balazs Keri1d20cc22018-07-16 12:16:39 +00001671 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001672 if (FromM->isImplicit()) {
1673 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1674 if (!ToMOrErr)
1675 return ToMOrErr.takeError();
1676 }
1677
1678 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001679}
1680
Balazs Keri3b30d652018-10-19 13:32:20 +00001681static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1682 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001683 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri57949eb2019-03-25 09:16:39 +00001684 if (ExpectedDecl ToTypedefOrErr = Importer.Import_New(FromTypedef))
1685 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1686 else
1687 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001688 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001689 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001690}
1691
Balazs Keri3b30d652018-10-19 13:32:20 +00001692Error ASTNodeImporter::ImportDefinition(
1693 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001694 if (To->getDefinition() || To->isBeingDefined()) {
1695 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001696 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001697
Balazs Keri3b30d652018-10-19 13:32:20 +00001698 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001699 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001700
Douglas Gregore2e50d332010-12-01 01:36:18 +00001701 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001702
Balazs Keri3b30d652018-10-19 13:32:20 +00001703 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1704 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001705
Douglas Gregore2e50d332010-12-01 01:36:18 +00001706 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001707 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1708 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1709 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001710
1711 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1712 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1713 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001714 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001715 ToData.Aggregate = FromData.Aggregate;
1716 ToData.PlainOldData = FromData.PlainOldData;
1717 ToData.Empty = FromData.Empty;
1718 ToData.Polymorphic = FromData.Polymorphic;
1719 ToData.Abstract = FromData.Abstract;
1720 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001721 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1722 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1723 ToData.HasBasesWithNonStaticDataMembers =
1724 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001725 ToData.HasPrivateFields = FromData.HasPrivateFields;
1726 ToData.HasProtectedFields = FromData.HasProtectedFields;
1727 ToData.HasPublicFields = FromData.HasPublicFields;
1728 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001729 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001730 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001731 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001732 ToData.HasUninitializedReferenceMember
1733 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001734 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001735 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1736 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001737 ToData.NeedOverloadResolutionForCopyConstructor
1738 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001739 ToData.NeedOverloadResolutionForMoveConstructor
1740 = FromData.NeedOverloadResolutionForMoveConstructor;
1741 ToData.NeedOverloadResolutionForMoveAssignment
1742 = FromData.NeedOverloadResolutionForMoveAssignment;
1743 ToData.NeedOverloadResolutionForDestructor
1744 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001745 ToData.DefaultedCopyConstructorIsDeleted
1746 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001747 ToData.DefaultedMoveConstructorIsDeleted
1748 = FromData.DefaultedMoveConstructorIsDeleted;
1749 ToData.DefaultedMoveAssignmentIsDeleted
1750 = FromData.DefaultedMoveAssignmentIsDeleted;
1751 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001752 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1753 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001754 ToData.HasConstexprNonCopyMoveConstructor
1755 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001756 ToData.HasDefaultedDefaultConstructor
1757 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001758 ToData.DefaultedDefaultConstructorIsConstexpr
1759 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001760 ToData.HasConstexprDefaultConstructor
1761 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001762 ToData.HasNonLiteralTypeFieldsOrBases
1763 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001764 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001765 ToData.UserProvidedDefaultConstructor
1766 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001767 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001768 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1769 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1770 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1771 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001772 ToData.ImplicitCopyAssignmentHasConstParam
1773 = FromData.ImplicitCopyAssignmentHasConstParam;
1774 ToData.HasDeclaredCopyConstructorWithConstParam
1775 = FromData.HasDeclaredCopyConstructorWithConstParam;
1776 ToData.HasDeclaredCopyAssignmentWithConstParam
1777 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001778
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001779 // Copy over the data stored in RecordDeclBits
1780 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1781
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001782 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001783 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001784 ExpectedType TyOrErr = import(Base1.getType());
1785 if (!TyOrErr)
1786 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001787
1788 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001789 if (Base1.isPackExpansion()) {
1790 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1791 EllipsisLoc = *LocOrErr;
1792 else
1793 return LocOrErr.takeError();
1794 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001795
1796 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001797 if (Error Err =
1798 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1799 return Err;
1800
1801 auto RangeOrErr = import(Base1.getSourceRange());
1802 if (!RangeOrErr)
1803 return RangeOrErr.takeError();
1804
1805 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1806 if (!TSIOrErr)
1807 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001808
Douglas Gregore2e50d332010-12-01 01:36:18 +00001809 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001810 new (Importer.getToContext()) CXXBaseSpecifier(
1811 *RangeOrErr,
1812 Base1.isVirtual(),
1813 Base1.isBaseOfClass(),
1814 Base1.getAccessSpecifierAsWritten(),
1815 *TSIOrErr,
1816 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001817 }
1818 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001819 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001820 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001821
Douglas Gregor2e15c842012-02-01 21:00:38 +00001822 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001823 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1824 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001825
Douglas Gregore2e50d332010-12-01 01:36:18 +00001826 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001827 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001828}
1829
Balazs Keri3b30d652018-10-19 13:32:20 +00001830Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001831 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001832 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001833
Gabor Martonac3a5d62018-09-17 12:04:52 +00001834 Expr *FromInit = From->getInit();
1835 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001836 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001837
Balazs Keri3b30d652018-10-19 13:32:20 +00001838 ExpectedExpr ToInitOrErr = import(FromInit);
1839 if (!ToInitOrErr)
1840 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001841
Balazs Keri3b30d652018-10-19 13:32:20 +00001842 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001843 if (From->isInitKnownICE()) {
1844 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1845 Eval->CheckedICE = true;
1846 Eval->IsICE = From->isInitICE();
1847 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001848
1849 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001850 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001851}
1852
Balazs Keri3b30d652018-10-19 13:32:20 +00001853Error ASTNodeImporter::ImportDefinition(
1854 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001855 if (To->getDefinition() || To->isBeingDefined()) {
1856 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001857 return ImportDeclContext(From, /*ForceImport=*/true);
1858 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001859 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001860
Douglas Gregord451ea92011-07-29 23:31:30 +00001861 To->startDefinition();
1862
Balazs Keri3b30d652018-10-19 13:32:20 +00001863 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1864 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001865
Balazs Keri3b30d652018-10-19 13:32:20 +00001866 ExpectedType ToTypeOrErr =
1867 import(Importer.getFromContext().getTypeDeclType(From));
1868 if (!ToTypeOrErr)
1869 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001870
Balazs Keri3b30d652018-10-19 13:32:20 +00001871 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1872 if (!ToPromotionTypeOrErr)
1873 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001874
1875 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001876 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1877 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001878
Douglas Gregord451ea92011-07-29 23:31:30 +00001879 // FIXME: we might need to merge the number of positive or negative bits
1880 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001881 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001882 From->getNumPositiveBits(),
1883 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001884 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001885}
1886
Balazs Keri3b30d652018-10-19 13:32:20 +00001887Error ASTNodeImporter::ImportTemplateArguments(
1888 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1889 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001890 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001891 if (auto ToOrErr = import(FromArgs[I]))
1892 ToArgs.push_back(*ToOrErr);
1893 else
1894 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001895 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001896
Balazs Keri3b30d652018-10-19 13:32:20 +00001897 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001898}
1899
Balazs Keri3b30d652018-10-19 13:32:20 +00001900// FIXME: Do not forget to remove this and use only 'import'.
1901Expected<TemplateArgument>
1902ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1903 return import(From);
1904}
1905
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001906template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001907Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001908 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1909 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001910 if (auto ToLocOrErr = import(FromLoc))
1911 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001912 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001913 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001914 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001915 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001916}
1917
Gabor Marton26f72a92018-07-12 09:42:05 +00001918static StructuralEquivalenceKind
1919getStructuralEquivalenceKind(const ASTImporter &Importer) {
1920 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1921 : StructuralEquivalenceKind::Default;
1922}
1923
Gabor Marton950fb572018-07-17 12:39:27 +00001924bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1925 StructuralEquivalenceContext Ctx(
1926 Importer.getFromContext(), Importer.getToContext(),
1927 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1928 false, Complain);
1929 return Ctx.IsEquivalent(From, To);
1930}
1931
1932bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001933 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001934 // Eliminate a potential failure point where we attempt to re-import
1935 // something we're trying to import while completing ToRecord.
1936 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1937 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001938 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001939 if (ToOriginRecord)
1940 ToRecord = ToOriginRecord;
1941 }
1942
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001943 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001944 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001945 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001946 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001947 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001948 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001949}
1950
Larisse Voufo39a1e502013-08-06 01:03:05 +00001951bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1952 bool Complain) {
1953 StructuralEquivalenceContext Ctx(
1954 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001955 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1956 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001957 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001958}
1959
Douglas Gregor98c10182010-02-12 22:17:39 +00001960bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001961 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00001962 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001963 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
1964 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
1965 ToEnum = ToOriginEnum;
1966
Gabor Marton26f72a92018-07-12 09:42:05 +00001967 StructuralEquivalenceContext Ctx(
1968 Importer.getFromContext(), Importer.getToContext(),
1969 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001970 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001971}
1972
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001973bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1974 FunctionTemplateDecl *To) {
1975 StructuralEquivalenceContext Ctx(
1976 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001977 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1978 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001979 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001980}
1981
Balazs Keric7797c42018-07-11 09:37:24 +00001982bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1983 StructuralEquivalenceContext Ctx(
1984 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001985 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1986 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001987 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001988}
1989
Douglas Gregor91155082012-11-14 22:29:20 +00001990bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001991 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001992 const llvm::APSInt &FromVal = FromEC->getInitVal();
1993 const llvm::APSInt &ToVal = ToEC->getInitVal();
1994
1995 return FromVal.isSigned() == ToVal.isSigned() &&
1996 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1997 FromVal == ToVal;
1998}
1999
2000bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002001 ClassTemplateDecl *To) {
2002 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2003 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002004 Importer.getNonEquivalentDecls(),
2005 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002006 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002007}
2008
Larisse Voufo39a1e502013-08-06 01:03:05 +00002009bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2010 VarTemplateDecl *To) {
2011 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2012 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002013 Importer.getNonEquivalentDecls(),
2014 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002015 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002016}
2017
Balazs Keri3b30d652018-10-19 13:32:20 +00002018ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002019 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002020 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002021 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002022}
2023
Balazs Keri3b30d652018-10-19 13:32:20 +00002024ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2025 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2026 << D->getDeclKindName();
2027 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2028}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002029
Balazs Keri3b30d652018-10-19 13:32:20 +00002030ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2031 // Import the context of this declaration.
2032 DeclContext *DC, *LexicalDC;
2033 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2034 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002035
2036 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002037 ExpectedSLoc LocOrErr = import(D->getLocation());
2038 if (!LocOrErr)
2039 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002040
Gabor Marton26f72a92018-07-12 09:42:05 +00002041 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002042 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002043 return ToD;
2044
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002045 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002046 LexicalDC->addDeclInternal(ToD);
2047 return ToD;
2048}
2049
Balazs Keri3b30d652018-10-19 13:32:20 +00002050ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002051 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002052 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002053
Gabor Marton26f72a92018-07-12 09:42:05 +00002054 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002055
Sean Callanan65198272011-11-17 23:20:56 +00002056 return ToD;
2057}
2058
Balazs Keri3b30d652018-10-19 13:32:20 +00002059ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2060 ExpectedSLoc LocOrErr = import(D->getLocation());
2061 if (!LocOrErr)
2062 return LocOrErr.takeError();
2063 auto ColonLocOrErr = import(D->getColonLoc());
2064 if (!ColonLocOrErr)
2065 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002066
2067 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002068 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2069 if (!DCOrErr)
2070 return DCOrErr.takeError();
2071 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002072
Gabor Marton26f72a92018-07-12 09:42:05 +00002073 AccessSpecDecl *ToD;
2074 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002075 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002076 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002077
2078 // Lexical DeclContext and Semantic DeclContext
2079 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002080 ToD->setLexicalDeclContext(DC);
2081 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002082
Gabor Marton26f72a92018-07-12 09:42:05 +00002083 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002084}
2085
Balazs Keri3b30d652018-10-19 13:32:20 +00002086ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2087 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2088 if (!DCOrErr)
2089 return DCOrErr.takeError();
2090 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002091 DeclContext *LexicalDC = DC;
2092
Balazs Keri3b30d652018-10-19 13:32:20 +00002093 SourceLocation ToLocation, ToRParenLoc;
2094 Expr *ToAssertExpr;
2095 StringLiteral *ToMessage;
2096 if (auto Imp = importSeq(
2097 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2098 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2099 else
2100 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002101
Gabor Marton26f72a92018-07-12 09:42:05 +00002102 StaticAssertDecl *ToD;
2103 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002104 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2105 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002106 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002107
2108 ToD->setLexicalDeclContext(LexicalDC);
2109 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002110 return ToD;
2111}
2112
Balazs Keri3b30d652018-10-19 13:32:20 +00002113ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002114 // Import the major distinguishing characteristics of this namespace.
2115 DeclContext *DC, *LexicalDC;
2116 DeclarationName Name;
2117 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002118 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002119 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2120 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002121 if (ToD)
2122 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002123
2124 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002125 if (!Name) {
2126 // This is an anonymous namespace. Adopt an existing anonymous
2127 // namespace if we can.
2128 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002129 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002130 MergeWithNamespace = TU->getAnonymousNamespace();
2131 else
2132 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2133 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002134 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002135 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002136 for (auto *FoundDecl : FoundDecls) {
2137 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002138 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002139
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002140 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002141 MergeWithNamespace = FoundNS;
2142 ConflictingDecls.clear();
2143 break;
2144 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002145
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002146 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002147 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002148
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002149 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002150 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002151 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002152 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002153 if (!Name)
2154 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002155 }
2156 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002157
Balazs Keri3b30d652018-10-19 13:32:20 +00002158 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2159 if (!BeginLocOrErr)
2160 return BeginLocOrErr.takeError();
2161
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002162 // Create the "to" namespace, if needed.
2163 NamespaceDecl *ToNamespace = MergeWithNamespace;
2164 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002165 if (GetImportedOrCreateDecl(
2166 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002167 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002168 /*PrevDecl=*/nullptr))
2169 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002170 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002171 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002172
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002173 // If this is an anonymous namespace, register it as the anonymous
2174 // namespace within its context.
2175 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002176 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002177 TU->setAnonymousNamespace(ToNamespace);
2178 else
2179 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2180 }
2181 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002182 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002183
Balazs Keri3b30d652018-10-19 13:32:20 +00002184 if (Error Err = ImportDeclContext(D))
2185 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002186
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002187 return ToNamespace;
2188}
2189
Balazs Keri3b30d652018-10-19 13:32:20 +00002190ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002191 // Import the major distinguishing characteristics of this namespace.
2192 DeclContext *DC, *LexicalDC;
2193 DeclarationName Name;
2194 SourceLocation Loc;
2195 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002196 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2197 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002198 if (LookupD)
2199 return LookupD;
2200
2201 // NOTE: No conflict resolution is done for namespace aliases now.
2202
Balazs Keri3b30d652018-10-19 13:32:20 +00002203 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2204 NestedNameSpecifierLoc ToQualifierLoc;
2205 NamespaceDecl *ToNamespace;
2206 if (auto Imp = importSeq(
2207 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2208 D->getTargetNameLoc(), D->getNamespace()))
2209 std::tie(
2210 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2211 ToNamespace) = *Imp;
2212 else
2213 return Imp.takeError();
2214 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002215
Gabor Marton26f72a92018-07-12 09:42:05 +00002216 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002217 if (GetImportedOrCreateDecl(
2218 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2219 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002220 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002221
2222 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002223 LexicalDC->addDeclInternal(ToD);
2224
2225 return ToD;
2226}
2227
Balazs Keri3b30d652018-10-19 13:32:20 +00002228ExpectedDecl
2229ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002230 // Import the major distinguishing characteristics of this typedef.
2231 DeclContext *DC, *LexicalDC;
2232 DeclarationName Name;
2233 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002234 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002235 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2236 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002237 if (ToD)
2238 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002239
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002240 // If this typedef is not in block scope, determine whether we've
2241 // seen a typedef with the same name (that we can merge with) or any
2242 // other entity by that name (which name lookup could conflict with).
2243 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002244 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002245 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002246 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002247 for (auto *FoundDecl : FoundDecls) {
2248 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002249 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002250 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002251 QualType FromUT = D->getUnderlyingType();
2252 QualType FoundUT = FoundTypedef->getUnderlyingType();
2253 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2254 // If the "From" context has a complete underlying type but we
2255 // already have a complete underlying type then return with that.
2256 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002257 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002258 }
2259 // FIXME Handle redecl chain.
2260 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002261 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002262
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002263 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002264 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002265
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002266 if (!ConflictingDecls.empty()) {
2267 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002268 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002269 ConflictingDecls.size());
2270 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002271 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002272 }
2273 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002274
Balazs Keri3b30d652018-10-19 13:32:20 +00002275 QualType ToUnderlyingType;
2276 TypeSourceInfo *ToTypeSourceInfo;
2277 SourceLocation ToBeginLoc;
2278 if (auto Imp = importSeq(
2279 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2280 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2281 else
2282 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002283
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002284 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002285 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002286 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002287 if (IsAlias) {
2288 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002289 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2290 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002291 return ToTypedef;
2292 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002293 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2294 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002295 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002296
Douglas Gregordd483172010-02-22 17:42:47 +00002297 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002298 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002299
2300 // Templated declarations should not appear in DeclContext.
2301 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2302 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2303 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002304
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002305 return ToTypedef;
2306}
2307
Balazs Keri3b30d652018-10-19 13:32:20 +00002308ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002309 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2310}
2311
Balazs Keri3b30d652018-10-19 13:32:20 +00002312ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002313 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2314}
2315
Balazs Keri3b30d652018-10-19 13:32:20 +00002316ExpectedDecl
2317ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002318 // Import the major distinguishing characteristics of this typedef.
2319 DeclContext *DC, *LexicalDC;
2320 DeclarationName Name;
2321 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002322 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002323 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2324 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002325 if (FoundD)
2326 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002327
2328 // If this typedef is not in block scope, determine whether we've
2329 // seen a typedef with the same name (that we can merge with) or any
2330 // other entity by that name (which name lookup could conflict with).
2331 if (!DC->isFunctionOrMethod()) {
2332 SmallVector<NamedDecl *, 4> ConflictingDecls;
2333 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002334 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002335 for (auto *FoundDecl : FoundDecls) {
2336 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002337 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002338 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002339 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002340 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002341 }
2342
2343 if (!ConflictingDecls.empty()) {
2344 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2345 ConflictingDecls.data(),
2346 ConflictingDecls.size());
2347 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002348 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002349 }
2350 }
2351
Balazs Keri3b30d652018-10-19 13:32:20 +00002352 TemplateParameterList *ToTemplateParameters;
2353 TypeAliasDecl *ToTemplatedDecl;
2354 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2355 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2356 else
2357 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002358
Gabor Marton26f72a92018-07-12 09:42:05 +00002359 TypeAliasTemplateDecl *ToAlias;
2360 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002361 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002362 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002363
Balazs Keri3b30d652018-10-19 13:32:20 +00002364 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002365
Gabor Horvath7a91c082017-11-14 11:30:38 +00002366 ToAlias->setAccess(D->getAccess());
2367 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002368 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002369 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002370}
2371
Balazs Keri3b30d652018-10-19 13:32:20 +00002372ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002373 // Import the major distinguishing characteristics of this label.
2374 DeclContext *DC, *LexicalDC;
2375 DeclarationName Name;
2376 SourceLocation Loc;
2377 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002378 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2379 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002380 if (ToD)
2381 return ToD;
2382
2383 assert(LexicalDC->isFunctionOrMethod());
2384
Gabor Marton26f72a92018-07-12 09:42:05 +00002385 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002386 if (D->isGnuLocal()) {
2387 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2388 if (!BeginLocOrErr)
2389 return BeginLocOrErr.takeError();
2390 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2391 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2392 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002393
Balazs Keri3b30d652018-10-19 13:32:20 +00002394 } else {
2395 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2396 Name.getAsIdentifierInfo()))
2397 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002398
Balazs Keri3b30d652018-10-19 13:32:20 +00002399 }
2400
2401 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2402 if (!ToStmtOrErr)
2403 return ToStmtOrErr.takeError();
2404
2405 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002406 ToLabel->setLexicalDeclContext(LexicalDC);
2407 LexicalDC->addDeclInternal(ToLabel);
2408 return ToLabel;
2409}
2410
Balazs Keri3b30d652018-10-19 13:32:20 +00002411ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002412 // Import the major distinguishing characteristics of this enum.
2413 DeclContext *DC, *LexicalDC;
2414 DeclarationName Name;
2415 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002416 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002417 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2418 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002419 if (ToD)
2420 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002421
Douglas Gregor98c10182010-02-12 22:17:39 +00002422 // Figure out what enum name we're looking for.
2423 unsigned IDNS = Decl::IDNS_Tag;
2424 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002425 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002426 if (Error Err = importInto(
2427 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2428 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002429 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002430 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002431 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002432
Douglas Gregor98c10182010-02-12 22:17:39 +00002433 // We may already have an enum of the same name; try to find and match it.
2434 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002435 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002436 auto FoundDecls =
2437 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002438 for (auto *FoundDecl : FoundDecls) {
2439 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002440 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002441
Balazs Keri3b30d652018-10-19 13:32:20 +00002442 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002443 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002444 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002445 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002446
Balazs Keri3b30d652018-10-19 13:32:20 +00002447 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002448 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002449 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002450 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002451
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002452 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002453 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002454
Douglas Gregor98c10182010-02-12 22:17:39 +00002455 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002456 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002457 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002458 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002459 if (!Name)
2460 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002461 }
2462 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002463
Balazs Keri3b30d652018-10-19 13:32:20 +00002464 SourceLocation ToBeginLoc;
2465 NestedNameSpecifierLoc ToQualifierLoc;
2466 QualType ToIntegerType;
2467 if (auto Imp = importSeq(
2468 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2469 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2470 else
2471 return Imp.takeError();
2472
Douglas Gregor98c10182010-02-12 22:17:39 +00002473 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002474 EnumDecl *D2;
2475 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002476 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002477 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2478 D->isScopedUsingClassTag(), D->isFixed()))
2479 return D2;
2480
Balazs Keri3b30d652018-10-19 13:32:20 +00002481 D2->setQualifierInfo(ToQualifierLoc);
2482 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002483 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002484 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002485 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002486
Douglas Gregor98c10182010-02-12 22:17:39 +00002487 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002488 if (D->isCompleteDefinition())
2489 if (Error Err = ImportDefinition(D, D2))
2490 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002491
Douglas Gregor3996e242010-02-15 22:01:00 +00002492 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002493}
2494
Balazs Keri3b30d652018-10-19 13:32:20 +00002495ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002496 bool IsFriendTemplate = false;
2497 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2498 IsFriendTemplate =
2499 DCXX->getDescribedClassTemplate() &&
2500 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2501 Decl::FOK_None;
2502 }
2503
Douglas Gregor5c73e912010-02-11 00:48:18 +00002504 // Import the major distinguishing characteristics of this record.
2505 DeclContext *DC, *LexicalDC;
2506 DeclarationName Name;
2507 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002508 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002509 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2510 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002511 if (ToD)
2512 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002513
Douglas Gregor5c73e912010-02-11 00:48:18 +00002514 // Figure out what structure name we're looking for.
2515 unsigned IDNS = Decl::IDNS_Tag;
2516 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002517 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002518 if (Error Err = importInto(
2519 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2520 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002521 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002522 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002523 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002524
2525 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002526 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002527 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002528 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002529 auto FoundDecls =
2530 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002531 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002532 // We're going to have to compare D against potentially conflicting Decls,
2533 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002534 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2535 D->getASTContext().getExternalSource()->CompleteType(D);
2536 }
2537
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002538 for (auto *FoundDecl : FoundDecls) {
2539 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002540 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002541
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002542 Decl *Found = FoundDecl;
2543 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2544 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002545 Found = Tag->getDecl();
2546 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002547
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002548 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002549 // Do not emit false positive diagnostic in case of unnamed
2550 // struct/union and in case of anonymous structs. Would be false
2551 // because there may be several anonymous/unnamed structs in a class.
2552 // E.g. these are both valid:
2553 // struct A { // unnamed structs
2554 // struct { struct A *next; } entry0;
2555 // struct { struct A *next; } entry1;
2556 // };
2557 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2558 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002559 if (!IsStructuralMatch(D, FoundRecord, false))
2560 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002561
Gabor Marton7df342a2018-12-17 12:42:12 +00002562 if (IsStructuralMatch(D, FoundRecord)) {
2563 RecordDecl *FoundDef = FoundRecord->getDefinition();
2564 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002565 // FIXME: Structural equivalence check should check for same
2566 // user-defined methods.
2567 Importer.MapImported(D, FoundDef);
2568 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2569 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2570 assert(FoundCXX && "Record type mismatch");
2571
Gabor Marton7df342a2018-12-17 12:42:12 +00002572 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002573 // FoundDef may not have every implicit method that D has
2574 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002575 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2576 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002577 }
Douglas Gregor25791052010-02-12 00:09:27 +00002578 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002579 PrevDecl = FoundRecord->getMostRecentDecl();
2580 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002581 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002582 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002583
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002584 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002585 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002586
Douglas Gregordd6006f2012-07-17 21:16:27 +00002587 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002588 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002589 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002590 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002591 if (!Name)
2592 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002593 }
2594 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002595
Balazs Keri3b30d652018-10-19 13:32:20 +00002596 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2597 if (!BeginLocOrErr)
2598 return BeginLocOrErr.takeError();
2599
Douglas Gregor5c73e912010-02-11 00:48:18 +00002600 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002601 RecordDecl *D2 = nullptr;
2602 CXXRecordDecl *D2CXX = nullptr;
2603 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2604 if (DCXX->isLambda()) {
2605 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2606 if (!TInfoOrErr)
2607 return TInfoOrErr.takeError();
2608 if (GetImportedOrCreateSpecialDecl(
2609 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2610 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2611 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2612 return D2CXX;
2613 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2614 if (!CDeclOrErr)
2615 return CDeclOrErr.takeError();
2616 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2617 } else if (DCXX->isInjectedClassName()) {
2618 // We have to be careful to do a similar dance to the one in
2619 // Sema::ActOnStartCXXMemberDeclarations
2620 const bool DelayTypeCreation = true;
2621 if (GetImportedOrCreateDecl(
2622 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2623 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2624 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2625 return D2CXX;
2626 Importer.getToContext().getTypeDeclType(
2627 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2628 } else {
2629 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2630 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2631 Name.getAsIdentifierInfo(),
2632 cast_or_null<CXXRecordDecl>(PrevDecl)))
2633 return D2CXX;
2634 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002635
Gabor Marton7df342a2018-12-17 12:42:12 +00002636 D2 = D2CXX;
2637 D2->setAccess(D->getAccess());
2638 D2->setLexicalDeclContext(LexicalDC);
2639 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2640 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002641
Gabor Marton7df342a2018-12-17 12:42:12 +00002642 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2643 DC->makeDeclVisibleInContext(D2);
2644
2645 if (ClassTemplateDecl *FromDescribed =
2646 DCXX->getDescribedClassTemplate()) {
2647 ClassTemplateDecl *ToDescribed;
2648 if (Error Err = importInto(ToDescribed, FromDescribed))
2649 return std::move(Err);
2650 D2CXX->setDescribedClassTemplate(ToDescribed);
2651 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2652 // In a record describing a template the type should be an
2653 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2654 // previously set type to the correct value here (ToDescribed is not
2655 // available at record create).
2656 // FIXME: The previous type is cleared but not removed from
2657 // ASTContext's internal storage.
2658 CXXRecordDecl *Injected = nullptr;
2659 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2660 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2661 if (Record && Record->isInjectedClassName()) {
2662 Injected = Record;
2663 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002664 }
2665 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002666 // Create an injected type for the whole redecl chain.
2667 SmallVector<Decl *, 2> Redecls =
2668 getCanonicalForwardRedeclChain(D2CXX);
2669 for (auto *R : Redecls) {
2670 auto *RI = cast<CXXRecordDecl>(R);
2671 RI->setTypeForDecl(nullptr);
2672 // Below we create a new injected type and assign that to the
2673 // canonical decl, subsequent declarations in the chain will reuse
2674 // that type.
2675 Importer.getToContext().getInjectedClassNameType(
2676 RI, ToDescribed->getInjectedClassNameSpecialization());
2677 }
2678 // Set the new type for the previous injected decl too.
2679 if (Injected) {
2680 Injected->setTypeForDecl(nullptr);
2681 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2682 }
2683 }
2684 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002685 DCXX->getMemberSpecializationInfo()) {
2686 TemplateSpecializationKind SK =
2687 MemberInfo->getTemplateSpecializationKind();
2688 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002689
2690 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2691 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2692 else
2693 return ToInstOrErr.takeError();
2694
2695 if (ExpectedSLoc POIOrErr =
2696 import(MemberInfo->getPointOfInstantiation()))
2697 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2698 *POIOrErr);
2699 else
2700 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002701 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002702
Gabor Marton7df342a2018-12-17 12:42:12 +00002703 } else {
2704 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2705 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2706 Name.getAsIdentifierInfo(), PrevDecl))
2707 return D2;
2708 D2->setLexicalDeclContext(LexicalDC);
2709 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002710 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002711
Gabor Marton7df342a2018-12-17 12:42:12 +00002712 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2713 D2->setQualifierInfo(*QualifierLocOrErr);
2714 else
2715 return QualifierLocOrErr.takeError();
2716
2717 if (D->isAnonymousStructOrUnion())
2718 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002719
Balazs Keri3b30d652018-10-19 13:32:20 +00002720 if (D->isCompleteDefinition())
2721 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2722 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002723
Douglas Gregor3996e242010-02-15 22:01:00 +00002724 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002725}
2726
Balazs Keri3b30d652018-10-19 13:32:20 +00002727ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002728 // Import the major distinguishing characteristics of this enumerator.
2729 DeclContext *DC, *LexicalDC;
2730 DeclarationName Name;
2731 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002732 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002733 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2734 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002735 if (ToD)
2736 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002737
Fangrui Song6907ce22018-07-30 19:24:48 +00002738 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002739 // in the same context.
2740 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002741 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002742 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002743 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002744 for (auto *FoundDecl : FoundDecls) {
2745 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002746 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002747
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002748 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002749 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002750 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002751 }
2752
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002753 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002754 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002755
Douglas Gregor98c10182010-02-12 22:17:39 +00002756 if (!ConflictingDecls.empty()) {
2757 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002758 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002759 ConflictingDecls.size());
2760 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002761 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002762 }
2763 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002764
Balazs Keri3b30d652018-10-19 13:32:20 +00002765 ExpectedType TypeOrErr = import(D->getType());
2766 if (!TypeOrErr)
2767 return TypeOrErr.takeError();
2768
2769 ExpectedExpr InitOrErr = import(D->getInitExpr());
2770 if (!InitOrErr)
2771 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002772
Gabor Marton26f72a92018-07-12 09:42:05 +00002773 EnumConstantDecl *ToEnumerator;
2774 if (GetImportedOrCreateDecl(
2775 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002776 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002777 return ToEnumerator;
2778
Douglas Gregordd483172010-02-22 17:42:47 +00002779 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002780 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002781 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002782 return ToEnumerator;
2783}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002784
Balazs Keri1efc9742019-05-07 10:55:11 +00002785Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2786 DeclaratorDecl *ToD) {
2787 unsigned int Num = FromD->getNumTemplateParameterLists();
2788 if (Num == 0)
2789 return Error::success();
2790 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2791 for (unsigned int I = 0; I < Num; ++I)
2792 if (Expected<TemplateParameterList *> ToTPListOrErr =
2793 import(FromD->getTemplateParameterList(I)))
2794 ToTPLists[I] = *ToTPListOrErr;
2795 else
2796 return ToTPListOrErr.takeError();
2797 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2798 return Error::success();
2799}
2800
Balazs Keri3b30d652018-10-19 13:32:20 +00002801Error ASTNodeImporter::ImportTemplateInformation(
2802 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002803 switch (FromFD->getTemplatedKind()) {
2804 case FunctionDecl::TK_NonTemplate:
2805 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002806 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002807
2808 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002809 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002810
2811 if (Expected<FunctionDecl *> InstFDOrErr =
2812 import(FromFD->getInstantiatedFromMemberFunction()))
2813 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2814 else
2815 return InstFDOrErr.takeError();
2816
2817 if (ExpectedSLoc POIOrErr = import(
2818 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2819 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2820 else
2821 return POIOrErr.takeError();
2822
2823 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002824 }
2825
2826 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002827 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002828 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002829 if (!FunctionAndArgsOrErr)
2830 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002831
2832 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002833 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002834
Gabor Marton5254e642018-06-27 13:32:50 +00002835 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002836 TemplateArgumentListInfo ToTAInfo;
2837 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002838 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002839 if (Error Err = ImportTemplateArgumentListInfo(
2840 *FromTAArgsAsWritten, ToTAInfo))
2841 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002842
Balazs Keri3b30d652018-10-19 13:32:20 +00002843 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2844 if (!POIOrErr)
2845 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002846
Balazs Keri1efc9742019-05-07 10:55:11 +00002847 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
2848 return Err;
2849
Gabor Marton5254e642018-06-27 13:32:50 +00002850 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002851 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002852 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2853 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2854 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002855 }
2856
2857 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2858 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2859 UnresolvedSet<8> TemplDecls;
2860 unsigned NumTemplates = FromInfo->getNumTemplates();
2861 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002862 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2863 import(FromInfo->getTemplate(I)))
2864 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002865 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002866 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002867 }
2868
2869 // Import TemplateArgumentListInfo.
2870 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002871 if (Error Err = ImportTemplateArgumentListInfo(
2872 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2873 llvm::makeArrayRef(
2874 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2875 ToTAInfo))
2876 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002877
2878 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2879 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002880 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002881 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002882 }
Sam McCallfdc32072018-01-26 12:06:44 +00002883 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002884}
2885
Balazs Keri3b30d652018-10-19 13:32:20 +00002886Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002887ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002888 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002889 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002890 if (!FunctionAndArgsOrErr)
2891 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002892
Balazs Keri3b30d652018-10-19 13:32:20 +00002893 FunctionTemplateDecl *Template;
2894 TemplateArgsTy ToTemplArgs;
2895 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002896 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002897 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002898 return FoundSpec;
2899}
2900
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002901Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2902 FunctionDecl *ToFD) {
2903 if (Stmt *FromBody = FromFD->getBody()) {
2904 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2905 ToFD->setBody(*ToBodyOrErr);
2906 else
2907 return ToBodyOrErr.takeError();
2908 }
2909 return Error::success();
2910}
2911
Gabor Marton458d1452019-02-14 13:07:03 +00002912template <typename T>
2913bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2914 if (From->hasExternalFormalLinkage())
2915 return Found->hasExternalFormalLinkage();
2916 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2917 return false;
2918 if (From->isInAnonymousNamespace())
2919 return Found->isInAnonymousNamespace();
2920 else
2921 return !Found->isInAnonymousNamespace() &&
2922 !Found->hasExternalFormalLinkage();
2923}
2924
Balazs Keri3b30d652018-10-19 13:32:20 +00002925ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002926
Balazs Keri3b30d652018-10-19 13:32:20 +00002927 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002928 auto RedeclIt = Redecls.begin();
2929 // Import the first part of the decl chain. I.e. import all previous
2930 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002931 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2932 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2933 if (!ToRedeclOrErr)
2934 return ToRedeclOrErr.takeError();
2935 }
Gabor Marton5254e642018-06-27 13:32:50 +00002936 assert(*RedeclIt == D);
2937
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002938 // Import the major distinguishing characteristics of this function.
2939 DeclContext *DC, *LexicalDC;
2940 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002941 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002942 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002943 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2944 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002945 if (ToD)
2946 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002947
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002948 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002949 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002950
Gabor Marton5254e642018-06-27 13:32:50 +00002951 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002952 // existing specialization in the "to" context. The lookup below will not
2953 // find any specialization, but would find the primary template; thus, we
2954 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00002955 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2956 if (D->getTemplatedKind() ==
2957 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002958 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
2959 if (!FoundFunctionOrErr)
2960 return FoundFunctionOrErr.takeError();
2961 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00002962 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2963 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00002964 FoundByLookup = FoundFunction;
2965 }
2966 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002967 // Try to find a function in our own ("to") context with the same name, same
2968 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002969 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002970 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002971 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00002972 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002973 for (auto *FoundDecl : FoundDecls) {
2974 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002975 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002976
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002977 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00002978 if (!hasSameVisibilityContext(FoundFunction, D))
2979 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002980
Gabor Marton458d1452019-02-14 13:07:03 +00002981 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00002982 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2983 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00002984 FoundByLookup = FoundFunction;
2985 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002986 }
Gabor Marton458d1452019-02-14 13:07:03 +00002987 // FIXME: Check for overloading more carefully, e.g., by boosting
2988 // Sema::IsOverload out to the AST library.
2989
2990 // Function overloading is okay in C++.
2991 if (Importer.getToContext().getLangOpts().CPlusPlus)
2992 continue;
2993
2994 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00002995 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00002996 << Name << D->getType() << FoundFunction->getType();
2997 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
2998 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002999 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003000
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003001 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003002 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003003
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003004 if (!ConflictingDecls.empty()) {
3005 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003006 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003007 ConflictingDecls.size());
3008 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003009 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003010 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003011 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003012
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003013 // We do not allow more than one in-class declaration of a function. This is
3014 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3015 // assumes there is only one in-class declaration. Building a redecl
3016 // chain would result in more than one in-class declaration for
3017 // overrides (even if they are part of the same redecl chain inside the
3018 // derived class.)
3019 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003020 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003021 if (D->getLexicalDeclContext() == D->getDeclContext()) {
3022 if (!D->doesThisDeclarationHaveABody())
3023 return Importer.MapImported(D, FoundByLookup);
3024 else {
3025 // Let's continue and build up the redecl chain in this case.
3026 // FIXME Merge the functions into one decl.
3027 }
3028 }
3029 }
3030 }
3031
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003032 DeclarationNameInfo NameInfo(Name, Loc);
3033 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003034 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3035 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003036
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003037 QualType FromTy = D->getType();
3038 bool usedDifferentExceptionSpec = false;
3039
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003040 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003041 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3042 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3043 // FunctionDecl that we are importing the FunctionProtoType for.
3044 // To avoid an infinite recursion when importing, create the FunctionDecl
3045 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003046 if (FromEPI.ExceptionSpec.SourceDecl ||
3047 FromEPI.ExceptionSpec.SourceTemplate ||
3048 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003049 FunctionProtoType::ExtProtoInfo DefaultEPI;
3050 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003051 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003052 usedDifferentExceptionSpec = true;
3053 }
3054 }
3055
Balazs Keri3b30d652018-10-19 13:32:20 +00003056 QualType T;
3057 TypeSourceInfo *TInfo;
3058 SourceLocation ToInnerLocStart, ToEndLoc;
3059 NestedNameSpecifierLoc ToQualifierLoc;
3060 if (auto Imp = importSeq(
3061 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3062 D->getQualifierLoc(), D->getEndLoc()))
3063 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3064 else
3065 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003066
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003067 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003068 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003069 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003070 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3071 Parameters.push_back(*ToPOrErr);
3072 else
3073 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003074 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003075
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003076 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003077 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003078 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003079 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Hans Wennborgd2b9fc82019-05-06 09:51:10 +00003080 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3081 ToInnerLocStart, NameInfo, T, TInfo,
3082 FromConstructor->isExplicit(),
3083 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003084 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003085 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3086
3087 auto Imp =
3088 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3089 FromDtor->getOperatorDeleteThisArg());
3090
3091 if (!Imp)
3092 return Imp.takeError();
3093
3094 FunctionDecl *ToOperatorDelete;
3095 Expr *ToThisArg;
3096 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3097
Gabor Marton26f72a92018-07-12 09:42:05 +00003098 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003099 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3100 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3101 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003102 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003103
3104 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3105
3106 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003107 } else if (CXXConversionDecl *FromConversion =
3108 dyn_cast<CXXConversionDecl>(D)) {
3109 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3110 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003111 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Hans Wennborgd2b9fc82019-05-06 09:51:10 +00003112 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003113 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003114 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003115 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3116 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003117 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003118 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3119 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003120 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003121 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003122 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003123 D->getStorageClass(), D->isInlineSpecified(),
3124 D->hasWrittenPrototype(), D->isConstexpr()))
3125 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003126 }
John McCall3e11ebe2010-03-15 10:12:16 +00003127
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003128 // Connect the redecl chain.
3129 if (FoundByLookup) {
3130 auto *Recent = const_cast<FunctionDecl *>(
3131 FoundByLookup->getMostRecentDecl());
3132 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003133 // FIXME Probably we should merge exception specifications. E.g. In the
3134 // "To" context the existing function may have exception specification with
3135 // noexcept-unevaluated, while the newly imported function may have an
3136 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3137 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003138 }
3139
3140 // Import Ctor initializers.
3141 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3142 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3143 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3144 // Import first, then allocate memory and copy if there was no error.
3145 if (Error Err = ImportContainerChecked(
3146 FromConstructor->inits(), CtorInitializers))
3147 return std::move(Err);
3148 auto **Memory =
3149 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3150 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3151 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3152 ToCtor->setCtorInitializers(Memory);
3153 ToCtor->setNumCtorInitializers(NumInitializers);
3154 }
3155 }
3156
Balazs Keri3b30d652018-10-19 13:32:20 +00003157 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003158 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003159 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003160 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3161 ToFunction->setTrivial(D->isTrivial());
3162 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003163 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003164
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003165 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003166 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003167 Param->setOwningFunction(ToFunction);
3168 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003169 }
David Blaikie9c70e042011-09-21 18:16:56 +00003170 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003171
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003172 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3173 // params it refers to.
3174 if (TInfo) {
3175 if (auto ProtoLoc =
3176 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3177 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3178 ProtoLoc.setParam(I, Parameters[I]);
3179 }
3180 }
3181
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003182 if (usedDifferentExceptionSpec) {
3183 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003184 if (ExpectedType TyOrErr = import(D->getType()))
3185 ToFunction->setType(*TyOrErr);
3186 else
3187 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003188 }
3189
Balazs Keria35798d2018-07-17 09:52:41 +00003190 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003191 if (FromFT) {
3192 auto ToFTOrErr = import(FromFT);
3193 if (!ToFTOrErr)
3194 return ToFTOrErr.takeError();
3195 }
Balazs Keria35798d2018-07-17 09:52:41 +00003196
Gabor Marton5254e642018-06-27 13:32:50 +00003197 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003198 Error Err = ImportFunctionDeclBody(D, ToFunction);
3199
3200 if (Err)
3201 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003202 }
3203
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003204 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003205
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003206 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003207 if (Error Err = ImportTemplateInformation(D, ToFunction))
3208 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003209
Gabor Marton5254e642018-06-27 13:32:50 +00003210 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3211
3212 // TODO Can we generalize this approach to other AST nodes as well?
3213 if (D->getDeclContext()->containsDeclAndLoad(D))
3214 DC->addDeclInternal(ToFunction);
3215 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003216 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003217
Gabor Marton5254e642018-06-27 13:32:50 +00003218 // Friend declaration's lexical context is the befriending class, but the
3219 // semantic context is the enclosing scope of the befriending class.
3220 // We want the friend functions to be found in the semantic context by lookup.
3221 // FIXME should we handle this generically in VisitFriendDecl?
3222 // In Other cases when LexicalDC != DC we don't want it to be added,
3223 // e.g out-of-class definitions like void B::f() {} .
3224 if (LexicalDC != DC && IsFriend) {
3225 DC->makeDeclVisibleInContext(ToFunction);
3226 }
3227
Gabor Marton7a0841e2018-10-29 10:18:28 +00003228 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3229 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3230
Gabor Marton5254e642018-06-27 13:32:50 +00003231 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003232 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3233 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3234 if (!ToRedeclOrErr)
3235 return ToRedeclOrErr.takeError();
3236 }
Gabor Marton5254e642018-06-27 13:32:50 +00003237
Douglas Gregor43f54792010-02-17 02:12:47 +00003238 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003239}
3240
Balazs Keri3b30d652018-10-19 13:32:20 +00003241ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003242 return VisitFunctionDecl(D);
3243}
3244
Balazs Keri3b30d652018-10-19 13:32:20 +00003245ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003246 return VisitCXXMethodDecl(D);
3247}
3248
Balazs Keri3b30d652018-10-19 13:32:20 +00003249ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003250 return VisitCXXMethodDecl(D);
3251}
3252
Balazs Keri3b30d652018-10-19 13:32:20 +00003253ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003254 return VisitCXXMethodDecl(D);
3255}
3256
Balazs Keri3b30d652018-10-19 13:32:20 +00003257ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003258 // Import the major distinguishing characteristics of a variable.
3259 DeclContext *DC, *LexicalDC;
3260 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003261 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003262 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003263 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3264 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003265 if (ToD)
3266 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003267
Fangrui Song6907ce22018-07-30 19:24:48 +00003268 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003269 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003270 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003271 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003272 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003273 if (!Name &&
3274 ASTImporter::getFieldIndex(D) !=
3275 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003276 continue;
3277
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003278 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003279 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003280 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003281 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3282 // initializer of a FieldDecl might not had been instantiated in the
3283 // "To" context. However, the "From" context might instantiated that,
3284 // thus we have to merge that.
3285 if (Expr *FromInitializer = D->getInClassInitializer()) {
3286 // We don't have yet the initializer set.
3287 if (FoundField->hasInClassInitializer() &&
3288 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003289 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3290 FoundField->setInClassInitializer(*ToInitializerOrErr);
3291 else {
3292 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003293 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003294 // FIXME: warning message?
3295 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003296 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003297 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003298 }
3299 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003300 return FoundField;
3301 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003302
Balazs Keri3b30d652018-10-19 13:32:20 +00003303 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003304 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003305 << Name << D->getType() << FoundField->getType();
3306 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3307 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003308
3309 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003310 }
3311 }
3312
Balazs Keri3b30d652018-10-19 13:32:20 +00003313 QualType ToType;
3314 TypeSourceInfo *ToTInfo;
3315 Expr *ToBitWidth;
3316 SourceLocation ToInnerLocStart;
3317 Expr *ToInitializer;
3318 if (auto Imp = importSeq(
3319 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3320 D->getInnerLocStart(), D->getInClassInitializer()))
3321 std::tie(
3322 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3323 else
3324 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003325
Gabor Marton26f72a92018-07-12 09:42:05 +00003326 FieldDecl *ToField;
3327 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003328 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3329 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3330 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003331 return ToField;
3332
Douglas Gregordd483172010-02-22 17:42:47 +00003333 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003334 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003335 if (ToInitializer)
3336 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003337 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003338 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003339 return ToField;
3340}
3341
Balazs Keri3b30d652018-10-19 13:32:20 +00003342ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003343 // Import the major distinguishing characteristics of a variable.
3344 DeclContext *DC, *LexicalDC;
3345 DeclarationName Name;
3346 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003347 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003348 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3349 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003350 if (ToD)
3351 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003352
Fangrui Song6907ce22018-07-30 19:24:48 +00003353 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003354 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003355 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003356 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003357 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003358 if (!Name &&
3359 ASTImporter::getFieldIndex(D) !=
3360 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003361 continue;
3362
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003363 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003364 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003365 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003366 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003367 return FoundField;
3368 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003369
3370 // If there are more anonymous fields to check, continue.
3371 if (!Name && I < N-1)
3372 continue;
3373
Balazs Keri3b30d652018-10-19 13:32:20 +00003374 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003375 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003376 << Name << D->getType() << FoundField->getType();
3377 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3378 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003379
3380 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003381 }
3382 }
3383
Francois Pichet783dd6e2010-11-21 06:08:52 +00003384 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003385 auto TypeOrErr = import(D->getType());
3386 if (!TypeOrErr)
3387 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003388
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003389 auto **NamedChain =
3390 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003391
3392 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003393 for (auto *PI : D->chain())
3394 if (Expected<NamedDecl *> ToD = import(PI))
3395 NamedChain[i++] = *ToD;
3396 else
3397 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003398
Gabor Marton26f72a92018-07-12 09:42:05 +00003399 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3400 IndirectFieldDecl *ToIndirectField;
3401 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003402 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003403 // FIXME here we leak `NamedChain` which is allocated before
3404 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003405
Francois Pichet783dd6e2010-11-21 06:08:52 +00003406 ToIndirectField->setAccess(D->getAccess());
3407 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003408 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003409 return ToIndirectField;
3410}
3411
Balazs Keri3b30d652018-10-19 13:32:20 +00003412ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003413 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003414 DeclContext *DC, *LexicalDC;
3415 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3416 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003417
3418 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003419 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003420 auto *RD = cast<CXXRecordDecl>(DC);
3421 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003422
3423 while (ImportedFriend) {
3424 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003425 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3426 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003427 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003428
3429 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3430 if (Importer.IsStructurallyEquivalent(
3431 D->getFriendType()->getType(),
3432 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003433 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003434 }
3435 ImportedFriend = ImportedFriend->getNextFriend();
3436 }
3437
3438 // Not found. Create it.
3439 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003440 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003441 NamedDecl *ToFriendD;
3442 if (Error Err = importInto(ToFriendD, FriendD))
3443 return std::move(Err);
3444
3445 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003446 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3447 ToFriendD->setObjectOfFriendDecl(false);
3448
3449 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003450 } else { // The friend is a type, not a decl.
3451 if (auto TSIOrErr = import(D->getFriendType()))
3452 ToFU = *TSIOrErr;
3453 else
3454 return TSIOrErr.takeError();
3455 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003456
3457 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003458 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003459 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003460 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003461 ToTPLists[I] = *ListOrErr;
3462 else
3463 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003464 }
3465
Balazs Keri3b30d652018-10-19 13:32:20 +00003466 auto LocationOrErr = import(D->getLocation());
3467 if (!LocationOrErr)
3468 return LocationOrErr.takeError();
3469 auto FriendLocOrErr = import(D->getFriendLoc());
3470 if (!FriendLocOrErr)
3471 return FriendLocOrErr.takeError();
3472
Gabor Marton26f72a92018-07-12 09:42:05 +00003473 FriendDecl *FrD;
3474 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003475 *LocationOrErr, ToFU,
3476 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003477 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003478
3479 FrD->setAccess(D->getAccess());
3480 FrD->setLexicalDeclContext(LexicalDC);
3481 LexicalDC->addDeclInternal(FrD);
3482 return FrD;
3483}
3484
Balazs Keri3b30d652018-10-19 13:32:20 +00003485ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003486 // Import the major distinguishing characteristics of an ivar.
3487 DeclContext *DC, *LexicalDC;
3488 DeclarationName Name;
3489 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003490 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003491 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3492 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003493 if (ToD)
3494 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003495
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003496 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003497 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003498 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003499 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003500 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003501 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003502 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003503 return FoundIvar;
3504 }
3505
Gabor Marton410f32c2019-04-01 15:29:55 +00003506 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003507 << Name << D->getType() << FoundIvar->getType();
3508 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3509 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003510
3511 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003512 }
3513 }
3514
Balazs Keri3b30d652018-10-19 13:32:20 +00003515 QualType ToType;
3516 TypeSourceInfo *ToTypeSourceInfo;
3517 Expr *ToBitWidth;
3518 SourceLocation ToInnerLocStart;
3519 if (auto Imp = importSeq(
3520 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3521 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3522 else
3523 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003524
Gabor Marton26f72a92018-07-12 09:42:05 +00003525 ObjCIvarDecl *ToIvar;
3526 if (GetImportedOrCreateDecl(
3527 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003528 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3529 ToType, ToTypeSourceInfo,
3530 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003531 return ToIvar;
3532
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003533 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003534 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003535 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003536}
3537
Balazs Keri3b30d652018-10-19 13:32:20 +00003538ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003539
3540 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3541 auto RedeclIt = Redecls.begin();
3542 // Import the first part of the decl chain. I.e. import all previous
3543 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003544 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3545 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3546 if (!RedeclOrErr)
3547 return RedeclOrErr.takeError();
3548 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003549 assert(*RedeclIt == D);
3550
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003551 // Import the major distinguishing characteristics of a variable.
3552 DeclContext *DC, *LexicalDC;
3553 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003554 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003555 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003556 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3557 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003558 if (ToD)
3559 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003560
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003561 // Try to find a variable in our own ("to") context with the same name and
3562 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003563 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003564 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003565 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003566 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003567 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003568 for (auto *FoundDecl : FoundDecls) {
3569 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003570 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003571
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003572 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003573 if (!hasSameVisibilityContext(FoundVar, D))
3574 continue;
3575 if (Importer.IsStructurallyEquivalent(D->getType(),
3576 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003577
Gabor Marton458d1452019-02-14 13:07:03 +00003578 // The VarDecl in the "From" context has a definition, but in the
3579 // "To" context we already have a definition.
3580 VarDecl *FoundDef = FoundVar->getDefinition();
3581 if (D->isThisDeclarationADefinition() && FoundDef)
3582 // FIXME Check for ODR error if the two definitions have
3583 // different initializers?
3584 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003585
Gabor Marton458d1452019-02-14 13:07:03 +00003586 // The VarDecl in the "From" context has an initializer, but in the
3587 // "To" context we already have an initializer.
3588 const VarDecl *FoundDInit = nullptr;
3589 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3590 // FIXME Diagnose ODR error if the two initializers are different?
3591 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3592
3593 FoundByLookup = FoundVar;
3594 break;
3595 }
3596
3597 const ArrayType *FoundArray
3598 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3599 const ArrayType *TArray
3600 = Importer.getToContext().getAsArrayType(D->getType());
3601 if (FoundArray && TArray) {
3602 if (isa<IncompleteArrayType>(FoundArray) &&
3603 isa<ConstantArrayType>(TArray)) {
3604 // Import the type.
3605 if (auto TyOrErr = import(D->getType()))
3606 FoundVar->setType(*TyOrErr);
3607 else
3608 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003609
3610 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003611 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003612 } else if (isa<IncompleteArrayType>(TArray) &&
3613 isa<ConstantArrayType>(FoundArray)) {
3614 FoundByLookup = FoundVar;
3615 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003616 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003617 }
Gabor Marton458d1452019-02-14 13:07:03 +00003618
Gabor Marton410f32c2019-04-01 15:29:55 +00003619 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003620 << Name << D->getType() << FoundVar->getType();
3621 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3622 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003623 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003624
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003625 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003626 }
3627
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003628 if (!ConflictingDecls.empty()) {
3629 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003630 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003631 ConflictingDecls.size());
3632 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003633 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003634 }
3635 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003636
Balazs Keri3b30d652018-10-19 13:32:20 +00003637 QualType ToType;
3638 TypeSourceInfo *ToTypeSourceInfo;
3639 SourceLocation ToInnerLocStart;
3640 NestedNameSpecifierLoc ToQualifierLoc;
3641 if (auto Imp = importSeq(
3642 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3643 D->getQualifierLoc()))
3644 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3645 else
3646 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003647
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003648 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003649 VarDecl *ToVar;
3650 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003651 ToInnerLocStart, Loc,
3652 Name.getAsIdentifierInfo(),
3653 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003654 D->getStorageClass()))
3655 return ToVar;
3656
Balazs Keri3b30d652018-10-19 13:32:20 +00003657 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003658 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003659 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003660
Gabor Martonac3a5d62018-09-17 12:04:52 +00003661 if (FoundByLookup) {
3662 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3663 ToVar->setPreviousDecl(Recent);
3664 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003665
Balazs Keri3b30d652018-10-19 13:32:20 +00003666 if (Error Err = ImportInitializer(D, ToVar))
3667 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003668
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003669 if (D->isConstexpr())
3670 ToVar->setConstexpr(true);
3671
Gabor Martonac3a5d62018-09-17 12:04:52 +00003672 if (D->getDeclContext()->containsDeclAndLoad(D))
3673 DC->addDeclInternal(ToVar);
3674 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3675 LexicalDC->addDeclInternal(ToVar);
3676
3677 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003678 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3679 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3680 if (!RedeclOrErr)
3681 return RedeclOrErr.takeError();
3682 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003683
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003684 return ToVar;
3685}
3686
Balazs Keri3b30d652018-10-19 13:32:20 +00003687ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003688 // Parameters are created in the translation unit's context, then moved
3689 // into the function declaration's context afterward.
3690 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003691
Balazs Keri3b30d652018-10-19 13:32:20 +00003692 DeclarationName ToDeclName;
3693 SourceLocation ToLocation;
3694 QualType ToType;
3695 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3696 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3697 else
3698 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003699
Douglas Gregor8b228d72010-02-17 21:22:52 +00003700 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003701 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003702 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3703 ToLocation, ToDeclName.getAsIdentifierInfo(),
3704 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003705 return ToParm;
3706 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003707}
3708
Balazs Keri3b30d652018-10-19 13:32:20 +00003709ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003710 // Parameters are created in the translation unit's context, then moved
3711 // into the function declaration's context afterward.
3712 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003713
Balazs Keri3b30d652018-10-19 13:32:20 +00003714 DeclarationName ToDeclName;
3715 SourceLocation ToLocation, ToInnerLocStart;
3716 QualType ToType;
3717 TypeSourceInfo *ToTypeSourceInfo;
3718 if (auto Imp = importSeq(
3719 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3720 D->getTypeSourceInfo()))
3721 std::tie(
3722 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3723 ToTypeSourceInfo) = *Imp;
3724 else
3725 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003726
Gabor Marton26f72a92018-07-12 09:42:05 +00003727 ParmVarDecl *ToParm;
3728 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003729 ToInnerLocStart, ToLocation,
3730 ToDeclName.getAsIdentifierInfo(), ToType,
3731 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003732 /*DefaultArg*/ nullptr))
3733 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003734
3735 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003736 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003737 ToParm->setKNRPromoted(D->isKNRPromoted());
3738
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003739 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003740 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3741 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3742 else
3743 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003744 } else if (D->hasUnparsedDefaultArg()) {
3745 ToParm->setUnparsedDefaultArg();
3746 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003747 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3748 ToParm->setDefaultArg(*ToDefArgOrErr);
3749 else
3750 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003751 }
Sean Callanan59721b32015-04-28 18:41:46 +00003752
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003753 if (D->isObjCMethodParameter()) {
3754 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3755 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3756 } else {
3757 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3758 D->getFunctionScopeIndex());
3759 }
3760
Gabor Marton26f72a92018-07-12 09:42:05 +00003761 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003762}
3763
Balazs Keri3b30d652018-10-19 13:32:20 +00003764ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003765 // Import the major distinguishing characteristics of a method.
3766 DeclContext *DC, *LexicalDC;
3767 DeclarationName Name;
3768 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003769 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003770 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3771 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003772 if (ToD)
3773 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003774
Gabor Marton54058b52018-12-17 13:53:12 +00003775 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003776 for (auto *FoundDecl : FoundDecls) {
3777 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003778 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3779 continue;
3780
3781 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003782 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3783 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003784 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003785 << D->isInstanceMethod() << Name << D->getReturnType()
3786 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003787 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003788 diag::note_odr_objc_method_here)
3789 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003790
3791 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003792 }
3793
3794 // Check the number of parameters.
3795 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003796 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003797 << D->isInstanceMethod() << Name
3798 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003799 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003800 diag::note_odr_objc_method_here)
3801 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003802
3803 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003804 }
3805
3806 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003807 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003808 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3809 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003810 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003811 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003812 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003813 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003814 << D->isInstanceMethod() << Name
3815 << (*P)->getType() << (*FoundP)->getType();
3816 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3817 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003818
3819 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003820 }
3821 }
3822
3823 // Check variadic/non-variadic.
3824 // Check the number of parameters.
3825 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003826 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003827 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003828 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003829 diag::note_odr_objc_method_here)
3830 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003831
3832 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003833 }
3834
3835 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003836 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003837 }
3838 }
3839
Balazs Keri3b30d652018-10-19 13:32:20 +00003840 SourceLocation ToEndLoc;
3841 QualType ToReturnType;
3842 TypeSourceInfo *ToReturnTypeSourceInfo;
3843 if (auto Imp = importSeq(
3844 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3845 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3846 else
3847 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003848
Gabor Marton26f72a92018-07-12 09:42:05 +00003849 ObjCMethodDecl *ToMethod;
3850 if (GetImportedOrCreateDecl(
3851 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003852 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3853 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003854 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3855 D->getImplementationControl(), D->hasRelatedResultType()))
3856 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003857
3858 // FIXME: When we decide to merge method definitions, we'll need to
3859 // deal with implicit parameters.
3860
3861 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003862 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003863 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003864 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3865 ToParams.push_back(*ToPOrErr);
3866 else
3867 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003868 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003869
Douglas Gregor43f54792010-02-17 02:12:47 +00003870 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003871 for (auto *ToParam : ToParams) {
3872 ToParam->setOwningFunction(ToMethod);
3873 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003874 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003875
Balazs Keri3b30d652018-10-19 13:32:20 +00003876 SmallVector<SourceLocation, 12> FromSelLocs;
3877 D->getSelectorLocs(FromSelLocs);
3878 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3879 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3880 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003881
Balazs Keri3b30d652018-10-19 13:32:20 +00003882 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003883
3884 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003885 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003886 return ToMethod;
3887}
3888
Balazs Keri3b30d652018-10-19 13:32:20 +00003889ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003890 // Import the major distinguishing characteristics of a category.
3891 DeclContext *DC, *LexicalDC;
3892 DeclarationName Name;
3893 SourceLocation Loc;
3894 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003895 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3896 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003897 if (ToD)
3898 return ToD;
3899
Balazs Keri3b30d652018-10-19 13:32:20 +00003900 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3901 TypeSourceInfo *ToTypeSourceInfo;
3902 if (auto Imp = importSeq(
3903 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3904 D->getTypeSourceInfo()))
3905 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3906 else
3907 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003908
Gabor Marton26f72a92018-07-12 09:42:05 +00003909 ObjCTypeParamDecl *Result;
3910 if (GetImportedOrCreateDecl(
3911 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003912 ToVarianceLoc, D->getIndex(),
3913 ToLocation, Name.getAsIdentifierInfo(),
3914 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003915 return Result;
3916
Douglas Gregor85f3f952015-07-07 03:57:15 +00003917 Result->setLexicalDeclContext(LexicalDC);
3918 return Result;
3919}
3920
Balazs Keri3b30d652018-10-19 13:32:20 +00003921ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003922 // Import the major distinguishing characteristics of a category.
3923 DeclContext *DC, *LexicalDC;
3924 DeclarationName Name;
3925 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003926 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003927 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3928 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003929 if (ToD)
3930 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003931
Balazs Keri3b30d652018-10-19 13:32:20 +00003932 ObjCInterfaceDecl *ToInterface;
3933 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3934 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003935
Douglas Gregor84c51c32010-02-18 01:47:50 +00003936 // Determine if we've already encountered this category.
3937 ObjCCategoryDecl *MergeWithCategory
3938 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3939 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3940 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003941 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3942 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3943 if (auto Imp = importSeq(
3944 D->getAtStartLoc(), D->getCategoryNameLoc(),
3945 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3946 std::tie(
3947 ToAtStartLoc, ToCategoryNameLoc,
3948 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3949 else
3950 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003951
3952 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003953 ToAtStartLoc, Loc,
3954 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003955 Name.getAsIdentifierInfo(), ToInterface,
3956 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00003957 ToIvarLBraceLoc,
3958 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00003959 return ToCategory;
3960
Douglas Gregor84c51c32010-02-18 01:47:50 +00003961 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003962 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00003963 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003964 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00003965 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
3966 ToCategory->setTypeParamList(*PListOrErr);
3967 else
3968 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003969
Douglas Gregor84c51c32010-02-18 01:47:50 +00003970 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003971 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3972 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003973 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3974 = D->protocol_loc_begin();
3975 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3976 FromProtoEnd = D->protocol_end();
3977 FromProto != FromProtoEnd;
3978 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003979 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
3980 Protocols.push_back(*ToProtoOrErr);
3981 else
3982 return ToProtoOrErr.takeError();
3983
3984 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
3985 ProtocolLocs.push_back(*ToProtoLocOrErr);
3986 else
3987 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00003988 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003989
Douglas Gregor84c51c32010-02-18 01:47:50 +00003990 // FIXME: If we're merging, make sure that the protocol list is the same.
3991 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3992 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00003993
Douglas Gregor84c51c32010-02-18 01:47:50 +00003994 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003995 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003996 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003997
Douglas Gregor84c51c32010-02-18 01:47:50 +00003998 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00003999 if (Error Err = ImportDeclContext(D))
4000 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004001
Douglas Gregor84c51c32010-02-18 01:47:50 +00004002 // If we have an implementation, import it as well.
4003 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004004 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4005 import(D->getImplementation()))
4006 ToCategory->setImplementation(*ToImplOrErr);
4007 else
4008 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004009 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004010
Douglas Gregor84c51c32010-02-18 01:47:50 +00004011 return ToCategory;
4012}
4013
Balazs Keri3b30d652018-10-19 13:32:20 +00004014Error ASTNodeImporter::ImportDefinition(
4015 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004016 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004017 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004018 if (Error Err = ImportDeclContext(From))
4019 return Err;
4020 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004021 }
4022
4023 // Start the protocol definition
4024 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004025
Douglas Gregor2aa53772012-01-24 17:42:07 +00004026 // Import protocols
4027 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4028 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004029 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4030 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004031 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4032 FromProtoEnd = From->protocol_end();
4033 FromProto != FromProtoEnd;
4034 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004035 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4036 Protocols.push_back(*ToProtoOrErr);
4037 else
4038 return ToProtoOrErr.takeError();
4039
4040 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4041 ProtocolLocs.push_back(*ToProtoLocOrErr);
4042 else
4043 return ToProtoLocOrErr.takeError();
4044
Douglas Gregor2aa53772012-01-24 17:42:07 +00004045 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004046
Douglas Gregor2aa53772012-01-24 17:42:07 +00004047 // FIXME: If we're merging, make sure that the protocol list is the same.
4048 To->setProtocolList(Protocols.data(), Protocols.size(),
4049 ProtocolLocs.data(), Importer.getToContext());
4050
Douglas Gregor2e15c842012-02-01 21:00:38 +00004051 if (shouldForceImportDeclContext(Kind)) {
4052 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004053 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4054 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004055 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004056 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004057}
4058
Balazs Keri3b30d652018-10-19 13:32:20 +00004059ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004060 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004061 // from, but this particular declaration is not that definition, import the
4062 // definition and map to that.
4063 ObjCProtocolDecl *Definition = D->getDefinition();
4064 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004065 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4066 return Importer.MapImported(D, *ImportedDefOrErr);
4067 else
4068 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 }
4070
Douglas Gregor84c51c32010-02-18 01:47:50 +00004071 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004072 DeclContext *DC, *LexicalDC;
4073 DeclarationName Name;
4074 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004075 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004076 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4077 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004078 if (ToD)
4079 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004080
Craig Topper36250ad2014-05-12 05:36:57 +00004081 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004082 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004083 for (auto *FoundDecl : FoundDecls) {
4084 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004085 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004086
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004087 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004088 break;
4089 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004090
Douglas Gregor98d156a2010-02-17 16:12:00 +00004091 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004092 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004093 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4094 if (!ToAtBeginLocOrErr)
4095 return ToAtBeginLocOrErr.takeError();
4096
Gabor Marton26f72a92018-07-12 09:42:05 +00004097 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4098 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004099 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004100 /*PrevDecl=*/nullptr))
4101 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004102 ToProto->setLexicalDeclContext(LexicalDC);
4103 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004104 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004105
4106 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004107
Balazs Keri3b30d652018-10-19 13:32:20 +00004108 if (D->isThisDeclarationADefinition())
4109 if (Error Err = ImportDefinition(D, ToProto))
4110 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004111
Douglas Gregor98d156a2010-02-17 16:12:00 +00004112 return ToProto;
4113}
4114
Balazs Keri3b30d652018-10-19 13:32:20 +00004115ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4116 DeclContext *DC, *LexicalDC;
4117 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4118 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004119
Balazs Keri3b30d652018-10-19 13:32:20 +00004120 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4121 if (!ExternLocOrErr)
4122 return ExternLocOrErr.takeError();
4123
4124 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4125 if (!LangLocOrErr)
4126 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004127
4128 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004129
4130 LinkageSpecDecl *ToLinkageSpec;
4131 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004132 *ExternLocOrErr, *LangLocOrErr,
4133 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004134 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004135
4136 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004137 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4138 if (!RBraceLocOrErr)
4139 return RBraceLocOrErr.takeError();
4140 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004141 }
4142
4143 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4144 LexicalDC->addDeclInternal(ToLinkageSpec);
4145
Sean Callanan0aae0412014-12-10 00:00:37 +00004146 return ToLinkageSpec;
4147}
4148
Balazs Keri3b30d652018-10-19 13:32:20 +00004149ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004150 DeclContext *DC, *LexicalDC;
4151 DeclarationName Name;
4152 SourceLocation Loc;
4153 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004154 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4155 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004156 if (ToD)
4157 return ToD;
4158
Balazs Keri3b30d652018-10-19 13:32:20 +00004159 SourceLocation ToLoc, ToUsingLoc;
4160 NestedNameSpecifierLoc ToQualifierLoc;
4161 if (auto Imp = importSeq(
4162 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4163 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4164 else
4165 return Imp.takeError();
4166
4167 DeclarationNameInfo NameInfo(Name, ToLoc);
4168 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4169 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004170
Gabor Marton26f72a92018-07-12 09:42:05 +00004171 UsingDecl *ToUsing;
4172 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004173 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004174 D->hasTypename()))
4175 return ToUsing;
4176
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004177 ToUsing->setLexicalDeclContext(LexicalDC);
4178 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004179
4180 if (NamedDecl *FromPattern =
4181 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004182 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4183 Importer.getToContext().setInstantiatedFromUsingDecl(
4184 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004185 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004186 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004187 }
4188
Balazs Keri3b30d652018-10-19 13:32:20 +00004189 for (UsingShadowDecl *FromShadow : D->shadows()) {
4190 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4191 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004192 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004193 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004194 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004195 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004196 }
4197 return ToUsing;
4198}
4199
Balazs Keri3b30d652018-10-19 13:32:20 +00004200ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004201 DeclContext *DC, *LexicalDC;
4202 DeclarationName Name;
4203 SourceLocation Loc;
4204 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004205 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4206 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004207 if (ToD)
4208 return ToD;
4209
Balazs Keri3b30d652018-10-19 13:32:20 +00004210 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4211 if (!ToUsingOrErr)
4212 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004213
Balazs Keri3b30d652018-10-19 13:32:20 +00004214 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4215 if (!ToTargetOrErr)
4216 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004217
Gabor Marton26f72a92018-07-12 09:42:05 +00004218 UsingShadowDecl *ToShadow;
4219 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004220 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004221 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004222
4223 ToShadow->setLexicalDeclContext(LexicalDC);
4224 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004225
4226 if (UsingShadowDecl *FromPattern =
4227 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004228 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4229 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4230 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004231 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004232 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004233 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004234 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004235 }
4236
4237 LexicalDC->addDeclInternal(ToShadow);
4238
4239 return ToShadow;
4240}
4241
Balazs Keri3b30d652018-10-19 13:32:20 +00004242ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004243 DeclContext *DC, *LexicalDC;
4244 DeclarationName Name;
4245 SourceLocation Loc;
4246 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004247 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4248 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004249 if (ToD)
4250 return ToD;
4251
Balazs Keri3b30d652018-10-19 13:32:20 +00004252 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4253 if (!ToComAncestorOrErr)
4254 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004255
Balazs Keri3b30d652018-10-19 13:32:20 +00004256 NamespaceDecl *ToNominatedNamespace;
4257 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4258 NestedNameSpecifierLoc ToQualifierLoc;
4259 if (auto Imp = importSeq(
4260 D->getNominatedNamespace(), D->getUsingLoc(),
4261 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4262 D->getIdentLocation()))
4263 std::tie(
4264 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4265 ToQualifierLoc, ToIdentLocation) = *Imp;
4266 else
4267 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004268
Gabor Marton26f72a92018-07-12 09:42:05 +00004269 UsingDirectiveDecl *ToUsingDir;
4270 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004271 ToUsingLoc,
4272 ToNamespaceKeyLocation,
4273 ToQualifierLoc,
4274 ToIdentLocation,
4275 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004276 return ToUsingDir;
4277
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004278 ToUsingDir->setLexicalDeclContext(LexicalDC);
4279 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004280
4281 return ToUsingDir;
4282}
4283
Balazs Keri3b30d652018-10-19 13:32:20 +00004284ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004285 UnresolvedUsingValueDecl *D) {
4286 DeclContext *DC, *LexicalDC;
4287 DeclarationName Name;
4288 SourceLocation Loc;
4289 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004290 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4291 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004292 if (ToD)
4293 return ToD;
4294
Balazs Keri3b30d652018-10-19 13:32:20 +00004295 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4296 NestedNameSpecifierLoc ToQualifierLoc;
4297 if (auto Imp = importSeq(
4298 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4299 D->getEllipsisLoc()))
4300 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4301 else
4302 return Imp.takeError();
4303
4304 DeclarationNameInfo NameInfo(Name, ToLoc);
4305 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4306 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004307
Gabor Marton26f72a92018-07-12 09:42:05 +00004308 UnresolvedUsingValueDecl *ToUsingValue;
4309 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004310 ToUsingLoc, ToQualifierLoc, NameInfo,
4311 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004312 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004313
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004314 ToUsingValue->setAccess(D->getAccess());
4315 ToUsingValue->setLexicalDeclContext(LexicalDC);
4316 LexicalDC->addDeclInternal(ToUsingValue);
4317
4318 return ToUsingValue;
4319}
4320
Balazs Keri3b30d652018-10-19 13:32:20 +00004321ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004322 UnresolvedUsingTypenameDecl *D) {
4323 DeclContext *DC, *LexicalDC;
4324 DeclarationName Name;
4325 SourceLocation Loc;
4326 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004327 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4328 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004329 if (ToD)
4330 return ToD;
4331
Balazs Keri3b30d652018-10-19 13:32:20 +00004332 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4333 NestedNameSpecifierLoc ToQualifierLoc;
4334 if (auto Imp = importSeq(
4335 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4336 D->getEllipsisLoc()))
4337 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4338 else
4339 return Imp.takeError();
4340
Gabor Marton26f72a92018-07-12 09:42:05 +00004341 UnresolvedUsingTypenameDecl *ToUsing;
4342 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004343 ToUsingLoc, ToTypenameLoc,
4344 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004345 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004346
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004347 ToUsing->setAccess(D->getAccess());
4348 ToUsing->setLexicalDeclContext(LexicalDC);
4349 LexicalDC->addDeclInternal(ToUsing);
4350
4351 return ToUsing;
4352}
4353
Balazs Keri3b30d652018-10-19 13:32:20 +00004354
4355Error ASTNodeImporter::ImportDefinition(
4356 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004357 if (To->getDefinition()) {
4358 // Check consistency of superclass.
4359 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4360 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004361 if (auto FromSuperOrErr = import(FromSuper))
4362 FromSuper = *FromSuperOrErr;
4363 else
4364 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004365 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004366
4367 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004368 if ((bool)FromSuper != (bool)ToSuper ||
4369 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004370 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004371 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004372 << To->getDeclName();
4373 if (ToSuper)
4374 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4375 << To->getSuperClass()->getDeclName();
4376 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004377 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004378 diag::note_odr_objc_missing_superclass);
4379 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004380 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004381 diag::note_odr_objc_superclass)
4382 << From->getSuperClass()->getDeclName();
4383 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004384 Importer.FromDiag(From->getLocation(),
4385 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004386 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004387
Douglas Gregor2e15c842012-02-01 21:00:38 +00004388 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004389 if (Error Err = ImportDeclContext(From))
4390 return Err;
4391 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004392 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004393
Douglas Gregor2aa53772012-01-24 17:42:07 +00004394 // Start the definition.
4395 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004396
Douglas Gregor2aa53772012-01-24 17:42:07 +00004397 // If this class has a superclass, import it.
4398 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004399 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4400 To->setSuperClass(*SuperTInfoOrErr);
4401 else
4402 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004403 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004404
Douglas Gregor2aa53772012-01-24 17:42:07 +00004405 // Import protocols
4406 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4407 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004408 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4409 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004410
Douglas Gregor2aa53772012-01-24 17:42:07 +00004411 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4412 FromProtoEnd = From->protocol_end();
4413 FromProto != FromProtoEnd;
4414 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004415 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4416 Protocols.push_back(*ToProtoOrErr);
4417 else
4418 return ToProtoOrErr.takeError();
4419
4420 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4421 ProtocolLocs.push_back(*ToProtoLocOrErr);
4422 else
4423 return ToProtoLocOrErr.takeError();
4424
Douglas Gregor2aa53772012-01-24 17:42:07 +00004425 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004426
Douglas Gregor2aa53772012-01-24 17:42:07 +00004427 // FIXME: If we're merging, make sure that the protocol list is the same.
4428 To->setProtocolList(Protocols.data(), Protocols.size(),
4429 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004430
Douglas Gregor2aa53772012-01-24 17:42:07 +00004431 // Import categories. When the categories themselves are imported, they'll
4432 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004433 for (auto *Cat : From->known_categories()) {
4434 auto ToCatOrErr = import(Cat);
4435 if (!ToCatOrErr)
4436 return ToCatOrErr.takeError();
4437 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004438
Douglas Gregor2aa53772012-01-24 17:42:07 +00004439 // If we have an @implementation, import it as well.
4440 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004441 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4442 import(From->getImplementation()))
4443 To->setImplementation(*ToImplOrErr);
4444 else
4445 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004446 }
4447
Douglas Gregor2e15c842012-02-01 21:00:38 +00004448 if (shouldForceImportDeclContext(Kind)) {
4449 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004450 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4451 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004452 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004453 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004454}
4455
Balazs Keri3b30d652018-10-19 13:32:20 +00004456Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004457ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4458 if (!list)
4459 return nullptr;
4460
4461 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004462 for (auto *fromTypeParam : *list) {
4463 if (auto toTypeParamOrErr = import(fromTypeParam))
4464 toTypeParams.push_back(*toTypeParamOrErr);
4465 else
4466 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004467 }
4468
Balazs Keri3b30d652018-10-19 13:32:20 +00004469 auto LAngleLocOrErr = import(list->getLAngleLoc());
4470 if (!LAngleLocOrErr)
4471 return LAngleLocOrErr.takeError();
4472
4473 auto RAngleLocOrErr = import(list->getRAngleLoc());
4474 if (!RAngleLocOrErr)
4475 return RAngleLocOrErr.takeError();
4476
Douglas Gregor85f3f952015-07-07 03:57:15 +00004477 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004478 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004479 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004480 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004481}
4482
Balazs Keri3b30d652018-10-19 13:32:20 +00004483ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004484 // If this class has a definition in the translation unit we're coming from,
4485 // but this particular declaration is not that definition, import the
4486 // definition and map to that.
4487 ObjCInterfaceDecl *Definition = D->getDefinition();
4488 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004489 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4490 return Importer.MapImported(D, *ImportedDefOrErr);
4491 else
4492 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004493 }
4494
Douglas Gregor45635322010-02-16 01:20:57 +00004495 // Import the major distinguishing characteristics of an @interface.
4496 DeclContext *DC, *LexicalDC;
4497 DeclarationName Name;
4498 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004499 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004500 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4501 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004502 if (ToD)
4503 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004504
Douglas Gregor2aa53772012-01-24 17:42:07 +00004505 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004506 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004507 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004508 for (auto *FoundDecl : FoundDecls) {
4509 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004510 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004511
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004512 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004513 break;
4514 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004515
Douglas Gregor2aa53772012-01-24 17:42:07 +00004516 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004517 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004518 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004519 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4520 if (!AtBeginLocOrErr)
4521 return AtBeginLocOrErr.takeError();
4522
Gabor Marton26f72a92018-07-12 09:42:05 +00004523 if (GetImportedOrCreateDecl(
4524 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004525 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004526 /*TypeParamList=*/nullptr,
4527 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4528 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004529 ToIface->setLexicalDeclContext(LexicalDC);
4530 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004531 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004532 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004533 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004534 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004535 if (auto ToPListOrErr =
4536 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4537 ToIface->setTypeParamList(*ToPListOrErr);
4538 else
4539 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004540
Balazs Keri3b30d652018-10-19 13:32:20 +00004541 if (D->isThisDeclarationADefinition())
4542 if (Error Err = ImportDefinition(D, ToIface))
4543 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004544
Douglas Gregor98d156a2010-02-17 16:12:00 +00004545 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004546}
4547
Balazs Keri3b30d652018-10-19 13:32:20 +00004548ExpectedDecl
4549ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4550 ObjCCategoryDecl *Category;
4551 if (Error Err = importInto(Category, D->getCategoryDecl()))
4552 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004553
Douglas Gregor4da9d682010-12-07 15:32:12 +00004554 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4555 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004556 DeclContext *DC, *LexicalDC;
4557 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4558 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004559
Balazs Keri3b30d652018-10-19 13:32:20 +00004560 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4561 if (auto Imp = importSeq(
4562 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4563 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4564 else
4565 return Imp.takeError();
4566
Gabor Marton26f72a92018-07-12 09:42:05 +00004567 if (GetImportedOrCreateDecl(
4568 ToImpl, D, Importer.getToContext(), DC,
4569 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004570 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004571 return ToImpl;
4572
Balazs Keri3b30d652018-10-19 13:32:20 +00004573 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004574 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004575 Category->setImplementation(ToImpl);
4576 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004577
Gabor Marton26f72a92018-07-12 09:42:05 +00004578 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004579 if (Error Err = ImportDeclContext(D))
4580 return std::move(Err);
4581
Douglas Gregor4da9d682010-12-07 15:32:12 +00004582 return ToImpl;
4583}
4584
Balazs Keri3b30d652018-10-19 13:32:20 +00004585ExpectedDecl
4586ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004587 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004588 ObjCInterfaceDecl *Iface;
4589 if (Error Err = importInto(Iface, D->getClassInterface()))
4590 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004591
4592 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004593 ObjCInterfaceDecl *Super;
4594 if (Error Err = importInto(Super, D->getSuperClass()))
4595 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004596
4597 ObjCImplementationDecl *Impl = Iface->getImplementation();
4598 if (!Impl) {
4599 // We haven't imported an implementation yet. Create a new @implementation
4600 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004601 DeclContext *DC, *LexicalDC;
4602 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4603 return std::move(Err);
4604
4605 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4606 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4607 if (auto Imp = importSeq(
4608 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4609 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4610 std::tie(
4611 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4612 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4613 else
4614 return Imp.takeError();
4615
Gabor Marton26f72a92018-07-12 09:42:05 +00004616 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004617 DC, Iface, Super,
4618 ToLocation,
4619 ToAtStartLoc,
4620 ToSuperClassLoc,
4621 ToIvarLBraceLoc,
4622 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004623 return Impl;
4624
Balazs Keri3b30d652018-10-19 13:32:20 +00004625 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004626
Douglas Gregorda8025c2010-12-07 01:26:03 +00004627 // Associate the implementation with the class it implements.
4628 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004629 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004630 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004631 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004632
4633 // Verify that the existing @implementation has the same superclass.
4634 if ((Super && !Impl->getSuperClass()) ||
4635 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004636 (Super && Impl->getSuperClass() &&
4637 !declaresSameEntity(Super->getCanonicalDecl(),
4638 Impl->getSuperClass()))) {
4639 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004640 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004641 << Iface->getDeclName();
4642 // FIXME: It would be nice to have the location of the superclass
4643 // below.
4644 if (Impl->getSuperClass())
4645 Importer.ToDiag(Impl->getLocation(),
4646 diag::note_odr_objc_superclass)
4647 << Impl->getSuperClass()->getDeclName();
4648 else
4649 Importer.ToDiag(Impl->getLocation(),
4650 diag::note_odr_objc_missing_superclass);
4651 if (D->getSuperClass())
4652 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004653 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004654 << D->getSuperClass()->getDeclName();
4655 else
4656 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004657 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004658
4659 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004660 }
4661 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004662
Douglas Gregorda8025c2010-12-07 01:26:03 +00004663 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004664 if (Error Err = ImportDeclContext(D))
4665 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004666
4667 return Impl;
4668}
4669
Balazs Keri3b30d652018-10-19 13:32:20 +00004670ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004671 // Import the major distinguishing characteristics of an @property.
4672 DeclContext *DC, *LexicalDC;
4673 DeclarationName Name;
4674 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004675 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004676 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4677 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004678 if (ToD)
4679 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004680
4681 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004682 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004683 for (auto *FoundDecl : FoundDecls) {
4684 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004685 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004686 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004687 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004688 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004689 << Name << D->getType() << FoundProp->getType();
4690 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4691 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004692
4693 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004694 }
4695
4696 // FIXME: Check property attributes, getters, setters, etc.?
4697
4698 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004699 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004700 return FoundProp;
4701 }
4702 }
4703
Balazs Keri3b30d652018-10-19 13:32:20 +00004704 QualType ToType;
4705 TypeSourceInfo *ToTypeSourceInfo;
4706 SourceLocation ToAtLoc, ToLParenLoc;
4707 if (auto Imp = importSeq(
4708 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4709 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4710 else
4711 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004712
4713 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004714 ObjCPropertyDecl *ToProperty;
4715 if (GetImportedOrCreateDecl(
4716 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004717 Name.getAsIdentifierInfo(), ToAtLoc,
4718 ToLParenLoc, ToType,
4719 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004720 return ToProperty;
4721
Balazs Keri3b30d652018-10-19 13:32:20 +00004722 Selector ToGetterName, ToSetterName;
4723 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4724 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4725 ObjCIvarDecl *ToPropertyIvarDecl;
4726 if (auto Imp = importSeq(
4727 D->getGetterName(), D->getSetterName(),
4728 D->getGetterNameLoc(), D->getSetterNameLoc(),
4729 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4730 D->getPropertyIvarDecl()))
4731 std::tie(
4732 ToGetterName, ToSetterName,
4733 ToGetterNameLoc, ToSetterNameLoc,
4734 ToGetterMethodDecl, ToSetterMethodDecl,
4735 ToPropertyIvarDecl) = *Imp;
4736 else
4737 return Imp.takeError();
4738
Douglas Gregora11c4582010-02-17 18:02:10 +00004739 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004740 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004741
4742 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004743 ToProperty->setPropertyAttributesAsWritten(
4744 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004745 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4746 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4747 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4748 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4749 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004750 return ToProperty;
4751}
4752
Balazs Keri3b30d652018-10-19 13:32:20 +00004753ExpectedDecl
4754ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4755 ObjCPropertyDecl *Property;
4756 if (Error Err = importInto(Property, D->getPropertyDecl()))
4757 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004758
Balazs Keri3b30d652018-10-19 13:32:20 +00004759 DeclContext *DC, *LexicalDC;
4760 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4761 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004762
Balazs Keri3b30d652018-10-19 13:32:20 +00004763 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004764
4765 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004766 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004767 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4768 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004769
4770 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004771 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4772 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004773 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004774 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4775 if (auto Imp = importSeq(
4776 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4777 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4778 else
4779 return Imp.takeError();
4780
Gabor Marton26f72a92018-07-12 09:42:05 +00004781 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004782 ToBeginLoc,
4783 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004784 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004785 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004786 return ToImpl;
4787
Douglas Gregor14a49e22010-12-07 18:32:03 +00004788 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004789 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004790 } else {
4791 // Check that we have the same kind of property implementation (@synthesize
4792 // vs. @dynamic).
4793 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004794 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004795 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004796 << Property->getDeclName()
4797 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004798 == ObjCPropertyImplDecl::Dynamic);
4799 Importer.FromDiag(D->getLocation(),
4800 diag::note_odr_objc_property_impl_kind)
4801 << D->getPropertyDecl()->getDeclName()
4802 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004803
4804 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004805 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004806
4807 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004808 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4809 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004810 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004811 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004812 << Property->getDeclName()
4813 << ToImpl->getPropertyIvarDecl()->getDeclName()
4814 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004815 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004816 diag::note_odr_objc_synthesize_ivar_here)
4817 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004818
4819 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004820 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004821
Douglas Gregor14a49e22010-12-07 18:32:03 +00004822 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004823 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004824 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004825
Douglas Gregor14a49e22010-12-07 18:32:03 +00004826 return ToImpl;
4827}
4828
Balazs Keri3b30d652018-10-19 13:32:20 +00004829ExpectedDecl
4830ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004831 // For template arguments, we adopt the translation unit as our declaration
4832 // context. This context will be fixed when the actual template declaration
4833 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004834
Douglas Gregora082a492010-11-30 19:14:50 +00004835 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004836
4837 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4838 if (!BeginLocOrErr)
4839 return BeginLocOrErr.takeError();
4840
4841 ExpectedSLoc LocationOrErr = import(D->getLocation());
4842 if (!LocationOrErr)
4843 return LocationOrErr.takeError();
4844
Gabor Marton26f72a92018-07-12 09:42:05 +00004845 TemplateTypeParmDecl *ToD = nullptr;
4846 (void)GetImportedOrCreateDecl(
4847 ToD, D, Importer.getToContext(),
4848 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004849 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004850 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4851 D->wasDeclaredWithTypename(), D->isParameterPack());
4852 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004853}
4854
Balazs Keri3b30d652018-10-19 13:32:20 +00004855ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004856ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004857 DeclarationName ToDeclName;
4858 SourceLocation ToLocation, ToInnerLocStart;
4859 QualType ToType;
4860 TypeSourceInfo *ToTypeSourceInfo;
4861 if (auto Imp = importSeq(
4862 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4863 D->getInnerLocStart()))
4864 std::tie(
4865 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4866 ToInnerLocStart) = *Imp;
4867 else
4868 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004869
Douglas Gregora082a492010-11-30 19:14:50 +00004870 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004871
4872 NonTypeTemplateParmDecl *ToD = nullptr;
4873 (void)GetImportedOrCreateDecl(
4874 ToD, D, Importer.getToContext(),
4875 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004876 ToInnerLocStart, ToLocation, D->getDepth(),
4877 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4878 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004879 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004880}
4881
Balazs Keri3b30d652018-10-19 13:32:20 +00004882ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004883ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4884 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004885 auto NameOrErr = import(D->getDeclName());
4886 if (!NameOrErr)
4887 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004888
Douglas Gregora082a492010-11-30 19:14:50 +00004889 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004890 ExpectedSLoc LocationOrErr = import(D->getLocation());
4891 if (!LocationOrErr)
4892 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004893
Douglas Gregora082a492010-11-30 19:14:50 +00004894 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004895 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004896 if (!TemplateParamsOrErr)
4897 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004898
Douglas Gregora082a492010-11-30 19:14:50 +00004899 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004900
4901 TemplateTemplateParmDecl *ToD = nullptr;
4902 (void)GetImportedOrCreateDecl(
4903 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004904 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4905 D->getDepth(), D->getPosition(), D->isParameterPack(),
4906 (*NameOrErr).getAsIdentifierInfo(),
4907 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004908 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004909}
4910
Gabor Marton16d98c22019-03-07 13:01:51 +00004911// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004912// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004913template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4914 assert(D->getTemplatedDecl() && "Should be called on templates only");
4915 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004916 if (!ToTemplatedDef)
4917 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004918 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4919 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004920}
4921
Balazs Keri3b30d652018-10-19 13:32:20 +00004922ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004923 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4924
Douglas Gregora082a492010-11-30 19:14:50 +00004925 // Import the major distinguishing characteristics of this class template.
4926 DeclContext *DC, *LexicalDC;
4927 DeclarationName Name;
4928 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004929 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004930 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4931 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004932 if (ToD)
4933 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004934
Gabor Marton7df342a2018-12-17 12:42:12 +00004935 ClassTemplateDecl *FoundByLookup = nullptr;
4936
Douglas Gregora082a492010-11-30 19:14:50 +00004937 // We may already have a template of the same name; try to find and match it.
4938 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004939 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004940 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004941 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004942 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4943 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004944 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004945
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004946 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004947 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4948 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004949
Douglas Gregora082a492010-11-30 19:14:50 +00004950 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004951 ClassTemplateDecl *TemplateWithDef =
4952 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004953 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4954 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004955 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004956 FoundByLookup = FoundTemplate;
4957 break;
Gabor Marton9581c332018-05-23 13:53:36 +00004958 }
Douglas Gregora082a492010-11-30 19:14:50 +00004959 }
Gabor Marton9581c332018-05-23 13:53:36 +00004960
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004961 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004962 }
Gabor Marton9581c332018-05-23 13:53:36 +00004963
Douglas Gregora082a492010-11-30 19:14:50 +00004964 if (!ConflictingDecls.empty()) {
4965 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004966 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004967 ConflictingDecls.size());
4968 }
Gabor Marton9581c332018-05-23 13:53:36 +00004969
Douglas Gregora082a492010-11-30 19:14:50 +00004970 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00004971 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00004972 }
4973
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004974 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4975
Douglas Gregora082a492010-11-30 19:14:50 +00004976 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00004977 CXXRecordDecl *ToTemplated;
4978 if (Error Err = importInto(ToTemplated, FromTemplated))
4979 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004980
Douglas Gregora082a492010-11-30 19:14:50 +00004981 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00004982 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004983 if (!TemplateParamsOrErr)
4984 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004985
Gabor Marton26f72a92018-07-12 09:42:05 +00004986 ClassTemplateDecl *D2;
4987 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00004988 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00004989 return D2;
4990
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004991 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004992
Douglas Gregora082a492010-11-30 19:14:50 +00004993 D2->setAccess(D->getAccess());
4994 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00004995
4996 if (D->getDeclContext()->containsDeclAndLoad(D))
4997 DC->addDeclInternal(D2);
4998 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00004999 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005000
Gabor Marton7df342a2018-12-17 12:42:12 +00005001 if (FoundByLookup) {
5002 auto *Recent =
5003 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5004
5005 // It is possible that during the import of the class template definition
5006 // we start the import of a fwd friend decl of the very same class template
5007 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5008 // had been created earlier and by that time the lookup could not find
5009 // anything existing, so it has no previous decl. Later, (still during the
5010 // import of the fwd friend decl) we start to import the definition again
5011 // and this time the lookup finds the previous fwd friend class template.
5012 // In this case we must set up the previous decl for the templated decl.
5013 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005014 assert(FoundByLookup->getTemplatedDecl() &&
5015 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005016 CXXRecordDecl *PrevTemplated =
5017 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5018 if (ToTemplated != PrevTemplated)
5019 ToTemplated->setPreviousDecl(PrevTemplated);
5020 }
5021
5022 D2->setPreviousDecl(Recent);
5023 }
5024
5025 if (LexicalDC != DC && IsFriend)
5026 DC->makeDeclVisibleInContext(D2);
5027
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005028 if (FromTemplated->isCompleteDefinition() &&
5029 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005030 // FIXME: Import definition!
5031 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005032
Douglas Gregora082a492010-11-30 19:14:50 +00005033 return D2;
5034}
5035
Balazs Keri3b30d652018-10-19 13:32:20 +00005036ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005037 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005038 ClassTemplateDecl *ClassTemplate;
5039 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5040 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005041
Douglas Gregore2e50d332010-12-01 01:36:18 +00005042 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005043 DeclContext *DC, *LexicalDC;
5044 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5045 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005046
5047 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005048 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005049 if (Error Err = ImportTemplateArguments(
5050 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5051 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005052
Douglas Gregore2e50d332010-12-01 01:36:18 +00005053 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005054 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005055 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005056 ClassTemplatePartialSpecializationDecl *PartialSpec =
5057 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5058 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005059 PrevDecl =
5060 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005061 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005062 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005063
Gabor Marton7f8c4002019-03-19 13:34:10 +00005064 if (PrevDecl) {
5065 if (IsStructuralMatch(D, PrevDecl)) {
5066 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5067 Importer.MapImported(D, PrevDecl->getDefinition());
5068 // Import those default field initializers which have been
5069 // instantiated in the "From" context, but not in the "To" context.
5070 for (auto *FromField : D->fields())
5071 Importer.Import(FromField);
Gabor Marton42e15de2018-08-22 11:52:14 +00005072
Gabor Marton7f8c4002019-03-19 13:34:10 +00005073 // Import those methods which have been instantiated in the
5074 // "From" context, but not in the "To" context.
5075 for (CXXMethodDecl *FromM : D->methods())
5076 Importer.Import(FromM);
Gabor Marton42e15de2018-08-22 11:52:14 +00005077
Gabor Marton7f8c4002019-03-19 13:34:10 +00005078 // TODO Import instantiated default arguments.
5079 // TODO Import instantiated exception specifications.
5080 //
5081 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5082 // what else could be fused during an AST merge.
5083 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005084 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005085 } else { // ODR violation.
5086 // FIXME HandleNameConflict
5087 return nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005088 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005089 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005090
Gabor Marton7f8c4002019-03-19 13:34:10 +00005091 // Import the location of this declaration.
5092 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5093 if (!BeginLocOrErr)
5094 return BeginLocOrErr.takeError();
5095 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5096 if (!IdLocOrErr)
5097 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005098
Gabor Marton7f8c4002019-03-19 13:34:10 +00005099 // Create the specialization.
5100 ClassTemplateSpecializationDecl *D2 = nullptr;
5101 if (PartialSpec) {
5102 // Import TemplateArgumentListInfo.
5103 TemplateArgumentListInfo ToTAInfo;
5104 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5105 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5106 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005107
Gabor Marton7f8c4002019-03-19 13:34:10 +00005108 QualType CanonInjType;
5109 if (Error Err = importInto(
5110 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5111 return std::move(Err);
5112 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005113
Balazs Keridec09162019-03-20 15:42:42 +00005114 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005115 if (!ToTPListOrErr)
5116 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005117
Gabor Marton7f8c4002019-03-19 13:34:10 +00005118 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5119 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5120 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5121 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5122 ToTAInfo, CanonInjType,
5123 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5124 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005125
Gabor Marton7f8c4002019-03-19 13:34:10 +00005126 // Update InsertPos, because preceding import calls may have invalidated
5127 // it by adding new specializations.
5128 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5129 // Add this partial specialization to the class template.
5130 ClassTemplate->AddPartialSpecialization(
5131 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005132
Gabor Marton7f8c4002019-03-19 13:34:10 +00005133 } else { // Not a partial specialization.
5134 if (GetImportedOrCreateDecl(
5135 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5136 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5137 PrevDecl))
5138 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005139
Gabor Marton7f8c4002019-03-19 13:34:10 +00005140 // Update InsertPos, because preceding import calls may have invalidated
5141 // it by adding new specializations.
5142 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5143 // Add this specialization to the class template.
5144 ClassTemplate->AddSpecialization(D2, InsertPos);
5145 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005146
Gabor Marton7f8c4002019-03-19 13:34:10 +00005147 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005148
Gabor Marton7f8c4002019-03-19 13:34:10 +00005149 // Set the context of this specialization/instantiation.
5150 D2->setLexicalDeclContext(LexicalDC);
5151
5152 // Add to the DC only if it was an explicit specialization/instantiation.
5153 if (D2->isExplicitInstantiationOrSpecialization()) {
5154 LexicalDC->addDeclInternal(D2);
5155 }
5156
5157 // Import the qualifier, if any.
5158 if (auto LocOrErr = import(D->getQualifierLoc()))
5159 D2->setQualifierInfo(*LocOrErr);
5160 else
5161 return LocOrErr.takeError();
5162
5163 if (auto *TSI = D->getTypeAsWritten()) {
5164 if (auto TInfoOrErr = import(TSI))
5165 D2->setTypeAsWritten(*TInfoOrErr);
5166 else
5167 return TInfoOrErr.takeError();
5168
5169 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5170 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005171 else
5172 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005173
Gabor Marton7f8c4002019-03-19 13:34:10 +00005174 if (auto LocOrErr = import(D->getExternLoc()))
5175 D2->setExternLoc(*LocOrErr);
5176 else
5177 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005178 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005179
5180 if (D->getPointOfInstantiation().isValid()) {
5181 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5182 D2->setPointOfInstantiation(*POIOrErr);
5183 else
5184 return POIOrErr.takeError();
5185 }
5186
5187 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5188
Balazs Keri3b30d652018-10-19 13:32:20 +00005189 if (D->isCompleteDefinition())
5190 if (Error Err = ImportDefinition(D, D2))
5191 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005192
Douglas Gregore2e50d332010-12-01 01:36:18 +00005193 return D2;
5194}
5195
Balazs Keri3b30d652018-10-19 13:32:20 +00005196ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005197 // If this variable has a definition in the translation unit we're coming
5198 // from,
5199 // but this particular declaration is not that definition, import the
5200 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005201 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005202 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5203 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005204 if (ExpectedDecl ImportedDefOrErr = import(
5205 Definition->getDescribedVarTemplate()))
5206 return Importer.MapImported(D, *ImportedDefOrErr);
5207 else
5208 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005209 }
5210
5211 // Import the major distinguishing characteristics of this variable template.
5212 DeclContext *DC, *LexicalDC;
5213 DeclarationName Name;
5214 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005215 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005216 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5217 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005218 if (ToD)
5219 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005220
5221 // We may already have a template of the same name; try to find and match it.
5222 assert(!DC->isFunctionOrMethod() &&
5223 "Variable templates cannot be declared at function scope");
5224 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005225 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005226 for (auto *FoundDecl : FoundDecls) {
5227 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005228 continue;
5229
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005230 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005231 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005232 if (IsStructuralMatch(D, FoundTemplate)) {
5233 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005234 Importer.MapImported(D->getTemplatedDecl(),
5235 FoundTemplate->getTemplatedDecl());
5236 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005237 }
5238 }
5239
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005240 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005241 }
5242
5243 if (!ConflictingDecls.empty()) {
5244 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5245 ConflictingDecls.data(),
5246 ConflictingDecls.size());
5247 }
5248
5249 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005250 // FIXME: Is it possible to get other error than name conflict?
5251 // (Put this `if` into the previous `if`?)
5252 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005253
5254 VarDecl *DTemplated = D->getTemplatedDecl();
5255
5256 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005257 // FIXME: Value not used?
5258 ExpectedType TypeOrErr = import(DTemplated->getType());
5259 if (!TypeOrErr)
5260 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005261
5262 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005263 VarDecl *ToTemplated;
5264 if (Error Err = importInto(ToTemplated, DTemplated))
5265 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005266
5267 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005268 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005269 if (!TemplateParamsOrErr)
5270 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005271
Gabor Marton26f72a92018-07-12 09:42:05 +00005272 VarTemplateDecl *ToVarTD;
5273 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005274 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005275 return ToVarTD;
5276
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005277 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005278
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005279 ToVarTD->setAccess(D->getAccess());
5280 ToVarTD->setLexicalDeclContext(LexicalDC);
5281 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005282
Larisse Voufo39a1e502013-08-06 01:03:05 +00005283 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005284 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005285 // FIXME: Import definition!
5286 }
5287
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005288 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005289}
5290
Balazs Keri3b30d652018-10-19 13:32:20 +00005291ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005292 VarTemplateSpecializationDecl *D) {
5293 // If this record has a definition in the translation unit we're coming from,
5294 // but this particular declaration is not that definition, import the
5295 // definition and map to that.
5296 VarDecl *Definition = D->getDefinition();
5297 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005298 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5299 return Importer.MapImported(D, *ImportedDefOrErr);
5300 else
5301 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005302 }
5303
Balazs Keri3b30d652018-10-19 13:32:20 +00005304 VarTemplateDecl *VarTemplate;
5305 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5306 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005307
5308 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005309 DeclContext *DC, *LexicalDC;
5310 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5311 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005312
5313 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005314 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5315 if (!BeginLocOrErr)
5316 return BeginLocOrErr.takeError();
5317
5318 auto IdLocOrErr = import(D->getLocation());
5319 if (!IdLocOrErr)
5320 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005321
5322 // Import template arguments.
5323 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005324 if (Error Err = ImportTemplateArguments(
5325 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5326 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005327
5328 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005329 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005330 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005331 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005332 if (D2) {
5333 // We already have a variable template specialization with these template
5334 // arguments.
5335
5336 // FIXME: Check for specialization vs. instantiation errors.
5337
5338 if (VarDecl *FoundDef = D2->getDefinition()) {
5339 if (!D->isThisDeclarationADefinition() ||
5340 IsStructuralMatch(D, FoundDef)) {
5341 // The record types structurally match, or the "from" translation
5342 // unit only had a forward declaration anyway; call it the same
5343 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005344 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005345 }
5346 }
5347 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005348 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005349 QualType T;
5350 if (Error Err = importInto(T, D->getType()))
5351 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005352
Balazs Keri3b30d652018-10-19 13:32:20 +00005353 auto TInfoOrErr = import(D->getTypeSourceInfo());
5354 if (!TInfoOrErr)
5355 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005356
5357 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005358 if (Error Err = ImportTemplateArgumentListInfo(
5359 D->getTemplateArgsInfo(), ToTAInfo))
5360 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005361
5362 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005363 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005364 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5365 // Import TemplateArgumentListInfo
5366 TemplateArgumentListInfo ArgInfos;
5367 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5368 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005369 if (Error Err = ImportTemplateArgumentListInfo(
5370 *FromTAArgsAsWritten, ArgInfos))
5371 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005372
Balazs Keridec09162019-03-20 15:42:42 +00005373 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005374 if (!ToTPListOrErr)
5375 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005376
Gabor Marton26f72a92018-07-12 09:42:05 +00005377 PartVarSpecDecl *ToPartial;
5378 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005379 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5380 VarTemplate, T, *TInfoOrErr,
5381 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005382 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005383
Balazs Keri3b30d652018-10-19 13:32:20 +00005384 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5385 FromPartial->getInstantiatedFromMember()))
5386 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5387 else
5388 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005389
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005390 if (FromPartial->isMemberSpecialization())
5391 ToPartial->setMemberSpecialization();
5392
5393 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005394
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005395 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005396 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5397 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5398 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005399 D->getStorageClass(), TemplateArgs))
5400 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005401 }
5402
Balazs Keri3b30d652018-10-19 13:32:20 +00005403 if (D->getPointOfInstantiation().isValid()) {
5404 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5405 D2->setPointOfInstantiation(*POIOrErr);
5406 else
5407 return POIOrErr.takeError();
5408 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005409
Larisse Voufo39a1e502013-08-06 01:03:05 +00005410 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005411 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005412
5413 // Add this specialization to the class template.
5414 VarTemplate->AddSpecialization(D2, InsertPos);
5415
5416 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005417 if (auto LocOrErr = import(D->getQualifierLoc()))
5418 D2->setQualifierInfo(*LocOrErr);
5419 else
5420 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005421
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005422 if (D->isConstexpr())
5423 D2->setConstexpr(true);
5424
Larisse Voufo39a1e502013-08-06 01:03:05 +00005425 // Add the specialization to this context.
5426 D2->setLexicalDeclContext(LexicalDC);
5427 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005428
5429 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005430 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005431
Balazs Keri3b30d652018-10-19 13:32:20 +00005432 if (Error Err = ImportInitializer(D, D2))
5433 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005434
5435 return D2;
5436}
5437
Balazs Keri3b30d652018-10-19 13:32:20 +00005438ExpectedDecl
5439ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005440 DeclContext *DC, *LexicalDC;
5441 DeclarationName Name;
5442 SourceLocation Loc;
5443 NamedDecl *ToD;
5444
Balazs Keri3b30d652018-10-19 13:32:20 +00005445 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5446 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005447
5448 if (ToD)
5449 return ToD;
5450
Gabor Marton16d98c22019-03-07 13:01:51 +00005451 const FunctionTemplateDecl *FoundByLookup = nullptr;
5452
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005453 // Try to find a function in our own ("to") context with the same name, same
5454 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005455 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005456 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005457 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005458 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005459 for (auto *FoundDecl : FoundDecls) {
5460 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005461 continue;
5462
Gabor Marton16d98c22019-03-07 13:01:51 +00005463 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5464 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005465 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005466 if (IsStructuralMatch(D, FoundTemplate)) {
5467 FunctionTemplateDecl *TemplateWithDef =
5468 getTemplateDefinition(FoundTemplate);
5469 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5470 return Importer.MapImported(D, TemplateWithDef);
5471 }
5472 FoundByLookup = FoundTemplate;
5473 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005474 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005475 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005476 }
5477 }
5478 }
5479 }
5480
Balazs Keridec09162019-03-20 15:42:42 +00005481 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005482 if (!ParamsOrErr)
5483 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005484
Balazs Keri3b30d652018-10-19 13:32:20 +00005485 FunctionDecl *TemplatedFD;
5486 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5487 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005488
Gabor Marton26f72a92018-07-12 09:42:05 +00005489 FunctionTemplateDecl *ToFunc;
5490 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005491 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005492 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005493
5494 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005495
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005496 ToFunc->setAccess(D->getAccess());
5497 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005498 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005499
5500 if (FoundByLookup) {
5501 auto *Recent =
5502 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5503 if (!TemplatedFD->getPreviousDecl()) {
5504 assert(FoundByLookup->getTemplatedDecl() &&
5505 "Found decl must have its templated decl set");
5506 auto *PrevTemplated =
5507 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5508 if (TemplatedFD != PrevTemplated)
5509 TemplatedFD->setPreviousDecl(PrevTemplated);
5510 }
5511 ToFunc->setPreviousDecl(Recent);
5512 }
5513
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005514 return ToFunc;
5515}
5516
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005517//----------------------------------------------------------------------------
5518// Import Statements
5519//----------------------------------------------------------------------------
5520
Balazs Keri3b30d652018-10-19 13:32:20 +00005521ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005522 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5523 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005524 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005525}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005526
Balazs Keri3b30d652018-10-19 13:32:20 +00005527
5528ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005529 SmallVector<IdentifierInfo *, 4> Names;
5530 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5531 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005532 // ToII is nullptr when no symbolic name is given for output operand
5533 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005534 Names.push_back(ToII);
5535 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005536
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005537 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5538 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005539 // ToII is nullptr when no symbolic name is given for input operand
5540 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005541 Names.push_back(ToII);
5542 }
5543
5544 SmallVector<StringLiteral *, 4> Clobbers;
5545 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005546 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5547 Clobbers.push_back(*ClobberOrErr);
5548 else
5549 return ClobberOrErr.takeError();
5550
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005551 }
5552
5553 SmallVector<StringLiteral *, 4> Constraints;
5554 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005555 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5556 Constraints.push_back(*OutputOrErr);
5557 else
5558 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005559 }
5560
5561 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005562 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5563 Constraints.push_back(*InputOrErr);
5564 else
5565 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005566 }
5567
5568 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005569 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5570 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005571
Balazs Keri3b30d652018-10-19 13:32:20 +00005572 if (Error Err = ImportArrayChecked(
5573 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5574 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005575
Balazs Keri3b30d652018-10-19 13:32:20 +00005576 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5577 if (!AsmLocOrErr)
5578 return AsmLocOrErr.takeError();
5579 auto AsmStrOrErr = import(S->getAsmString());
5580 if (!AsmStrOrErr)
5581 return AsmStrOrErr.takeError();
5582 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5583 if (!RParenLocOrErr)
5584 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005585
5586 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005587 Importer.getToContext(),
5588 *AsmLocOrErr,
5589 S->isSimple(),
5590 S->isVolatile(),
5591 S->getNumOutputs(),
5592 S->getNumInputs(),
5593 Names.data(),
5594 Constraints.data(),
5595 Exprs.data(),
5596 *AsmStrOrErr,
5597 S->getNumClobbers(),
5598 Clobbers.data(),
5599 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005600}
5601
Balazs Keri3b30d652018-10-19 13:32:20 +00005602ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5603 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5604 if (!Imp)
5605 return Imp.takeError();
5606
5607 DeclGroupRef ToDG;
5608 SourceLocation ToBeginLoc, ToEndLoc;
5609 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5610
5611 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005612}
5613
Balazs Keri3b30d652018-10-19 13:32:20 +00005614ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5615 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5616 if (!ToSemiLocOrErr)
5617 return ToSemiLocOrErr.takeError();
5618 return new (Importer.getToContext()) NullStmt(
5619 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005620}
5621
Balazs Keri3b30d652018-10-19 13:32:20 +00005622ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005623 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005624
Balazs Keri3b30d652018-10-19 13:32:20 +00005625 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5626 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005627
Balazs Keri3b30d652018-10-19 13:32:20 +00005628 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5629 if (!ToLBracLocOrErr)
5630 return ToLBracLocOrErr.takeError();
5631
5632 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5633 if (!ToRBracLocOrErr)
5634 return ToRBracLocOrErr.takeError();
5635
5636 return CompoundStmt::Create(
5637 Importer.getToContext(), ToStmts,
5638 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005639}
5640
Balazs Keri3b30d652018-10-19 13:32:20 +00005641ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5642 auto Imp = importSeq(
5643 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5644 S->getEllipsisLoc(), S->getColonLoc());
5645 if (!Imp)
5646 return Imp.takeError();
5647
5648 Expr *ToLHS, *ToRHS;
5649 Stmt *ToSubStmt;
5650 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5651 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5652 *Imp;
5653
Bruno Ricci5b30571752018-10-28 12:30:53 +00005654 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5655 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005656 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005657
Gabor Horvath480892b2017-10-18 09:25:18 +00005658 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005659}
5660
Balazs Keri3b30d652018-10-19 13:32:20 +00005661ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5662 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5663 if (!Imp)
5664 return Imp.takeError();
5665
5666 SourceLocation ToDefaultLoc, ToColonLoc;
5667 Stmt *ToSubStmt;
5668 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5669
5670 return new (Importer.getToContext()) DefaultStmt(
5671 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005672}
5673
Balazs Keri3b30d652018-10-19 13:32:20 +00005674ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5675 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5676 if (!Imp)
5677 return Imp.takeError();
5678
5679 SourceLocation ToIdentLoc;
5680 LabelDecl *ToLabelDecl;
5681 Stmt *ToSubStmt;
5682 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5683
5684 return new (Importer.getToContext()) LabelStmt(
5685 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005686}
5687
Balazs Keri3b30d652018-10-19 13:32:20 +00005688ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5689 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5690 if (!ToAttrLocOrErr)
5691 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005692 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5693 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005694 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5695 return std::move(Err);
5696 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5697 if (!ToSubStmtOrErr)
5698 return ToSubStmtOrErr.takeError();
5699
5700 return AttributedStmt::Create(
5701 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005702}
5703
Balazs Keri3b30d652018-10-19 13:32:20 +00005704ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5705 auto Imp = importSeq(
5706 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5707 S->getThen(), S->getElseLoc(), S->getElse());
5708 if (!Imp)
5709 return Imp.takeError();
5710
5711 SourceLocation ToIfLoc, ToElseLoc;
5712 Stmt *ToInit, *ToThen, *ToElse;
5713 VarDecl *ToConditionVariable;
5714 Expr *ToCond;
5715 std::tie(
5716 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5717 *Imp;
5718
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005719 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5720 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5721 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005722}
5723
Balazs Keri3b30d652018-10-19 13:32:20 +00005724ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5725 auto Imp = importSeq(
5726 S->getInit(), S->getConditionVariable(), S->getCond(),
5727 S->getBody(), S->getSwitchLoc());
5728 if (!Imp)
5729 return Imp.takeError();
5730
5731 Stmt *ToInit, *ToBody;
5732 VarDecl *ToConditionVariable;
5733 Expr *ToCond;
5734 SourceLocation ToSwitchLoc;
5735 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5736
Bruno Riccie2806f82018-10-29 16:12:37 +00005737 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5738 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005739 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005740 ToStmt->setSwitchLoc(ToSwitchLoc);
5741
Sean Callanan59721b32015-04-28 18:41:46 +00005742 // Now we have to re-chain the cases.
5743 SwitchCase *LastChainedSwitchCase = nullptr;
5744 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5745 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005746 Expected<SwitchCase *> ToSCOrErr = import(SC);
5747 if (!ToSCOrErr)
5748 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005749 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005750 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005751 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005752 ToStmt->setSwitchCaseList(*ToSCOrErr);
5753 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005754 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005755
Sean Callanan59721b32015-04-28 18:41:46 +00005756 return ToStmt;
5757}
5758
Balazs Keri3b30d652018-10-19 13:32:20 +00005759ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5760 auto Imp = importSeq(
5761 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5762 if (!Imp)
5763 return Imp.takeError();
5764
5765 VarDecl *ToConditionVariable;
5766 Expr *ToCond;
5767 Stmt *ToBody;
5768 SourceLocation ToWhileLoc;
5769 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5770
Bruno Riccibacf7512018-10-30 13:42:41 +00005771 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5772 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005773}
5774
Balazs Keri3b30d652018-10-19 13:32:20 +00005775ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5776 auto Imp = importSeq(
5777 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5778 S->getRParenLoc());
5779 if (!Imp)
5780 return Imp.takeError();
5781
5782 Stmt *ToBody;
5783 Expr *ToCond;
5784 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5785 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5786
5787 return new (Importer.getToContext()) DoStmt(
5788 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005789}
5790
Balazs Keri3b30d652018-10-19 13:32:20 +00005791ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5792 auto Imp = importSeq(
5793 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5794 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5795 if (!Imp)
5796 return Imp.takeError();
5797
5798 Stmt *ToInit;
5799 Expr *ToCond, *ToInc;
5800 VarDecl *ToConditionVariable;
5801 Stmt *ToBody;
5802 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5803 std::tie(
5804 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5805 ToLParenLoc, ToRParenLoc) = *Imp;
5806
5807 return new (Importer.getToContext()) ForStmt(
5808 Importer.getToContext(),
5809 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5810 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005811}
5812
Balazs Keri3b30d652018-10-19 13:32:20 +00005813ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5814 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5815 if (!Imp)
5816 return Imp.takeError();
5817
5818 LabelDecl *ToLabel;
5819 SourceLocation ToGotoLoc, ToLabelLoc;
5820 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5821
5822 return new (Importer.getToContext()) GotoStmt(
5823 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005824}
5825
Balazs Keri3b30d652018-10-19 13:32:20 +00005826ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5827 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5828 if (!Imp)
5829 return Imp.takeError();
5830
5831 SourceLocation ToGotoLoc, ToStarLoc;
5832 Expr *ToTarget;
5833 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5834
5835 return new (Importer.getToContext()) IndirectGotoStmt(
5836 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005837}
5838
Balazs Keri3b30d652018-10-19 13:32:20 +00005839ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5840 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5841 if (!ToContinueLocOrErr)
5842 return ToContinueLocOrErr.takeError();
5843 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005844}
5845
Balazs Keri3b30d652018-10-19 13:32:20 +00005846ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5847 auto ToBreakLocOrErr = import(S->getBreakLoc());
5848 if (!ToBreakLocOrErr)
5849 return ToBreakLocOrErr.takeError();
5850 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005851}
5852
Balazs Keri3b30d652018-10-19 13:32:20 +00005853ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5854 auto Imp = importSeq(
5855 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5856 if (!Imp)
5857 return Imp.takeError();
5858
5859 SourceLocation ToReturnLoc;
5860 Expr *ToRetValue;
5861 const VarDecl *ToNRVOCandidate;
5862 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5863
Bruno Ricci023b1d12018-10-30 14:40:49 +00005864 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5865 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005866}
5867
Balazs Keri3b30d652018-10-19 13:32:20 +00005868ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5869 auto Imp = importSeq(
5870 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5871 if (!Imp)
5872 return Imp.takeError();
5873
5874 SourceLocation ToCatchLoc;
5875 VarDecl *ToExceptionDecl;
5876 Stmt *ToHandlerBlock;
5877 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5878
5879 return new (Importer.getToContext()) CXXCatchStmt (
5880 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005881}
5882
Balazs Keri3b30d652018-10-19 13:32:20 +00005883ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5884 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5885 if (!ToTryLocOrErr)
5886 return ToTryLocOrErr.takeError();
5887
5888 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5889 if (!ToTryBlockOrErr)
5890 return ToTryBlockOrErr.takeError();
5891
Sean Callanan59721b32015-04-28 18:41:46 +00005892 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5893 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5894 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005895 if (auto ToHandlerOrErr = import(FromHandler))
5896 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005897 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005898 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005899 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005900
5901 return CXXTryStmt::Create(
5902 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005903}
5904
Balazs Keri3b30d652018-10-19 13:32:20 +00005905ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5906 auto Imp1 = importSeq(
5907 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5908 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5909 if (!Imp1)
5910 return Imp1.takeError();
5911 auto Imp2 = importSeq(
5912 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5913 if (!Imp2)
5914 return Imp2.takeError();
5915
5916 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5917 Expr *ToCond, *ToInc;
5918 Stmt *ToInit, *ToBody;
5919 std::tie(
5920 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5921 ToBody) = *Imp1;
5922 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5923 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5924
5925 return new (Importer.getToContext()) CXXForRangeStmt(
5926 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5927 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005928}
5929
Balazs Keri3b30d652018-10-19 13:32:20 +00005930ExpectedStmt
5931ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5932 auto Imp = importSeq(
5933 S->getElement(), S->getCollection(), S->getBody(),
5934 S->getForLoc(), S->getRParenLoc());
5935 if (!Imp)
5936 return Imp.takeError();
5937
5938 Stmt *ToElement, *ToBody;
5939 Expr *ToCollection;
5940 SourceLocation ToForLoc, ToRParenLoc;
5941 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5942
5943 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
5944 ToCollection,
5945 ToBody,
5946 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00005947 ToRParenLoc);
5948}
5949
Balazs Keri3b30d652018-10-19 13:32:20 +00005950ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5951 auto Imp = importSeq(
5952 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
5953 S->getCatchBody());
5954 if (!Imp)
5955 return Imp.takeError();
5956
5957 SourceLocation ToAtCatchLoc, ToRParenLoc;
5958 VarDecl *ToCatchParamDecl;
5959 Stmt *ToCatchBody;
5960 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
5961
5962 return new (Importer.getToContext()) ObjCAtCatchStmt (
5963 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00005964}
5965
Balazs Keri3b30d652018-10-19 13:32:20 +00005966ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5967 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
5968 if (!ToAtFinallyLocOrErr)
5969 return ToAtFinallyLocOrErr.takeError();
5970 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
5971 if (!ToAtFinallyStmtOrErr)
5972 return ToAtFinallyStmtOrErr.takeError();
5973 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
5974 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005975}
5976
Balazs Keri3b30d652018-10-19 13:32:20 +00005977ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5978 auto Imp = importSeq(
5979 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
5980 if (!Imp)
5981 return Imp.takeError();
5982
5983 SourceLocation ToAtTryLoc;
5984 Stmt *ToTryBody, *ToFinallyStmt;
5985 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
5986
Sean Callanan59721b32015-04-28 18:41:46 +00005987 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5988 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5989 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005990 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
5991 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005992 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005993 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005994 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005995
Sean Callanan59721b32015-04-28 18:41:46 +00005996 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005997 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00005998 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005999 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006000}
6001
Balazs Keri3b30d652018-10-19 13:32:20 +00006002ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006003 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006004 auto Imp = importSeq(
6005 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6006 if (!Imp)
6007 return Imp.takeError();
6008
6009 SourceLocation ToAtSynchronizedLoc;
6010 Expr *ToSynchExpr;
6011 Stmt *ToSynchBody;
6012 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6013
Sean Callanan59721b32015-04-28 18:41:46 +00006014 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6015 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6016}
6017
Balazs Keri3b30d652018-10-19 13:32:20 +00006018ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6019 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6020 if (!ToThrowLocOrErr)
6021 return ToThrowLocOrErr.takeError();
6022 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6023 if (!ToThrowExprOrErr)
6024 return ToThrowExprOrErr.takeError();
6025 return new (Importer.getToContext()) ObjCAtThrowStmt(
6026 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006027}
6028
Balazs Keri3b30d652018-10-19 13:32:20 +00006029ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6030 ObjCAutoreleasePoolStmt *S) {
6031 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6032 if (!ToAtLocOrErr)
6033 return ToAtLocOrErr.takeError();
6034 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6035 if (!ToSubStmtOrErr)
6036 return ToSubStmtOrErr.takeError();
6037 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6038 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006039}
6040
6041//----------------------------------------------------------------------------
6042// Import Expressions
6043//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006044ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006045 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6046 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006047 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006048}
6049
Balazs Keri3b30d652018-10-19 13:32:20 +00006050ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6051 auto Imp = importSeq(
6052 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6053 E->getRParenLoc(), E->getType());
6054 if (!Imp)
6055 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006056
Balazs Keri3b30d652018-10-19 13:32:20 +00006057 SourceLocation ToBuiltinLoc, ToRParenLoc;
6058 Expr *ToSubExpr;
6059 TypeSourceInfo *ToWrittenTypeInfo;
6060 QualType ToType;
6061 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6062 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006063
6064 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006065 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6066 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006067}
6068
Tom Roeder521f0042019-02-26 19:26:41 +00006069ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6070 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6071 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6072 if (!Imp)
6073 return Imp.takeError();
6074
6075 Expr *ToCond;
6076 Expr *ToLHS;
6077 Expr *ToRHS;
6078 SourceLocation ToBuiltinLoc, ToRParenLoc;
6079 QualType ToType;
6080 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6081
6082 ExprValueKind VK = E->getValueKind();
6083 ExprObjectKind OK = E->getObjectKind();
6084
6085 bool TypeDependent = ToCond->isTypeDependent();
6086 bool ValueDependent = ToCond->isValueDependent();
6087
6088 // The value of CondIsTrue only matters if the value is not
6089 // condition-dependent.
6090 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6091
6092 return new (Importer.getToContext())
6093 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6094 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6095}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006096
Balazs Keri3b30d652018-10-19 13:32:20 +00006097ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6098 ExpectedType TypeOrErr = import(E->getType());
6099 if (!TypeOrErr)
6100 return TypeOrErr.takeError();
6101
6102 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6103 if (!BeginLocOrErr)
6104 return BeginLocOrErr.takeError();
6105
6106 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006107}
6108
Balazs Keri3b30d652018-10-19 13:32:20 +00006109ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6110 auto Imp = importSeq(
6111 E->getBeginLoc(), E->getType(), E->getFunctionName());
6112 if (!Imp)
6113 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006114
Balazs Keri3b30d652018-10-19 13:32:20 +00006115 SourceLocation ToBeginLoc;
6116 QualType ToType;
6117 StringLiteral *ToFunctionName;
6118 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006119
Bruno Ricci17ff0262018-10-27 19:21:19 +00006120 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6121 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006122}
6123
Balazs Keri3b30d652018-10-19 13:32:20 +00006124ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6125 auto Imp = importSeq(
6126 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6127 E->getLocation(), E->getType());
6128 if (!Imp)
6129 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006130
Balazs Keri3b30d652018-10-19 13:32:20 +00006131 NestedNameSpecifierLoc ToQualifierLoc;
6132 SourceLocation ToTemplateKeywordLoc, ToLocation;
6133 ValueDecl *ToDecl;
6134 QualType ToType;
6135 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6136 *Imp;
6137
6138 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006139 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006140 auto FoundDOrErr = import(E->getFoundDecl());
6141 if (!FoundDOrErr)
6142 return FoundDOrErr.takeError();
6143 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006144 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006145
Aleksei Sidorina693b372016-09-28 10:16:56 +00006146 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006147 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006148 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006149 if (Error Err =
6150 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6151 return std::move(Err);
6152 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006153 }
6154
Balazs Keri3b30d652018-10-19 13:32:20 +00006155 auto *ToE = DeclRefExpr::Create(
6156 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6157 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6158 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006159 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006160 ToE->setHadMultipleCandidates(true);
6161 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006162}
6163
Balazs Keri3b30d652018-10-19 13:32:20 +00006164ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6165 ExpectedType TypeOrErr = import(E->getType());
6166 if (!TypeOrErr)
6167 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006168
Balazs Keri3b30d652018-10-19 13:32:20 +00006169 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006170}
6171
Balazs Keri3b30d652018-10-19 13:32:20 +00006172ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6173 ExpectedExpr ToInitOrErr = import(E->getInit());
6174 if (!ToInitOrErr)
6175 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006176
Balazs Keri3b30d652018-10-19 13:32:20 +00006177 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6178 if (!ToEqualOrColonLocOrErr)
6179 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006180
Balazs Keri3b30d652018-10-19 13:32:20 +00006181 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006182 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006183 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6184 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6185 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006186 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006187 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006188 }
6189
Balazs Keri3b30d652018-10-19 13:32:20 +00006190 SmallVector<Designator, 4> ToDesignators(E->size());
6191 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6192 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006193
6194 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006195 Importer.getToContext(), ToDesignators,
6196 ToIndexExprs, *ToEqualOrColonLocOrErr,
6197 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006198}
6199
Balazs Keri3b30d652018-10-19 13:32:20 +00006200ExpectedStmt
6201ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6202 ExpectedType ToTypeOrErr = import(E->getType());
6203 if (!ToTypeOrErr)
6204 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006205
Balazs Keri3b30d652018-10-19 13:32:20 +00006206 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6207 if (!ToLocationOrErr)
6208 return ToLocationOrErr.takeError();
6209
6210 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6211 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006212}
6213
Balazs Keri3b30d652018-10-19 13:32:20 +00006214ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6215 ExpectedType ToTypeOrErr = import(E->getType());
6216 if (!ToTypeOrErr)
6217 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006218
Balazs Keri3b30d652018-10-19 13:32:20 +00006219 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6220 if (!ToLocationOrErr)
6221 return ToLocationOrErr.takeError();
6222
6223 return IntegerLiteral::Create(
6224 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006225}
6226
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006227
Balazs Keri3b30d652018-10-19 13:32:20 +00006228ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6229 ExpectedType ToTypeOrErr = import(E->getType());
6230 if (!ToTypeOrErr)
6231 return ToTypeOrErr.takeError();
6232
6233 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6234 if (!ToLocationOrErr)
6235 return ToLocationOrErr.takeError();
6236
6237 return FloatingLiteral::Create(
6238 Importer.getToContext(), E->getValue(), E->isExact(),
6239 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006240}
6241
Balazs Keri3b30d652018-10-19 13:32:20 +00006242ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6243 auto ToTypeOrErr = import(E->getType());
6244 if (!ToTypeOrErr)
6245 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006246
Balazs Keri3b30d652018-10-19 13:32:20 +00006247 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6248 if (!ToSubExprOrErr)
6249 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006250
Balazs Keri3b30d652018-10-19 13:32:20 +00006251 return new (Importer.getToContext()) ImaginaryLiteral(
6252 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006253}
6254
Balazs Keri3b30d652018-10-19 13:32:20 +00006255ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6256 ExpectedType ToTypeOrErr = import(E->getType());
6257 if (!ToTypeOrErr)
6258 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006259
Balazs Keri3b30d652018-10-19 13:32:20 +00006260 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6261 if (!ToLocationOrErr)
6262 return ToLocationOrErr.takeError();
6263
6264 return new (Importer.getToContext()) CharacterLiteral(
6265 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006266}
6267
Balazs Keri3b30d652018-10-19 13:32:20 +00006268ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6269 ExpectedType ToTypeOrErr = import(E->getType());
6270 if (!ToTypeOrErr)
6271 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006272
Balazs Keri3b30d652018-10-19 13:32:20 +00006273 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6274 if (Error Err = ImportArrayChecked(
6275 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6276 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006277
Balazs Keri3b30d652018-10-19 13:32:20 +00006278 return StringLiteral::Create(
6279 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6280 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006281}
6282
Balazs Keri3b30d652018-10-19 13:32:20 +00006283ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6284 auto Imp = importSeq(
6285 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6286 E->getInitializer());
6287 if (!Imp)
6288 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006289
Balazs Keri3b30d652018-10-19 13:32:20 +00006290 SourceLocation ToLParenLoc;
6291 TypeSourceInfo *ToTypeSourceInfo;
6292 QualType ToType;
6293 Expr *ToInitializer;
6294 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006295
6296 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006297 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6298 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006299}
6300
Balazs Keri3b30d652018-10-19 13:32:20 +00006301ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6302 auto Imp = importSeq(
6303 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6304 if (!Imp)
6305 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006306
Balazs Keri3b30d652018-10-19 13:32:20 +00006307 SourceLocation ToBuiltinLoc, ToRParenLoc;
6308 QualType ToType;
6309 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6310
6311 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6312 if (Error Err = ImportArrayChecked(
6313 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6314 ToExprs.begin()))
6315 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006316
6317 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006318 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006319}
6320
Balazs Keri3b30d652018-10-19 13:32:20 +00006321ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6322 auto Imp = importSeq(
6323 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6324 if (!Imp)
6325 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006326
Balazs Keri3b30d652018-10-19 13:32:20 +00006327 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6328 LabelDecl *ToLabel;
6329 QualType ToType;
6330 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006331
6332 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006333 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006334}
6335
Bill Wendling8003edc2018-11-09 00:41:36 +00006336ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6337 auto Imp = importSeq(E->getSubExpr());
6338 if (!Imp)
6339 return Imp.takeError();
6340
6341 Expr *ToSubExpr;
6342 std::tie(ToSubExpr) = *Imp;
6343
Fangrui Song407659a2018-11-30 23:41:18 +00006344 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006345}
6346
Balazs Keri3b30d652018-10-19 13:32:20 +00006347ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6348 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6349 if (!Imp)
6350 return Imp.takeError();
6351
6352 SourceLocation ToLParen, ToRParen;
6353 Expr *ToSubExpr;
6354 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006355
Fangrui Song6907ce22018-07-30 19:24:48 +00006356 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006357 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006358}
6359
Balazs Keri3b30d652018-10-19 13:32:20 +00006360ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6361 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6362 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6363 return std::move(Err);
6364
6365 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6366 if (!ToLParenLocOrErr)
6367 return ToLParenLocOrErr.takeError();
6368
6369 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6370 if (!ToRParenLocOrErr)
6371 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006372
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006373 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6374 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006375}
6376
Balazs Keri3b30d652018-10-19 13:32:20 +00006377ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6378 auto Imp = importSeq(
6379 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6380 if (!Imp)
6381 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006382
Balazs Keri3b30d652018-10-19 13:32:20 +00006383 CompoundStmt *ToSubStmt;
6384 QualType ToType;
6385 SourceLocation ToLParenLoc, ToRParenLoc;
6386 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006387
Balazs Keri3b30d652018-10-19 13:32:20 +00006388 return new (Importer.getToContext()) StmtExpr(
6389 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006390}
6391
Balazs Keri3b30d652018-10-19 13:32:20 +00006392ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6393 auto Imp = importSeq(
6394 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6395 if (!Imp)
6396 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006397
Balazs Keri3b30d652018-10-19 13:32:20 +00006398 Expr *ToSubExpr;
6399 QualType ToType;
6400 SourceLocation ToOperatorLoc;
6401 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006402
Aaron Ballmana5038552018-01-09 13:07:03 +00006403 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006404 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6405 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006406}
6407
Balazs Keri3b30d652018-10-19 13:32:20 +00006408ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006409ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006410 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6411 if (!Imp)
6412 return Imp.takeError();
6413
6414 QualType ToType;
6415 SourceLocation ToOperatorLoc, ToRParenLoc;
6416 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006417
Douglas Gregord8552cd2010-02-19 01:24:23 +00006418 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006419 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6420 import(E->getArgumentTypeInfo());
6421 if (!ToArgumentTypeInfoOrErr)
6422 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006423
Balazs Keri3b30d652018-10-19 13:32:20 +00006424 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6425 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6426 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006427 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006428
Balazs Keri3b30d652018-10-19 13:32:20 +00006429 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6430 if (!ToArgumentExprOrErr)
6431 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006432
Balazs Keri3b30d652018-10-19 13:32:20 +00006433 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6434 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006435}
6436
Balazs Keri3b30d652018-10-19 13:32:20 +00006437ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6438 auto Imp = importSeq(
6439 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6440 if (!Imp)
6441 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006442
Balazs Keri3b30d652018-10-19 13:32:20 +00006443 Expr *ToLHS, *ToRHS;
6444 QualType ToType;
6445 SourceLocation ToOperatorLoc;
6446 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006447
Balazs Keri3b30d652018-10-19 13:32:20 +00006448 return new (Importer.getToContext()) BinaryOperator(
6449 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6450 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006451}
6452
Balazs Keri3b30d652018-10-19 13:32:20 +00006453ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6454 auto Imp = importSeq(
6455 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6456 E->getRHS(), E->getType());
6457 if (!Imp)
6458 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006459
Balazs Keri3b30d652018-10-19 13:32:20 +00006460 Expr *ToCond, *ToLHS, *ToRHS;
6461 SourceLocation ToQuestionLoc, ToColonLoc;
6462 QualType ToType;
6463 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006464
6465 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006466 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6467 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006468}
6469
Balazs Keri3b30d652018-10-19 13:32:20 +00006470ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006471 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006472 auto Imp = importSeq(
6473 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6474 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6475 if (!Imp)
6476 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006477
Balazs Keri3b30d652018-10-19 13:32:20 +00006478 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6479 OpaqueValueExpr *ToOpaqueValue;
6480 SourceLocation ToQuestionLoc, ToColonLoc;
6481 QualType ToType;
6482 std::tie(
6483 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6484 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006485
6486 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006487 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6488 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6489 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006490}
6491
Balazs Keri3b30d652018-10-19 13:32:20 +00006492ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6493 auto Imp = importSeq(
6494 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6495 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6496 if (!Imp)
6497 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006498
Balazs Keri3b30d652018-10-19 13:32:20 +00006499 SourceLocation ToBeginLoc, ToEndLoc;
6500 TypeSourceInfo *ToQueriedTypeSourceInfo;
6501 Expr *ToDimensionExpression;
6502 QualType ToType;
6503 std::tie(
6504 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6505 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006506
6507 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006508 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6509 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006510}
6511
Balazs Keri3b30d652018-10-19 13:32:20 +00006512ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6513 auto Imp = importSeq(
6514 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6515 if (!Imp)
6516 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006517
Balazs Keri3b30d652018-10-19 13:32:20 +00006518 SourceLocation ToBeginLoc, ToEndLoc;
6519 Expr *ToQueriedExpression;
6520 QualType ToType;
6521 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006522
6523 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006524 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6525 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006526}
6527
Balazs Keri3b30d652018-10-19 13:32:20 +00006528ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6529 auto Imp = importSeq(
6530 E->getLocation(), E->getType(), E->getSourceExpr());
6531 if (!Imp)
6532 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006533
Balazs Keri3b30d652018-10-19 13:32:20 +00006534 SourceLocation ToLocation;
6535 QualType ToType;
6536 Expr *ToSourceExpr;
6537 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006538
6539 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006540 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006541}
6542
Balazs Keri3b30d652018-10-19 13:32:20 +00006543ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6544 auto Imp = importSeq(
6545 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6546 if (!Imp)
6547 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006548
Balazs Keri3b30d652018-10-19 13:32:20 +00006549 Expr *ToLHS, *ToRHS;
6550 SourceLocation ToRBracketLoc;
6551 QualType ToType;
6552 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006553
6554 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006555 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6556 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006557}
6558
Balazs Keri3b30d652018-10-19 13:32:20 +00006559ExpectedStmt
6560ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6561 auto Imp = importSeq(
6562 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6563 E->getComputationResultType(), E->getOperatorLoc());
6564 if (!Imp)
6565 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006566
Balazs Keri3b30d652018-10-19 13:32:20 +00006567 Expr *ToLHS, *ToRHS;
6568 QualType ToType, ToComputationLHSType, ToComputationResultType;
6569 SourceLocation ToOperatorLoc;
6570 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6571 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006572
Balazs Keri3b30d652018-10-19 13:32:20 +00006573 return new (Importer.getToContext()) CompoundAssignOperator(
6574 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6575 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6576 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006577}
6578
Balazs Keri3b30d652018-10-19 13:32:20 +00006579Expected<CXXCastPath>
6580ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6581 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006582 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006583 if (auto SpecOrErr = import(*I))
6584 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006585 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006586 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006587 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006588 return Path;
John McCallcf142162010-08-07 06:22:56 +00006589}
6590
Balazs Keri3b30d652018-10-19 13:32:20 +00006591ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6592 ExpectedType ToTypeOrErr = import(E->getType());
6593 if (!ToTypeOrErr)
6594 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006595
Balazs Keri3b30d652018-10-19 13:32:20 +00006596 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6597 if (!ToSubExprOrErr)
6598 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006599
Balazs Keri3b30d652018-10-19 13:32:20 +00006600 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6601 if (!ToBasePathOrErr)
6602 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006603
Balazs Keri3b30d652018-10-19 13:32:20 +00006604 return ImplicitCastExpr::Create(
6605 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6606 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006607}
6608
Balazs Keri3b30d652018-10-19 13:32:20 +00006609ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6610 auto Imp1 = importSeq(
6611 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6612 if (!Imp1)
6613 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006614
Balazs Keri3b30d652018-10-19 13:32:20 +00006615 QualType ToType;
6616 Expr *ToSubExpr;
6617 TypeSourceInfo *ToTypeInfoAsWritten;
6618 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006619
Balazs Keri3b30d652018-10-19 13:32:20 +00006620 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6621 if (!ToBasePathOrErr)
6622 return ToBasePathOrErr.takeError();
6623 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006624
Aleksei Sidorina693b372016-09-28 10:16:56 +00006625 switch (E->getStmtClass()) {
6626 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006627 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006628 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6629 if (!ToLParenLocOrErr)
6630 return ToLParenLocOrErr.takeError();
6631 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6632 if (!ToRParenLocOrErr)
6633 return ToRParenLocOrErr.takeError();
6634 return CStyleCastExpr::Create(
6635 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6636 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6637 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006638 }
6639
6640 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006641 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006642 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6643 if (!ToLParenLocOrErr)
6644 return ToLParenLocOrErr.takeError();
6645 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6646 if (!ToRParenLocOrErr)
6647 return ToRParenLocOrErr.takeError();
6648 return CXXFunctionalCastExpr::Create(
6649 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6650 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6651 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006652 }
6653
6654 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006655 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6656 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6657 if (!ToLParenLocOrErr)
6658 return ToLParenLocOrErr.takeError();
6659 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6660 if (!ToBridgeKeywordLocOrErr)
6661 return ToBridgeKeywordLocOrErr.takeError();
6662 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6663 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6664 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006665 }
6666 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006667 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006668 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006669 }
6670}
6671
Balazs Keri3b30d652018-10-19 13:32:20 +00006672ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6673 SmallVector<OffsetOfNode, 4> ToNodes;
6674 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6675 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006676
Balazs Keri3b30d652018-10-19 13:32:20 +00006677 SourceLocation ToBeginLoc, ToEndLoc;
6678 if (FromNode.getKind() != OffsetOfNode::Base) {
6679 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6680 if (!Imp)
6681 return Imp.takeError();
6682 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6683 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006684
Balazs Keri3b30d652018-10-19 13:32:20 +00006685 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006686 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006687 ToNodes.push_back(
6688 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006689 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006690 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006691 auto ToBSOrErr = import(FromNode.getBase());
6692 if (!ToBSOrErr)
6693 return ToBSOrErr.takeError();
6694 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006695 break;
6696 }
6697 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006698 auto ToFieldOrErr = import(FromNode.getField());
6699 if (!ToFieldOrErr)
6700 return ToFieldOrErr.takeError();
6701 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006702 break;
6703 }
6704 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006705 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6706 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006707 break;
6708 }
6709 }
6710 }
6711
Balazs Keri3b30d652018-10-19 13:32:20 +00006712 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6713 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6714 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6715 if (!ToIndexExprOrErr)
6716 return ToIndexExprOrErr.takeError();
6717 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006718 }
6719
Balazs Keri3b30d652018-10-19 13:32:20 +00006720 auto Imp = importSeq(
6721 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6722 E->getRParenLoc());
6723 if (!Imp)
6724 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006725
Balazs Keri3b30d652018-10-19 13:32:20 +00006726 QualType ToType;
6727 TypeSourceInfo *ToTypeSourceInfo;
6728 SourceLocation ToOperatorLoc, ToRParenLoc;
6729 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6730
6731 return OffsetOfExpr::Create(
6732 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6733 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006734}
6735
Balazs Keri3b30d652018-10-19 13:32:20 +00006736ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6737 auto Imp = importSeq(
6738 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6739 if (!Imp)
6740 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006741
Balazs Keri3b30d652018-10-19 13:32:20 +00006742 QualType ToType;
6743 Expr *ToOperand;
6744 SourceLocation ToBeginLoc, ToEndLoc;
6745 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006746
Balazs Keri3b30d652018-10-19 13:32:20 +00006747 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006748 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006749 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006750 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006751 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006752
Balazs Keri3b30d652018-10-19 13:32:20 +00006753 return new (Importer.getToContext()) CXXNoexceptExpr(
6754 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006755}
6756
Balazs Keri3b30d652018-10-19 13:32:20 +00006757ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6758 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6759 if (!Imp)
6760 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006761
Balazs Keri3b30d652018-10-19 13:32:20 +00006762 Expr *ToSubExpr;
6763 QualType ToType;
6764 SourceLocation ToThrowLoc;
6765 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006766
6767 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006768 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006769}
6770
Balazs Keri3b30d652018-10-19 13:32:20 +00006771ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6772 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6773 if (!ToUsedLocOrErr)
6774 return ToUsedLocOrErr.takeError();
6775
6776 auto ToParamOrErr = import(E->getParam());
6777 if (!ToParamOrErr)
6778 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006779
6780 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006781 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006782}
6783
Balazs Keri3b30d652018-10-19 13:32:20 +00006784ExpectedStmt
6785ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6786 auto Imp = importSeq(
6787 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6788 if (!Imp)
6789 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006790
Balazs Keri3b30d652018-10-19 13:32:20 +00006791 QualType ToType;
6792 TypeSourceInfo *ToTypeSourceInfo;
6793 SourceLocation ToRParenLoc;
6794 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006795
6796 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006797 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006798}
6799
Balazs Keri3b30d652018-10-19 13:32:20 +00006800ExpectedStmt
6801ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6802 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6803 if (!ToSubExprOrErr)
6804 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006805
Balazs Keri3b30d652018-10-19 13:32:20 +00006806 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6807 if (!ToDtorOrErr)
6808 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006809
6810 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006811 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6812 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006813}
6814
Balazs Keri3b30d652018-10-19 13:32:20 +00006815ExpectedStmt
6816ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6817 auto Imp = importSeq(
6818 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6819 E->getParenOrBraceRange());
6820 if (!Imp)
6821 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006822
Balazs Keri3b30d652018-10-19 13:32:20 +00006823 CXXConstructorDecl *ToConstructor;
6824 QualType ToType;
6825 TypeSourceInfo *ToTypeSourceInfo;
6826 SourceRange ToParenOrBraceRange;
6827 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006828
Balazs Keri3b30d652018-10-19 13:32:20 +00006829 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6830 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6831 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006832
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006833 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006834 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6835 ToParenOrBraceRange, E->hadMultipleCandidates(),
6836 E->isListInitialization(), E->isStdInitListInitialization(),
6837 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006838}
6839
Balazs Keri3b30d652018-10-19 13:32:20 +00006840ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006841ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006842 auto Imp = importSeq(
6843 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6844 if (!Imp)
6845 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006846
Balazs Keri3b30d652018-10-19 13:32:20 +00006847 QualType ToType;
6848 Expr *ToTemporaryExpr;
6849 const ValueDecl *ToExtendingDecl;
6850 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006851
6852 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006853 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006854
6855 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006856 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006857 return ToMTE;
6858}
6859
Balazs Keri3b30d652018-10-19 13:32:20 +00006860ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6861 auto Imp = importSeq(
6862 E->getType(), E->getPattern(), E->getEllipsisLoc());
6863 if (!Imp)
6864 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006865
Balazs Keri3b30d652018-10-19 13:32:20 +00006866 QualType ToType;
6867 Expr *ToPattern;
6868 SourceLocation ToEllipsisLoc;
6869 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006870
6871 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006872 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006873}
6874
Balazs Keri3b30d652018-10-19 13:32:20 +00006875ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6876 auto Imp = importSeq(
6877 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6878 if (!Imp)
6879 return Imp.takeError();
6880
6881 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6882 NamedDecl *ToPack;
6883 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006884
6885 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006886 if (!E->isValueDependent())
6887 Length = E->getPackLength();
6888
Balazs Keri3b30d652018-10-19 13:32:20 +00006889 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006890 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006891 if (Error Err = ImportTemplateArguments(
6892 E->getPartialArguments().data(),
6893 E->getPartialArguments().size(),
6894 ToPartialArguments))
6895 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006896 }
6897
6898 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006899 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6900 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006901}
6902
Aleksei Sidorina693b372016-09-28 10:16:56 +00006903
Balazs Keri3b30d652018-10-19 13:32:20 +00006904ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6905 auto Imp = importSeq(
6906 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6907 E->getArraySize(), E->getInitializer(), E->getType(),
6908 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6909 E->getDirectInitRange());
6910 if (!Imp)
6911 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006912
Balazs Keri3b30d652018-10-19 13:32:20 +00006913 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6914 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00006915 Optional<Expr *> ToArraySize;
6916 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00006917 QualType ToType;
6918 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6919 std::tie(
6920 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6921 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006922
Balazs Keri3b30d652018-10-19 13:32:20 +00006923 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6924 if (Error Err =
6925 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6926 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006927
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006928 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006929 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6930 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6931 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6932 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6933 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006934}
6935
Balazs Keri3b30d652018-10-19 13:32:20 +00006936ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6937 auto Imp = importSeq(
6938 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6939 if (!Imp)
6940 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006941
Balazs Keri3b30d652018-10-19 13:32:20 +00006942 QualType ToType;
6943 FunctionDecl *ToOperatorDelete;
6944 Expr *ToArgument;
6945 SourceLocation ToBeginLoc;
6946 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006947
6948 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006949 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6950 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6951 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006952}
6953
Balazs Keri3b30d652018-10-19 13:32:20 +00006954ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6955 auto Imp = importSeq(
6956 E->getType(), E->getLocation(), E->getConstructor(),
6957 E->getParenOrBraceRange());
6958 if (!Imp)
6959 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006960
Balazs Keri3b30d652018-10-19 13:32:20 +00006961 QualType ToType;
6962 SourceLocation ToLocation;
6963 CXXConstructorDecl *ToConstructor;
6964 SourceRange ToParenOrBraceRange;
6965 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006966
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006967 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006968 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6969 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006970
Balazs Keri3b30d652018-10-19 13:32:20 +00006971 return CXXConstructExpr::Create(
6972 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6973 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6974 E->isListInitialization(), E->isStdInitListInitialization(),
6975 E->requiresZeroInitialization(), E->getConstructionKind(),
6976 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006977}
6978
Balazs Keri3b30d652018-10-19 13:32:20 +00006979ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
6980 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6981 if (!ToSubExprOrErr)
6982 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006983
Balazs Keri3b30d652018-10-19 13:32:20 +00006984 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
6985 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
6986 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006987
Balazs Keri3b30d652018-10-19 13:32:20 +00006988 return ExprWithCleanups::Create(
6989 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
6990 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006991}
6992
Balazs Keri3b30d652018-10-19 13:32:20 +00006993ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6994 auto Imp = importSeq(
6995 E->getCallee(), E->getType(), E->getRParenLoc());
6996 if (!Imp)
6997 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006998
Balazs Keri3b30d652018-10-19 13:32:20 +00006999 Expr *ToCallee;
7000 QualType ToType;
7001 SourceLocation ToRParenLoc;
7002 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007003
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007004 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007005 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7006 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007007
Bruno Riccic5885cf2018-12-21 15:20:32 +00007008 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7009 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007010}
7011
Balazs Keri3b30d652018-10-19 13:32:20 +00007012ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7013 ExpectedType ToTypeOrErr = import(E->getType());
7014 if (!ToTypeOrErr)
7015 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007016
Balazs Keri3b30d652018-10-19 13:32:20 +00007017 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7018 if (!ToLocationOrErr)
7019 return ToLocationOrErr.takeError();
7020
7021 return new (Importer.getToContext()) CXXThisExpr(
7022 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007023}
7024
Balazs Keri3b30d652018-10-19 13:32:20 +00007025ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7026 ExpectedType ToTypeOrErr = import(E->getType());
7027 if (!ToTypeOrErr)
7028 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007029
Balazs Keri3b30d652018-10-19 13:32:20 +00007030 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7031 if (!ToLocationOrErr)
7032 return ToLocationOrErr.takeError();
7033
7034 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7035 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007036}
7037
Balazs Keri3b30d652018-10-19 13:32:20 +00007038ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7039 auto Imp1 = importSeq(
7040 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7041 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7042 if (!Imp1)
7043 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007044
Balazs Keri3b30d652018-10-19 13:32:20 +00007045 Expr *ToBase;
7046 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7047 NestedNameSpecifierLoc ToQualifierLoc;
7048 ValueDecl *ToMemberDecl;
7049 QualType ToType;
7050 std::tie(
7051 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7052 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007053
Balazs Keri3b30d652018-10-19 13:32:20 +00007054 auto Imp2 = importSeq(
7055 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7056 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7057 if (!Imp2)
7058 return Imp2.takeError();
7059 NamedDecl *ToDecl;
7060 DeclarationName ToName;
7061 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7062 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007063
7064 DeclAccessPair ToFoundDecl =
7065 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007066
Balazs Keri3b30d652018-10-19 13:32:20 +00007067 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007068
Gabor Marton5caba302019-03-07 13:38:20 +00007069 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007070 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007071 if (Error Err =
7072 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7073 E->template_arguments(), ToTAInfo))
7074 return std::move(Err);
7075 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007076 }
7077
Balazs Keri3b30d652018-10-19 13:32:20 +00007078 return MemberExpr::Create(
7079 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7080 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
Gabor Marton5caba302019-03-07 13:38:20 +00007081 ToMemberNameInfo, ResInfo, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007082}
7083
Balazs Keri3b30d652018-10-19 13:32:20 +00007084ExpectedStmt
7085ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7086 auto Imp = importSeq(
7087 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7088 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7089 if (!Imp)
7090 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007091
Balazs Keri3b30d652018-10-19 13:32:20 +00007092 Expr *ToBase;
7093 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7094 NestedNameSpecifierLoc ToQualifierLoc;
7095 TypeSourceInfo *ToScopeTypeInfo;
7096 std::tie(
7097 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7098 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007099
7100 PseudoDestructorTypeStorage Storage;
7101 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7102 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007103 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7104 if (!ToDestroyedTypeLocOrErr)
7105 return ToDestroyedTypeLocOrErr.takeError();
7106 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007107 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007108 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7109 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7110 else
7111 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007112 }
7113
7114 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007115 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7116 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007117}
7118
Balazs Keri3b30d652018-10-19 13:32:20 +00007119ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007120 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007121 auto Imp = importSeq(
7122 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7123 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7124 if (!Imp)
7125 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007126
Balazs Keri3b30d652018-10-19 13:32:20 +00007127 QualType ToType;
7128 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7129 NestedNameSpecifierLoc ToQualifierLoc;
7130 NamedDecl *ToFirstQualifierFoundInScope;
7131 std::tie(
7132 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7133 ToFirstQualifierFoundInScope) = *Imp;
7134
7135 Expr *ToBase = nullptr;
7136 if (!E->isImplicitAccess()) {
7137 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7138 ToBase = *ToBaseOrErr;
7139 else
7140 return ToBaseOrErr.takeError();
7141 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007142
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007143 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007144 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007145 if (Error Err = ImportTemplateArgumentListInfo(
7146 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7147 ToTAInfo))
7148 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007149 ResInfo = &ToTAInfo;
7150 }
7151
Balazs Keri3b30d652018-10-19 13:32:20 +00007152 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7153 if (!ToMemberNameInfoOrErr)
7154 return ToMemberNameInfoOrErr.takeError();
7155 DeclarationNameInfo ToMemberNameInfo(
7156 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007157 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007158 if (Error Err = ImportDeclarationNameLoc(
7159 E->getMemberNameInfo(), ToMemberNameInfo))
7160 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007161
7162 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007163 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7164 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7165 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007166}
7167
Balazs Keri3b30d652018-10-19 13:32:20 +00007168ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007169ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007170 auto Imp = importSeq(
7171 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7172 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7173 if (!Imp)
7174 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007175
Balazs Keri3b30d652018-10-19 13:32:20 +00007176 NestedNameSpecifierLoc ToQualifierLoc;
7177 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7178 DeclarationName ToDeclName;
7179 std::tie(
7180 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7181 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007182
Balazs Keri3b30d652018-10-19 13:32:20 +00007183 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7184 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7185 return std::move(Err);
7186
7187 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007188 TemplateArgumentListInfo *ResInfo = nullptr;
7189 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007190 if (Error Err =
7191 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7192 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007193 ResInfo = &ToTAInfo;
7194 }
7195
7196 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007197 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7198 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007199}
7200
Balazs Keri3b30d652018-10-19 13:32:20 +00007201ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7202 CXXUnresolvedConstructExpr *E) {
7203 auto Imp = importSeq(
7204 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7205 if (!Imp)
7206 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007207
Balazs Keri3b30d652018-10-19 13:32:20 +00007208 SourceLocation ToLParenLoc, ToRParenLoc;
7209 TypeSourceInfo *ToTypeSourceInfo;
7210 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7211
7212 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7213 if (Error Err =
7214 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7215 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007216
7217 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007218 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7219 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007220}
7221
Balazs Keri3b30d652018-10-19 13:32:20 +00007222ExpectedStmt
7223ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7224 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7225 if (!ToNamingClassOrErr)
7226 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007227
Balazs Keri3b30d652018-10-19 13:32:20 +00007228 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7229 if (!ToQualifierLocOrErr)
7230 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007231
Balazs Keri3b30d652018-10-19 13:32:20 +00007232 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7233 if (!ToNameInfoOrErr)
7234 return ToNameInfoOrErr.takeError();
7235 DeclarationNameInfo ToNameInfo(
7236 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007237 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007238 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7239 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007240
7241 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007242 for (auto *D : E->decls())
7243 if (auto ToDOrErr = import(D))
7244 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007245 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007246 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007247
Balazs Keri3b30d652018-10-19 13:32:20 +00007248 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7249 TemplateArgumentListInfo ToTAInfo;
7250 if (Error Err = ImportTemplateArgumentListInfo(
7251 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7252 ToTAInfo))
7253 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007254
Balazs Keri3b30d652018-10-19 13:32:20 +00007255 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7256 if (!ToTemplateKeywordLocOrErr)
7257 return ToTemplateKeywordLocOrErr.takeError();
7258
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007259 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007260 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7261 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7262 ToDecls.begin(), ToDecls.end());
7263 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007264
7265 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007266 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7267 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7268 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007269}
7270
Balazs Keri3b30d652018-10-19 13:32:20 +00007271ExpectedStmt
7272ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7273 auto Imp1 = importSeq(
7274 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7275 E->getTemplateKeywordLoc());
7276 if (!Imp1)
7277 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007278
Balazs Keri3b30d652018-10-19 13:32:20 +00007279 QualType ToType;
7280 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7281 NestedNameSpecifierLoc ToQualifierLoc;
7282 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7283
7284 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7285 if (!Imp2)
7286 return Imp2.takeError();
7287 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7288 // Import additional name location/type info.
7289 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7290 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007291
7292 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007293 for (Decl *D : E->decls())
7294 if (auto ToDOrErr = import(D))
7295 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007296 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007297 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007298
7299 TemplateArgumentListInfo ToTAInfo;
7300 TemplateArgumentListInfo *ResInfo = nullptr;
7301 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007302 if (Error Err =
7303 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7304 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007305 ResInfo = &ToTAInfo;
7306 }
7307
Balazs Keri3b30d652018-10-19 13:32:20 +00007308 Expr *ToBase = nullptr;
7309 if (!E->isImplicitAccess()) {
7310 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7311 ToBase = *ToBaseOrErr;
7312 else
7313 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007314 }
7315
7316 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007317 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7318 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7319 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007320}
7321
Balazs Keri3b30d652018-10-19 13:32:20 +00007322ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7323 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7324 if (!Imp)
7325 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007326
Balazs Keri3b30d652018-10-19 13:32:20 +00007327 Expr *ToCallee;
7328 QualType ToType;
7329 SourceLocation ToRParenLoc;
7330 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007331
7332 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007333 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7334 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7335 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007336
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007337 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007338 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007339 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007340 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7341 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007342 }
7343
Bruno Riccic5885cf2018-12-21 15:20:32 +00007344 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7345 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7346 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007347}
7348
Balazs Keri3b30d652018-10-19 13:32:20 +00007349ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7350 CXXRecordDecl *FromClass = E->getLambdaClass();
7351 auto ToClassOrErr = import(FromClass);
7352 if (!ToClassOrErr)
7353 return ToClassOrErr.takeError();
7354 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007355
7356 // NOTE: lambda classes are created with BeingDefined flag set up.
7357 // It means that ImportDefinition doesn't work for them and we should fill it
7358 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007359 if (ToClass->isBeingDefined())
7360 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7361 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007362
Balazs Keri3b30d652018-10-19 13:32:20 +00007363 auto ToCallOpOrErr = import(E->getCallOperator());
7364 if (!ToCallOpOrErr)
7365 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007366
7367 ToClass->completeDefinition();
7368
Balazs Keri3b30d652018-10-19 13:32:20 +00007369 SmallVector<LambdaCapture, 8> ToCaptures;
7370 ToCaptures.reserve(E->capture_size());
7371 for (const auto &FromCapture : E->captures()) {
7372 if (auto ToCaptureOrErr = import(FromCapture))
7373 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007374 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007375 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007376 }
7377
Balazs Keri3b30d652018-10-19 13:32:20 +00007378 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7379 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7380 return std::move(Err);
7381
7382 auto Imp = importSeq(
7383 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7384 if (!Imp)
7385 return Imp.takeError();
7386
7387 SourceRange ToIntroducerRange;
7388 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7389 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007390
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007391 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007392 Importer.getToContext(), ToClass, ToIntroducerRange,
7393 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7394 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7395 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007396}
7397
Sean Callanan8bca9962016-03-28 21:43:01 +00007398
Balazs Keri3b30d652018-10-19 13:32:20 +00007399ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7400 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7401 if (!Imp)
7402 return Imp.takeError();
7403
7404 SourceLocation ToLBraceLoc, ToRBraceLoc;
7405 QualType ToType;
7406 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7407
7408 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7409 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7410 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007411
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007412 ASTContext &ToCtx = Importer.getToContext();
7413 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007414 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7415 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007416
Balazs Keri3b30d652018-10-19 13:32:20 +00007417 if (E->hasArrayFiller()) {
7418 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7419 To->setArrayFiller(*ToFillerOrErr);
7420 else
7421 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007422 }
7423
Balazs Keri3b30d652018-10-19 13:32:20 +00007424 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7425 if (auto ToFDOrErr = import(FromFD))
7426 To->setInitializedFieldInUnion(*ToFDOrErr);
7427 else
7428 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007429 }
7430
Balazs Keri3b30d652018-10-19 13:32:20 +00007431 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7432 if (auto ToSyntFormOrErr = import(SyntForm))
7433 To->setSyntacticForm(*ToSyntFormOrErr);
7434 else
7435 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007436 }
7437
Gabor Martona20ce602018-09-03 13:10:53 +00007438 // Copy InitListExprBitfields, which are not handled in the ctor of
7439 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007440 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007441
7442 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007443}
7444
Balazs Keri3b30d652018-10-19 13:32:20 +00007445ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007446 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007447 ExpectedType ToTypeOrErr = import(E->getType());
7448 if (!ToTypeOrErr)
7449 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007450
Balazs Keri3b30d652018-10-19 13:32:20 +00007451 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7452 if (!ToSubExprOrErr)
7453 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007454
Balazs Keri3b30d652018-10-19 13:32:20 +00007455 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7456 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007457}
7458
Balazs Keri3b30d652018-10-19 13:32:20 +00007459ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007460 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007461 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7462 if (!Imp)
7463 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007464
Balazs Keri3b30d652018-10-19 13:32:20 +00007465 SourceLocation ToLocation;
7466 QualType ToType;
7467 CXXConstructorDecl *ToConstructor;
7468 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007469
7470 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007471 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7472 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007473}
7474
Balazs Keri3b30d652018-10-19 13:32:20 +00007475ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7476 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7477 if (!Imp)
7478 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007479
Balazs Keri3b30d652018-10-19 13:32:20 +00007480 QualType ToType;
7481 Expr *ToCommonExpr, *ToSubExpr;
7482 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007483
Balazs Keri3b30d652018-10-19 13:32:20 +00007484 return new (Importer.getToContext()) ArrayInitLoopExpr(
7485 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007486}
7487
Balazs Keri3b30d652018-10-19 13:32:20 +00007488ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7489 ExpectedType ToTypeOrErr = import(E->getType());
7490 if (!ToTypeOrErr)
7491 return ToTypeOrErr.takeError();
7492 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007493}
7494
Balazs Keri3b30d652018-10-19 13:32:20 +00007495ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7496 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7497 if (!ToBeginLocOrErr)
7498 return ToBeginLocOrErr.takeError();
7499
7500 auto ToFieldOrErr = import(E->getField());
7501 if (!ToFieldOrErr)
7502 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007503
7504 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007505 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007506}
7507
Balazs Keri3b30d652018-10-19 13:32:20 +00007508ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7509 auto Imp = importSeq(
7510 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7511 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7512 if (!Imp)
7513 return Imp.takeError();
7514
7515 QualType ToType;
7516 Expr *ToSubExpr;
7517 TypeSourceInfo *ToTypeInfoAsWritten;
7518 SourceLocation ToOperatorLoc, ToRParenLoc;
7519 SourceRange ToAngleBrackets;
7520 std::tie(
7521 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7522 ToAngleBrackets) = *Imp;
7523
Sean Callanandd2c1742016-05-16 20:48:03 +00007524 ExprValueKind VK = E->getValueKind();
7525 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007526 auto ToBasePathOrErr = ImportCastPath(E);
7527 if (!ToBasePathOrErr)
7528 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007529
Sean Callanandd2c1742016-05-16 20:48:03 +00007530 if (isa<CXXStaticCastExpr>(E)) {
7531 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007532 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7533 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007534 } else if (isa<CXXDynamicCastExpr>(E)) {
7535 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007536 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7537 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007538 } else if (isa<CXXReinterpretCastExpr>(E)) {
7539 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007540 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7541 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007542 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007543 return CXXConstCastExpr::Create(
7544 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7545 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007546 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007547 llvm_unreachable("Unknown cast type");
7548 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007549 }
7550}
7551
Balazs Keri3b30d652018-10-19 13:32:20 +00007552ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007553 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007554 auto Imp = importSeq(
7555 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7556 if (!Imp)
7557 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007558
Balazs Keri3b30d652018-10-19 13:32:20 +00007559 QualType ToType;
7560 SourceLocation ToExprLoc;
7561 NonTypeTemplateParmDecl *ToParameter;
7562 Expr *ToReplacement;
7563 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007564
7565 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007566 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007567}
7568
Balazs Keri3b30d652018-10-19 13:32:20 +00007569ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7570 auto Imp = importSeq(
7571 E->getType(), E->getBeginLoc(), E->getEndLoc());
7572 if (!Imp)
7573 return Imp.takeError();
7574
7575 QualType ToType;
7576 SourceLocation ToBeginLoc, ToEndLoc;
7577 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007578
7579 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007580 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7581 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007582
7583 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7584 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007585 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007586
7587 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007588 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7589 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007590}
7591
Balazs Keri3b30d652018-10-19 13:32:20 +00007592ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7593 ExpectedType ToTypeOrErr = import(E->getType());
7594 if (!ToTypeOrErr)
7595 return ToTypeOrErr.takeError();
7596
7597 auto ToSourceRangeOrErr = import(E->getSourceRange());
7598 if (!ToSourceRangeOrErr)
7599 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007600
7601 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007602 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7603 return new (Importer.getToContext()) CXXTypeidExpr(
7604 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7605 else
7606 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007607 }
7608
Balazs Keri3b30d652018-10-19 13:32:20 +00007609 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7610 if (!ToExprOperandOrErr)
7611 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007612
Balazs Keri3b30d652018-10-19 13:32:20 +00007613 return new (Importer.getToContext()) CXXTypeidExpr(
7614 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007615}
7616
Lang Hames19e07e12017-06-20 21:06:00 +00007617void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7618 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007619 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7620 if (auto ImportedOrErr = import(FromOverriddenMethod))
7621 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7622 (*ImportedOrErr)->getCanonicalDecl()));
7623 else
7624 consumeError(ImportedOrErr.takeError());
7625 }
Lang Hames19e07e12017-06-20 21:06:00 +00007626}
7627
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007628ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007629 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007630 bool MinimalImport,
7631 ASTImporterLookupTable *LookupTable)
7632 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007633 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7634 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007635
7636 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7637 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007638}
7639
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007640ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007641
Gabor Marton54058b52018-12-17 13:53:12 +00007642Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7643 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7644 "Try to get field index for non-field.");
7645
7646 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7647 if (!Owner)
7648 return None;
7649
7650 unsigned Index = 0;
7651 for (const auto *D : Owner->decls()) {
7652 if (D == F)
7653 return Index;
7654
7655 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7656 ++Index;
7657 }
7658
7659 llvm_unreachable("Field was not found in its parent context.");
7660
7661 return None;
7662}
7663
7664ASTImporter::FoundDeclsTy
7665ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7666 // We search in the redecl context because of transparent contexts.
7667 // E.g. a simple C language enum is a transparent context:
7668 // enum E { A, B };
7669 // Now if we had a global variable in the TU
7670 // int A;
7671 // then the enum constant 'A' and the variable 'A' violates ODR.
7672 // We can diagnose this only if we search in the redecl context.
7673 DeclContext *ReDC = DC->getRedeclContext();
7674 if (LookupTable) {
7675 ASTImporterLookupTable::LookupResult LookupResult =
7676 LookupTable->lookup(ReDC, Name);
7677 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7678 } else {
7679 // FIXME Can we remove this kind of lookup?
7680 // Or lldb really needs this C/C++ lookup?
7681 FoundDeclsTy Result;
7682 ReDC->localUncachedLookup(Name, Result);
7683 return Result;
7684 }
7685}
7686
7687void ASTImporter::AddToLookupTable(Decl *ToD) {
7688 if (LookupTable)
7689 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7690 LookupTable->add(ToND);
7691}
7692
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007693Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7694 // Import the decl using ASTNodeImporter.
7695 ASTNodeImporter Importer(*this);
7696 return Importer.Visit(FromD);
7697}
7698
7699void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7700 MapImported(FromD, ToD);
7701 AddToLookupTable(ToD);
7702}
7703
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007704Expected<QualType> ASTImporter::Import_New(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007705 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007706 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007707
Balazs Keri3b30d652018-10-19 13:32:20 +00007708 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007709
7710 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007711 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007712 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007713 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007714 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007715
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007716 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007717 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007718 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007719 if (!ToTOrErr)
7720 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007721
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007722 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007723 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007724
Balazs Keri3b30d652018-10-19 13:32:20 +00007725 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007726}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007727QualType ASTImporter::Import(QualType From) {
7728 llvm::Expected<QualType> To = Import_New(From);
7729 if (To)
7730 return *To;
7731 else
7732 llvm::consumeError(To.takeError());
7733 return {};
7734}
Douglas Gregor96e578d2010-02-05 17:54:41 +00007735
Balazs Keri4a3d7582018-11-27 18:36:31 +00007736Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007737 if (!FromTSI)
7738 return FromTSI;
7739
7740 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007741 // on the type and a single location. Implement a real version of this.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007742 ExpectedType TOrErr = Import_New(FromTSI->getType());
7743 if (!TOrErr)
7744 return TOrErr.takeError();
7745 ExpectedSLoc BeginLocOrErr = Import_New(FromTSI->getTypeLoc().getBeginLoc());
7746 if (!BeginLocOrErr)
7747 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007748
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007749 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7750}
7751TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *From) {
7752 llvm::Expected<TypeSourceInfo *> To = Import_New(From);
7753 if (To)
7754 return *To;
7755 else
7756 llvm::consumeError(To.takeError());
7757 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007758}
7759
Balazs Keri4a3d7582018-11-27 18:36:31 +00007760Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007761 Attr *ToAttr = FromAttr->clone(ToContext);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007762 if (auto ToRangeOrErr = Import_New(FromAttr->getRange()))
7763 ToAttr->setRange(*ToRangeOrErr);
7764 else
7765 return ToRangeOrErr.takeError();
7766
Davide Italianofaee83d2018-11-28 19:15:23 +00007767 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007768}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007769Attr *ASTImporter::Import(const Attr *From) {
7770 llvm::Expected<Attr *> To = Import_New(From);
7771 if (To)
7772 return *To;
7773 else
7774 llvm::consumeError(To.takeError());
7775 return nullptr;
7776}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007777
Gabor Martonbe77a982018-12-12 11:22:55 +00007778Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7779 auto Pos = ImportedDecls.find(FromD);
7780 if (Pos != ImportedDecls.end())
7781 return Pos->second;
7782 else
Sean Callanan59721b32015-04-28 18:41:46 +00007783 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007784}
7785
Gabor Marton458d1452019-02-14 13:07:03 +00007786TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7787 auto FromDPos = ImportedFromDecls.find(ToD);
7788 if (FromDPos == ImportedFromDecls.end())
7789 return nullptr;
7790 return FromDPos->second->getTranslationUnitDecl();
7791}
7792
Balazs Keri4a3d7582018-11-27 18:36:31 +00007793Expected<Decl *> ASTImporter::Import_New(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007794 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007795 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007796
Douglas Gregord451ea92011-07-29 23:31:30 +00007797
Gabor Marton26f72a92018-07-12 09:42:05 +00007798 // Check whether we've already imported this declaration.
7799 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7800 if (ToD) {
7801 // If FromD has some updated flags after last import, apply it
7802 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007803 return ToD;
7804 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007805
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007806 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007807 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007808 if (!ToDOrErr)
7809 return ToDOrErr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007810 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007811
Gabor Marton7f8c4002019-03-19 13:34:10 +00007812 // FIXME Use getImportDeclErrorIfAny() here (and return with the error) once
7813 // the error handling is finished in GetImportedOrCreateSpecialDecl().
7814 if (!ToD) {
7815 return nullptr;
7816 }
7817
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007818 // Make sure that ImportImpl registered the imported decl.
7819 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
7820
Gabor Marton54058b52018-12-17 13:53:12 +00007821 // Once the decl is connected to the existing declarations, i.e. when the
7822 // redecl chain is properly set then we populate the lookup again.
7823 // This way the primary context will be able to find all decls.
7824 AddToLookupTable(ToD);
7825
Gabor Marton26f72a92018-07-12 09:42:05 +00007826 // Notify subclasses.
7827 Imported(FromD, ToD);
7828
Gabor Martonac3a5d62018-09-17 12:04:52 +00007829 updateFlags(FromD, ToD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007830 return ToDOrErr;
7831}
7832Decl *ASTImporter::Import(Decl *From) {
7833 llvm::Expected<Decl *> To = Import_New(From);
7834 if (To)
7835 return *To;
7836 else
7837 llvm::consumeError(To.takeError());
7838 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007839}
7840
Balazs Keri3b30d652018-10-19 13:32:20 +00007841Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007842 if (!FromDC)
7843 return FromDC;
7844
Balazs Keria1f6b102019-04-08 13:59:15 +00007845 ExpectedDecl ToDCOrErr = Import_New(cast<Decl>(FromDC));
7846 if (!ToDCOrErr)
7847 return ToDCOrErr.takeError();
7848 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00007849
Fangrui Song6907ce22018-07-30 19:24:48 +00007850 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007851 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007852 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7853 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007854 if (ToRecord->isCompleteDefinition()) {
7855 // Do nothing.
7856 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007857 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7858 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7859 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007860 } else {
7861 CompleteDecl(ToRecord);
7862 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007863 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7864 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007865 if (ToEnum->isCompleteDefinition()) {
7866 // Do nothing.
7867 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007868 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7869 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7870 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007871 } else {
7872 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007873 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007874 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7875 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007876 if (ToClass->getDefinition()) {
7877 // Do nothing.
7878 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007879 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7880 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7881 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007882 } else {
7883 CompleteDecl(ToClass);
7884 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007885 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7886 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007887 if (ToProto->getDefinition()) {
7888 // Do nothing.
7889 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007890 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7891 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7892 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007893 } else {
7894 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007895 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007896 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007897
Douglas Gregor95d82832012-01-24 18:36:04 +00007898 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007899}
7900
Balazs Keri4a3d7582018-11-27 18:36:31 +00007901Expected<Expr *> ASTImporter::Import_New(Expr *FromE) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007902 if (ExpectedStmt ToSOrErr = Import_New(cast_or_null<Stmt>(FromE)))
7903 return cast_or_null<Expr>(*ToSOrErr);
7904 else
7905 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00007906}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007907Expr *ASTImporter::Import(Expr *From) {
7908 llvm::Expected<Expr *> To = Import_New(From);
7909 if (To)
7910 return *To;
7911 else
7912 llvm::consumeError(To.takeError());
7913 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007914}
7915
Balazs Keri4a3d7582018-11-27 18:36:31 +00007916Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007917 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007918 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007919
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007920 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007921 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7922 if (Pos != ImportedStmts.end())
7923 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007924
Balazs Keri3b30d652018-10-19 13:32:20 +00007925 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007926 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007927 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007928 if (!ToSOrErr)
7929 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007930
Balazs Keri3b30d652018-10-19 13:32:20 +00007931 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007932 auto *FromE = cast<Expr>(FromS);
7933 // Copy ExprBitfields, which may not be handled in Expr subclasses
7934 // constructors.
7935 ToE->setValueKind(FromE->getValueKind());
7936 ToE->setObjectKind(FromE->getObjectKind());
7937 ToE->setTypeDependent(FromE->isTypeDependent());
7938 ToE->setValueDependent(FromE->isValueDependent());
7939 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7940 ToE->setContainsUnexpandedParameterPack(
7941 FromE->containsUnexpandedParameterPack());
7942 }
7943
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007944 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00007945 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007946 return ToSOrErr;
7947}
7948Stmt *ASTImporter::Import(Stmt *From) {
7949 llvm::Expected<Stmt *> To = Import_New(From);
7950 if (To)
7951 return *To;
7952 else
7953 llvm::consumeError(To.takeError());
7954 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007955}
7956
Balazs Keri4a3d7582018-11-27 18:36:31 +00007957Expected<NestedNameSpecifier *>
7958ASTImporter::Import_New(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007959 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007960 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007961
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007962 NestedNameSpecifier *Prefix;
7963 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
7964 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00007965
7966 switch (FromNNS->getKind()) {
7967 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007968 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
7969 return NestedNameSpecifier::Create(ToContext, Prefix,
7970 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00007971
7972 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007973 if (ExpectedDecl NSOrErr = Import_New(FromNNS->getAsNamespace())) {
7974 return NestedNameSpecifier::Create(ToContext, Prefix,
7975 cast<NamespaceDecl>(*NSOrErr));
7976 } else
7977 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007978
7979 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007980 if (ExpectedDecl NSADOrErr = Import_New(FromNNS->getAsNamespaceAlias()))
7981 return NestedNameSpecifier::Create(ToContext, Prefix,
7982 cast<NamespaceAliasDecl>(*NSADOrErr));
7983 else
7984 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007985
7986 case NestedNameSpecifier::Global:
7987 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7988
Nikola Smiljanic67860242014-09-26 00:28:20 +00007989 case NestedNameSpecifier::Super:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007990 if (ExpectedDecl RDOrErr = Import_New(FromNNS->getAsRecordDecl()))
7991 return NestedNameSpecifier::SuperSpecifier(ToContext,
7992 cast<CXXRecordDecl>(*RDOrErr));
7993 else
7994 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00007995
Douglas Gregor90ebf252011-04-27 16:48:40 +00007996 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007997 case NestedNameSpecifier::TypeSpecWithTemplate:
7998 if (Expected<QualType> TyOrErr =
7999 Import_New(QualType(FromNNS->getAsType(), 0u))) {
8000 bool TSTemplate =
8001 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
8002 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
8003 TyOrErr->getTypePtr());
8004 } else {
8005 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008006 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008007 }
8008
8009 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008010}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008011NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *From) {
8012 llvm::Expected<NestedNameSpecifier *> To = Import_New(From);
8013 if (To)
8014 return *To;
8015 else
8016 llvm::consumeError(To.takeError());
8017 return nullptr;
8018}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008019
Balazs Keri4a3d7582018-11-27 18:36:31 +00008020Expected<NestedNameSpecifierLoc>
8021ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008022 // Copied from NestedNameSpecifier mostly.
8023 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8024 NestedNameSpecifierLoc NNS = FromNNS;
8025
8026 // Push each of the nested-name-specifiers's onto a stack for
8027 // serialization in reverse order.
8028 while (NNS) {
8029 NestedNames.push_back(NNS);
8030 NNS = NNS.getPrefix();
8031 }
8032
8033 NestedNameSpecifierLocBuilder Builder;
8034
8035 while (!NestedNames.empty()) {
8036 NNS = NestedNames.pop_back_val();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008037 NestedNameSpecifier *Spec;
8038 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8039 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008040
8041 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008042
8043 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8044 if (Kind != NestedNameSpecifier::Super) {
8045 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8046 return std::move(Err);
8047
8048 if (Kind != NestedNameSpecifier::Global)
8049 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8050 return std::move(Err);
8051 }
8052
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008053 switch (Kind) {
8054 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008055 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8056 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008057 break;
8058
8059 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008060 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8061 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008062 break;
8063
8064 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008065 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8066 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008067 break;
8068
8069 case NestedNameSpecifier::TypeSpec:
8070 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008071 SourceLocation ToTLoc;
8072 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8073 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008074 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008075 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008076 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8077 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008078 break;
8079 }
8080
8081 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008082 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008083 break;
8084
8085 case NestedNameSpecifier::Super: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008086 auto ToSourceRangeOrErr = Import_New(NNS.getSourceRange());
8087 if (!ToSourceRangeOrErr)
8088 return ToSourceRangeOrErr.takeError();
8089
8090 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8091 ToSourceRangeOrErr->getBegin(),
8092 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008093 }
8094 }
8095 }
8096
8097 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008098}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008099NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc From) {
8100 llvm::Expected<NestedNameSpecifierLoc> To = Import_New(From);
8101 if (To)
8102 return *To;
8103 else
8104 llvm::consumeError(To.takeError());
8105 return {};
8106}
Douglas Gregor14454802011-02-25 02:25:35 +00008107
Balazs Keri4a3d7582018-11-27 18:36:31 +00008108Expected<TemplateName> ASTImporter::Import_New(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008109 switch (From.getKind()) {
8110 case TemplateName::Template:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008111 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8112 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8113 else
8114 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008115
Douglas Gregore2e50d332010-12-01 01:36:18 +00008116 case TemplateName::OverloadedTemplate: {
8117 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8118 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008119 for (auto *I : *FromStorage) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008120 if (auto ToOrErr = Import_New(I))
8121 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008122 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008123 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008124 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008125 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008126 ToTemplates.end());
8127 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008128
Richard Smithb23c5e82019-05-09 03:31:27 +00008129 case TemplateName::AssumedTemplate: {
8130 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
8131 auto DeclNameOrErr = Import_New(FromStorage->getDeclName());
8132 if (!DeclNameOrErr)
8133 return DeclNameOrErr.takeError();
8134 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8135 }
8136
Douglas Gregore2e50d332010-12-01 01:36:18 +00008137 case TemplateName::QualifiedTemplate: {
8138 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008139 auto QualifierOrErr = Import_New(QTN->getQualifier());
8140 if (!QualifierOrErr)
8141 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008142
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008143 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8144 return ToContext.getQualifiedTemplateName(
8145 *QualifierOrErr, QTN->hasTemplateKeyword(),
8146 cast<TemplateDecl>(*ToTemplateOrErr));
8147 else
8148 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008149 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008150
Douglas Gregore2e50d332010-12-01 01:36:18 +00008151 case TemplateName::DependentTemplate: {
8152 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008153 auto QualifierOrErr = Import_New(DTN->getQualifier());
8154 if (!QualifierOrErr)
8155 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008156
Douglas Gregore2e50d332010-12-01 01:36:18 +00008157 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008158 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008159 Import(DTN->getIdentifier()));
8160 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008161
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008162 return ToContext.getDependentTemplateName(*QualifierOrErr,
8163 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008164 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008165
8166 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008167 SubstTemplateTemplateParmStorage *Subst =
8168 From.getAsSubstTemplateTemplateParm();
8169 ExpectedDecl ParamOrErr = Import_New(Subst->getParameter());
8170 if (!ParamOrErr)
8171 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008172
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008173 auto ReplacementOrErr = Import_New(Subst->getReplacement());
8174 if (!ReplacementOrErr)
8175 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008176
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008177 return ToContext.getSubstTemplateTemplateParm(
8178 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008179 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008180
Douglas Gregor5590be02011-01-15 06:45:20 +00008181 case TemplateName::SubstTemplateTemplateParmPack: {
8182 SubstTemplateTemplateParmPackStorage *SubstPack
8183 = From.getAsSubstTemplateTemplateParmPack();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008184 ExpectedDecl ParamOrErr = Import_New(SubstPack->getParameterPack());
8185 if (!ParamOrErr)
8186 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008187
Douglas Gregor5590be02011-01-15 06:45:20 +00008188 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008189 auto ArgPackOrErr =
8190 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8191 if (!ArgPackOrErr)
8192 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008193
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008194 return ToContext.getSubstTemplateTemplateParmPack(
8195 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008196 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008197 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008198
Douglas Gregore2e50d332010-12-01 01:36:18 +00008199 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008200}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008201TemplateName ASTImporter::Import(TemplateName From) {
8202 llvm::Expected<TemplateName> To = Import_New(From);
8203 if (To)
8204 return *To;
8205 else
8206 llvm::consumeError(To.takeError());
8207 return {};
8208}
Douglas Gregore2e50d332010-12-01 01:36:18 +00008209
Balazs Keri4a3d7582018-11-27 18:36:31 +00008210Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008211 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008212 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008213
Douglas Gregor811663e2010-02-10 00:15:17 +00008214 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008215 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008216
Douglas Gregor811663e2010-02-10 00:15:17 +00008217 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008218 Expected<FileID> ToFileIDOrErr = Import_New(Decomposed.first, IsBuiltin);
8219 if (!ToFileIDOrErr)
8220 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008221 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008222 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8223}
8224SourceLocation ASTImporter::Import(SourceLocation From) {
8225 llvm::Expected<SourceLocation> To = Import_New(From);
8226 if (To)
8227 return *To;
8228 else
8229 llvm::consumeError(To.takeError());
8230 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008231}
8232
Balazs Keri4a3d7582018-11-27 18:36:31 +00008233Expected<SourceRange> ASTImporter::Import_New(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008234 SourceLocation ToBegin, ToEnd;
8235 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8236 return std::move(Err);
8237 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8238 return std::move(Err);
8239
8240 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008241}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008242SourceRange ASTImporter::Import(SourceRange From) {
8243 llvm::Expected<SourceRange> To = Import_New(From);
8244 if (To)
8245 return *To;
8246 else
8247 llvm::consumeError(To.takeError());
8248 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008249}
8250
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008251Expected<FileID> ASTImporter::Import_New(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008252 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008253 if (Pos != ImportedFileIDs.end())
8254 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008255
Douglas Gregor811663e2010-02-10 00:15:17 +00008256 SourceManager &FromSM = FromContext.getSourceManager();
8257 SourceManager &ToSM = ToContext.getSourceManager();
8258 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008259
8260 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008261 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008262 if (FromSLoc.isExpansion()) {
8263 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008264 ExpectedSLoc ToSpLoc = Import_New(FromEx.getSpellingLoc());
8265 if (!ToSpLoc)
8266 return ToSpLoc.takeError();
8267 ExpectedSLoc ToExLocS = Import_New(FromEx.getExpansionLocStart());
8268 if (!ToExLocS)
8269 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008270 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8271 SourceLocation MLoc;
8272 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008273 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008274 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008275 if (ExpectedSLoc ToExLocE = Import_New(FromEx.getExpansionLocEnd()))
8276 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8277 FromEx.isExpansionTokenRange());
8278 else
8279 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008280 }
8281 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008282 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008283 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008284
8285 if (!IsBuiltin) {
8286 // Include location of this file.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008287 ExpectedSLoc ToIncludeLoc =
8288 Import_New(FromSLoc.getFile().getIncludeLoc());
8289 if (!ToIncludeLoc)
8290 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008291
8292 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8293 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8294 // disk again
8295 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8296 // than mmap the files several times.
8297 const FileEntry *Entry =
8298 ToFileManager.getFile(Cache->OrigEntry->getName());
8299 // FIXME: The filename may be a virtual name that does probably not
8300 // point to a valid file and we get no Entry here. In this case try with
8301 // the memory buffer below.
8302 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008303 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008304 FromSLoc.getFile().getFileCharacteristic());
8305 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008306 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008307
8308 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008309 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008310 bool Invalid = true;
8311 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8312 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8313 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008314 // FIXME: Use a new error kind?
8315 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008316
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008317 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8318 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8319 FromBuf->getBufferIdentifier());
8320 ToID = ToSM.createFileID(std::move(ToBuf),
8321 FromSLoc.getFile().getFileCharacteristic());
8322 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008323 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008324
Balazs Keri9cf39df2019-02-27 16:31:48 +00008325 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8326
Sebastian Redl99219f12010-09-30 01:03:06 +00008327 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008328 return ToID;
8329}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008330FileID ASTImporter::Import(FileID From, bool IsBuiltin) {
8331 llvm::Expected<FileID> To = Import_New(From, IsBuiltin);
8332 if (To)
8333 return *To;
8334 else
8335 llvm::consumeError(To.takeError());
8336 return {};
8337}
Douglas Gregor811663e2010-02-10 00:15:17 +00008338
Balazs Keri4a3d7582018-11-27 18:36:31 +00008339Expected<CXXCtorInitializer *>
8340ASTImporter::Import_New(CXXCtorInitializer *From) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008341 ExpectedExpr ToExprOrErr = Import_New(From->getInit());
8342 if (!ToExprOrErr)
8343 return ToExprOrErr.takeError();
8344
8345 auto LParenLocOrErr = Import_New(From->getLParenLoc());
8346 if (!LParenLocOrErr)
8347 return LParenLocOrErr.takeError();
8348
8349 auto RParenLocOrErr = Import_New(From->getRParenLoc());
8350 if (!RParenLocOrErr)
8351 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008352
8353 if (From->isBaseInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008354 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8355 if (!ToTInfoOrErr)
8356 return ToTInfoOrErr.takeError();
8357
8358 SourceLocation EllipsisLoc;
8359 if (From->isPackExpansion())
8360 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8361 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008362
8363 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008364 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8365 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008366 } else if (From->isMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008367 ExpectedDecl ToFieldOrErr = Import_New(From->getMember());
8368 if (!ToFieldOrErr)
8369 return ToFieldOrErr.takeError();
8370
8371 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8372 if (!MemberLocOrErr)
8373 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008374
8375 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008376 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8377 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008378 } else if (From->isIndirectMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008379 ExpectedDecl ToIFieldOrErr = Import_New(From->getIndirectMember());
8380 if (!ToIFieldOrErr)
8381 return ToIFieldOrErr.takeError();
8382
8383 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8384 if (!MemberLocOrErr)
8385 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008386
8387 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008388 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8389 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008390 } else if (From->isDelegatingInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008391 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8392 if (!ToTInfoOrErr)
8393 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008394
8395 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008396 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8397 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008398 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008399 // FIXME: assert?
8400 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008401 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008402}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008403CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8404 llvm::Expected<CXXCtorInitializer *> To = Import_New(From);
8405 if (To)
8406 return *To;
8407 else
8408 llvm::consumeError(To.takeError());
8409 return nullptr;
8410}
Sean Callanandd2c1742016-05-16 20:48:03 +00008411
Balazs Keri4a3d7582018-11-27 18:36:31 +00008412Expected<CXXBaseSpecifier *>
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008413ASTImporter::Import_New(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008414 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8415 if (Pos != ImportedCXXBaseSpecifiers.end())
8416 return Pos->second;
8417
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008418 Expected<SourceRange> ToSourceRange = Import_New(BaseSpec->getSourceRange());
8419 if (!ToSourceRange)
8420 return ToSourceRange.takeError();
8421 Expected<TypeSourceInfo *> ToTSI = Import_New(BaseSpec->getTypeSourceInfo());
8422 if (!ToTSI)
8423 return ToTSI.takeError();
8424 ExpectedSLoc ToEllipsisLoc = Import_New(BaseSpec->getEllipsisLoc());
8425 if (!ToEllipsisLoc)
8426 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008427 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008428 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8429 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008430 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8431 return Imported;
8432}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008433CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *From) {
8434 llvm::Expected<CXXBaseSpecifier *> To = Import_New(From);
8435 if (To)
8436 return *To;
8437 else
8438 llvm::consumeError(To.takeError());
8439 return nullptr;
8440}
Aleksei Sidorina693b372016-09-28 10:16:56 +00008441
Balazs Keri3b30d652018-10-19 13:32:20 +00008442Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008443 Decl *To = Import(From);
8444 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008445 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008446
Don Hintonf170dff2019-03-19 06:14:14 +00008447 auto *FromDC = cast<DeclContext>(From);
8448 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008449
Don Hintonf170dff2019-03-19 06:14:14 +00008450 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8451 if (!ToRecord->getDefinition()) {
8452 return Importer.ImportDefinition(
8453 cast<RecordDecl>(FromDC), ToRecord,
8454 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008455 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008456 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008457
Don Hintonf170dff2019-03-19 06:14:14 +00008458 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8459 if (!ToEnum->getDefinition()) {
8460 return Importer.ImportDefinition(
8461 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8462 }
8463 }
8464
8465 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8466 if (!ToIFace->getDefinition()) {
8467 return Importer.ImportDefinition(
8468 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8469 ASTNodeImporter::IDK_Everything);
8470 }
8471 }
8472
8473 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8474 if (!ToProto->getDefinition()) {
8475 return Importer.ImportDefinition(
8476 cast<ObjCProtocolDecl>(FromDC), ToProto,
8477 ASTNodeImporter::IDK_Everything);
8478 }
8479 }
8480
8481 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008482}
8483
8484void ASTImporter::ImportDefinition(Decl *From) {
8485 Error Err = ImportDefinition_New(From);
8486 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008487}
8488
Balazs Keri4a3d7582018-11-27 18:36:31 +00008489Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008490 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008491 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008492
8493 switch (FromName.getNameKind()) {
8494 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008495 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008496
8497 case DeclarationName::ObjCZeroArgSelector:
8498 case DeclarationName::ObjCOneArgSelector:
8499 case DeclarationName::ObjCMultiArgSelector:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008500 if (auto ToSelOrErr = Import_New(FromName.getObjCSelector()))
8501 return DeclarationName(*ToSelOrErr);
8502 else
8503 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008504
8505 case DeclarationName::CXXConstructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008506 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8507 return ToContext.DeclarationNames.getCXXConstructorName(
8508 ToContext.getCanonicalType(*ToTyOrErr));
8509 else
8510 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008511 }
8512
8513 case DeclarationName::CXXDestructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008514 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8515 return ToContext.DeclarationNames.getCXXDestructorName(
8516 ToContext.getCanonicalType(*ToTyOrErr));
8517 else
8518 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008519 }
8520
Richard Smith35845152017-02-07 01:37:30 +00008521 case DeclarationName::CXXDeductionGuideName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008522 if (auto ToTemplateOrErr =
8523 Import_New(FromName.getCXXDeductionGuideTemplate()))
8524 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8525 cast<TemplateDecl>(*ToTemplateOrErr));
8526 else
8527 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008528 }
8529
Douglas Gregor96e578d2010-02-05 17:54:41 +00008530 case DeclarationName::CXXConversionFunctionName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008531 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8532 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8533 ToContext.getCanonicalType(*ToTyOrErr));
8534 else
8535 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008536 }
8537
8538 case DeclarationName::CXXOperatorName:
8539 return ToContext.DeclarationNames.getCXXOperatorName(
8540 FromName.getCXXOverloadedOperator());
8541
8542 case DeclarationName::CXXLiteralOperatorName:
8543 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008544 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008545
8546 case DeclarationName::CXXUsingDirective:
8547 // FIXME: STATICS!
8548 return DeclarationName::getUsingDirectiveName();
8549 }
8550
David Blaikiee4d798f2012-01-20 21:50:17 +00008551 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008552}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008553DeclarationName ASTImporter::Import(DeclarationName From) {
8554 llvm::Expected<DeclarationName> To = Import_New(From);
8555 if (To)
8556 return *To;
8557 else
8558 llvm::consumeError(To.takeError());
8559 return {};
8560}
Douglas Gregor96e578d2010-02-05 17:54:41 +00008561
Douglas Gregore2e50d332010-12-01 01:36:18 +00008562IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008563 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008564 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008565
Sean Callananf94ef1d2016-05-14 06:11:19 +00008566 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8567
8568 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8569 ToId->setBuiltinID(FromId->getBuiltinID());
8570
8571 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008572}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008573
Balazs Keri4a3d7582018-11-27 18:36:31 +00008574Expected<Selector> ASTImporter::Import_New(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008575 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008576 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008577
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008578 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008579 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8580 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8581 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8582 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8583}
8584
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008585DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8586 DeclContext *DC,
8587 unsigned IDNS,
8588 NamedDecl **Decls,
8589 unsigned NumDecls) {
8590 return Name;
8591}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008592Selector ASTImporter::Import(Selector From) {
8593 llvm::Expected<Selector> To = Import_New(From);
8594 if (To)
8595 return *To;
8596 else
8597 llvm::consumeError(To.takeError());
8598 return {};
8599}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008600
8601DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008602 if (LastDiagFromFrom)
8603 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8604 FromContext.getDiagnostics());
8605 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008606 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008607}
8608
8609DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008610 if (!LastDiagFromFrom)
8611 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8612 ToContext.getDiagnostics());
8613 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008614 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008615}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008616
Douglas Gregor2e15c842012-02-01 21:00:38 +00008617void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008618 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008619 if (!ID->getDefinition())
8620 ID->startDefinition();
8621 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008622 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008623 if (!PD->getDefinition())
8624 PD->startDefinition();
8625 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008626 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008627 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8628 TD->startDefinition();
8629 TD->setCompleteDefinition(true);
8630 }
8631 }
8632 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008633 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008634 }
8635}
8636
Gabor Marton26f72a92018-07-12 09:42:05 +00008637Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8638 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8639 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8640 "Try to import an already imported Decl");
8641 if (Pos != ImportedDecls.end())
8642 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008643 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008644 // This mapping should be maintained only in this function. Therefore do not
8645 // check for additional consistency.
8646 ImportedFromDecls[To] = From;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008647 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008648}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008649
Douglas Gregordd6006f2012-07-17 21:16:27 +00008650bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8651 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008652 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8653 ImportedTypes.find(From.getTypePtr());
8654 if (Pos != ImportedTypes.end()) {
8655 if (ExpectedType ToFromOrErr = Import_New(From)) {
8656 if (ToContext.hasSameType(*ToFromOrErr, To))
8657 return true;
8658 } else {
8659 llvm::consumeError(ToFromOrErr.takeError());
8660 }
8661 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008662
Douglas Gregordd6006f2012-07-17 21:16:27 +00008663 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008664 getStructuralEquivalenceKind(*this), false,
8665 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008666 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008667}