blob: 74a1887753c576298651a175617ba17a828b78a4 [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor96e578d2010-02-05 17:54:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTImporter class which imports AST nodes from one
10// context into another context.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000013
Douglas Gregor96e578d2010-02-05 17:54:41 +000014#include "clang/AST/ASTImporter.h"
Gabor Marton54058b52018-12-17 13:53:12 +000015#include "clang/AST/ASTImporterLookupTable.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Balazs Keri3b30d652018-10-19 13:32:20 +000074 using llvm::make_error;
75 using llvm::Error;
76 using llvm::Expected;
77 using ExpectedType = llvm::Expected<QualType>;
78 using ExpectedStmt = llvm::Expected<Stmt *>;
79 using ExpectedExpr = llvm::Expected<Expr *>;
80 using ExpectedDecl = llvm::Expected<Decl *>;
81 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000082
Balazs Keri3b30d652018-10-19 13:32:20 +000083 std::string ImportError::toString() const {
84 // FIXME: Improve error texts.
85 switch (Error) {
86 case NameConflict:
87 return "NameConflict";
88 case UnsupportedConstruct:
89 return "UnsupportedConstruct";
90 case Unknown:
91 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000092 }
Balazs Keri2a13d662018-10-19 15:16:51 +000093 llvm_unreachable("Invalid error code.");
94 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
Gabor Marton7df342a2018-12-17 12:42:12 +0000125 if (auto *TD = dyn_cast<TagDecl>(D))
126 return getCanonicalForwardRedeclChain<TagDecl>(TD);
Gabor Martonac3a5d62018-09-17 12:04:52 +0000127 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000128 }
129
Gabor Marton26f72a92018-07-12 09:42:05 +0000130 void updateFlags(const Decl *From, Decl *To) {
131 // Check if some flags or attrs are new in 'From' and copy into 'To'.
132 // FIXME: Other flags or attrs?
133 if (From->isUsed(false) && !To->isUsed(false))
134 To->setIsUsed();
135 }
136
Balazs Keri3b30d652018-10-19 13:32:20 +0000137 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
138 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
139 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000140 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000141
Balazs Keri3b30d652018-10-19 13:32:20 +0000142 // Use this instead of Importer.importInto .
143 template <typename ImportT>
144 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
145 return Importer.importInto(To, From);
146 }
147
148 // Use this to import pointers of specific type.
149 template <typename ImportT>
150 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000151 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000152 if (ToOrErr)
153 To = cast_or_null<ImportT>(*ToOrErr);
154 return ToOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +0000155 }
156
157 // Call the import function of ASTImporter for a baseclass of type `T` and
158 // cast the return value to `T`.
159 template <typename T>
160 Expected<T *> import(T *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000161 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000162 if (!ToOrErr)
163 return ToOrErr.takeError();
164 return cast_or_null<T>(*ToOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +0000165 }
166
167 template <typename T>
168 Expected<T *> import(const T *From) {
169 return import(const_cast<T *>(From));
170 }
171
172 // Call the import function of ASTImporter for type `T`.
173 template <typename T>
174 Expected<T> import(const T &From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000175 return Importer.Import(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000176 }
177
Richard Smithb9fb1212019-05-06 03:47:15 +0000178 // Import an Optional<T> by importing the contained T, if any.
179 template<typename T>
180 Expected<Optional<T>> import(Optional<T> From) {
181 if (!From)
182 return Optional<T>();
183 return import(*From);
184 }
185
Balazs Keri3b30d652018-10-19 13:32:20 +0000186 template <class T>
187 Expected<std::tuple<T>>
188 importSeq(const T &From) {
189 Expected<T> ToOrErr = import(From);
190 if (!ToOrErr)
191 return ToOrErr.takeError();
192 return std::make_tuple<T>(std::move(*ToOrErr));
193 }
194
195 // Import multiple objects with a single function call.
196 // This should work for every type for which a variant of `import` exists.
197 // The arguments are processed from left to right and import is stopped on
198 // first error.
199 template <class THead, class... TTail>
200 Expected<std::tuple<THead, TTail...>>
201 importSeq(const THead &FromHead, const TTail &...FromTail) {
202 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
203 if (!ToHeadOrErr)
204 return ToHeadOrErr.takeError();
205 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
206 if (!ToTailOrErr)
207 return ToTailOrErr.takeError();
208 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
209 }
210
211// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000212 template <typename ToDeclT> struct CallOverloadedCreateFun {
213 template <typename... Args>
214 auto operator()(Args &&... args)
215 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
216 return ToDeclT::Create(std::forward<Args>(args)...);
217 }
218 };
219
220 // Always use these functions to create a Decl during import. There are
221 // certain tasks which must be done after the Decl was created, e.g. we
222 // must immediately register that as an imported Decl. The parameter `ToD`
223 // will be set to the newly created Decl or if had been imported before
224 // then to the already imported Decl. Returns a bool value set to true if
225 // the `FromD` had been imported before.
226 template <typename ToDeclT, typename FromDeclT, typename... Args>
227 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
228 Args &&... args) {
229 // There may be several overloads of ToDeclT::Create. We must make sure
230 // to call the one which would be chosen by the arguments, thus we use a
231 // wrapper for the overload set.
232 CallOverloadedCreateFun<ToDeclT> OC;
233 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
234 std::forward<Args>(args)...);
235 }
236 // Use this overload if a special Type is needed to be created. E.g if we
237 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
238 // then:
239 // TypedefNameDecl *ToTypedef;
240 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
241 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
242 typename... Args>
243 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
244 Args &&... args) {
245 CallOverloadedCreateFun<NewDeclT> OC;
246 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
247 std::forward<Args>(args)...);
248 }
249 // Use this version if a special create function must be
250 // used, e.g. CXXRecordDecl::CreateLambda .
251 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
252 typename... Args>
253 LLVM_NODISCARD bool
254 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
255 FromDeclT *FromD, Args &&... args) {
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()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00001684 if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef))
Balazs Keri57949eb2019-03-25 09:16:39 +00001685 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)) {
Richard Smith76b90272019-05-09 03:59:21 +00003079 Expr *ExplicitExpr = nullptr;
3080 if (FromConstructor->getExplicitSpecifier().getExpr()) {
3081 auto Imp = importSeq(FromConstructor->getExplicitSpecifier().getExpr());
3082 if (!Imp)
3083 return Imp.takeError();
3084 std::tie(ExplicitExpr) = *Imp;
3085 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003086 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Richard Smith76b90272019-05-09 03:59:21 +00003087 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3088 ToInnerLocStart, NameInfo, T, TInfo,
3089 ExplicitSpecifier(
3090 ExplicitExpr,
3091 FromConstructor->getExplicitSpecifier().getKind()),
3092 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003093 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003094 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3095
3096 auto Imp =
3097 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3098 FromDtor->getOperatorDeleteThisArg());
3099
3100 if (!Imp)
3101 return Imp.takeError();
3102
3103 FunctionDecl *ToOperatorDelete;
3104 Expr *ToThisArg;
3105 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3106
Gabor Marton26f72a92018-07-12 09:42:05 +00003107 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003108 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3109 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3110 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003111 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003112
3113 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3114
3115 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003116 } else if (CXXConversionDecl *FromConversion =
3117 dyn_cast<CXXConversionDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003118 Expr *ExplicitExpr = nullptr;
3119 if (FromConversion->getExplicitSpecifier().getExpr()) {
3120 auto Imp = importSeq(FromConversion->getExplicitSpecifier().getExpr());
3121 if (!Imp)
3122 return Imp.takeError();
3123 std::tie(ExplicitExpr) = *Imp;
3124 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003125 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3126 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003127 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith76b90272019-05-09 03:59:21 +00003128 ExplicitSpecifier(ExplicitExpr,
3129 FromConversion->getExplicitSpecifier().getKind()),
3130 D->isConstexpr(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003131 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003132 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003133 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3134 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003135 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003136 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3137 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003138 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003139 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003140 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003141 D->getStorageClass(), D->isInlineSpecified(),
3142 D->hasWrittenPrototype(), D->isConstexpr()))
3143 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003144 }
John McCall3e11ebe2010-03-15 10:12:16 +00003145
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003146 // Connect the redecl chain.
3147 if (FoundByLookup) {
3148 auto *Recent = const_cast<FunctionDecl *>(
3149 FoundByLookup->getMostRecentDecl());
3150 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003151 // FIXME Probably we should merge exception specifications. E.g. In the
3152 // "To" context the existing function may have exception specification with
3153 // noexcept-unevaluated, while the newly imported function may have an
3154 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3155 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003156 }
3157
3158 // Import Ctor initializers.
3159 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3160 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3161 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3162 // Import first, then allocate memory and copy if there was no error.
3163 if (Error Err = ImportContainerChecked(
3164 FromConstructor->inits(), CtorInitializers))
3165 return std::move(Err);
3166 auto **Memory =
3167 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3168 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3169 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3170 ToCtor->setCtorInitializers(Memory);
3171 ToCtor->setNumCtorInitializers(NumInitializers);
3172 }
3173 }
3174
Balazs Keri3b30d652018-10-19 13:32:20 +00003175 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003176 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003177 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003178 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3179 ToFunction->setTrivial(D->isTrivial());
3180 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003181 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003182
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003183 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003184 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003185 Param->setOwningFunction(ToFunction);
3186 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003187 }
David Blaikie9c70e042011-09-21 18:16:56 +00003188 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003189
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003190 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3191 // params it refers to.
3192 if (TInfo) {
3193 if (auto ProtoLoc =
3194 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3195 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3196 ProtoLoc.setParam(I, Parameters[I]);
3197 }
3198 }
3199
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003200 if (usedDifferentExceptionSpec) {
3201 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003202 if (ExpectedType TyOrErr = import(D->getType()))
3203 ToFunction->setType(*TyOrErr);
3204 else
3205 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003206 }
3207
Balazs Keria35798d2018-07-17 09:52:41 +00003208 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003209 if (FromFT) {
3210 auto ToFTOrErr = import(FromFT);
3211 if (!ToFTOrErr)
3212 return ToFTOrErr.takeError();
3213 }
Balazs Keria35798d2018-07-17 09:52:41 +00003214
Gabor Marton5254e642018-06-27 13:32:50 +00003215 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003216 Error Err = ImportFunctionDeclBody(D, ToFunction);
3217
3218 if (Err)
3219 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003220 }
3221
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003222 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003223
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003224 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003225 if (Error Err = ImportTemplateInformation(D, ToFunction))
3226 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003227
Gabor Marton5254e642018-06-27 13:32:50 +00003228 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3229
3230 // TODO Can we generalize this approach to other AST nodes as well?
3231 if (D->getDeclContext()->containsDeclAndLoad(D))
3232 DC->addDeclInternal(ToFunction);
3233 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003234 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003235
Gabor Marton5254e642018-06-27 13:32:50 +00003236 // Friend declaration's lexical context is the befriending class, but the
3237 // semantic context is the enclosing scope of the befriending class.
3238 // We want the friend functions to be found in the semantic context by lookup.
3239 // FIXME should we handle this generically in VisitFriendDecl?
3240 // In Other cases when LexicalDC != DC we don't want it to be added,
3241 // e.g out-of-class definitions like void B::f() {} .
3242 if (LexicalDC != DC && IsFriend) {
3243 DC->makeDeclVisibleInContext(ToFunction);
3244 }
3245
Gabor Marton7a0841e2018-10-29 10:18:28 +00003246 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3247 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3248
Gabor Marton5254e642018-06-27 13:32:50 +00003249 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003250 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3251 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3252 if (!ToRedeclOrErr)
3253 return ToRedeclOrErr.takeError();
3254 }
Gabor Marton5254e642018-06-27 13:32:50 +00003255
Douglas Gregor43f54792010-02-17 02:12:47 +00003256 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003257}
3258
Balazs Keri3b30d652018-10-19 13:32:20 +00003259ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003260 return VisitFunctionDecl(D);
3261}
3262
Balazs Keri3b30d652018-10-19 13:32:20 +00003263ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003264 return VisitCXXMethodDecl(D);
3265}
3266
Balazs Keri3b30d652018-10-19 13:32:20 +00003267ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003268 return VisitCXXMethodDecl(D);
3269}
3270
Balazs Keri3b30d652018-10-19 13:32:20 +00003271ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003272 return VisitCXXMethodDecl(D);
3273}
3274
Balazs Keri3b30d652018-10-19 13:32:20 +00003275ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003276 // Import the major distinguishing characteristics of a variable.
3277 DeclContext *DC, *LexicalDC;
3278 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003279 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003280 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003281 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3282 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003283 if (ToD)
3284 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003285
Fangrui Song6907ce22018-07-30 19:24:48 +00003286 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003287 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003288 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003289 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003290 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003291 if (!Name &&
3292 ASTImporter::getFieldIndex(D) !=
3293 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003294 continue;
3295
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003296 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003297 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003298 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003299 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3300 // initializer of a FieldDecl might not had been instantiated in the
3301 // "To" context. However, the "From" context might instantiated that,
3302 // thus we have to merge that.
3303 if (Expr *FromInitializer = D->getInClassInitializer()) {
3304 // We don't have yet the initializer set.
3305 if (FoundField->hasInClassInitializer() &&
3306 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003307 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3308 FoundField->setInClassInitializer(*ToInitializerOrErr);
3309 else {
3310 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003311 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003312 // FIXME: warning message?
3313 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003314 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003315 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003316 }
3317 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003318 return FoundField;
3319 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003320
Balazs Keri3b30d652018-10-19 13:32:20 +00003321 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003322 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003323 << Name << D->getType() << FoundField->getType();
3324 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3325 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003326
3327 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003328 }
3329 }
3330
Balazs Keri3b30d652018-10-19 13:32:20 +00003331 QualType ToType;
3332 TypeSourceInfo *ToTInfo;
3333 Expr *ToBitWidth;
3334 SourceLocation ToInnerLocStart;
3335 Expr *ToInitializer;
3336 if (auto Imp = importSeq(
3337 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3338 D->getInnerLocStart(), D->getInClassInitializer()))
3339 std::tie(
3340 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3341 else
3342 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003343
Gabor Marton26f72a92018-07-12 09:42:05 +00003344 FieldDecl *ToField;
3345 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003346 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3347 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3348 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003349 return ToField;
3350
Douglas Gregordd483172010-02-22 17:42:47 +00003351 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003352 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003353 if (ToInitializer)
3354 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003355 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003356 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003357 return ToField;
3358}
3359
Balazs Keri3b30d652018-10-19 13:32:20 +00003360ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003361 // Import the major distinguishing characteristics of a variable.
3362 DeclContext *DC, *LexicalDC;
3363 DeclarationName Name;
3364 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003365 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003366 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3367 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003368 if (ToD)
3369 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003370
Fangrui Song6907ce22018-07-30 19:24:48 +00003371 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003372 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003373 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003374 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003375 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003376 if (!Name &&
3377 ASTImporter::getFieldIndex(D) !=
3378 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003379 continue;
3380
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003381 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003382 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003383 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003384 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003385 return FoundField;
3386 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003387
3388 // If there are more anonymous fields to check, continue.
3389 if (!Name && I < N-1)
3390 continue;
3391
Balazs Keri3b30d652018-10-19 13:32:20 +00003392 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003393 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003394 << Name << D->getType() << FoundField->getType();
3395 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3396 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003397
3398 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003399 }
3400 }
3401
Francois Pichet783dd6e2010-11-21 06:08:52 +00003402 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003403 auto TypeOrErr = import(D->getType());
3404 if (!TypeOrErr)
3405 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003406
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003407 auto **NamedChain =
3408 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003409
3410 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003411 for (auto *PI : D->chain())
3412 if (Expected<NamedDecl *> ToD = import(PI))
3413 NamedChain[i++] = *ToD;
3414 else
3415 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003416
Gabor Marton26f72a92018-07-12 09:42:05 +00003417 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3418 IndirectFieldDecl *ToIndirectField;
3419 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003420 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003421 // FIXME here we leak `NamedChain` which is allocated before
3422 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003423
Francois Pichet783dd6e2010-11-21 06:08:52 +00003424 ToIndirectField->setAccess(D->getAccess());
3425 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003426 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003427 return ToIndirectField;
3428}
3429
Balazs Keri3b30d652018-10-19 13:32:20 +00003430ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003431 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003432 DeclContext *DC, *LexicalDC;
3433 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3434 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003435
3436 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003437 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003438 auto *RD = cast<CXXRecordDecl>(DC);
3439 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003440
3441 while (ImportedFriend) {
3442 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003443 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3444 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003445 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003446
3447 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3448 if (Importer.IsStructurallyEquivalent(
3449 D->getFriendType()->getType(),
3450 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003451 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003452 }
3453 ImportedFriend = ImportedFriend->getNextFriend();
3454 }
3455
3456 // Not found. Create it.
3457 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003458 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003459 NamedDecl *ToFriendD;
3460 if (Error Err = importInto(ToFriendD, FriendD))
3461 return std::move(Err);
3462
3463 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003464 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3465 ToFriendD->setObjectOfFriendDecl(false);
3466
3467 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003468 } else { // The friend is a type, not a decl.
3469 if (auto TSIOrErr = import(D->getFriendType()))
3470 ToFU = *TSIOrErr;
3471 else
3472 return TSIOrErr.takeError();
3473 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003474
3475 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003476 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003477 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003478 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003479 ToTPLists[I] = *ListOrErr;
3480 else
3481 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003482 }
3483
Balazs Keri3b30d652018-10-19 13:32:20 +00003484 auto LocationOrErr = import(D->getLocation());
3485 if (!LocationOrErr)
3486 return LocationOrErr.takeError();
3487 auto FriendLocOrErr = import(D->getFriendLoc());
3488 if (!FriendLocOrErr)
3489 return FriendLocOrErr.takeError();
3490
Gabor Marton26f72a92018-07-12 09:42:05 +00003491 FriendDecl *FrD;
3492 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003493 *LocationOrErr, ToFU,
3494 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003495 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003496
3497 FrD->setAccess(D->getAccess());
3498 FrD->setLexicalDeclContext(LexicalDC);
3499 LexicalDC->addDeclInternal(FrD);
3500 return FrD;
3501}
3502
Balazs Keri3b30d652018-10-19 13:32:20 +00003503ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003504 // Import the major distinguishing characteristics of an ivar.
3505 DeclContext *DC, *LexicalDC;
3506 DeclarationName Name;
3507 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003508 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003509 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3510 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003511 if (ToD)
3512 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003513
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003514 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003515 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003516 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003517 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003518 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003519 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003520 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003521 return FoundIvar;
3522 }
3523
Gabor Marton410f32c2019-04-01 15:29:55 +00003524 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003525 << Name << D->getType() << FoundIvar->getType();
3526 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3527 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003528
3529 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003530 }
3531 }
3532
Balazs Keri3b30d652018-10-19 13:32:20 +00003533 QualType ToType;
3534 TypeSourceInfo *ToTypeSourceInfo;
3535 Expr *ToBitWidth;
3536 SourceLocation ToInnerLocStart;
3537 if (auto Imp = importSeq(
3538 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3539 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3540 else
3541 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003542
Gabor Marton26f72a92018-07-12 09:42:05 +00003543 ObjCIvarDecl *ToIvar;
3544 if (GetImportedOrCreateDecl(
3545 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003546 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3547 ToType, ToTypeSourceInfo,
3548 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003549 return ToIvar;
3550
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003551 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003552 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003553 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003554}
3555
Balazs Keri3b30d652018-10-19 13:32:20 +00003556ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003557
3558 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3559 auto RedeclIt = Redecls.begin();
3560 // Import the first part of the decl chain. I.e. import all previous
3561 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003562 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3563 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3564 if (!RedeclOrErr)
3565 return RedeclOrErr.takeError();
3566 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003567 assert(*RedeclIt == D);
3568
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003569 // Import the major distinguishing characteristics of a variable.
3570 DeclContext *DC, *LexicalDC;
3571 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003572 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003573 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003574 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3575 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003576 if (ToD)
3577 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003578
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003579 // Try to find a variable in our own ("to") context with the same name and
3580 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003581 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003582 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003583 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003584 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003585 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003586 for (auto *FoundDecl : FoundDecls) {
3587 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003588 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003589
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003590 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003591 if (!hasSameVisibilityContext(FoundVar, D))
3592 continue;
3593 if (Importer.IsStructurallyEquivalent(D->getType(),
3594 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003595
Gabor Marton458d1452019-02-14 13:07:03 +00003596 // The VarDecl in the "From" context has a definition, but in the
3597 // "To" context we already have a definition.
3598 VarDecl *FoundDef = FoundVar->getDefinition();
3599 if (D->isThisDeclarationADefinition() && FoundDef)
3600 // FIXME Check for ODR error if the two definitions have
3601 // different initializers?
3602 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003603
Gabor Marton458d1452019-02-14 13:07:03 +00003604 // The VarDecl in the "From" context has an initializer, but in the
3605 // "To" context we already have an initializer.
3606 const VarDecl *FoundDInit = nullptr;
3607 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3608 // FIXME Diagnose ODR error if the two initializers are different?
3609 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3610
3611 FoundByLookup = FoundVar;
3612 break;
3613 }
3614
3615 const ArrayType *FoundArray
3616 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3617 const ArrayType *TArray
3618 = Importer.getToContext().getAsArrayType(D->getType());
3619 if (FoundArray && TArray) {
3620 if (isa<IncompleteArrayType>(FoundArray) &&
3621 isa<ConstantArrayType>(TArray)) {
3622 // Import the type.
3623 if (auto TyOrErr = import(D->getType()))
3624 FoundVar->setType(*TyOrErr);
3625 else
3626 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003627
3628 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003629 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003630 } else if (isa<IncompleteArrayType>(TArray) &&
3631 isa<ConstantArrayType>(FoundArray)) {
3632 FoundByLookup = FoundVar;
3633 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003634 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003635 }
Gabor Marton458d1452019-02-14 13:07:03 +00003636
Gabor Marton410f32c2019-04-01 15:29:55 +00003637 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003638 << Name << D->getType() << FoundVar->getType();
3639 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3640 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003641 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003642
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003643 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003644 }
3645
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003646 if (!ConflictingDecls.empty()) {
3647 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003648 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003649 ConflictingDecls.size());
3650 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003651 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003652 }
3653 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003654
Balazs Keri3b30d652018-10-19 13:32:20 +00003655 QualType ToType;
3656 TypeSourceInfo *ToTypeSourceInfo;
3657 SourceLocation ToInnerLocStart;
3658 NestedNameSpecifierLoc ToQualifierLoc;
3659 if (auto Imp = importSeq(
3660 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3661 D->getQualifierLoc()))
3662 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3663 else
3664 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003665
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003666 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003667 VarDecl *ToVar;
3668 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003669 ToInnerLocStart, Loc,
3670 Name.getAsIdentifierInfo(),
3671 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003672 D->getStorageClass()))
3673 return ToVar;
3674
Balazs Keri3b30d652018-10-19 13:32:20 +00003675 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003676 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003677 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003678
Gabor Martonac3a5d62018-09-17 12:04:52 +00003679 if (FoundByLookup) {
3680 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3681 ToVar->setPreviousDecl(Recent);
3682 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003683
Balazs Keri3b30d652018-10-19 13:32:20 +00003684 if (Error Err = ImportInitializer(D, ToVar))
3685 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003686
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003687 if (D->isConstexpr())
3688 ToVar->setConstexpr(true);
3689
Gabor Martonac3a5d62018-09-17 12:04:52 +00003690 if (D->getDeclContext()->containsDeclAndLoad(D))
3691 DC->addDeclInternal(ToVar);
3692 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3693 LexicalDC->addDeclInternal(ToVar);
3694
3695 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003696 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3697 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3698 if (!RedeclOrErr)
3699 return RedeclOrErr.takeError();
3700 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003701
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003702 return ToVar;
3703}
3704
Balazs Keri3b30d652018-10-19 13:32:20 +00003705ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003706 // Parameters are created in the translation unit's context, then moved
3707 // into the function declaration's context afterward.
3708 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003709
Balazs Keri3b30d652018-10-19 13:32:20 +00003710 DeclarationName ToDeclName;
3711 SourceLocation ToLocation;
3712 QualType ToType;
3713 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3714 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3715 else
3716 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003717
Douglas Gregor8b228d72010-02-17 21:22:52 +00003718 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003719 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003720 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3721 ToLocation, ToDeclName.getAsIdentifierInfo(),
3722 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003723 return ToParm;
3724 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003725}
3726
Balazs Keri3b30d652018-10-19 13:32:20 +00003727ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003728 // Parameters are created in the translation unit's context, then moved
3729 // into the function declaration's context afterward.
3730 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003731
Balazs Keri3b30d652018-10-19 13:32:20 +00003732 DeclarationName ToDeclName;
3733 SourceLocation ToLocation, ToInnerLocStart;
3734 QualType ToType;
3735 TypeSourceInfo *ToTypeSourceInfo;
3736 if (auto Imp = importSeq(
3737 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3738 D->getTypeSourceInfo()))
3739 std::tie(
3740 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3741 ToTypeSourceInfo) = *Imp;
3742 else
3743 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003744
Gabor Marton26f72a92018-07-12 09:42:05 +00003745 ParmVarDecl *ToParm;
3746 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003747 ToInnerLocStart, ToLocation,
3748 ToDeclName.getAsIdentifierInfo(), ToType,
3749 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003750 /*DefaultArg*/ nullptr))
3751 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003752
3753 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003754 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003755 ToParm->setKNRPromoted(D->isKNRPromoted());
3756
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003757 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003758 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3759 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3760 else
3761 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003762 } else if (D->hasUnparsedDefaultArg()) {
3763 ToParm->setUnparsedDefaultArg();
3764 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003765 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3766 ToParm->setDefaultArg(*ToDefArgOrErr);
3767 else
3768 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003769 }
Sean Callanan59721b32015-04-28 18:41:46 +00003770
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003771 if (D->isObjCMethodParameter()) {
3772 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3773 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3774 } else {
3775 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3776 D->getFunctionScopeIndex());
3777 }
3778
Gabor Marton26f72a92018-07-12 09:42:05 +00003779 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003780}
3781
Balazs Keri3b30d652018-10-19 13:32:20 +00003782ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003783 // Import the major distinguishing characteristics of a method.
3784 DeclContext *DC, *LexicalDC;
3785 DeclarationName Name;
3786 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003787 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003788 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3789 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003790 if (ToD)
3791 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003792
Gabor Marton54058b52018-12-17 13:53:12 +00003793 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003794 for (auto *FoundDecl : FoundDecls) {
3795 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003796 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3797 continue;
3798
3799 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003800 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3801 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003802 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003803 << D->isInstanceMethod() << Name << D->getReturnType()
3804 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003805 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003806 diag::note_odr_objc_method_here)
3807 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003808
3809 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003810 }
3811
3812 // Check the number of parameters.
3813 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003814 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003815 << D->isInstanceMethod() << Name
3816 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003817 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003818 diag::note_odr_objc_method_here)
3819 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003820
3821 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003822 }
3823
3824 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003825 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003826 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3827 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003828 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003829 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003830 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003831 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003832 << D->isInstanceMethod() << Name
3833 << (*P)->getType() << (*FoundP)->getType();
3834 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3835 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003836
3837 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003838 }
3839 }
3840
3841 // Check variadic/non-variadic.
3842 // Check the number of parameters.
3843 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003844 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003845 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003846 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003847 diag::note_odr_objc_method_here)
3848 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003849
3850 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003851 }
3852
3853 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003854 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003855 }
3856 }
3857
Balazs Keri3b30d652018-10-19 13:32:20 +00003858 SourceLocation ToEndLoc;
3859 QualType ToReturnType;
3860 TypeSourceInfo *ToReturnTypeSourceInfo;
3861 if (auto Imp = importSeq(
3862 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3863 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3864 else
3865 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003866
Gabor Marton26f72a92018-07-12 09:42:05 +00003867 ObjCMethodDecl *ToMethod;
3868 if (GetImportedOrCreateDecl(
3869 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003870 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3871 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003872 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3873 D->getImplementationControl(), D->hasRelatedResultType()))
3874 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003875
3876 // FIXME: When we decide to merge method definitions, we'll need to
3877 // deal with implicit parameters.
3878
3879 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003880 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003881 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003882 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3883 ToParams.push_back(*ToPOrErr);
3884 else
3885 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003886 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003887
Douglas Gregor43f54792010-02-17 02:12:47 +00003888 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003889 for (auto *ToParam : ToParams) {
3890 ToParam->setOwningFunction(ToMethod);
3891 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003892 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003893
Balazs Keri3b30d652018-10-19 13:32:20 +00003894 SmallVector<SourceLocation, 12> FromSelLocs;
3895 D->getSelectorLocs(FromSelLocs);
3896 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3897 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3898 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003899
Balazs Keri3b30d652018-10-19 13:32:20 +00003900 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003901
3902 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003903 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003904 return ToMethod;
3905}
3906
Balazs Keri3b30d652018-10-19 13:32:20 +00003907ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003908 // Import the major distinguishing characteristics of a category.
3909 DeclContext *DC, *LexicalDC;
3910 DeclarationName Name;
3911 SourceLocation Loc;
3912 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003913 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3914 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003915 if (ToD)
3916 return ToD;
3917
Balazs Keri3b30d652018-10-19 13:32:20 +00003918 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3919 TypeSourceInfo *ToTypeSourceInfo;
3920 if (auto Imp = importSeq(
3921 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3922 D->getTypeSourceInfo()))
3923 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3924 else
3925 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003926
Gabor Marton26f72a92018-07-12 09:42:05 +00003927 ObjCTypeParamDecl *Result;
3928 if (GetImportedOrCreateDecl(
3929 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003930 ToVarianceLoc, D->getIndex(),
3931 ToLocation, Name.getAsIdentifierInfo(),
3932 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003933 return Result;
3934
Douglas Gregor85f3f952015-07-07 03:57:15 +00003935 Result->setLexicalDeclContext(LexicalDC);
3936 return Result;
3937}
3938
Balazs Keri3b30d652018-10-19 13:32:20 +00003939ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003940 // Import the major distinguishing characteristics of a category.
3941 DeclContext *DC, *LexicalDC;
3942 DeclarationName Name;
3943 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003944 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003945 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3946 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003947 if (ToD)
3948 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003949
Balazs Keri3b30d652018-10-19 13:32:20 +00003950 ObjCInterfaceDecl *ToInterface;
3951 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3952 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003953
Douglas Gregor84c51c32010-02-18 01:47:50 +00003954 // Determine if we've already encountered this category.
3955 ObjCCategoryDecl *MergeWithCategory
3956 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3957 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3958 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003959 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3960 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3961 if (auto Imp = importSeq(
3962 D->getAtStartLoc(), D->getCategoryNameLoc(),
3963 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3964 std::tie(
3965 ToAtStartLoc, ToCategoryNameLoc,
3966 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3967 else
3968 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003969
3970 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003971 ToAtStartLoc, Loc,
3972 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003973 Name.getAsIdentifierInfo(), ToInterface,
3974 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00003975 ToIvarLBraceLoc,
3976 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00003977 return ToCategory;
3978
Douglas Gregor84c51c32010-02-18 01:47:50 +00003979 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003980 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00003981 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003982 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00003983 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
3984 ToCategory->setTypeParamList(*PListOrErr);
3985 else
3986 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003987
Douglas Gregor84c51c32010-02-18 01:47:50 +00003988 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003989 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3990 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003991 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3992 = D->protocol_loc_begin();
3993 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3994 FromProtoEnd = D->protocol_end();
3995 FromProto != FromProtoEnd;
3996 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003997 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
3998 Protocols.push_back(*ToProtoOrErr);
3999 else
4000 return ToProtoOrErr.takeError();
4001
4002 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4003 ProtocolLocs.push_back(*ToProtoLocOrErr);
4004 else
4005 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004006 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004007
Douglas Gregor84c51c32010-02-18 01:47:50 +00004008 // FIXME: If we're merging, make sure that the protocol list is the same.
4009 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4010 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004011
Douglas Gregor84c51c32010-02-18 01:47:50 +00004012 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004013 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004014 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004015
Douglas Gregor84c51c32010-02-18 01:47:50 +00004016 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004017 if (Error Err = ImportDeclContext(D))
4018 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004019
Douglas Gregor84c51c32010-02-18 01:47:50 +00004020 // If we have an implementation, import it as well.
4021 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004022 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4023 import(D->getImplementation()))
4024 ToCategory->setImplementation(*ToImplOrErr);
4025 else
4026 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004027 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004028
Douglas Gregor84c51c32010-02-18 01:47:50 +00004029 return ToCategory;
4030}
4031
Balazs Keri3b30d652018-10-19 13:32:20 +00004032Error ASTNodeImporter::ImportDefinition(
4033 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004034 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004035 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004036 if (Error Err = ImportDeclContext(From))
4037 return Err;
4038 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004039 }
4040
4041 // Start the protocol definition
4042 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004043
Douglas Gregor2aa53772012-01-24 17:42:07 +00004044 // Import protocols
4045 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4046 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004047 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4048 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004049 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4050 FromProtoEnd = From->protocol_end();
4051 FromProto != FromProtoEnd;
4052 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004053 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4054 Protocols.push_back(*ToProtoOrErr);
4055 else
4056 return ToProtoOrErr.takeError();
4057
4058 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4059 ProtocolLocs.push_back(*ToProtoLocOrErr);
4060 else
4061 return ToProtoLocOrErr.takeError();
4062
Douglas Gregor2aa53772012-01-24 17:42:07 +00004063 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004064
Douglas Gregor2aa53772012-01-24 17:42:07 +00004065 // FIXME: If we're merging, make sure that the protocol list is the same.
4066 To->setProtocolList(Protocols.data(), Protocols.size(),
4067 ProtocolLocs.data(), Importer.getToContext());
4068
Douglas Gregor2e15c842012-02-01 21:00:38 +00004069 if (shouldForceImportDeclContext(Kind)) {
4070 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004071 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4072 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004073 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004074 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004075}
4076
Balazs Keri3b30d652018-10-19 13:32:20 +00004077ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004078 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004079 // from, but this particular declaration is not that definition, import the
4080 // definition and map to that.
4081 ObjCProtocolDecl *Definition = D->getDefinition();
4082 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004083 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4084 return Importer.MapImported(D, *ImportedDefOrErr);
4085 else
4086 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004087 }
4088
Douglas Gregor84c51c32010-02-18 01:47:50 +00004089 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004090 DeclContext *DC, *LexicalDC;
4091 DeclarationName Name;
4092 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004093 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004094 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4095 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004096 if (ToD)
4097 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004098
Craig Topper36250ad2014-05-12 05:36:57 +00004099 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004100 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004101 for (auto *FoundDecl : FoundDecls) {
4102 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004103 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004104
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004105 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004106 break;
4107 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004108
Douglas Gregor98d156a2010-02-17 16:12:00 +00004109 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004110 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004111 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4112 if (!ToAtBeginLocOrErr)
4113 return ToAtBeginLocOrErr.takeError();
4114
Gabor Marton26f72a92018-07-12 09:42:05 +00004115 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4116 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004117 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004118 /*PrevDecl=*/nullptr))
4119 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004120 ToProto->setLexicalDeclContext(LexicalDC);
4121 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004122 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004123
4124 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004125
Balazs Keri3b30d652018-10-19 13:32:20 +00004126 if (D->isThisDeclarationADefinition())
4127 if (Error Err = ImportDefinition(D, ToProto))
4128 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004129
Douglas Gregor98d156a2010-02-17 16:12:00 +00004130 return ToProto;
4131}
4132
Balazs Keri3b30d652018-10-19 13:32:20 +00004133ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4134 DeclContext *DC, *LexicalDC;
4135 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4136 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004137
Balazs Keri3b30d652018-10-19 13:32:20 +00004138 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4139 if (!ExternLocOrErr)
4140 return ExternLocOrErr.takeError();
4141
4142 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4143 if (!LangLocOrErr)
4144 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004145
4146 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004147
4148 LinkageSpecDecl *ToLinkageSpec;
4149 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004150 *ExternLocOrErr, *LangLocOrErr,
4151 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004152 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004153
4154 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004155 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4156 if (!RBraceLocOrErr)
4157 return RBraceLocOrErr.takeError();
4158 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004159 }
4160
4161 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4162 LexicalDC->addDeclInternal(ToLinkageSpec);
4163
Sean Callanan0aae0412014-12-10 00:00:37 +00004164 return ToLinkageSpec;
4165}
4166
Balazs Keri3b30d652018-10-19 13:32:20 +00004167ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004168 DeclContext *DC, *LexicalDC;
4169 DeclarationName Name;
4170 SourceLocation Loc;
4171 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004172 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4173 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004174 if (ToD)
4175 return ToD;
4176
Balazs Keri3b30d652018-10-19 13:32:20 +00004177 SourceLocation ToLoc, ToUsingLoc;
4178 NestedNameSpecifierLoc ToQualifierLoc;
4179 if (auto Imp = importSeq(
4180 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4181 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4182 else
4183 return Imp.takeError();
4184
4185 DeclarationNameInfo NameInfo(Name, ToLoc);
4186 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4187 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004188
Gabor Marton26f72a92018-07-12 09:42:05 +00004189 UsingDecl *ToUsing;
4190 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004191 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004192 D->hasTypename()))
4193 return ToUsing;
4194
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004195 ToUsing->setLexicalDeclContext(LexicalDC);
4196 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004197
4198 if (NamedDecl *FromPattern =
4199 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004200 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4201 Importer.getToContext().setInstantiatedFromUsingDecl(
4202 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004203 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004204 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004205 }
4206
Balazs Keri3b30d652018-10-19 13:32:20 +00004207 for (UsingShadowDecl *FromShadow : D->shadows()) {
4208 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4209 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004210 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004211 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004212 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004213 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004214 }
4215 return ToUsing;
4216}
4217
Balazs Keri3b30d652018-10-19 13:32:20 +00004218ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004219 DeclContext *DC, *LexicalDC;
4220 DeclarationName Name;
4221 SourceLocation Loc;
4222 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004223 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4224 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004225 if (ToD)
4226 return ToD;
4227
Balazs Keri3b30d652018-10-19 13:32:20 +00004228 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4229 if (!ToUsingOrErr)
4230 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004231
Balazs Keri3b30d652018-10-19 13:32:20 +00004232 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4233 if (!ToTargetOrErr)
4234 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004235
Gabor Marton26f72a92018-07-12 09:42:05 +00004236 UsingShadowDecl *ToShadow;
4237 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004238 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004239 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004240
4241 ToShadow->setLexicalDeclContext(LexicalDC);
4242 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004243
4244 if (UsingShadowDecl *FromPattern =
4245 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004246 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4247 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4248 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004249 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004250 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004251 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004252 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004253 }
4254
4255 LexicalDC->addDeclInternal(ToShadow);
4256
4257 return ToShadow;
4258}
4259
Balazs Keri3b30d652018-10-19 13:32:20 +00004260ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004261 DeclContext *DC, *LexicalDC;
4262 DeclarationName Name;
4263 SourceLocation Loc;
4264 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004265 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4266 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004267 if (ToD)
4268 return ToD;
4269
Balazs Keri3b30d652018-10-19 13:32:20 +00004270 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4271 if (!ToComAncestorOrErr)
4272 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004273
Balazs Keri3b30d652018-10-19 13:32:20 +00004274 NamespaceDecl *ToNominatedNamespace;
4275 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4276 NestedNameSpecifierLoc ToQualifierLoc;
4277 if (auto Imp = importSeq(
4278 D->getNominatedNamespace(), D->getUsingLoc(),
4279 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4280 D->getIdentLocation()))
4281 std::tie(
4282 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4283 ToQualifierLoc, ToIdentLocation) = *Imp;
4284 else
4285 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004286
Gabor Marton26f72a92018-07-12 09:42:05 +00004287 UsingDirectiveDecl *ToUsingDir;
4288 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004289 ToUsingLoc,
4290 ToNamespaceKeyLocation,
4291 ToQualifierLoc,
4292 ToIdentLocation,
4293 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004294 return ToUsingDir;
4295
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004296 ToUsingDir->setLexicalDeclContext(LexicalDC);
4297 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004298
4299 return ToUsingDir;
4300}
4301
Balazs Keri3b30d652018-10-19 13:32:20 +00004302ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004303 UnresolvedUsingValueDecl *D) {
4304 DeclContext *DC, *LexicalDC;
4305 DeclarationName Name;
4306 SourceLocation Loc;
4307 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004308 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4309 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004310 if (ToD)
4311 return ToD;
4312
Balazs Keri3b30d652018-10-19 13:32:20 +00004313 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4314 NestedNameSpecifierLoc ToQualifierLoc;
4315 if (auto Imp = importSeq(
4316 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4317 D->getEllipsisLoc()))
4318 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4319 else
4320 return Imp.takeError();
4321
4322 DeclarationNameInfo NameInfo(Name, ToLoc);
4323 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4324 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004325
Gabor Marton26f72a92018-07-12 09:42:05 +00004326 UnresolvedUsingValueDecl *ToUsingValue;
4327 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004328 ToUsingLoc, ToQualifierLoc, NameInfo,
4329 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004330 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004331
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004332 ToUsingValue->setAccess(D->getAccess());
4333 ToUsingValue->setLexicalDeclContext(LexicalDC);
4334 LexicalDC->addDeclInternal(ToUsingValue);
4335
4336 return ToUsingValue;
4337}
4338
Balazs Keri3b30d652018-10-19 13:32:20 +00004339ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004340 UnresolvedUsingTypenameDecl *D) {
4341 DeclContext *DC, *LexicalDC;
4342 DeclarationName Name;
4343 SourceLocation Loc;
4344 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004345 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4346 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004347 if (ToD)
4348 return ToD;
4349
Balazs Keri3b30d652018-10-19 13:32:20 +00004350 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4351 NestedNameSpecifierLoc ToQualifierLoc;
4352 if (auto Imp = importSeq(
4353 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4354 D->getEllipsisLoc()))
4355 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4356 else
4357 return Imp.takeError();
4358
Gabor Marton26f72a92018-07-12 09:42:05 +00004359 UnresolvedUsingTypenameDecl *ToUsing;
4360 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004361 ToUsingLoc, ToTypenameLoc,
4362 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004363 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004364
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004365 ToUsing->setAccess(D->getAccess());
4366 ToUsing->setLexicalDeclContext(LexicalDC);
4367 LexicalDC->addDeclInternal(ToUsing);
4368
4369 return ToUsing;
4370}
4371
Balazs Keri3b30d652018-10-19 13:32:20 +00004372
4373Error ASTNodeImporter::ImportDefinition(
4374 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004375 if (To->getDefinition()) {
4376 // Check consistency of superclass.
4377 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4378 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004379 if (auto FromSuperOrErr = import(FromSuper))
4380 FromSuper = *FromSuperOrErr;
4381 else
4382 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004383 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004384
4385 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004386 if ((bool)FromSuper != (bool)ToSuper ||
4387 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004388 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004389 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004390 << To->getDeclName();
4391 if (ToSuper)
4392 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4393 << To->getSuperClass()->getDeclName();
4394 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004395 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004396 diag::note_odr_objc_missing_superclass);
4397 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004398 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004399 diag::note_odr_objc_superclass)
4400 << From->getSuperClass()->getDeclName();
4401 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004402 Importer.FromDiag(From->getLocation(),
4403 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004404 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004405
Douglas Gregor2e15c842012-02-01 21:00:38 +00004406 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004407 if (Error Err = ImportDeclContext(From))
4408 return Err;
4409 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004410 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004411
Douglas Gregor2aa53772012-01-24 17:42:07 +00004412 // Start the definition.
4413 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004414
Douglas Gregor2aa53772012-01-24 17:42:07 +00004415 // If this class has a superclass, import it.
4416 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004417 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4418 To->setSuperClass(*SuperTInfoOrErr);
4419 else
4420 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004421 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004422
Douglas Gregor2aa53772012-01-24 17:42:07 +00004423 // Import protocols
4424 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4425 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004426 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4427 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004428
Douglas Gregor2aa53772012-01-24 17:42:07 +00004429 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4430 FromProtoEnd = From->protocol_end();
4431 FromProto != FromProtoEnd;
4432 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004433 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4434 Protocols.push_back(*ToProtoOrErr);
4435 else
4436 return ToProtoOrErr.takeError();
4437
4438 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4439 ProtocolLocs.push_back(*ToProtoLocOrErr);
4440 else
4441 return ToProtoLocOrErr.takeError();
4442
Douglas Gregor2aa53772012-01-24 17:42:07 +00004443 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004444
Douglas Gregor2aa53772012-01-24 17:42:07 +00004445 // FIXME: If we're merging, make sure that the protocol list is the same.
4446 To->setProtocolList(Protocols.data(), Protocols.size(),
4447 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004448
Douglas Gregor2aa53772012-01-24 17:42:07 +00004449 // Import categories. When the categories themselves are imported, they'll
4450 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004451 for (auto *Cat : From->known_categories()) {
4452 auto ToCatOrErr = import(Cat);
4453 if (!ToCatOrErr)
4454 return ToCatOrErr.takeError();
4455 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004456
Douglas Gregor2aa53772012-01-24 17:42:07 +00004457 // If we have an @implementation, import it as well.
4458 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004459 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4460 import(From->getImplementation()))
4461 To->setImplementation(*ToImplOrErr);
4462 else
4463 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004464 }
4465
Douglas Gregor2e15c842012-02-01 21:00:38 +00004466 if (shouldForceImportDeclContext(Kind)) {
4467 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004468 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4469 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004470 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004471 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004472}
4473
Balazs Keri3b30d652018-10-19 13:32:20 +00004474Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004475ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4476 if (!list)
4477 return nullptr;
4478
4479 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004480 for (auto *fromTypeParam : *list) {
4481 if (auto toTypeParamOrErr = import(fromTypeParam))
4482 toTypeParams.push_back(*toTypeParamOrErr);
4483 else
4484 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004485 }
4486
Balazs Keri3b30d652018-10-19 13:32:20 +00004487 auto LAngleLocOrErr = import(list->getLAngleLoc());
4488 if (!LAngleLocOrErr)
4489 return LAngleLocOrErr.takeError();
4490
4491 auto RAngleLocOrErr = import(list->getRAngleLoc());
4492 if (!RAngleLocOrErr)
4493 return RAngleLocOrErr.takeError();
4494
Douglas Gregor85f3f952015-07-07 03:57:15 +00004495 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004496 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004497 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004498 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004499}
4500
Balazs Keri3b30d652018-10-19 13:32:20 +00004501ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004502 // If this class has a definition in the translation unit we're coming from,
4503 // but this particular declaration is not that definition, import the
4504 // definition and map to that.
4505 ObjCInterfaceDecl *Definition = D->getDefinition();
4506 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004507 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4508 return Importer.MapImported(D, *ImportedDefOrErr);
4509 else
4510 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004511 }
4512
Douglas Gregor45635322010-02-16 01:20:57 +00004513 // Import the major distinguishing characteristics of an @interface.
4514 DeclContext *DC, *LexicalDC;
4515 DeclarationName Name;
4516 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004517 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004518 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4519 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004520 if (ToD)
4521 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004522
Douglas Gregor2aa53772012-01-24 17:42:07 +00004523 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004524 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004525 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004526 for (auto *FoundDecl : FoundDecls) {
4527 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004528 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004529
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004530 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004531 break;
4532 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004533
Douglas Gregor2aa53772012-01-24 17:42:07 +00004534 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004535 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004536 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004537 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4538 if (!AtBeginLocOrErr)
4539 return AtBeginLocOrErr.takeError();
4540
Gabor Marton26f72a92018-07-12 09:42:05 +00004541 if (GetImportedOrCreateDecl(
4542 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004543 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004544 /*TypeParamList=*/nullptr,
4545 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4546 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004547 ToIface->setLexicalDeclContext(LexicalDC);
4548 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004549 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004550 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004551 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004552 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004553 if (auto ToPListOrErr =
4554 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4555 ToIface->setTypeParamList(*ToPListOrErr);
4556 else
4557 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004558
Balazs Keri3b30d652018-10-19 13:32:20 +00004559 if (D->isThisDeclarationADefinition())
4560 if (Error Err = ImportDefinition(D, ToIface))
4561 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004562
Douglas Gregor98d156a2010-02-17 16:12:00 +00004563 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004564}
4565
Balazs Keri3b30d652018-10-19 13:32:20 +00004566ExpectedDecl
4567ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4568 ObjCCategoryDecl *Category;
4569 if (Error Err = importInto(Category, D->getCategoryDecl()))
4570 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004571
Douglas Gregor4da9d682010-12-07 15:32:12 +00004572 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4573 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004574 DeclContext *DC, *LexicalDC;
4575 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4576 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004577
Balazs Keri3b30d652018-10-19 13:32:20 +00004578 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4579 if (auto Imp = importSeq(
4580 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4581 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4582 else
4583 return Imp.takeError();
4584
Gabor Marton26f72a92018-07-12 09:42:05 +00004585 if (GetImportedOrCreateDecl(
4586 ToImpl, D, Importer.getToContext(), DC,
4587 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004588 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004589 return ToImpl;
4590
Balazs Keri3b30d652018-10-19 13:32:20 +00004591 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004592 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004593 Category->setImplementation(ToImpl);
4594 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004595
Gabor Marton26f72a92018-07-12 09:42:05 +00004596 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004597 if (Error Err = ImportDeclContext(D))
4598 return std::move(Err);
4599
Douglas Gregor4da9d682010-12-07 15:32:12 +00004600 return ToImpl;
4601}
4602
Balazs Keri3b30d652018-10-19 13:32:20 +00004603ExpectedDecl
4604ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004605 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004606 ObjCInterfaceDecl *Iface;
4607 if (Error Err = importInto(Iface, D->getClassInterface()))
4608 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004609
4610 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004611 ObjCInterfaceDecl *Super;
4612 if (Error Err = importInto(Super, D->getSuperClass()))
4613 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004614
4615 ObjCImplementationDecl *Impl = Iface->getImplementation();
4616 if (!Impl) {
4617 // We haven't imported an implementation yet. Create a new @implementation
4618 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004619 DeclContext *DC, *LexicalDC;
4620 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4621 return std::move(Err);
4622
4623 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4624 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4625 if (auto Imp = importSeq(
4626 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4627 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4628 std::tie(
4629 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4630 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4631 else
4632 return Imp.takeError();
4633
Gabor Marton26f72a92018-07-12 09:42:05 +00004634 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004635 DC, Iface, Super,
4636 ToLocation,
4637 ToAtStartLoc,
4638 ToSuperClassLoc,
4639 ToIvarLBraceLoc,
4640 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004641 return Impl;
4642
Balazs Keri3b30d652018-10-19 13:32:20 +00004643 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004644
Douglas Gregorda8025c2010-12-07 01:26:03 +00004645 // Associate the implementation with the class it implements.
4646 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004647 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004648 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004649 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004650
4651 // Verify that the existing @implementation has the same superclass.
4652 if ((Super && !Impl->getSuperClass()) ||
4653 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004654 (Super && Impl->getSuperClass() &&
4655 !declaresSameEntity(Super->getCanonicalDecl(),
4656 Impl->getSuperClass()))) {
4657 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004658 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004659 << Iface->getDeclName();
4660 // FIXME: It would be nice to have the location of the superclass
4661 // below.
4662 if (Impl->getSuperClass())
4663 Importer.ToDiag(Impl->getLocation(),
4664 diag::note_odr_objc_superclass)
4665 << Impl->getSuperClass()->getDeclName();
4666 else
4667 Importer.ToDiag(Impl->getLocation(),
4668 diag::note_odr_objc_missing_superclass);
4669 if (D->getSuperClass())
4670 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004671 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004672 << D->getSuperClass()->getDeclName();
4673 else
4674 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004675 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004676
4677 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004678 }
4679 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004680
Douglas Gregorda8025c2010-12-07 01:26:03 +00004681 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004682 if (Error Err = ImportDeclContext(D))
4683 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004684
4685 return Impl;
4686}
4687
Balazs Keri3b30d652018-10-19 13:32:20 +00004688ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004689 // Import the major distinguishing characteristics of an @property.
4690 DeclContext *DC, *LexicalDC;
4691 DeclarationName Name;
4692 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004693 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004694 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4695 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004696 if (ToD)
4697 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004698
4699 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004700 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004701 for (auto *FoundDecl : FoundDecls) {
4702 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004703 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004704 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004705 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004706 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004707 << Name << D->getType() << FoundProp->getType();
4708 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4709 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004710
4711 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004712 }
4713
4714 // FIXME: Check property attributes, getters, setters, etc.?
4715
4716 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004717 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004718 return FoundProp;
4719 }
4720 }
4721
Balazs Keri3b30d652018-10-19 13:32:20 +00004722 QualType ToType;
4723 TypeSourceInfo *ToTypeSourceInfo;
4724 SourceLocation ToAtLoc, ToLParenLoc;
4725 if (auto Imp = importSeq(
4726 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4727 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4728 else
4729 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004730
4731 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004732 ObjCPropertyDecl *ToProperty;
4733 if (GetImportedOrCreateDecl(
4734 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004735 Name.getAsIdentifierInfo(), ToAtLoc,
4736 ToLParenLoc, ToType,
4737 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004738 return ToProperty;
4739
Balazs Keri3b30d652018-10-19 13:32:20 +00004740 Selector ToGetterName, ToSetterName;
4741 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4742 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4743 ObjCIvarDecl *ToPropertyIvarDecl;
4744 if (auto Imp = importSeq(
4745 D->getGetterName(), D->getSetterName(),
4746 D->getGetterNameLoc(), D->getSetterNameLoc(),
4747 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4748 D->getPropertyIvarDecl()))
4749 std::tie(
4750 ToGetterName, ToSetterName,
4751 ToGetterNameLoc, ToSetterNameLoc,
4752 ToGetterMethodDecl, ToSetterMethodDecl,
4753 ToPropertyIvarDecl) = *Imp;
4754 else
4755 return Imp.takeError();
4756
Douglas Gregora11c4582010-02-17 18:02:10 +00004757 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004758 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004759
4760 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004761 ToProperty->setPropertyAttributesAsWritten(
4762 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004763 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4764 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4765 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4766 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4767 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004768 return ToProperty;
4769}
4770
Balazs Keri3b30d652018-10-19 13:32:20 +00004771ExpectedDecl
4772ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4773 ObjCPropertyDecl *Property;
4774 if (Error Err = importInto(Property, D->getPropertyDecl()))
4775 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004776
Balazs Keri3b30d652018-10-19 13:32:20 +00004777 DeclContext *DC, *LexicalDC;
4778 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4779 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004780
Balazs Keri3b30d652018-10-19 13:32:20 +00004781 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004782
4783 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004784 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004785 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4786 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004787
4788 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004789 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4790 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004791 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004792 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4793 if (auto Imp = importSeq(
4794 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4795 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4796 else
4797 return Imp.takeError();
4798
Gabor Marton26f72a92018-07-12 09:42:05 +00004799 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004800 ToBeginLoc,
4801 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004802 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004803 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004804 return ToImpl;
4805
Douglas Gregor14a49e22010-12-07 18:32:03 +00004806 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004807 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004808 } else {
4809 // Check that we have the same kind of property implementation (@synthesize
4810 // vs. @dynamic).
4811 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004812 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004813 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004814 << Property->getDeclName()
4815 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004816 == ObjCPropertyImplDecl::Dynamic);
4817 Importer.FromDiag(D->getLocation(),
4818 diag::note_odr_objc_property_impl_kind)
4819 << D->getPropertyDecl()->getDeclName()
4820 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004821
4822 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004823 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004824
4825 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004826 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4827 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004828 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004829 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004830 << Property->getDeclName()
4831 << ToImpl->getPropertyIvarDecl()->getDeclName()
4832 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004833 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004834 diag::note_odr_objc_synthesize_ivar_here)
4835 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004836
4837 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004838 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004839
Douglas Gregor14a49e22010-12-07 18:32:03 +00004840 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004841 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004842 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004843
Douglas Gregor14a49e22010-12-07 18:32:03 +00004844 return ToImpl;
4845}
4846
Balazs Keri3b30d652018-10-19 13:32:20 +00004847ExpectedDecl
4848ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004849 // For template arguments, we adopt the translation unit as our declaration
4850 // context. This context will be fixed when the actual template declaration
4851 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004852
Douglas Gregora082a492010-11-30 19:14:50 +00004853 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004854
4855 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4856 if (!BeginLocOrErr)
4857 return BeginLocOrErr.takeError();
4858
4859 ExpectedSLoc LocationOrErr = import(D->getLocation());
4860 if (!LocationOrErr)
4861 return LocationOrErr.takeError();
4862
Gabor Marton26f72a92018-07-12 09:42:05 +00004863 TemplateTypeParmDecl *ToD = nullptr;
4864 (void)GetImportedOrCreateDecl(
4865 ToD, D, Importer.getToContext(),
4866 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004867 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004868 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4869 D->wasDeclaredWithTypename(), D->isParameterPack());
4870 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004871}
4872
Balazs Keri3b30d652018-10-19 13:32:20 +00004873ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004874ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004875 DeclarationName ToDeclName;
4876 SourceLocation ToLocation, ToInnerLocStart;
4877 QualType ToType;
4878 TypeSourceInfo *ToTypeSourceInfo;
4879 if (auto Imp = importSeq(
4880 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4881 D->getInnerLocStart()))
4882 std::tie(
4883 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4884 ToInnerLocStart) = *Imp;
4885 else
4886 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004887
Douglas Gregora082a492010-11-30 19:14:50 +00004888 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004889
4890 NonTypeTemplateParmDecl *ToD = nullptr;
4891 (void)GetImportedOrCreateDecl(
4892 ToD, D, Importer.getToContext(),
4893 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004894 ToInnerLocStart, ToLocation, D->getDepth(),
4895 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4896 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004897 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004898}
4899
Balazs Keri3b30d652018-10-19 13:32:20 +00004900ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004901ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4902 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004903 auto NameOrErr = import(D->getDeclName());
4904 if (!NameOrErr)
4905 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004906
Douglas Gregora082a492010-11-30 19:14:50 +00004907 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004908 ExpectedSLoc LocationOrErr = import(D->getLocation());
4909 if (!LocationOrErr)
4910 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004911
Douglas Gregora082a492010-11-30 19:14:50 +00004912 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004913 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004914 if (!TemplateParamsOrErr)
4915 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004916
Douglas Gregora082a492010-11-30 19:14:50 +00004917 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004918
4919 TemplateTemplateParmDecl *ToD = nullptr;
4920 (void)GetImportedOrCreateDecl(
4921 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004922 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4923 D->getDepth(), D->getPosition(), D->isParameterPack(),
4924 (*NameOrErr).getAsIdentifierInfo(),
4925 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004926 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004927}
4928
Gabor Marton16d98c22019-03-07 13:01:51 +00004929// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004930// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004931template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4932 assert(D->getTemplatedDecl() && "Should be called on templates only");
4933 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004934 if (!ToTemplatedDef)
4935 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004936 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4937 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004938}
4939
Balazs Keri3b30d652018-10-19 13:32:20 +00004940ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004941 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4942
Douglas Gregora082a492010-11-30 19:14:50 +00004943 // Import the major distinguishing characteristics of this class template.
4944 DeclContext *DC, *LexicalDC;
4945 DeclarationName Name;
4946 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004947 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004948 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4949 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004950 if (ToD)
4951 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004952
Gabor Marton7df342a2018-12-17 12:42:12 +00004953 ClassTemplateDecl *FoundByLookup = nullptr;
4954
Douglas Gregora082a492010-11-30 19:14:50 +00004955 // We may already have a template of the same name; try to find and match it.
4956 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004957 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004958 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004959 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004960 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4961 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004962 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004963
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004964 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004965 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4966 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004967
Douglas Gregora082a492010-11-30 19:14:50 +00004968 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004969 ClassTemplateDecl *TemplateWithDef =
4970 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004971 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4972 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004973 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004974 FoundByLookup = FoundTemplate;
4975 break;
Gabor Marton9581c332018-05-23 13:53:36 +00004976 }
Douglas Gregora082a492010-11-30 19:14:50 +00004977 }
Gabor Marton9581c332018-05-23 13:53:36 +00004978
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004979 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004980 }
Gabor Marton9581c332018-05-23 13:53:36 +00004981
Douglas Gregora082a492010-11-30 19:14:50 +00004982 if (!ConflictingDecls.empty()) {
4983 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004984 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004985 ConflictingDecls.size());
4986 }
Gabor Marton9581c332018-05-23 13:53:36 +00004987
Douglas Gregora082a492010-11-30 19:14:50 +00004988 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00004989 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00004990 }
4991
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004992 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4993
Douglas Gregora082a492010-11-30 19:14:50 +00004994 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00004995 CXXRecordDecl *ToTemplated;
4996 if (Error Err = importInto(ToTemplated, FromTemplated))
4997 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004998
Douglas Gregora082a492010-11-30 19:14:50 +00004999 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005000 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005001 if (!TemplateParamsOrErr)
5002 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005003
Gabor Marton26f72a92018-07-12 09:42:05 +00005004 ClassTemplateDecl *D2;
5005 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005006 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005007 return D2;
5008
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005009 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005010
Douglas Gregora082a492010-11-30 19:14:50 +00005011 D2->setAccess(D->getAccess());
5012 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00005013
5014 if (D->getDeclContext()->containsDeclAndLoad(D))
5015 DC->addDeclInternal(D2);
5016 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00005017 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005018
Gabor Marton7df342a2018-12-17 12:42:12 +00005019 if (FoundByLookup) {
5020 auto *Recent =
5021 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5022
5023 // It is possible that during the import of the class template definition
5024 // we start the import of a fwd friend decl of the very same class template
5025 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5026 // had been created earlier and by that time the lookup could not find
5027 // anything existing, so it has no previous decl. Later, (still during the
5028 // import of the fwd friend decl) we start to import the definition again
5029 // and this time the lookup finds the previous fwd friend class template.
5030 // In this case we must set up the previous decl for the templated decl.
5031 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005032 assert(FoundByLookup->getTemplatedDecl() &&
5033 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005034 CXXRecordDecl *PrevTemplated =
5035 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5036 if (ToTemplated != PrevTemplated)
5037 ToTemplated->setPreviousDecl(PrevTemplated);
5038 }
5039
5040 D2->setPreviousDecl(Recent);
5041 }
5042
5043 if (LexicalDC != DC && IsFriend)
5044 DC->makeDeclVisibleInContext(D2);
5045
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005046 if (FromTemplated->isCompleteDefinition() &&
5047 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005048 // FIXME: Import definition!
5049 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005050
Douglas Gregora082a492010-11-30 19:14:50 +00005051 return D2;
5052}
5053
Balazs Keri3b30d652018-10-19 13:32:20 +00005054ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005055 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005056 ClassTemplateDecl *ClassTemplate;
5057 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5058 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005059
Douglas Gregore2e50d332010-12-01 01:36:18 +00005060 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005061 DeclContext *DC, *LexicalDC;
5062 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5063 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005064
5065 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005066 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005067 if (Error Err = ImportTemplateArguments(
5068 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5069 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005070
Douglas Gregore2e50d332010-12-01 01:36:18 +00005071 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005072 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005073 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005074 ClassTemplatePartialSpecializationDecl *PartialSpec =
5075 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5076 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005077 PrevDecl =
5078 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005079 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005080 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005081
Gabor Marton7f8c4002019-03-19 13:34:10 +00005082 if (PrevDecl) {
5083 if (IsStructuralMatch(D, PrevDecl)) {
5084 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5085 Importer.MapImported(D, PrevDecl->getDefinition());
5086 // Import those default field initializers which have been
5087 // instantiated in the "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005088 for (auto *FromField : D->fields()) {
5089 auto ToOrErr = import(FromField);
5090 if (!ToOrErr)
5091 return ToOrErr.takeError();
5092 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005093
Gabor Marton7f8c4002019-03-19 13:34:10 +00005094 // Import those methods which have been instantiated in the
5095 // "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005096 for (CXXMethodDecl *FromM : D->methods()) {
5097 auto ToOrErr = import(FromM);
5098 if (!ToOrErr)
5099 return ToOrErr.takeError();
5100 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005101
Gabor Marton7f8c4002019-03-19 13:34:10 +00005102 // TODO Import instantiated default arguments.
5103 // TODO Import instantiated exception specifications.
5104 //
5105 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5106 // what else could be fused during an AST merge.
5107 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005108 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005109 } else { // ODR violation.
5110 // FIXME HandleNameConflict
5111 return nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005112 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005113 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005114
Gabor Marton7f8c4002019-03-19 13:34:10 +00005115 // Import the location of this declaration.
5116 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5117 if (!BeginLocOrErr)
5118 return BeginLocOrErr.takeError();
5119 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5120 if (!IdLocOrErr)
5121 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005122
Gabor Marton7f8c4002019-03-19 13:34:10 +00005123 // Create the specialization.
5124 ClassTemplateSpecializationDecl *D2 = nullptr;
5125 if (PartialSpec) {
5126 // Import TemplateArgumentListInfo.
5127 TemplateArgumentListInfo ToTAInfo;
5128 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5129 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5130 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005131
Gabor Marton7f8c4002019-03-19 13:34:10 +00005132 QualType CanonInjType;
5133 if (Error Err = importInto(
5134 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5135 return std::move(Err);
5136 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005137
Balazs Keridec09162019-03-20 15:42:42 +00005138 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005139 if (!ToTPListOrErr)
5140 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005141
Gabor Marton7f8c4002019-03-19 13:34:10 +00005142 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5143 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5144 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5145 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5146 ToTAInfo, CanonInjType,
5147 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5148 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005149
Gabor Marton7f8c4002019-03-19 13:34:10 +00005150 // Update InsertPos, because preceding import calls may have invalidated
5151 // it by adding new specializations.
5152 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5153 // Add this partial specialization to the class template.
5154 ClassTemplate->AddPartialSpecialization(
5155 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005156
Gabor Marton7f8c4002019-03-19 13:34:10 +00005157 } else { // Not a partial specialization.
5158 if (GetImportedOrCreateDecl(
5159 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5160 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5161 PrevDecl))
5162 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005163
Gabor Marton7f8c4002019-03-19 13:34:10 +00005164 // Update InsertPos, because preceding import calls may have invalidated
5165 // it by adding new specializations.
5166 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5167 // Add this specialization to the class template.
5168 ClassTemplate->AddSpecialization(D2, InsertPos);
5169 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005170
Gabor Marton7f8c4002019-03-19 13:34:10 +00005171 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005172
Gabor Marton7f8c4002019-03-19 13:34:10 +00005173 // Set the context of this specialization/instantiation.
5174 D2->setLexicalDeclContext(LexicalDC);
5175
5176 // Add to the DC only if it was an explicit specialization/instantiation.
5177 if (D2->isExplicitInstantiationOrSpecialization()) {
5178 LexicalDC->addDeclInternal(D2);
5179 }
5180
5181 // Import the qualifier, if any.
5182 if (auto LocOrErr = import(D->getQualifierLoc()))
5183 D2->setQualifierInfo(*LocOrErr);
5184 else
5185 return LocOrErr.takeError();
5186
5187 if (auto *TSI = D->getTypeAsWritten()) {
5188 if (auto TInfoOrErr = import(TSI))
5189 D2->setTypeAsWritten(*TInfoOrErr);
5190 else
5191 return TInfoOrErr.takeError();
5192
5193 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5194 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005195 else
5196 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005197
Gabor Marton7f8c4002019-03-19 13:34:10 +00005198 if (auto LocOrErr = import(D->getExternLoc()))
5199 D2->setExternLoc(*LocOrErr);
5200 else
5201 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005202 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005203
5204 if (D->getPointOfInstantiation().isValid()) {
5205 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5206 D2->setPointOfInstantiation(*POIOrErr);
5207 else
5208 return POIOrErr.takeError();
5209 }
5210
5211 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5212
Balazs Keri3b30d652018-10-19 13:32:20 +00005213 if (D->isCompleteDefinition())
5214 if (Error Err = ImportDefinition(D, D2))
5215 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005216
Douglas Gregore2e50d332010-12-01 01:36:18 +00005217 return D2;
5218}
5219
Balazs Keri3b30d652018-10-19 13:32:20 +00005220ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005221 // If this variable has a definition in the translation unit we're coming
5222 // from,
5223 // but this particular declaration is not that definition, import the
5224 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005225 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005226 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5227 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005228 if (ExpectedDecl ImportedDefOrErr = import(
5229 Definition->getDescribedVarTemplate()))
5230 return Importer.MapImported(D, *ImportedDefOrErr);
5231 else
5232 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005233 }
5234
5235 // Import the major distinguishing characteristics of this variable template.
5236 DeclContext *DC, *LexicalDC;
5237 DeclarationName Name;
5238 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005239 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005240 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5241 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005242 if (ToD)
5243 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005244
5245 // We may already have a template of the same name; try to find and match it.
5246 assert(!DC->isFunctionOrMethod() &&
5247 "Variable templates cannot be declared at function scope");
5248 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005249 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005250 for (auto *FoundDecl : FoundDecls) {
5251 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005252 continue;
5253
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005254 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005255 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005256 if (IsStructuralMatch(D, FoundTemplate)) {
5257 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005258 Importer.MapImported(D->getTemplatedDecl(),
5259 FoundTemplate->getTemplatedDecl());
5260 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005261 }
5262 }
5263
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005264 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005265 }
5266
5267 if (!ConflictingDecls.empty()) {
5268 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5269 ConflictingDecls.data(),
5270 ConflictingDecls.size());
5271 }
5272
5273 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005274 // FIXME: Is it possible to get other error than name conflict?
5275 // (Put this `if` into the previous `if`?)
5276 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005277
5278 VarDecl *DTemplated = D->getTemplatedDecl();
5279
5280 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005281 // FIXME: Value not used?
5282 ExpectedType TypeOrErr = import(DTemplated->getType());
5283 if (!TypeOrErr)
5284 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005285
5286 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005287 VarDecl *ToTemplated;
5288 if (Error Err = importInto(ToTemplated, DTemplated))
5289 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005290
5291 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005292 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005293 if (!TemplateParamsOrErr)
5294 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005295
Gabor Marton26f72a92018-07-12 09:42:05 +00005296 VarTemplateDecl *ToVarTD;
5297 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005298 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005299 return ToVarTD;
5300
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005301 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005302
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005303 ToVarTD->setAccess(D->getAccess());
5304 ToVarTD->setLexicalDeclContext(LexicalDC);
5305 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005306
Larisse Voufo39a1e502013-08-06 01:03:05 +00005307 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005308 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005309 // FIXME: Import definition!
5310 }
5311
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005312 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005313}
5314
Balazs Keri3b30d652018-10-19 13:32:20 +00005315ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005316 VarTemplateSpecializationDecl *D) {
5317 // If this record has a definition in the translation unit we're coming from,
5318 // but this particular declaration is not that definition, import the
5319 // definition and map to that.
5320 VarDecl *Definition = D->getDefinition();
5321 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005322 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5323 return Importer.MapImported(D, *ImportedDefOrErr);
5324 else
5325 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005326 }
5327
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00005328 VarTemplateDecl *VarTemplate = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005329 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5330 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005331
5332 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005333 DeclContext *DC, *LexicalDC;
5334 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5335 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005336
5337 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005338 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5339 if (!BeginLocOrErr)
5340 return BeginLocOrErr.takeError();
5341
5342 auto IdLocOrErr = import(D->getLocation());
5343 if (!IdLocOrErr)
5344 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005345
5346 // Import template arguments.
5347 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005348 if (Error Err = ImportTemplateArguments(
5349 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5350 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005351
5352 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005353 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005354 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005355 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005356 if (D2) {
5357 // We already have a variable template specialization with these template
5358 // arguments.
5359
5360 // FIXME: Check for specialization vs. instantiation errors.
5361
5362 if (VarDecl *FoundDef = D2->getDefinition()) {
5363 if (!D->isThisDeclarationADefinition() ||
5364 IsStructuralMatch(D, FoundDef)) {
5365 // The record types structurally match, or the "from" translation
5366 // unit only had a forward declaration anyway; call it the same
5367 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005368 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005369 }
5370 }
5371 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005372 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005373 QualType T;
5374 if (Error Err = importInto(T, D->getType()))
5375 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005376
Balazs Keri3b30d652018-10-19 13:32:20 +00005377 auto TInfoOrErr = import(D->getTypeSourceInfo());
5378 if (!TInfoOrErr)
5379 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005380
5381 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005382 if (Error Err = ImportTemplateArgumentListInfo(
5383 D->getTemplateArgsInfo(), ToTAInfo))
5384 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005385
5386 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005387 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005388 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5389 // Import TemplateArgumentListInfo
5390 TemplateArgumentListInfo ArgInfos;
5391 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5392 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005393 if (Error Err = ImportTemplateArgumentListInfo(
5394 *FromTAArgsAsWritten, ArgInfos))
5395 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005396
Balazs Keridec09162019-03-20 15:42:42 +00005397 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005398 if (!ToTPListOrErr)
5399 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005400
Gabor Marton26f72a92018-07-12 09:42:05 +00005401 PartVarSpecDecl *ToPartial;
5402 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005403 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5404 VarTemplate, T, *TInfoOrErr,
5405 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005406 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005407
Balazs Keri3b30d652018-10-19 13:32:20 +00005408 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5409 FromPartial->getInstantiatedFromMember()))
5410 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5411 else
5412 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005413
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005414 if (FromPartial->isMemberSpecialization())
5415 ToPartial->setMemberSpecialization();
5416
5417 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005418
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005419 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005420 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5421 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5422 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005423 D->getStorageClass(), TemplateArgs))
5424 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005425 }
5426
Balazs Keri3b30d652018-10-19 13:32:20 +00005427 if (D->getPointOfInstantiation().isValid()) {
5428 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5429 D2->setPointOfInstantiation(*POIOrErr);
5430 else
5431 return POIOrErr.takeError();
5432 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005433
Larisse Voufo39a1e502013-08-06 01:03:05 +00005434 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005435 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005436
5437 // Add this specialization to the class template.
5438 VarTemplate->AddSpecialization(D2, InsertPos);
5439
5440 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005441 if (auto LocOrErr = import(D->getQualifierLoc()))
5442 D2->setQualifierInfo(*LocOrErr);
5443 else
5444 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005445
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005446 if (D->isConstexpr())
5447 D2->setConstexpr(true);
5448
Larisse Voufo39a1e502013-08-06 01:03:05 +00005449 // Add the specialization to this context.
5450 D2->setLexicalDeclContext(LexicalDC);
5451 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005452
5453 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005454 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005455
Balazs Keri3b30d652018-10-19 13:32:20 +00005456 if (Error Err = ImportInitializer(D, D2))
5457 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005458
5459 return D2;
5460}
5461
Balazs Keri3b30d652018-10-19 13:32:20 +00005462ExpectedDecl
5463ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005464 DeclContext *DC, *LexicalDC;
5465 DeclarationName Name;
5466 SourceLocation Loc;
5467 NamedDecl *ToD;
5468
Balazs Keri3b30d652018-10-19 13:32:20 +00005469 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5470 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005471
5472 if (ToD)
5473 return ToD;
5474
Gabor Marton16d98c22019-03-07 13:01:51 +00005475 const FunctionTemplateDecl *FoundByLookup = nullptr;
5476
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005477 // Try to find a function in our own ("to") context with the same name, same
5478 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005479 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005480 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005481 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005482 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005483 for (auto *FoundDecl : FoundDecls) {
5484 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005485 continue;
5486
Gabor Marton16d98c22019-03-07 13:01:51 +00005487 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5488 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005489 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005490 if (IsStructuralMatch(D, FoundTemplate)) {
5491 FunctionTemplateDecl *TemplateWithDef =
5492 getTemplateDefinition(FoundTemplate);
5493 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5494 return Importer.MapImported(D, TemplateWithDef);
5495 }
5496 FoundByLookup = FoundTemplate;
5497 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005498 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005499 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005500 }
5501 }
5502 }
5503 }
5504
Balazs Keridec09162019-03-20 15:42:42 +00005505 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005506 if (!ParamsOrErr)
5507 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005508
Balazs Keri3b30d652018-10-19 13:32:20 +00005509 FunctionDecl *TemplatedFD;
5510 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5511 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005512
Gabor Marton26f72a92018-07-12 09:42:05 +00005513 FunctionTemplateDecl *ToFunc;
5514 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005515 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005516 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005517
5518 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005519
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005520 ToFunc->setAccess(D->getAccess());
5521 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005522 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005523
5524 if (FoundByLookup) {
5525 auto *Recent =
5526 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5527 if (!TemplatedFD->getPreviousDecl()) {
5528 assert(FoundByLookup->getTemplatedDecl() &&
5529 "Found decl must have its templated decl set");
5530 auto *PrevTemplated =
5531 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5532 if (TemplatedFD != PrevTemplated)
5533 TemplatedFD->setPreviousDecl(PrevTemplated);
5534 }
5535 ToFunc->setPreviousDecl(Recent);
5536 }
5537
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005538 return ToFunc;
5539}
5540
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005541//----------------------------------------------------------------------------
5542// Import Statements
5543//----------------------------------------------------------------------------
5544
Balazs Keri3b30d652018-10-19 13:32:20 +00005545ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005546 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5547 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005548 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005549}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005550
Balazs Keri3b30d652018-10-19 13:32:20 +00005551
5552ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005553 SmallVector<IdentifierInfo *, 4> Names;
5554 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5555 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005556 // ToII is nullptr when no symbolic name is given for output operand
5557 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005558 Names.push_back(ToII);
5559 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005560
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005561 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5562 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005563 // ToII is nullptr when no symbolic name is given for input operand
5564 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005565 Names.push_back(ToII);
5566 }
5567
5568 SmallVector<StringLiteral *, 4> Clobbers;
5569 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005570 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5571 Clobbers.push_back(*ClobberOrErr);
5572 else
5573 return ClobberOrErr.takeError();
5574
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005575 }
5576
5577 SmallVector<StringLiteral *, 4> Constraints;
5578 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005579 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5580 Constraints.push_back(*OutputOrErr);
5581 else
5582 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005583 }
5584
5585 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005586 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5587 Constraints.push_back(*InputOrErr);
5588 else
5589 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005590 }
5591
5592 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005593 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5594 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005595
Balazs Keri3b30d652018-10-19 13:32:20 +00005596 if (Error Err = ImportArrayChecked(
5597 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5598 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005599
Balazs Keri3b30d652018-10-19 13:32:20 +00005600 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5601 if (!AsmLocOrErr)
5602 return AsmLocOrErr.takeError();
5603 auto AsmStrOrErr = import(S->getAsmString());
5604 if (!AsmStrOrErr)
5605 return AsmStrOrErr.takeError();
5606 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5607 if (!RParenLocOrErr)
5608 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005609
5610 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005611 Importer.getToContext(),
5612 *AsmLocOrErr,
5613 S->isSimple(),
5614 S->isVolatile(),
5615 S->getNumOutputs(),
5616 S->getNumInputs(),
5617 Names.data(),
5618 Constraints.data(),
5619 Exprs.data(),
5620 *AsmStrOrErr,
5621 S->getNumClobbers(),
5622 Clobbers.data(),
5623 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005624}
5625
Balazs Keri3b30d652018-10-19 13:32:20 +00005626ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5627 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5628 if (!Imp)
5629 return Imp.takeError();
5630
5631 DeclGroupRef ToDG;
5632 SourceLocation ToBeginLoc, ToEndLoc;
5633 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5634
5635 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005636}
5637
Balazs Keri3b30d652018-10-19 13:32:20 +00005638ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5639 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5640 if (!ToSemiLocOrErr)
5641 return ToSemiLocOrErr.takeError();
5642 return new (Importer.getToContext()) NullStmt(
5643 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005644}
5645
Balazs Keri3b30d652018-10-19 13:32:20 +00005646ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005647 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005648
Balazs Keri3b30d652018-10-19 13:32:20 +00005649 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5650 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005651
Balazs Keri3b30d652018-10-19 13:32:20 +00005652 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5653 if (!ToLBracLocOrErr)
5654 return ToLBracLocOrErr.takeError();
5655
5656 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5657 if (!ToRBracLocOrErr)
5658 return ToRBracLocOrErr.takeError();
5659
5660 return CompoundStmt::Create(
5661 Importer.getToContext(), ToStmts,
5662 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005663}
5664
Balazs Keri3b30d652018-10-19 13:32:20 +00005665ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5666 auto Imp = importSeq(
5667 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5668 S->getEllipsisLoc(), S->getColonLoc());
5669 if (!Imp)
5670 return Imp.takeError();
5671
5672 Expr *ToLHS, *ToRHS;
5673 Stmt *ToSubStmt;
5674 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5675 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5676 *Imp;
5677
Bruno Ricci5b30571752018-10-28 12:30:53 +00005678 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5679 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005680 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005681
Gabor Horvath480892b2017-10-18 09:25:18 +00005682 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005683}
5684
Balazs Keri3b30d652018-10-19 13:32:20 +00005685ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5686 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5687 if (!Imp)
5688 return Imp.takeError();
5689
5690 SourceLocation ToDefaultLoc, ToColonLoc;
5691 Stmt *ToSubStmt;
5692 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5693
5694 return new (Importer.getToContext()) DefaultStmt(
5695 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005696}
5697
Balazs Keri3b30d652018-10-19 13:32:20 +00005698ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5699 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5700 if (!Imp)
5701 return Imp.takeError();
5702
5703 SourceLocation ToIdentLoc;
5704 LabelDecl *ToLabelDecl;
5705 Stmt *ToSubStmt;
5706 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5707
5708 return new (Importer.getToContext()) LabelStmt(
5709 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005710}
5711
Balazs Keri3b30d652018-10-19 13:32:20 +00005712ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5713 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5714 if (!ToAttrLocOrErr)
5715 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005716 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5717 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005718 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5719 return std::move(Err);
5720 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5721 if (!ToSubStmtOrErr)
5722 return ToSubStmtOrErr.takeError();
5723
5724 return AttributedStmt::Create(
5725 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005726}
5727
Balazs Keri3b30d652018-10-19 13:32:20 +00005728ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5729 auto Imp = importSeq(
5730 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5731 S->getThen(), S->getElseLoc(), S->getElse());
5732 if (!Imp)
5733 return Imp.takeError();
5734
5735 SourceLocation ToIfLoc, ToElseLoc;
5736 Stmt *ToInit, *ToThen, *ToElse;
5737 VarDecl *ToConditionVariable;
5738 Expr *ToCond;
5739 std::tie(
5740 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5741 *Imp;
5742
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005743 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5744 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5745 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005746}
5747
Balazs Keri3b30d652018-10-19 13:32:20 +00005748ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5749 auto Imp = importSeq(
5750 S->getInit(), S->getConditionVariable(), S->getCond(),
5751 S->getBody(), S->getSwitchLoc());
5752 if (!Imp)
5753 return Imp.takeError();
5754
5755 Stmt *ToInit, *ToBody;
5756 VarDecl *ToConditionVariable;
5757 Expr *ToCond;
5758 SourceLocation ToSwitchLoc;
5759 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5760
Bruno Riccie2806f82018-10-29 16:12:37 +00005761 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5762 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005763 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005764 ToStmt->setSwitchLoc(ToSwitchLoc);
5765
Sean Callanan59721b32015-04-28 18:41:46 +00005766 // Now we have to re-chain the cases.
5767 SwitchCase *LastChainedSwitchCase = nullptr;
5768 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5769 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005770 Expected<SwitchCase *> ToSCOrErr = import(SC);
5771 if (!ToSCOrErr)
5772 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005773 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005774 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005775 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005776 ToStmt->setSwitchCaseList(*ToSCOrErr);
5777 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005778 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005779
Sean Callanan59721b32015-04-28 18:41:46 +00005780 return ToStmt;
5781}
5782
Balazs Keri3b30d652018-10-19 13:32:20 +00005783ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5784 auto Imp = importSeq(
5785 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5786 if (!Imp)
5787 return Imp.takeError();
5788
5789 VarDecl *ToConditionVariable;
5790 Expr *ToCond;
5791 Stmt *ToBody;
5792 SourceLocation ToWhileLoc;
5793 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5794
Bruno Riccibacf7512018-10-30 13:42:41 +00005795 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5796 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005797}
5798
Balazs Keri3b30d652018-10-19 13:32:20 +00005799ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5800 auto Imp = importSeq(
5801 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5802 S->getRParenLoc());
5803 if (!Imp)
5804 return Imp.takeError();
5805
5806 Stmt *ToBody;
5807 Expr *ToCond;
5808 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5809 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5810
5811 return new (Importer.getToContext()) DoStmt(
5812 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005813}
5814
Balazs Keri3b30d652018-10-19 13:32:20 +00005815ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5816 auto Imp = importSeq(
5817 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5818 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5819 if (!Imp)
5820 return Imp.takeError();
5821
5822 Stmt *ToInit;
5823 Expr *ToCond, *ToInc;
5824 VarDecl *ToConditionVariable;
5825 Stmt *ToBody;
5826 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5827 std::tie(
5828 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5829 ToLParenLoc, ToRParenLoc) = *Imp;
5830
5831 return new (Importer.getToContext()) ForStmt(
5832 Importer.getToContext(),
5833 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5834 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005835}
5836
Balazs Keri3b30d652018-10-19 13:32:20 +00005837ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5838 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5839 if (!Imp)
5840 return Imp.takeError();
5841
5842 LabelDecl *ToLabel;
5843 SourceLocation ToGotoLoc, ToLabelLoc;
5844 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5845
5846 return new (Importer.getToContext()) GotoStmt(
5847 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005848}
5849
Balazs Keri3b30d652018-10-19 13:32:20 +00005850ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5851 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5852 if (!Imp)
5853 return Imp.takeError();
5854
5855 SourceLocation ToGotoLoc, ToStarLoc;
5856 Expr *ToTarget;
5857 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5858
5859 return new (Importer.getToContext()) IndirectGotoStmt(
5860 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005861}
5862
Balazs Keri3b30d652018-10-19 13:32:20 +00005863ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5864 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5865 if (!ToContinueLocOrErr)
5866 return ToContinueLocOrErr.takeError();
5867 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005868}
5869
Balazs Keri3b30d652018-10-19 13:32:20 +00005870ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5871 auto ToBreakLocOrErr = import(S->getBreakLoc());
5872 if (!ToBreakLocOrErr)
5873 return ToBreakLocOrErr.takeError();
5874 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005875}
5876
Balazs Keri3b30d652018-10-19 13:32:20 +00005877ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5878 auto Imp = importSeq(
5879 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5880 if (!Imp)
5881 return Imp.takeError();
5882
5883 SourceLocation ToReturnLoc;
5884 Expr *ToRetValue;
5885 const VarDecl *ToNRVOCandidate;
5886 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5887
Bruno Ricci023b1d12018-10-30 14:40:49 +00005888 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5889 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005890}
5891
Balazs Keri3b30d652018-10-19 13:32:20 +00005892ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5893 auto Imp = importSeq(
5894 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5895 if (!Imp)
5896 return Imp.takeError();
5897
5898 SourceLocation ToCatchLoc;
5899 VarDecl *ToExceptionDecl;
5900 Stmt *ToHandlerBlock;
5901 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5902
5903 return new (Importer.getToContext()) CXXCatchStmt (
5904 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005905}
5906
Balazs Keri3b30d652018-10-19 13:32:20 +00005907ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5908 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5909 if (!ToTryLocOrErr)
5910 return ToTryLocOrErr.takeError();
5911
5912 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5913 if (!ToTryBlockOrErr)
5914 return ToTryBlockOrErr.takeError();
5915
Sean Callanan59721b32015-04-28 18:41:46 +00005916 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5917 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5918 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005919 if (auto ToHandlerOrErr = import(FromHandler))
5920 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005921 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005922 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005923 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005924
5925 return CXXTryStmt::Create(
5926 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005927}
5928
Balazs Keri3b30d652018-10-19 13:32:20 +00005929ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5930 auto Imp1 = importSeq(
5931 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5932 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5933 if (!Imp1)
5934 return Imp1.takeError();
5935 auto Imp2 = importSeq(
5936 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5937 if (!Imp2)
5938 return Imp2.takeError();
5939
5940 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5941 Expr *ToCond, *ToInc;
5942 Stmt *ToInit, *ToBody;
5943 std::tie(
5944 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5945 ToBody) = *Imp1;
5946 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5947 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5948
5949 return new (Importer.getToContext()) CXXForRangeStmt(
5950 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5951 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005952}
5953
Balazs Keri3b30d652018-10-19 13:32:20 +00005954ExpectedStmt
5955ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5956 auto Imp = importSeq(
5957 S->getElement(), S->getCollection(), S->getBody(),
5958 S->getForLoc(), S->getRParenLoc());
5959 if (!Imp)
5960 return Imp.takeError();
5961
5962 Stmt *ToElement, *ToBody;
5963 Expr *ToCollection;
5964 SourceLocation ToForLoc, ToRParenLoc;
5965 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5966
5967 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
5968 ToCollection,
5969 ToBody,
5970 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00005971 ToRParenLoc);
5972}
5973
Balazs Keri3b30d652018-10-19 13:32:20 +00005974ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5975 auto Imp = importSeq(
5976 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
5977 S->getCatchBody());
5978 if (!Imp)
5979 return Imp.takeError();
5980
5981 SourceLocation ToAtCatchLoc, ToRParenLoc;
5982 VarDecl *ToCatchParamDecl;
5983 Stmt *ToCatchBody;
5984 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
5985
5986 return new (Importer.getToContext()) ObjCAtCatchStmt (
5987 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00005988}
5989
Balazs Keri3b30d652018-10-19 13:32:20 +00005990ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5991 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
5992 if (!ToAtFinallyLocOrErr)
5993 return ToAtFinallyLocOrErr.takeError();
5994 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
5995 if (!ToAtFinallyStmtOrErr)
5996 return ToAtFinallyStmtOrErr.takeError();
5997 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
5998 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005999}
6000
Balazs Keri3b30d652018-10-19 13:32:20 +00006001ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6002 auto Imp = importSeq(
6003 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6004 if (!Imp)
6005 return Imp.takeError();
6006
6007 SourceLocation ToAtTryLoc;
6008 Stmt *ToTryBody, *ToFinallyStmt;
6009 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6010
Sean Callanan59721b32015-04-28 18:41:46 +00006011 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6012 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6013 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006014 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6015 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006016 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006017 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006018 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006019
Sean Callanan59721b32015-04-28 18:41:46 +00006020 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006021 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006022 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006023 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006024}
6025
Balazs Keri3b30d652018-10-19 13:32:20 +00006026ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006027 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006028 auto Imp = importSeq(
6029 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6030 if (!Imp)
6031 return Imp.takeError();
6032
6033 SourceLocation ToAtSynchronizedLoc;
6034 Expr *ToSynchExpr;
6035 Stmt *ToSynchBody;
6036 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6037
Sean Callanan59721b32015-04-28 18:41:46 +00006038 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6039 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6040}
6041
Balazs Keri3b30d652018-10-19 13:32:20 +00006042ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6043 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6044 if (!ToThrowLocOrErr)
6045 return ToThrowLocOrErr.takeError();
6046 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6047 if (!ToThrowExprOrErr)
6048 return ToThrowExprOrErr.takeError();
6049 return new (Importer.getToContext()) ObjCAtThrowStmt(
6050 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006051}
6052
Balazs Keri3b30d652018-10-19 13:32:20 +00006053ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6054 ObjCAutoreleasePoolStmt *S) {
6055 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6056 if (!ToAtLocOrErr)
6057 return ToAtLocOrErr.takeError();
6058 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6059 if (!ToSubStmtOrErr)
6060 return ToSubStmtOrErr.takeError();
6061 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6062 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006063}
6064
6065//----------------------------------------------------------------------------
6066// Import Expressions
6067//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006068ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006069 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6070 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006071 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006072}
6073
Balazs Keri3b30d652018-10-19 13:32:20 +00006074ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6075 auto Imp = importSeq(
6076 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6077 E->getRParenLoc(), E->getType());
6078 if (!Imp)
6079 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006080
Balazs Keri3b30d652018-10-19 13:32:20 +00006081 SourceLocation ToBuiltinLoc, ToRParenLoc;
6082 Expr *ToSubExpr;
6083 TypeSourceInfo *ToWrittenTypeInfo;
6084 QualType ToType;
6085 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6086 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006087
6088 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006089 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6090 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006091}
6092
Tom Roeder521f0042019-02-26 19:26:41 +00006093ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6094 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6095 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6096 if (!Imp)
6097 return Imp.takeError();
6098
6099 Expr *ToCond;
6100 Expr *ToLHS;
6101 Expr *ToRHS;
6102 SourceLocation ToBuiltinLoc, ToRParenLoc;
6103 QualType ToType;
6104 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6105
6106 ExprValueKind VK = E->getValueKind();
6107 ExprObjectKind OK = E->getObjectKind();
6108
6109 bool TypeDependent = ToCond->isTypeDependent();
6110 bool ValueDependent = ToCond->isValueDependent();
6111
6112 // The value of CondIsTrue only matters if the value is not
6113 // condition-dependent.
6114 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6115
6116 return new (Importer.getToContext())
6117 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6118 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6119}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006120
Balazs Keri3b30d652018-10-19 13:32:20 +00006121ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6122 ExpectedType TypeOrErr = import(E->getType());
6123 if (!TypeOrErr)
6124 return TypeOrErr.takeError();
6125
6126 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6127 if (!BeginLocOrErr)
6128 return BeginLocOrErr.takeError();
6129
6130 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006131}
6132
Balazs Keri3b30d652018-10-19 13:32:20 +00006133ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6134 auto Imp = importSeq(
6135 E->getBeginLoc(), E->getType(), E->getFunctionName());
6136 if (!Imp)
6137 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006138
Balazs Keri3b30d652018-10-19 13:32:20 +00006139 SourceLocation ToBeginLoc;
6140 QualType ToType;
6141 StringLiteral *ToFunctionName;
6142 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006143
Bruno Ricci17ff0262018-10-27 19:21:19 +00006144 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6145 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006146}
6147
Balazs Keri3b30d652018-10-19 13:32:20 +00006148ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6149 auto Imp = importSeq(
6150 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6151 E->getLocation(), E->getType());
6152 if (!Imp)
6153 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006154
Balazs Keri3b30d652018-10-19 13:32:20 +00006155 NestedNameSpecifierLoc ToQualifierLoc;
6156 SourceLocation ToTemplateKeywordLoc, ToLocation;
6157 ValueDecl *ToDecl;
6158 QualType ToType;
6159 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6160 *Imp;
6161
6162 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006163 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006164 auto FoundDOrErr = import(E->getFoundDecl());
6165 if (!FoundDOrErr)
6166 return FoundDOrErr.takeError();
6167 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006168 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006169
Aleksei Sidorina693b372016-09-28 10:16:56 +00006170 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006171 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006172 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006173 if (Error Err =
6174 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6175 return std::move(Err);
6176 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006177 }
6178
Balazs Keri3b30d652018-10-19 13:32:20 +00006179 auto *ToE = DeclRefExpr::Create(
6180 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6181 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6182 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006183 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006184 ToE->setHadMultipleCandidates(true);
6185 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006186}
6187
Balazs Keri3b30d652018-10-19 13:32:20 +00006188ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6189 ExpectedType TypeOrErr = import(E->getType());
6190 if (!TypeOrErr)
6191 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006192
Balazs Keri3b30d652018-10-19 13:32:20 +00006193 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006194}
6195
Balazs Keri3b30d652018-10-19 13:32:20 +00006196ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6197 ExpectedExpr ToInitOrErr = import(E->getInit());
6198 if (!ToInitOrErr)
6199 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006200
Balazs Keri3b30d652018-10-19 13:32:20 +00006201 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6202 if (!ToEqualOrColonLocOrErr)
6203 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006204
Balazs Keri3b30d652018-10-19 13:32:20 +00006205 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006206 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006207 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6208 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6209 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006210 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006211 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006212 }
6213
Balazs Keri3b30d652018-10-19 13:32:20 +00006214 SmallVector<Designator, 4> ToDesignators(E->size());
6215 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6216 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006217
6218 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006219 Importer.getToContext(), ToDesignators,
6220 ToIndexExprs, *ToEqualOrColonLocOrErr,
6221 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006222}
6223
Balazs Keri3b30d652018-10-19 13:32:20 +00006224ExpectedStmt
6225ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6226 ExpectedType ToTypeOrErr = import(E->getType());
6227 if (!ToTypeOrErr)
6228 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006229
Balazs Keri3b30d652018-10-19 13:32:20 +00006230 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6231 if (!ToLocationOrErr)
6232 return ToLocationOrErr.takeError();
6233
6234 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6235 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006236}
6237
Balazs Keri3b30d652018-10-19 13:32:20 +00006238ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6239 ExpectedType ToTypeOrErr = import(E->getType());
6240 if (!ToTypeOrErr)
6241 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006242
Balazs Keri3b30d652018-10-19 13:32:20 +00006243 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6244 if (!ToLocationOrErr)
6245 return ToLocationOrErr.takeError();
6246
6247 return IntegerLiteral::Create(
6248 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006249}
6250
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006251
Balazs Keri3b30d652018-10-19 13:32:20 +00006252ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6253 ExpectedType ToTypeOrErr = import(E->getType());
6254 if (!ToTypeOrErr)
6255 return ToTypeOrErr.takeError();
6256
6257 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6258 if (!ToLocationOrErr)
6259 return ToLocationOrErr.takeError();
6260
6261 return FloatingLiteral::Create(
6262 Importer.getToContext(), E->getValue(), E->isExact(),
6263 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006264}
6265
Balazs Keri3b30d652018-10-19 13:32:20 +00006266ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6267 auto ToTypeOrErr = import(E->getType());
6268 if (!ToTypeOrErr)
6269 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006270
Balazs Keri3b30d652018-10-19 13:32:20 +00006271 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6272 if (!ToSubExprOrErr)
6273 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006274
Balazs Keri3b30d652018-10-19 13:32:20 +00006275 return new (Importer.getToContext()) ImaginaryLiteral(
6276 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006277}
6278
Balazs Keri3b30d652018-10-19 13:32:20 +00006279ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6280 ExpectedType ToTypeOrErr = import(E->getType());
6281 if (!ToTypeOrErr)
6282 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006283
Balazs Keri3b30d652018-10-19 13:32:20 +00006284 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6285 if (!ToLocationOrErr)
6286 return ToLocationOrErr.takeError();
6287
6288 return new (Importer.getToContext()) CharacterLiteral(
6289 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006290}
6291
Balazs Keri3b30d652018-10-19 13:32:20 +00006292ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6293 ExpectedType ToTypeOrErr = import(E->getType());
6294 if (!ToTypeOrErr)
6295 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006296
Balazs Keri3b30d652018-10-19 13:32:20 +00006297 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6298 if (Error Err = ImportArrayChecked(
6299 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6300 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006301
Balazs Keri3b30d652018-10-19 13:32:20 +00006302 return StringLiteral::Create(
6303 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6304 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006305}
6306
Balazs Keri3b30d652018-10-19 13:32:20 +00006307ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6308 auto Imp = importSeq(
6309 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6310 E->getInitializer());
6311 if (!Imp)
6312 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006313
Balazs Keri3b30d652018-10-19 13:32:20 +00006314 SourceLocation ToLParenLoc;
6315 TypeSourceInfo *ToTypeSourceInfo;
6316 QualType ToType;
6317 Expr *ToInitializer;
6318 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006319
6320 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006321 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6322 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006323}
6324
Balazs Keri3b30d652018-10-19 13:32:20 +00006325ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6326 auto Imp = importSeq(
6327 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6328 if (!Imp)
6329 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006330
Balazs Keri3b30d652018-10-19 13:32:20 +00006331 SourceLocation ToBuiltinLoc, ToRParenLoc;
6332 QualType ToType;
6333 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6334
6335 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6336 if (Error Err = ImportArrayChecked(
6337 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6338 ToExprs.begin()))
6339 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006340
6341 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006342 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006343}
6344
Balazs Keri3b30d652018-10-19 13:32:20 +00006345ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6346 auto Imp = importSeq(
6347 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6348 if (!Imp)
6349 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006350
Balazs Keri3b30d652018-10-19 13:32:20 +00006351 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6352 LabelDecl *ToLabel;
6353 QualType ToType;
6354 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006355
6356 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006357 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006358}
6359
Bill Wendling8003edc2018-11-09 00:41:36 +00006360ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6361 auto Imp = importSeq(E->getSubExpr());
6362 if (!Imp)
6363 return Imp.takeError();
6364
6365 Expr *ToSubExpr;
6366 std::tie(ToSubExpr) = *Imp;
6367
Fangrui Song407659a2018-11-30 23:41:18 +00006368 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006369}
6370
Balazs Keri3b30d652018-10-19 13:32:20 +00006371ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6372 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6373 if (!Imp)
6374 return Imp.takeError();
6375
6376 SourceLocation ToLParen, ToRParen;
6377 Expr *ToSubExpr;
6378 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006379
Fangrui Song6907ce22018-07-30 19:24:48 +00006380 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006381 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006382}
6383
Balazs Keri3b30d652018-10-19 13:32:20 +00006384ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6385 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6386 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6387 return std::move(Err);
6388
6389 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6390 if (!ToLParenLocOrErr)
6391 return ToLParenLocOrErr.takeError();
6392
6393 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6394 if (!ToRParenLocOrErr)
6395 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006396
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006397 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6398 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006399}
6400
Balazs Keri3b30d652018-10-19 13:32:20 +00006401ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6402 auto Imp = importSeq(
6403 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6404 if (!Imp)
6405 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006406
Balazs Keri3b30d652018-10-19 13:32:20 +00006407 CompoundStmt *ToSubStmt;
6408 QualType ToType;
6409 SourceLocation ToLParenLoc, ToRParenLoc;
6410 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006411
Balazs Keri3b30d652018-10-19 13:32:20 +00006412 return new (Importer.getToContext()) StmtExpr(
6413 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006414}
6415
Balazs Keri3b30d652018-10-19 13:32:20 +00006416ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6417 auto Imp = importSeq(
6418 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6419 if (!Imp)
6420 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006421
Balazs Keri3b30d652018-10-19 13:32:20 +00006422 Expr *ToSubExpr;
6423 QualType ToType;
6424 SourceLocation ToOperatorLoc;
6425 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006426
Aaron Ballmana5038552018-01-09 13:07:03 +00006427 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006428 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6429 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006430}
6431
Balazs Keri3b30d652018-10-19 13:32:20 +00006432ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006433ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006434 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6435 if (!Imp)
6436 return Imp.takeError();
6437
6438 QualType ToType;
6439 SourceLocation ToOperatorLoc, ToRParenLoc;
6440 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006441
Douglas Gregord8552cd2010-02-19 01:24:23 +00006442 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006443 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6444 import(E->getArgumentTypeInfo());
6445 if (!ToArgumentTypeInfoOrErr)
6446 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006447
Balazs Keri3b30d652018-10-19 13:32:20 +00006448 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6449 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6450 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006451 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006452
Balazs Keri3b30d652018-10-19 13:32:20 +00006453 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6454 if (!ToArgumentExprOrErr)
6455 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006456
Balazs Keri3b30d652018-10-19 13:32:20 +00006457 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6458 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006459}
6460
Balazs Keri3b30d652018-10-19 13:32:20 +00006461ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6462 auto Imp = importSeq(
6463 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6464 if (!Imp)
6465 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006466
Balazs Keri3b30d652018-10-19 13:32:20 +00006467 Expr *ToLHS, *ToRHS;
6468 QualType ToType;
6469 SourceLocation ToOperatorLoc;
6470 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006471
Balazs Keri3b30d652018-10-19 13:32:20 +00006472 return new (Importer.getToContext()) BinaryOperator(
6473 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6474 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006475}
6476
Balazs Keri3b30d652018-10-19 13:32:20 +00006477ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6478 auto Imp = importSeq(
6479 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6480 E->getRHS(), E->getType());
6481 if (!Imp)
6482 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006483
Balazs Keri3b30d652018-10-19 13:32:20 +00006484 Expr *ToCond, *ToLHS, *ToRHS;
6485 SourceLocation ToQuestionLoc, ToColonLoc;
6486 QualType ToType;
6487 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006488
6489 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006490 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6491 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006492}
6493
Balazs Keri3b30d652018-10-19 13:32:20 +00006494ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006495 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006496 auto Imp = importSeq(
6497 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6498 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6499 if (!Imp)
6500 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006501
Balazs Keri3b30d652018-10-19 13:32:20 +00006502 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6503 OpaqueValueExpr *ToOpaqueValue;
6504 SourceLocation ToQuestionLoc, ToColonLoc;
6505 QualType ToType;
6506 std::tie(
6507 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6508 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006509
6510 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006511 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6512 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6513 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006514}
6515
Balazs Keri3b30d652018-10-19 13:32:20 +00006516ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6517 auto Imp = importSeq(
6518 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6519 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6520 if (!Imp)
6521 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006522
Balazs Keri3b30d652018-10-19 13:32:20 +00006523 SourceLocation ToBeginLoc, ToEndLoc;
6524 TypeSourceInfo *ToQueriedTypeSourceInfo;
6525 Expr *ToDimensionExpression;
6526 QualType ToType;
6527 std::tie(
6528 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6529 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006530
6531 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006532 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6533 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006534}
6535
Balazs Keri3b30d652018-10-19 13:32:20 +00006536ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6537 auto Imp = importSeq(
6538 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6539 if (!Imp)
6540 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006541
Balazs Keri3b30d652018-10-19 13:32:20 +00006542 SourceLocation ToBeginLoc, ToEndLoc;
6543 Expr *ToQueriedExpression;
6544 QualType ToType;
6545 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006546
6547 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006548 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6549 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006550}
6551
Balazs Keri3b30d652018-10-19 13:32:20 +00006552ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6553 auto Imp = importSeq(
6554 E->getLocation(), E->getType(), E->getSourceExpr());
6555 if (!Imp)
6556 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006557
Balazs Keri3b30d652018-10-19 13:32:20 +00006558 SourceLocation ToLocation;
6559 QualType ToType;
6560 Expr *ToSourceExpr;
6561 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006562
6563 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006564 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006565}
6566
Balazs Keri3b30d652018-10-19 13:32:20 +00006567ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6568 auto Imp = importSeq(
6569 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6570 if (!Imp)
6571 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006572
Balazs Keri3b30d652018-10-19 13:32:20 +00006573 Expr *ToLHS, *ToRHS;
6574 SourceLocation ToRBracketLoc;
6575 QualType ToType;
6576 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006577
6578 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006579 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6580 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006581}
6582
Balazs Keri3b30d652018-10-19 13:32:20 +00006583ExpectedStmt
6584ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6585 auto Imp = importSeq(
6586 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6587 E->getComputationResultType(), E->getOperatorLoc());
6588 if (!Imp)
6589 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006590
Balazs Keri3b30d652018-10-19 13:32:20 +00006591 Expr *ToLHS, *ToRHS;
6592 QualType ToType, ToComputationLHSType, ToComputationResultType;
6593 SourceLocation ToOperatorLoc;
6594 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6595 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006596
Balazs Keri3b30d652018-10-19 13:32:20 +00006597 return new (Importer.getToContext()) CompoundAssignOperator(
6598 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6599 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6600 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006601}
6602
Balazs Keri3b30d652018-10-19 13:32:20 +00006603Expected<CXXCastPath>
6604ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6605 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006606 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006607 if (auto SpecOrErr = import(*I))
6608 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006609 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006610 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006611 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006612 return Path;
John McCallcf142162010-08-07 06:22:56 +00006613}
6614
Balazs Keri3b30d652018-10-19 13:32:20 +00006615ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6616 ExpectedType ToTypeOrErr = import(E->getType());
6617 if (!ToTypeOrErr)
6618 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006619
Balazs Keri3b30d652018-10-19 13:32:20 +00006620 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6621 if (!ToSubExprOrErr)
6622 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006623
Balazs Keri3b30d652018-10-19 13:32:20 +00006624 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6625 if (!ToBasePathOrErr)
6626 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006627
Balazs Keri3b30d652018-10-19 13:32:20 +00006628 return ImplicitCastExpr::Create(
6629 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6630 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006631}
6632
Balazs Keri3b30d652018-10-19 13:32:20 +00006633ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6634 auto Imp1 = importSeq(
6635 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6636 if (!Imp1)
6637 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006638
Balazs Keri3b30d652018-10-19 13:32:20 +00006639 QualType ToType;
6640 Expr *ToSubExpr;
6641 TypeSourceInfo *ToTypeInfoAsWritten;
6642 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006643
Balazs Keri3b30d652018-10-19 13:32:20 +00006644 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6645 if (!ToBasePathOrErr)
6646 return ToBasePathOrErr.takeError();
6647 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006648
Aleksei Sidorina693b372016-09-28 10:16:56 +00006649 switch (E->getStmtClass()) {
6650 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006651 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006652 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6653 if (!ToLParenLocOrErr)
6654 return ToLParenLocOrErr.takeError();
6655 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6656 if (!ToRParenLocOrErr)
6657 return ToRParenLocOrErr.takeError();
6658 return CStyleCastExpr::Create(
6659 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6660 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6661 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006662 }
6663
6664 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006665 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006666 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6667 if (!ToLParenLocOrErr)
6668 return ToLParenLocOrErr.takeError();
6669 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6670 if (!ToRParenLocOrErr)
6671 return ToRParenLocOrErr.takeError();
6672 return CXXFunctionalCastExpr::Create(
6673 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6674 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6675 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006676 }
6677
6678 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006679 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6680 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6681 if (!ToLParenLocOrErr)
6682 return ToLParenLocOrErr.takeError();
6683 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6684 if (!ToBridgeKeywordLocOrErr)
6685 return ToBridgeKeywordLocOrErr.takeError();
6686 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6687 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6688 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006689 }
6690 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006691 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006692 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006693 }
6694}
6695
Balazs Keri3b30d652018-10-19 13:32:20 +00006696ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6697 SmallVector<OffsetOfNode, 4> ToNodes;
6698 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6699 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006700
Balazs Keri3b30d652018-10-19 13:32:20 +00006701 SourceLocation ToBeginLoc, ToEndLoc;
6702 if (FromNode.getKind() != OffsetOfNode::Base) {
6703 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6704 if (!Imp)
6705 return Imp.takeError();
6706 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6707 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006708
Balazs Keri3b30d652018-10-19 13:32:20 +00006709 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006710 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006711 ToNodes.push_back(
6712 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006713 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006714 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006715 auto ToBSOrErr = import(FromNode.getBase());
6716 if (!ToBSOrErr)
6717 return ToBSOrErr.takeError();
6718 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006719 break;
6720 }
6721 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006722 auto ToFieldOrErr = import(FromNode.getField());
6723 if (!ToFieldOrErr)
6724 return ToFieldOrErr.takeError();
6725 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006726 break;
6727 }
6728 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006729 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6730 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006731 break;
6732 }
6733 }
6734 }
6735
Balazs Keri3b30d652018-10-19 13:32:20 +00006736 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6737 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6738 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6739 if (!ToIndexExprOrErr)
6740 return ToIndexExprOrErr.takeError();
6741 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006742 }
6743
Balazs Keri3b30d652018-10-19 13:32:20 +00006744 auto Imp = importSeq(
6745 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6746 E->getRParenLoc());
6747 if (!Imp)
6748 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006749
Balazs Keri3b30d652018-10-19 13:32:20 +00006750 QualType ToType;
6751 TypeSourceInfo *ToTypeSourceInfo;
6752 SourceLocation ToOperatorLoc, ToRParenLoc;
6753 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6754
6755 return OffsetOfExpr::Create(
6756 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6757 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006758}
6759
Balazs Keri3b30d652018-10-19 13:32:20 +00006760ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6761 auto Imp = importSeq(
6762 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6763 if (!Imp)
6764 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006765
Balazs Keri3b30d652018-10-19 13:32:20 +00006766 QualType ToType;
6767 Expr *ToOperand;
6768 SourceLocation ToBeginLoc, ToEndLoc;
6769 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006770
Balazs Keri3b30d652018-10-19 13:32:20 +00006771 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006772 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006773 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006774 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006775 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006776
Balazs Keri3b30d652018-10-19 13:32:20 +00006777 return new (Importer.getToContext()) CXXNoexceptExpr(
6778 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006779}
6780
Balazs Keri3b30d652018-10-19 13:32:20 +00006781ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6782 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6783 if (!Imp)
6784 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006785
Balazs Keri3b30d652018-10-19 13:32:20 +00006786 Expr *ToSubExpr;
6787 QualType ToType;
6788 SourceLocation ToThrowLoc;
6789 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006790
6791 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006792 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006793}
6794
Balazs Keri3b30d652018-10-19 13:32:20 +00006795ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6796 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6797 if (!ToUsedLocOrErr)
6798 return ToUsedLocOrErr.takeError();
6799
6800 auto ToParamOrErr = import(E->getParam());
6801 if (!ToParamOrErr)
6802 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006803
Eric Fiselier708afb52019-05-16 21:04:15 +00006804 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
6805 if (!UsedContextOrErr)
6806 return UsedContextOrErr.takeError();
6807
Aleksei Sidorina693b372016-09-28 10:16:56 +00006808 return CXXDefaultArgExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00006809 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr, *UsedContextOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006810}
6811
Balazs Keri3b30d652018-10-19 13:32:20 +00006812ExpectedStmt
6813ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6814 auto Imp = importSeq(
6815 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6816 if (!Imp)
6817 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006818
Balazs Keri3b30d652018-10-19 13:32:20 +00006819 QualType ToType;
6820 TypeSourceInfo *ToTypeSourceInfo;
6821 SourceLocation ToRParenLoc;
6822 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006823
6824 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006825 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006826}
6827
Balazs Keri3b30d652018-10-19 13:32:20 +00006828ExpectedStmt
6829ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6830 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6831 if (!ToSubExprOrErr)
6832 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006833
Balazs Keri3b30d652018-10-19 13:32:20 +00006834 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6835 if (!ToDtorOrErr)
6836 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006837
6838 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006839 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6840 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006841}
6842
Balazs Keri3b30d652018-10-19 13:32:20 +00006843ExpectedStmt
6844ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6845 auto Imp = importSeq(
6846 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6847 E->getParenOrBraceRange());
6848 if (!Imp)
6849 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006850
Balazs Keri3b30d652018-10-19 13:32:20 +00006851 CXXConstructorDecl *ToConstructor;
6852 QualType ToType;
6853 TypeSourceInfo *ToTypeSourceInfo;
6854 SourceRange ToParenOrBraceRange;
6855 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006856
Balazs Keri3b30d652018-10-19 13:32:20 +00006857 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6858 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6859 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006860
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006861 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006862 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6863 ToParenOrBraceRange, E->hadMultipleCandidates(),
6864 E->isListInitialization(), E->isStdInitListInitialization(),
6865 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006866}
6867
Balazs Keri3b30d652018-10-19 13:32:20 +00006868ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006869ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006870 auto Imp = importSeq(
6871 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6872 if (!Imp)
6873 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006874
Balazs Keri3b30d652018-10-19 13:32:20 +00006875 QualType ToType;
6876 Expr *ToTemporaryExpr;
6877 const ValueDecl *ToExtendingDecl;
6878 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006879
6880 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006881 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006882
6883 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006884 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006885 return ToMTE;
6886}
6887
Balazs Keri3b30d652018-10-19 13:32:20 +00006888ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6889 auto Imp = importSeq(
6890 E->getType(), E->getPattern(), E->getEllipsisLoc());
6891 if (!Imp)
6892 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006893
Balazs Keri3b30d652018-10-19 13:32:20 +00006894 QualType ToType;
6895 Expr *ToPattern;
6896 SourceLocation ToEllipsisLoc;
6897 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006898
6899 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006900 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006901}
6902
Balazs Keri3b30d652018-10-19 13:32:20 +00006903ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6904 auto Imp = importSeq(
6905 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6906 if (!Imp)
6907 return Imp.takeError();
6908
6909 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6910 NamedDecl *ToPack;
6911 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006912
6913 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006914 if (!E->isValueDependent())
6915 Length = E->getPackLength();
6916
Balazs Keri3b30d652018-10-19 13:32:20 +00006917 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006918 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006919 if (Error Err = ImportTemplateArguments(
6920 E->getPartialArguments().data(),
6921 E->getPartialArguments().size(),
6922 ToPartialArguments))
6923 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006924 }
6925
6926 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006927 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6928 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006929}
6930
Aleksei Sidorina693b372016-09-28 10:16:56 +00006931
Balazs Keri3b30d652018-10-19 13:32:20 +00006932ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6933 auto Imp = importSeq(
6934 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6935 E->getArraySize(), E->getInitializer(), E->getType(),
6936 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6937 E->getDirectInitRange());
6938 if (!Imp)
6939 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006940
Balazs Keri3b30d652018-10-19 13:32:20 +00006941 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6942 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00006943 Optional<Expr *> ToArraySize;
6944 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00006945 QualType ToType;
6946 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6947 std::tie(
6948 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6949 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006950
Balazs Keri3b30d652018-10-19 13:32:20 +00006951 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6952 if (Error Err =
6953 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6954 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006955
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006956 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006957 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6958 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6959 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6960 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6961 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006962}
6963
Balazs Keri3b30d652018-10-19 13:32:20 +00006964ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6965 auto Imp = importSeq(
6966 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6967 if (!Imp)
6968 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006969
Balazs Keri3b30d652018-10-19 13:32:20 +00006970 QualType ToType;
6971 FunctionDecl *ToOperatorDelete;
6972 Expr *ToArgument;
6973 SourceLocation ToBeginLoc;
6974 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006975
6976 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006977 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6978 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6979 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006980}
6981
Balazs Keri3b30d652018-10-19 13:32:20 +00006982ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6983 auto Imp = importSeq(
6984 E->getType(), E->getLocation(), E->getConstructor(),
6985 E->getParenOrBraceRange());
6986 if (!Imp)
6987 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006988
Balazs Keri3b30d652018-10-19 13:32:20 +00006989 QualType ToType;
6990 SourceLocation ToLocation;
6991 CXXConstructorDecl *ToConstructor;
6992 SourceRange ToParenOrBraceRange;
6993 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006994
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006995 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006996 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6997 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006998
Balazs Keri3b30d652018-10-19 13:32:20 +00006999 return CXXConstructExpr::Create(
7000 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7001 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7002 E->isListInitialization(), E->isStdInitListInitialization(),
7003 E->requiresZeroInitialization(), E->getConstructionKind(),
7004 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007005}
7006
Balazs Keri3b30d652018-10-19 13:32:20 +00007007ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7008 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7009 if (!ToSubExprOrErr)
7010 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007011
Balazs Keri3b30d652018-10-19 13:32:20 +00007012 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7013 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7014 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007015
Balazs Keri3b30d652018-10-19 13:32:20 +00007016 return ExprWithCleanups::Create(
7017 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7018 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007019}
7020
Balazs Keri3b30d652018-10-19 13:32:20 +00007021ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7022 auto Imp = importSeq(
7023 E->getCallee(), E->getType(), E->getRParenLoc());
7024 if (!Imp)
7025 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007026
Balazs Keri3b30d652018-10-19 13:32:20 +00007027 Expr *ToCallee;
7028 QualType ToType;
7029 SourceLocation ToRParenLoc;
7030 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007031
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007032 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007033 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7034 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007035
Bruno Riccic5885cf2018-12-21 15:20:32 +00007036 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7037 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007038}
7039
Balazs Keri3b30d652018-10-19 13:32:20 +00007040ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7041 ExpectedType ToTypeOrErr = import(E->getType());
7042 if (!ToTypeOrErr)
7043 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007044
Balazs Keri3b30d652018-10-19 13:32:20 +00007045 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7046 if (!ToLocationOrErr)
7047 return ToLocationOrErr.takeError();
7048
7049 return new (Importer.getToContext()) CXXThisExpr(
7050 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007051}
7052
Balazs Keri3b30d652018-10-19 13:32:20 +00007053ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7054 ExpectedType ToTypeOrErr = import(E->getType());
7055 if (!ToTypeOrErr)
7056 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007057
Balazs Keri3b30d652018-10-19 13:32:20 +00007058 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7059 if (!ToLocationOrErr)
7060 return ToLocationOrErr.takeError();
7061
7062 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7063 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007064}
7065
Balazs Keri3b30d652018-10-19 13:32:20 +00007066ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7067 auto Imp1 = importSeq(
7068 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7069 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7070 if (!Imp1)
7071 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007072
Balazs Keri3b30d652018-10-19 13:32:20 +00007073 Expr *ToBase;
7074 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7075 NestedNameSpecifierLoc ToQualifierLoc;
7076 ValueDecl *ToMemberDecl;
7077 QualType ToType;
7078 std::tie(
7079 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7080 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007081
Balazs Keri3b30d652018-10-19 13:32:20 +00007082 auto Imp2 = importSeq(
7083 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7084 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7085 if (!Imp2)
7086 return Imp2.takeError();
7087 NamedDecl *ToDecl;
7088 DeclarationName ToName;
7089 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7090 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007091
7092 DeclAccessPair ToFoundDecl =
7093 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007094
Balazs Keri3b30d652018-10-19 13:32:20 +00007095 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007096
Gabor Marton5caba302019-03-07 13:38:20 +00007097 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007098 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007099 if (Error Err =
7100 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7101 E->template_arguments(), ToTAInfo))
7102 return std::move(Err);
7103 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007104 }
7105
Balazs Keri3b30d652018-10-19 13:32:20 +00007106 return MemberExpr::Create(
7107 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7108 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
Gabor Marton5caba302019-03-07 13:38:20 +00007109 ToMemberNameInfo, ResInfo, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007110}
7111
Balazs Keri3b30d652018-10-19 13:32:20 +00007112ExpectedStmt
7113ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7114 auto Imp = importSeq(
7115 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7116 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7117 if (!Imp)
7118 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007119
Balazs Keri3b30d652018-10-19 13:32:20 +00007120 Expr *ToBase;
7121 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7122 NestedNameSpecifierLoc ToQualifierLoc;
7123 TypeSourceInfo *ToScopeTypeInfo;
7124 std::tie(
7125 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7126 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007127
7128 PseudoDestructorTypeStorage Storage;
7129 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7130 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007131 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7132 if (!ToDestroyedTypeLocOrErr)
7133 return ToDestroyedTypeLocOrErr.takeError();
7134 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007135 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007136 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7137 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7138 else
7139 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007140 }
7141
7142 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007143 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7144 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007145}
7146
Balazs Keri3b30d652018-10-19 13:32:20 +00007147ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007148 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007149 auto Imp = importSeq(
7150 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7151 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7152 if (!Imp)
7153 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007154
Balazs Keri3b30d652018-10-19 13:32:20 +00007155 QualType ToType;
7156 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7157 NestedNameSpecifierLoc ToQualifierLoc;
7158 NamedDecl *ToFirstQualifierFoundInScope;
7159 std::tie(
7160 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7161 ToFirstQualifierFoundInScope) = *Imp;
7162
7163 Expr *ToBase = nullptr;
7164 if (!E->isImplicitAccess()) {
7165 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7166 ToBase = *ToBaseOrErr;
7167 else
7168 return ToBaseOrErr.takeError();
7169 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007170
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007171 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007172 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007173 if (Error Err = ImportTemplateArgumentListInfo(
7174 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7175 ToTAInfo))
7176 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007177 ResInfo = &ToTAInfo;
7178 }
7179
Balazs Keri3b30d652018-10-19 13:32:20 +00007180 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7181 if (!ToMemberNameInfoOrErr)
7182 return ToMemberNameInfoOrErr.takeError();
7183 DeclarationNameInfo ToMemberNameInfo(
7184 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007185 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007186 if (Error Err = ImportDeclarationNameLoc(
7187 E->getMemberNameInfo(), ToMemberNameInfo))
7188 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007189
7190 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007191 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7192 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7193 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007194}
7195
Balazs Keri3b30d652018-10-19 13:32:20 +00007196ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007197ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007198 auto Imp = importSeq(
7199 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7200 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7201 if (!Imp)
7202 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007203
Balazs Keri3b30d652018-10-19 13:32:20 +00007204 NestedNameSpecifierLoc ToQualifierLoc;
7205 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7206 DeclarationName ToDeclName;
7207 std::tie(
7208 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7209 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007210
Balazs Keri3b30d652018-10-19 13:32:20 +00007211 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7212 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7213 return std::move(Err);
7214
7215 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007216 TemplateArgumentListInfo *ResInfo = nullptr;
7217 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007218 if (Error Err =
7219 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7220 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007221 ResInfo = &ToTAInfo;
7222 }
7223
7224 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007225 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7226 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007227}
7228
Balazs Keri3b30d652018-10-19 13:32:20 +00007229ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7230 CXXUnresolvedConstructExpr *E) {
7231 auto Imp = importSeq(
7232 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7233 if (!Imp)
7234 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007235
Balazs Keri3b30d652018-10-19 13:32:20 +00007236 SourceLocation ToLParenLoc, ToRParenLoc;
7237 TypeSourceInfo *ToTypeSourceInfo;
7238 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7239
7240 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7241 if (Error Err =
7242 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7243 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007244
7245 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007246 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7247 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007248}
7249
Balazs Keri3b30d652018-10-19 13:32:20 +00007250ExpectedStmt
7251ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7252 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7253 if (!ToNamingClassOrErr)
7254 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007255
Balazs Keri3b30d652018-10-19 13:32:20 +00007256 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7257 if (!ToQualifierLocOrErr)
7258 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007259
Balazs Keri3b30d652018-10-19 13:32:20 +00007260 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7261 if (!ToNameInfoOrErr)
7262 return ToNameInfoOrErr.takeError();
7263 DeclarationNameInfo ToNameInfo(
7264 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007265 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007266 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7267 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007268
7269 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007270 for (auto *D : E->decls())
7271 if (auto ToDOrErr = import(D))
7272 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007273 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007274 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007275
Balazs Keri3b30d652018-10-19 13:32:20 +00007276 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7277 TemplateArgumentListInfo ToTAInfo;
7278 if (Error Err = ImportTemplateArgumentListInfo(
7279 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7280 ToTAInfo))
7281 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007282
Balazs Keri3b30d652018-10-19 13:32:20 +00007283 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7284 if (!ToTemplateKeywordLocOrErr)
7285 return ToTemplateKeywordLocOrErr.takeError();
7286
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007287 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007288 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7289 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7290 ToDecls.begin(), ToDecls.end());
7291 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007292
7293 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007294 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7295 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7296 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007297}
7298
Balazs Keri3b30d652018-10-19 13:32:20 +00007299ExpectedStmt
7300ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7301 auto Imp1 = importSeq(
7302 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7303 E->getTemplateKeywordLoc());
7304 if (!Imp1)
7305 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007306
Balazs Keri3b30d652018-10-19 13:32:20 +00007307 QualType ToType;
7308 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7309 NestedNameSpecifierLoc ToQualifierLoc;
7310 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7311
7312 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7313 if (!Imp2)
7314 return Imp2.takeError();
7315 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7316 // Import additional name location/type info.
7317 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7318 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007319
7320 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007321 for (Decl *D : E->decls())
7322 if (auto ToDOrErr = import(D))
7323 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007324 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007325 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007326
7327 TemplateArgumentListInfo ToTAInfo;
7328 TemplateArgumentListInfo *ResInfo = nullptr;
7329 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007330 if (Error Err =
7331 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7332 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007333 ResInfo = &ToTAInfo;
7334 }
7335
Balazs Keri3b30d652018-10-19 13:32:20 +00007336 Expr *ToBase = nullptr;
7337 if (!E->isImplicitAccess()) {
7338 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7339 ToBase = *ToBaseOrErr;
7340 else
7341 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007342 }
7343
7344 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007345 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7346 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7347 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007348}
7349
Balazs Keri3b30d652018-10-19 13:32:20 +00007350ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7351 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7352 if (!Imp)
7353 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007354
Balazs Keri3b30d652018-10-19 13:32:20 +00007355 Expr *ToCallee;
7356 QualType ToType;
7357 SourceLocation ToRParenLoc;
7358 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007359
7360 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007361 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7362 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7363 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007364
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007365 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007366 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007367 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007368 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7369 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007370 }
7371
Bruno Riccic5885cf2018-12-21 15:20:32 +00007372 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7373 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7374 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007375}
7376
Balazs Keri3b30d652018-10-19 13:32:20 +00007377ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7378 CXXRecordDecl *FromClass = E->getLambdaClass();
7379 auto ToClassOrErr = import(FromClass);
7380 if (!ToClassOrErr)
7381 return ToClassOrErr.takeError();
7382 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007383
7384 // NOTE: lambda classes are created with BeingDefined flag set up.
7385 // It means that ImportDefinition doesn't work for them and we should fill it
7386 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007387 if (ToClass->isBeingDefined())
7388 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7389 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007390
Balazs Keri3b30d652018-10-19 13:32:20 +00007391 auto ToCallOpOrErr = import(E->getCallOperator());
7392 if (!ToCallOpOrErr)
7393 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007394
7395 ToClass->completeDefinition();
7396
Balazs Keri3b30d652018-10-19 13:32:20 +00007397 SmallVector<LambdaCapture, 8> ToCaptures;
7398 ToCaptures.reserve(E->capture_size());
7399 for (const auto &FromCapture : E->captures()) {
7400 if (auto ToCaptureOrErr = import(FromCapture))
7401 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007402 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007403 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007404 }
7405
Balazs Keri3b30d652018-10-19 13:32:20 +00007406 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7407 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7408 return std::move(Err);
7409
7410 auto Imp = importSeq(
7411 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7412 if (!Imp)
7413 return Imp.takeError();
7414
7415 SourceRange ToIntroducerRange;
7416 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7417 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007418
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007419 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007420 Importer.getToContext(), ToClass, ToIntroducerRange,
7421 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7422 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7423 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007424}
7425
Sean Callanan8bca9962016-03-28 21:43:01 +00007426
Balazs Keri3b30d652018-10-19 13:32:20 +00007427ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7428 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7429 if (!Imp)
7430 return Imp.takeError();
7431
7432 SourceLocation ToLBraceLoc, ToRBraceLoc;
7433 QualType ToType;
7434 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7435
7436 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7437 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7438 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007439
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007440 ASTContext &ToCtx = Importer.getToContext();
7441 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007442 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7443 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007444
Balazs Keri3b30d652018-10-19 13:32:20 +00007445 if (E->hasArrayFiller()) {
7446 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7447 To->setArrayFiller(*ToFillerOrErr);
7448 else
7449 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007450 }
7451
Balazs Keri3b30d652018-10-19 13:32:20 +00007452 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7453 if (auto ToFDOrErr = import(FromFD))
7454 To->setInitializedFieldInUnion(*ToFDOrErr);
7455 else
7456 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007457 }
7458
Balazs Keri3b30d652018-10-19 13:32:20 +00007459 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7460 if (auto ToSyntFormOrErr = import(SyntForm))
7461 To->setSyntacticForm(*ToSyntFormOrErr);
7462 else
7463 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007464 }
7465
Gabor Martona20ce602018-09-03 13:10:53 +00007466 // Copy InitListExprBitfields, which are not handled in the ctor of
7467 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007468 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007469
7470 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007471}
7472
Balazs Keri3b30d652018-10-19 13:32:20 +00007473ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007474 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007475 ExpectedType ToTypeOrErr = import(E->getType());
7476 if (!ToTypeOrErr)
7477 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007478
Balazs Keri3b30d652018-10-19 13:32:20 +00007479 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7480 if (!ToSubExprOrErr)
7481 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007482
Balazs Keri3b30d652018-10-19 13:32:20 +00007483 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7484 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007485}
7486
Balazs Keri3b30d652018-10-19 13:32:20 +00007487ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007488 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007489 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7490 if (!Imp)
7491 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007492
Balazs Keri3b30d652018-10-19 13:32:20 +00007493 SourceLocation ToLocation;
7494 QualType ToType;
7495 CXXConstructorDecl *ToConstructor;
7496 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007497
7498 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007499 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7500 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007501}
7502
Balazs Keri3b30d652018-10-19 13:32:20 +00007503ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7504 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7505 if (!Imp)
7506 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007507
Balazs Keri3b30d652018-10-19 13:32:20 +00007508 QualType ToType;
7509 Expr *ToCommonExpr, *ToSubExpr;
7510 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007511
Balazs Keri3b30d652018-10-19 13:32:20 +00007512 return new (Importer.getToContext()) ArrayInitLoopExpr(
7513 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007514}
7515
Balazs Keri3b30d652018-10-19 13:32:20 +00007516ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7517 ExpectedType ToTypeOrErr = import(E->getType());
7518 if (!ToTypeOrErr)
7519 return ToTypeOrErr.takeError();
7520 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007521}
7522
Balazs Keri3b30d652018-10-19 13:32:20 +00007523ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7524 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7525 if (!ToBeginLocOrErr)
7526 return ToBeginLocOrErr.takeError();
7527
7528 auto ToFieldOrErr = import(E->getField());
7529 if (!ToFieldOrErr)
7530 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007531
Eric Fiselier708afb52019-05-16 21:04:15 +00007532 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7533 if (!UsedContextOrErr)
7534 return UsedContextOrErr.takeError();
7535
Sean Callanandd2c1742016-05-16 20:48:03 +00007536 return CXXDefaultInitExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00007537 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007538}
7539
Balazs Keri3b30d652018-10-19 13:32:20 +00007540ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7541 auto Imp = importSeq(
7542 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7543 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7544 if (!Imp)
7545 return Imp.takeError();
7546
7547 QualType ToType;
7548 Expr *ToSubExpr;
7549 TypeSourceInfo *ToTypeInfoAsWritten;
7550 SourceLocation ToOperatorLoc, ToRParenLoc;
7551 SourceRange ToAngleBrackets;
7552 std::tie(
7553 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7554 ToAngleBrackets) = *Imp;
7555
Sean Callanandd2c1742016-05-16 20:48:03 +00007556 ExprValueKind VK = E->getValueKind();
7557 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007558 auto ToBasePathOrErr = ImportCastPath(E);
7559 if (!ToBasePathOrErr)
7560 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007561
Sean Callanandd2c1742016-05-16 20:48:03 +00007562 if (isa<CXXStaticCastExpr>(E)) {
7563 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007564 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7565 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007566 } else if (isa<CXXDynamicCastExpr>(E)) {
7567 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007568 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7569 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007570 } else if (isa<CXXReinterpretCastExpr>(E)) {
7571 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007572 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7573 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007574 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007575 return CXXConstCastExpr::Create(
7576 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7577 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007578 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007579 llvm_unreachable("Unknown cast type");
7580 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007581 }
7582}
7583
Balazs Keri3b30d652018-10-19 13:32:20 +00007584ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007585 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007586 auto Imp = importSeq(
7587 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7588 if (!Imp)
7589 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007590
Balazs Keri3b30d652018-10-19 13:32:20 +00007591 QualType ToType;
7592 SourceLocation ToExprLoc;
7593 NonTypeTemplateParmDecl *ToParameter;
7594 Expr *ToReplacement;
7595 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007596
7597 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007598 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007599}
7600
Balazs Keri3b30d652018-10-19 13:32:20 +00007601ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7602 auto Imp = importSeq(
7603 E->getType(), E->getBeginLoc(), E->getEndLoc());
7604 if (!Imp)
7605 return Imp.takeError();
7606
7607 QualType ToType;
7608 SourceLocation ToBeginLoc, ToEndLoc;
7609 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007610
7611 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007612 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7613 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007614
7615 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7616 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007617 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007618
7619 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007620 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7621 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007622}
7623
Balazs Keri3b30d652018-10-19 13:32:20 +00007624ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7625 ExpectedType ToTypeOrErr = import(E->getType());
7626 if (!ToTypeOrErr)
7627 return ToTypeOrErr.takeError();
7628
7629 auto ToSourceRangeOrErr = import(E->getSourceRange());
7630 if (!ToSourceRangeOrErr)
7631 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007632
7633 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007634 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7635 return new (Importer.getToContext()) CXXTypeidExpr(
7636 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7637 else
7638 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007639 }
7640
Balazs Keri3b30d652018-10-19 13:32:20 +00007641 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7642 if (!ToExprOperandOrErr)
7643 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007644
Balazs Keri3b30d652018-10-19 13:32:20 +00007645 return new (Importer.getToContext()) CXXTypeidExpr(
7646 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007647}
7648
Lang Hames19e07e12017-06-20 21:06:00 +00007649void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7650 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007651 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7652 if (auto ImportedOrErr = import(FromOverriddenMethod))
7653 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7654 (*ImportedOrErr)->getCanonicalDecl()));
7655 else
7656 consumeError(ImportedOrErr.takeError());
7657 }
Lang Hames19e07e12017-06-20 21:06:00 +00007658}
7659
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007660ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007661 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007662 bool MinimalImport,
7663 ASTImporterLookupTable *LookupTable)
7664 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007665 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7666 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007667
7668 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7669 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007670}
7671
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007672ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007673
Gabor Marton54058b52018-12-17 13:53:12 +00007674Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7675 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7676 "Try to get field index for non-field.");
7677
7678 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7679 if (!Owner)
7680 return None;
7681
7682 unsigned Index = 0;
7683 for (const auto *D : Owner->decls()) {
7684 if (D == F)
7685 return Index;
7686
7687 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7688 ++Index;
7689 }
7690
7691 llvm_unreachable("Field was not found in its parent context.");
7692
7693 return None;
7694}
7695
7696ASTImporter::FoundDeclsTy
7697ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7698 // We search in the redecl context because of transparent contexts.
7699 // E.g. a simple C language enum is a transparent context:
7700 // enum E { A, B };
7701 // Now if we had a global variable in the TU
7702 // int A;
7703 // then the enum constant 'A' and the variable 'A' violates ODR.
7704 // We can diagnose this only if we search in the redecl context.
7705 DeclContext *ReDC = DC->getRedeclContext();
7706 if (LookupTable) {
7707 ASTImporterLookupTable::LookupResult LookupResult =
7708 LookupTable->lookup(ReDC, Name);
7709 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7710 } else {
7711 // FIXME Can we remove this kind of lookup?
7712 // Or lldb really needs this C/C++ lookup?
7713 FoundDeclsTy Result;
7714 ReDC->localUncachedLookup(Name, Result);
7715 return Result;
7716 }
7717}
7718
7719void ASTImporter::AddToLookupTable(Decl *ToD) {
7720 if (LookupTable)
7721 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7722 LookupTable->add(ToND);
7723}
7724
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007725Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7726 // Import the decl using ASTNodeImporter.
7727 ASTNodeImporter Importer(*this);
7728 return Importer.Visit(FromD);
7729}
7730
7731void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7732 MapImported(FromD, ToD);
7733 AddToLookupTable(ToD);
7734}
7735
Gabor Marton5ac6d492019-05-15 10:29:48 +00007736Expected<QualType> ASTImporter::Import(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007737 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007738 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007739
Balazs Keri3b30d652018-10-19 13:32:20 +00007740 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007741
7742 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007743 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007744 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007745 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007746 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007747
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007748 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007749 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007750 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007751 if (!ToTOrErr)
7752 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007753
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007754 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007755 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007756
Balazs Keri3b30d652018-10-19 13:32:20 +00007757 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007758}
7759
Gabor Marton5ac6d492019-05-15 10:29:48 +00007760Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007761 if (!FromTSI)
7762 return FromTSI;
7763
7764 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007765 // on the type and a single location. Implement a real version of this.
Gabor Marton5ac6d492019-05-15 10:29:48 +00007766 ExpectedType TOrErr = Import(FromTSI->getType());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007767 if (!TOrErr)
7768 return TOrErr.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00007769 ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007770 if (!BeginLocOrErr)
7771 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007772
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007773 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7774}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007775
Gabor Marton5ac6d492019-05-15 10:29:48 +00007776Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007777 Attr *ToAttr = FromAttr->clone(ToContext);
Gabor Marton5ac6d492019-05-15 10:29:48 +00007778 if (auto ToRangeOrErr = Import(FromAttr->getRange()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007779 ToAttr->setRange(*ToRangeOrErr);
7780 else
7781 return ToRangeOrErr.takeError();
7782
Davide Italianofaee83d2018-11-28 19:15:23 +00007783 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007784}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007785
Gabor Martonbe77a982018-12-12 11:22:55 +00007786Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7787 auto Pos = ImportedDecls.find(FromD);
7788 if (Pos != ImportedDecls.end())
7789 return Pos->second;
7790 else
Sean Callanan59721b32015-04-28 18:41:46 +00007791 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007792}
7793
Gabor Marton458d1452019-02-14 13:07:03 +00007794TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7795 auto FromDPos = ImportedFromDecls.find(ToD);
7796 if (FromDPos == ImportedFromDecls.end())
7797 return nullptr;
7798 return FromDPos->second->getTranslationUnitDecl();
7799}
7800
Gabor Marton5ac6d492019-05-15 10:29:48 +00007801Expected<Decl *> ASTImporter::Import(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007802 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007803 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007804
Douglas Gregord451ea92011-07-29 23:31:30 +00007805
Gabor Marton26f72a92018-07-12 09:42:05 +00007806 // Check whether we've already imported this declaration.
7807 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7808 if (ToD) {
7809 // If FromD has some updated flags after last import, apply it
7810 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007811 return ToD;
7812 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007813
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007814 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007815 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007816 if (!ToDOrErr)
7817 return ToDOrErr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007818 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007819
Gabor Marton7f8c4002019-03-19 13:34:10 +00007820 // FIXME Use getImportDeclErrorIfAny() here (and return with the error) once
7821 // the error handling is finished in GetImportedOrCreateSpecialDecl().
7822 if (!ToD) {
7823 return nullptr;
7824 }
7825
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007826 // Make sure that ImportImpl registered the imported decl.
7827 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
7828
Gabor Marton54058b52018-12-17 13:53:12 +00007829 // Once the decl is connected to the existing declarations, i.e. when the
7830 // redecl chain is properly set then we populate the lookup again.
7831 // This way the primary context will be able to find all decls.
7832 AddToLookupTable(ToD);
7833
Gabor Marton26f72a92018-07-12 09:42:05 +00007834 // Notify subclasses.
7835 Imported(FromD, ToD);
7836
Gabor Martonac3a5d62018-09-17 12:04:52 +00007837 updateFlags(FromD, ToD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007838 return ToDOrErr;
7839}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007840
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
Gabor Marton5ac6d492019-05-15 10:29:48 +00007845 ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC));
Balazs Keria1f6b102019-04-08 13:59:15 +00007846 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
Gabor Marton5ac6d492019-05-15 10:29:48 +00007901Expected<Expr *> ASTImporter::Import(Expr *FromE) {
7902 if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE)))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007903 return cast_or_null<Expr>(*ToSOrErr);
7904 else
7905 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00007906}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007907
Gabor Marton5ac6d492019-05-15 10:29:48 +00007908Expected<Stmt *> ASTImporter::Import(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007909 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007910 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007911
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007912 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007913 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7914 if (Pos != ImportedStmts.end())
7915 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007916
Balazs Keri3b30d652018-10-19 13:32:20 +00007917 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007918 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007919 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007920 if (!ToSOrErr)
7921 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007922
Balazs Keri3b30d652018-10-19 13:32:20 +00007923 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007924 auto *FromE = cast<Expr>(FromS);
7925 // Copy ExprBitfields, which may not be handled in Expr subclasses
7926 // constructors.
7927 ToE->setValueKind(FromE->getValueKind());
7928 ToE->setObjectKind(FromE->getObjectKind());
7929 ToE->setTypeDependent(FromE->isTypeDependent());
7930 ToE->setValueDependent(FromE->isValueDependent());
7931 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7932 ToE->setContainsUnexpandedParameterPack(
7933 FromE->containsUnexpandedParameterPack());
7934 }
7935
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007936 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00007937 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007938 return ToSOrErr;
7939}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007940
Balazs Keri4a3d7582018-11-27 18:36:31 +00007941Expected<NestedNameSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00007942ASTImporter::Import(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007943 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007944 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007945
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007946 NestedNameSpecifier *Prefix;
7947 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
7948 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00007949
7950 switch (FromNNS->getKind()) {
7951 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007952 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
7953 return NestedNameSpecifier::Create(ToContext, Prefix,
7954 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00007955
7956 case NestedNameSpecifier::Namespace:
Gabor Marton5ac6d492019-05-15 10:29:48 +00007957 if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007958 return NestedNameSpecifier::Create(ToContext, Prefix,
7959 cast<NamespaceDecl>(*NSOrErr));
7960 } else
7961 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007962
7963 case NestedNameSpecifier::NamespaceAlias:
Gabor Marton5ac6d492019-05-15 10:29:48 +00007964 if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007965 return NestedNameSpecifier::Create(ToContext, Prefix,
7966 cast<NamespaceAliasDecl>(*NSADOrErr));
7967 else
7968 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007969
7970 case NestedNameSpecifier::Global:
7971 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7972
Nikola Smiljanic67860242014-09-26 00:28:20 +00007973 case NestedNameSpecifier::Super:
Gabor Marton5ac6d492019-05-15 10:29:48 +00007974 if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007975 return NestedNameSpecifier::SuperSpecifier(ToContext,
7976 cast<CXXRecordDecl>(*RDOrErr));
7977 else
7978 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00007979
Douglas Gregor90ebf252011-04-27 16:48:40 +00007980 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007981 case NestedNameSpecifier::TypeSpecWithTemplate:
7982 if (Expected<QualType> TyOrErr =
Gabor Marton5ac6d492019-05-15 10:29:48 +00007983 Import(QualType(FromNNS->getAsType(), 0u))) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007984 bool TSTemplate =
7985 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
7986 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
7987 TyOrErr->getTypePtr());
7988 } else {
7989 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007990 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00007991 }
7992
7993 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007994}
7995
Balazs Keri4a3d7582018-11-27 18:36:31 +00007996Expected<NestedNameSpecifierLoc>
Gabor Marton5ac6d492019-05-15 10:29:48 +00007997ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007998 // Copied from NestedNameSpecifier mostly.
7999 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8000 NestedNameSpecifierLoc NNS = FromNNS;
8001
8002 // Push each of the nested-name-specifiers's onto a stack for
8003 // serialization in reverse order.
8004 while (NNS) {
8005 NestedNames.push_back(NNS);
8006 NNS = NNS.getPrefix();
8007 }
8008
8009 NestedNameSpecifierLocBuilder Builder;
8010
8011 while (!NestedNames.empty()) {
8012 NNS = NestedNames.pop_back_val();
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00008013 NestedNameSpecifier *Spec = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008014 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8015 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008016
8017 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008018
8019 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8020 if (Kind != NestedNameSpecifier::Super) {
8021 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8022 return std::move(Err);
8023
8024 if (Kind != NestedNameSpecifier::Global)
8025 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8026 return std::move(Err);
8027 }
8028
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008029 switch (Kind) {
8030 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008031 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8032 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008033 break;
8034
8035 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008036 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8037 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008038 break;
8039
8040 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008041 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8042 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008043 break;
8044
8045 case NestedNameSpecifier::TypeSpec:
8046 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008047 SourceLocation ToTLoc;
8048 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8049 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008050 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008051 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008052 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8053 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008054 break;
8055 }
8056
8057 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008058 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008059 break;
8060
8061 case NestedNameSpecifier::Super: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008062 auto ToSourceRangeOrErr = Import(NNS.getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008063 if (!ToSourceRangeOrErr)
8064 return ToSourceRangeOrErr.takeError();
8065
8066 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8067 ToSourceRangeOrErr->getBegin(),
8068 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008069 }
8070 }
8071 }
8072
8073 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008074}
8075
Gabor Marton5ac6d492019-05-15 10:29:48 +00008076Expected<TemplateName> ASTImporter::Import(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008077 switch (From.getKind()) {
8078 case TemplateName::Template:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008079 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008080 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8081 else
8082 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008083
Douglas Gregore2e50d332010-12-01 01:36:18 +00008084 case TemplateName::OverloadedTemplate: {
8085 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8086 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008087 for (auto *I : *FromStorage) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008088 if (auto ToOrErr = Import(I))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008089 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008090 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008091 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008092 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008093 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008094 ToTemplates.end());
8095 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008096
Richard Smithb23c5e82019-05-09 03:31:27 +00008097 case TemplateName::AssumedTemplate: {
8098 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008099 auto DeclNameOrErr = Import(FromStorage->getDeclName());
Richard Smithb23c5e82019-05-09 03:31:27 +00008100 if (!DeclNameOrErr)
8101 return DeclNameOrErr.takeError();
8102 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8103 }
8104
Douglas Gregore2e50d332010-12-01 01:36:18 +00008105 case TemplateName::QualifiedTemplate: {
8106 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008107 auto QualifierOrErr = Import(QTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008108 if (!QualifierOrErr)
8109 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008110
Gabor Marton5ac6d492019-05-15 10:29:48 +00008111 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008112 return ToContext.getQualifiedTemplateName(
8113 *QualifierOrErr, QTN->hasTemplateKeyword(),
8114 cast<TemplateDecl>(*ToTemplateOrErr));
8115 else
8116 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008117 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008118
Douglas Gregore2e50d332010-12-01 01:36:18 +00008119 case TemplateName::DependentTemplate: {
8120 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008121 auto QualifierOrErr = Import(DTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008122 if (!QualifierOrErr)
8123 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008124
Douglas Gregore2e50d332010-12-01 01:36:18 +00008125 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008126 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008127 Import(DTN->getIdentifier()));
8128 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008129
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008130 return ToContext.getDependentTemplateName(*QualifierOrErr,
8131 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008132 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008133
8134 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008135 SubstTemplateTemplateParmStorage *Subst =
8136 From.getAsSubstTemplateTemplateParm();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008137 ExpectedDecl ParamOrErr = Import(Subst->getParameter());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008138 if (!ParamOrErr)
8139 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008140
Gabor Marton5ac6d492019-05-15 10:29:48 +00008141 auto ReplacementOrErr = Import(Subst->getReplacement());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008142 if (!ReplacementOrErr)
8143 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008144
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008145 return ToContext.getSubstTemplateTemplateParm(
8146 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008147 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008148
Douglas Gregor5590be02011-01-15 06:45:20 +00008149 case TemplateName::SubstTemplateTemplateParmPack: {
8150 SubstTemplateTemplateParmPackStorage *SubstPack
8151 = From.getAsSubstTemplateTemplateParmPack();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008152 ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008153 if (!ParamOrErr)
8154 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008155
Douglas Gregor5590be02011-01-15 06:45:20 +00008156 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008157 auto ArgPackOrErr =
8158 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8159 if (!ArgPackOrErr)
8160 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008161
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008162 return ToContext.getSubstTemplateTemplateParmPack(
8163 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008164 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008165 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008166
Douglas Gregore2e50d332010-12-01 01:36:18 +00008167 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008168}
8169
Gabor Marton5ac6d492019-05-15 10:29:48 +00008170Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008171 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008172 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008173
Douglas Gregor811663e2010-02-10 00:15:17 +00008174 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008175 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008176
Douglas Gregor811663e2010-02-10 00:15:17 +00008177 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008178 Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008179 if (!ToFileIDOrErr)
8180 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008181 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008182 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8183}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008184
Gabor Marton5ac6d492019-05-15 10:29:48 +00008185Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008186 SourceLocation ToBegin, ToEnd;
8187 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8188 return std::move(Err);
8189 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8190 return std::move(Err);
8191
8192 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008193}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008194
Gabor Marton5ac6d492019-05-15 10:29:48 +00008195Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008196 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008197 if (Pos != ImportedFileIDs.end())
8198 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008199
Douglas Gregor811663e2010-02-10 00:15:17 +00008200 SourceManager &FromSM = FromContext.getSourceManager();
8201 SourceManager &ToSM = ToContext.getSourceManager();
8202 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008203
8204 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008205 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008206 if (FromSLoc.isExpansion()) {
8207 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008208 ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008209 if (!ToSpLoc)
8210 return ToSpLoc.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008211 ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008212 if (!ToExLocS)
8213 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008214 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8215 SourceLocation MLoc;
8216 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008217 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008218 } else {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008219 if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008220 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8221 FromEx.isExpansionTokenRange());
8222 else
8223 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008224 }
8225 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008226 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008227 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008228
8229 if (!IsBuiltin) {
8230 // Include location of this file.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008231 ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008232 if (!ToIncludeLoc)
8233 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008234
8235 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8236 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8237 // disk again
8238 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8239 // than mmap the files several times.
8240 const FileEntry *Entry =
8241 ToFileManager.getFile(Cache->OrigEntry->getName());
8242 // FIXME: The filename may be a virtual name that does probably not
8243 // point to a valid file and we get no Entry here. In this case try with
8244 // the memory buffer below.
8245 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008246 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008247 FromSLoc.getFile().getFileCharacteristic());
8248 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008249 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008250
8251 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008252 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008253 bool Invalid = true;
8254 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8255 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8256 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008257 // FIXME: Use a new error kind?
8258 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008259
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008260 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8261 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8262 FromBuf->getBufferIdentifier());
8263 ToID = ToSM.createFileID(std::move(ToBuf),
8264 FromSLoc.getFile().getFileCharacteristic());
8265 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008266 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008267
Balazs Keri9cf39df2019-02-27 16:31:48 +00008268 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8269
Sebastian Redl99219f12010-09-30 01:03:06 +00008270 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008271 return ToID;
8272}
8273
Gabor Marton5ac6d492019-05-15 10:29:48 +00008274Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) {
8275 ExpectedExpr ToExprOrErr = Import(From->getInit());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008276 if (!ToExprOrErr)
8277 return ToExprOrErr.takeError();
8278
Gabor Marton5ac6d492019-05-15 10:29:48 +00008279 auto LParenLocOrErr = Import(From->getLParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008280 if (!LParenLocOrErr)
8281 return LParenLocOrErr.takeError();
8282
Gabor Marton5ac6d492019-05-15 10:29:48 +00008283 auto RParenLocOrErr = Import(From->getRParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008284 if (!RParenLocOrErr)
8285 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008286
8287 if (From->isBaseInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008288 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008289 if (!ToTInfoOrErr)
8290 return ToTInfoOrErr.takeError();
8291
8292 SourceLocation EllipsisLoc;
8293 if (From->isPackExpansion())
8294 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8295 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008296
8297 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008298 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8299 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008300 } else if (From->isMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008301 ExpectedDecl ToFieldOrErr = Import(From->getMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008302 if (!ToFieldOrErr)
8303 return ToFieldOrErr.takeError();
8304
Gabor Marton5ac6d492019-05-15 10:29:48 +00008305 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008306 if (!MemberLocOrErr)
8307 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008308
8309 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008310 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8311 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008312 } else if (From->isIndirectMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008313 ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008314 if (!ToIFieldOrErr)
8315 return ToIFieldOrErr.takeError();
8316
Gabor Marton5ac6d492019-05-15 10:29:48 +00008317 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008318 if (!MemberLocOrErr)
8319 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008320
8321 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008322 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8323 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008324 } else if (From->isDelegatingInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008325 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008326 if (!ToTInfoOrErr)
8327 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008328
8329 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008330 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8331 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008332 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008333 // FIXME: assert?
8334 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008335 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008336}
Sean Callanandd2c1742016-05-16 20:48:03 +00008337
Balazs Keri4a3d7582018-11-27 18:36:31 +00008338Expected<CXXBaseSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008339ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008340 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8341 if (Pos != ImportedCXXBaseSpecifiers.end())
8342 return Pos->second;
8343
Gabor Marton5ac6d492019-05-15 10:29:48 +00008344 Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008345 if (!ToSourceRange)
8346 return ToSourceRange.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008347 Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008348 if (!ToTSI)
8349 return ToTSI.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008350 ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008351 if (!ToEllipsisLoc)
8352 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008353 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008354 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8355 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008356 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8357 return Imported;
8358}
8359
Gabor Marton5ac6d492019-05-15 10:29:48 +00008360Error ASTImporter::ImportDefinition(Decl *From) {
8361 ExpectedDecl ToOrErr = Import(From);
8362 if (!ToOrErr)
8363 return ToOrErr.takeError();
8364 Decl *To = *ToOrErr;
Fangrui Song6907ce22018-07-30 19:24:48 +00008365
Don Hintonf170dff2019-03-19 06:14:14 +00008366 auto *FromDC = cast<DeclContext>(From);
8367 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008368
Don Hintonf170dff2019-03-19 06:14:14 +00008369 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8370 if (!ToRecord->getDefinition()) {
8371 return Importer.ImportDefinition(
8372 cast<RecordDecl>(FromDC), ToRecord,
8373 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008374 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008375 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008376
Don Hintonf170dff2019-03-19 06:14:14 +00008377 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8378 if (!ToEnum->getDefinition()) {
8379 return Importer.ImportDefinition(
8380 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8381 }
8382 }
8383
8384 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8385 if (!ToIFace->getDefinition()) {
8386 return Importer.ImportDefinition(
8387 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8388 ASTNodeImporter::IDK_Everything);
8389 }
8390 }
8391
8392 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8393 if (!ToProto->getDefinition()) {
8394 return Importer.ImportDefinition(
8395 cast<ObjCProtocolDecl>(FromDC), ToProto,
8396 ASTNodeImporter::IDK_Everything);
8397 }
8398 }
8399
8400 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008401}
8402
Gabor Marton5ac6d492019-05-15 10:29:48 +00008403Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008404 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008405 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008406
8407 switch (FromName.getNameKind()) {
8408 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008409 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008410
8411 case DeclarationName::ObjCZeroArgSelector:
8412 case DeclarationName::ObjCOneArgSelector:
8413 case DeclarationName::ObjCMultiArgSelector:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008414 if (auto ToSelOrErr = Import(FromName.getObjCSelector()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008415 return DeclarationName(*ToSelOrErr);
8416 else
8417 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008418
8419 case DeclarationName::CXXConstructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008420 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008421 return ToContext.DeclarationNames.getCXXConstructorName(
8422 ToContext.getCanonicalType(*ToTyOrErr));
8423 else
8424 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008425 }
8426
8427 case DeclarationName::CXXDestructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008428 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008429 return ToContext.DeclarationNames.getCXXDestructorName(
8430 ToContext.getCanonicalType(*ToTyOrErr));
8431 else
8432 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008433 }
8434
Richard Smith35845152017-02-07 01:37:30 +00008435 case DeclarationName::CXXDeductionGuideName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008436 if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008437 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8438 cast<TemplateDecl>(*ToTemplateOrErr));
8439 else
8440 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008441 }
8442
Douglas Gregor96e578d2010-02-05 17:54:41 +00008443 case DeclarationName::CXXConversionFunctionName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008444 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008445 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8446 ToContext.getCanonicalType(*ToTyOrErr));
8447 else
8448 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008449 }
8450
8451 case DeclarationName::CXXOperatorName:
8452 return ToContext.DeclarationNames.getCXXOperatorName(
8453 FromName.getCXXOverloadedOperator());
8454
8455 case DeclarationName::CXXLiteralOperatorName:
8456 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008457 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008458
8459 case DeclarationName::CXXUsingDirective:
8460 // FIXME: STATICS!
8461 return DeclarationName::getUsingDirectiveName();
8462 }
8463
David Blaikiee4d798f2012-01-20 21:50:17 +00008464 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008465}
8466
Douglas Gregore2e50d332010-12-01 01:36:18 +00008467IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008468 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008469 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008470
Sean Callananf94ef1d2016-05-14 06:11:19 +00008471 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8472
8473 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8474 ToId->setBuiltinID(FromId->getBuiltinID());
8475
8476 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008477}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008478
Gabor Marton5ac6d492019-05-15 10:29:48 +00008479Expected<Selector> ASTImporter::Import(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008480 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008481 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008482
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008483 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008484 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8485 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8486 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8487 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8488}
8489
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008490DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8491 DeclContext *DC,
8492 unsigned IDNS,
8493 NamedDecl **Decls,
8494 unsigned NumDecls) {
8495 return Name;
8496}
8497
8498DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008499 if (LastDiagFromFrom)
8500 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8501 FromContext.getDiagnostics());
8502 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008503 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008504}
8505
8506DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008507 if (!LastDiagFromFrom)
8508 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8509 ToContext.getDiagnostics());
8510 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008511 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008512}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008513
Douglas Gregor2e15c842012-02-01 21:00:38 +00008514void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008515 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008516 if (!ID->getDefinition())
8517 ID->startDefinition();
8518 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008519 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008520 if (!PD->getDefinition())
8521 PD->startDefinition();
8522 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008523 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008524 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8525 TD->startDefinition();
8526 TD->setCompleteDefinition(true);
8527 }
8528 }
8529 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008530 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008531 }
8532}
8533
Gabor Marton26f72a92018-07-12 09:42:05 +00008534Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8535 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8536 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8537 "Try to import an already imported Decl");
8538 if (Pos != ImportedDecls.end())
8539 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008540 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008541 // This mapping should be maintained only in this function. Therefore do not
8542 // check for additional consistency.
8543 ImportedFromDecls[To] = From;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008544 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008545}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008546
Douglas Gregordd6006f2012-07-17 21:16:27 +00008547bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8548 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008549 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8550 ImportedTypes.find(From.getTypePtr());
8551 if (Pos != ImportedTypes.end()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008552 if (ExpectedType ToFromOrErr = Import(From)) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008553 if (ToContext.hasSameType(*ToFromOrErr, To))
8554 return true;
8555 } else {
8556 llvm::consumeError(ToFromOrErr.takeError());
8557 }
8558 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008559
Douglas Gregordd6006f2012-07-17 21:16:27 +00008560 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008561 getStructuralEquivalenceKind(*this), false,
8562 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008563 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008564}