blob: f87f426336d0f808c44780ea10c6092f1e30e8ef [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000014
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTImporter.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);
125 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000126 }
127
Gabor Marton26f72a92018-07-12 09:42:05 +0000128 void updateFlags(const Decl *From, Decl *To) {
129 // Check if some flags or attrs are new in 'From' and copy into 'To'.
130 // FIXME: Other flags or attrs?
131 if (From->isUsed(false) && !To->isUsed(false))
132 To->setIsUsed();
133 }
134
Balazs Keri3b30d652018-10-19 13:32:20 +0000135 Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
136 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
137 "Try to get field index for non-field.");
138
139 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
140 if (!Owner)
141 return None;
142
143 unsigned Index = 0;
144 for (const auto *D : Owner->decls()) {
145 if (D == F)
146 return Index;
147
148 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
149 ++Index;
150 }
151
152 llvm_unreachable("Field was not found in its parent context.");
153
154 return None;
155 }
156
157 // FIXME: Temporary until every import returns Expected.
158 template <>
159 LLVM_NODISCARD Error
160 ASTImporter::importInto(SourceLocation &To, const SourceLocation &From) {
161 To = Import(From);
162 if (From.isValid() && To.isInvalid())
163 return llvm::make_error<ImportError>();
164 return Error::success();
165 }
166 // FIXME: Temporary until every import returns Expected.
167 template <>
168 LLVM_NODISCARD Error
169 ASTImporter::importInto(QualType &To, const QualType &From) {
170 To = Import(From);
171 if (!From.isNull() && To.isNull())
172 return llvm::make_error<ImportError>();
173 return Error::success();
174 }
175
176 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
177 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
178 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000180
Balazs Keri3b30d652018-10-19 13:32:20 +0000181 // Use this instead of Importer.importInto .
182 template <typename ImportT>
183 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
184 return Importer.importInto(To, From);
185 }
186
187 // Use this to import pointers of specific type.
188 template <typename ImportT>
189 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
190 auto ToI = Importer.Import(From);
191 if (!ToI && From)
192 return make_error<ImportError>();
193 To = cast_or_null<ImportT>(ToI);
194 return Error::success();
195 // FIXME: This should be the final code.
196 //auto ToOrErr = Importer.Import(From);
197 //if (ToOrErr) {
198 // To = cast_or_null<ImportT>(*ToOrErr);
199 //}
200 //return ToOrErr.takeError();
201 }
202
203 // Call the import function of ASTImporter for a baseclass of type `T` and
204 // cast the return value to `T`.
205 template <typename T>
206 Expected<T *> import(T *From) {
207 auto *To = Importer.Import(From);
208 if (!To && From)
209 return make_error<ImportError>();
210 return cast_or_null<T>(To);
211 // FIXME: This should be the final code.
212 //auto ToOrErr = Importer.Import(From);
213 //if (!ToOrErr)
214 // return ToOrErr.takeError();
215 //return cast_or_null<T>(*ToOrErr);
216 }
217
218 template <typename T>
219 Expected<T *> import(const T *From) {
220 return import(const_cast<T *>(From));
221 }
222
223 // Call the import function of ASTImporter for type `T`.
224 template <typename T>
225 Expected<T> import(const T &From) {
226 T To = Importer.Import(From);
227 T DefaultT;
228 if (To == DefaultT && !(From == DefaultT))
229 return make_error<ImportError>();
230 return To;
231 // FIXME: This should be the final code.
232 //return Importer.Import(From);
233 }
234
235 template <class T>
236 Expected<std::tuple<T>>
237 importSeq(const T &From) {
238 Expected<T> ToOrErr = import(From);
239 if (!ToOrErr)
240 return ToOrErr.takeError();
241 return std::make_tuple<T>(std::move(*ToOrErr));
242 }
243
244 // Import multiple objects with a single function call.
245 // This should work for every type for which a variant of `import` exists.
246 // The arguments are processed from left to right and import is stopped on
247 // first error.
248 template <class THead, class... TTail>
249 Expected<std::tuple<THead, TTail...>>
250 importSeq(const THead &FromHead, const TTail &...FromTail) {
251 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
252 if (!ToHeadOrErr)
253 return ToHeadOrErr.takeError();
254 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
255 if (!ToTailOrErr)
256 return ToTailOrErr.takeError();
257 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
258 }
259
260// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 template <typename ToDeclT> struct CallOverloadedCreateFun {
262 template <typename... Args>
263 auto operator()(Args &&... args)
264 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
265 return ToDeclT::Create(std::forward<Args>(args)...);
266 }
267 };
268
269 // Always use these functions to create a Decl during import. There are
270 // certain tasks which must be done after the Decl was created, e.g. we
271 // must immediately register that as an imported Decl. The parameter `ToD`
272 // will be set to the newly created Decl or if had been imported before
273 // then to the already imported Decl. Returns a bool value set to true if
274 // the `FromD` had been imported before.
275 template <typename ToDeclT, typename FromDeclT, typename... Args>
276 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
277 Args &&... args) {
278 // There may be several overloads of ToDeclT::Create. We must make sure
279 // to call the one which would be chosen by the arguments, thus we use a
280 // wrapper for the overload set.
281 CallOverloadedCreateFun<ToDeclT> OC;
282 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
283 std::forward<Args>(args)...);
284 }
285 // Use this overload if a special Type is needed to be created. E.g if we
286 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
287 // then:
288 // TypedefNameDecl *ToTypedef;
289 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
290 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
291 typename... Args>
292 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
293 Args &&... args) {
294 CallOverloadedCreateFun<NewDeclT> OC;
295 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
296 std::forward<Args>(args)...);
297 }
298 // Use this version if a special create function must be
299 // used, e.g. CXXRecordDecl::CreateLambda .
300 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
301 typename... Args>
302 LLVM_NODISCARD bool
303 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
304 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000305 // FIXME: This code is needed later.
306 //if (Importer.getImportDeclErrorIfAny(FromD)) {
307 // ToD = nullptr;
308 // return true; // Already imported but with error.
309 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000310 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
311 if (ToD)
312 return true; // Already imported.
313 ToD = CreateFun(std::forward<Args>(args)...);
314 InitializeImportedDecl(FromD, ToD);
315 return false; // A new Decl is created.
316 }
317
318 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
319 Importer.MapImported(FromD, ToD);
320 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
321 if (FromD->hasAttrs())
322 for (const Attr *FromAttr : FromD->getAttrs())
323 ToD->addAttr(Importer.Import(FromAttr));
324 if (FromD->isUsed())
325 ToD->setIsUsed();
326 if (FromD->isImplicit())
327 ToD->setImplicit();
328 }
329
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000331 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000332
Balazs Keri3b30d652018-10-19 13:32:20 +0000333 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
334 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
335 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336
337 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000338 ExpectedType VisitType(const Type *T);
339 ExpectedType VisitAtomicType(const AtomicType *T);
340 ExpectedType VisitBuiltinType(const BuiltinType *T);
341 ExpectedType VisitDecayedType(const DecayedType *T);
342 ExpectedType VisitComplexType(const ComplexType *T);
343 ExpectedType VisitPointerType(const PointerType *T);
344 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
345 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
346 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
347 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
348 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
349 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
350 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
351 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000352 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000353 ExpectedType VisitVectorType(const VectorType *T);
354 ExpectedType VisitExtVectorType(const ExtVectorType *T);
355 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
356 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
357 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
358 ExpectedType VisitParenType(const ParenType *T);
359 ExpectedType VisitTypedefType(const TypedefType *T);
360 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000361 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000362 ExpectedType VisitTypeOfType(const TypeOfType *T);
363 ExpectedType VisitDecltypeType(const DecltypeType *T);
364 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
365 ExpectedType VisitAutoType(const AutoType *T);
366 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000368 ExpectedType VisitRecordType(const RecordType *T);
369 ExpectedType VisitEnumType(const EnumType *T);
370 ExpectedType VisitAttributedType(const AttributedType *T);
371 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
372 ExpectedType VisitSubstTemplateTypeParmType(
373 const SubstTemplateTypeParmType *T);
374 ExpectedType VisitTemplateSpecializationType(
375 const TemplateSpecializationType *T);
376 ExpectedType VisitElaboratedType(const ElaboratedType *T);
377 ExpectedType VisitDependentNameType(const DependentNameType *T);
378 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
379 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000380 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
382 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
383 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000384
385 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000386 Error ImportDeclParts(
387 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
388 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
389 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
390 Error ImportDeclarationNameLoc(
391 const DeclarationNameInfo &From, DeclarationNameInfo &To);
392 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
393 Error ImportDeclContext(
394 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
395 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000396
Balazs Keri3b30d652018-10-19 13:32:20 +0000397 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000398
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000399 using Designator = DesignatedInitExpr::Designator;
400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000401 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000402 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000403 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000404 /// nothing (if minimal import is set) or might be everything (if minimal
405 /// import is not set).
406 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000407 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000408 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000410 /// DeclContext.
411 IDK_Basic
412 };
413
Douglas Gregor2e15c842012-02-01 21:00:38 +0000414 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
415 return IDK == IDK_Everything ||
416 (IDK == IDK_Default && !Importer.isMinimalImport());
417 }
418
Balazs Keri3b30d652018-10-19 13:32:20 +0000419 Error ImportInitializer(VarDecl *From, VarDecl *To);
420 Error ImportDefinition(
421 RecordDecl *From, RecordDecl *To,
422 ImportDefinitionKind Kind = IDK_Default);
423 Error ImportDefinition(
424 EnumDecl *From, EnumDecl *To,
425 ImportDefinitionKind Kind = IDK_Default);
426 Error ImportDefinition(
427 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
428 ImportDefinitionKind Kind = IDK_Default);
429 Error ImportDefinition(
430 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
431 ImportDefinitionKind Kind = IDK_Default);
432 Expected<TemplateParameterList *> ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000433 TemplateParameterList *Params);
Balazs Keri3b30d652018-10-19 13:32:20 +0000434 Error ImportTemplateArguments(
435 const TemplateArgument *FromArgs, unsigned NumFromArgs,
436 SmallVectorImpl<TemplateArgument> &ToArgs);
437 Expected<TemplateArgument>
438 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000439
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000440 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000441 Error ImportTemplateArgumentListInfo(
442 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000443
444 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000445 Error ImportTemplateArgumentListInfo(
446 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
447 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000448
Gabor Marton5254e642018-06-27 13:32:50 +0000449 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000450 using FunctionTemplateAndArgsTy =
451 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
452 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000453 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
454 FunctionDecl *FromFD);
455
Balazs Keri3b30d652018-10-19 13:32:20 +0000456 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000457
Gabor Marton950fb572018-07-17 12:39:27 +0000458 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000459 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
460 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000461 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
462 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000463 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000464 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000465 bool IsStructuralMatch(FunctionTemplateDecl *From,
466 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000467 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000468 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000469 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000470 ExpectedDecl VisitDecl(Decl *D);
471 ExpectedDecl VisitImportDecl(ImportDecl *D);
472 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
473 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
474 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
475 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
476 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
477 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
479 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
480 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
481 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
482 ExpectedDecl VisitLabelDecl(LabelDecl *D);
483 ExpectedDecl VisitEnumDecl(EnumDecl *D);
484 ExpectedDecl VisitRecordDecl(RecordDecl *D);
485 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
486 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
487 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
488 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
489 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
490 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
491 ExpectedDecl VisitFieldDecl(FieldDecl *D);
492 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
493 ExpectedDecl VisitFriendDecl(FriendDecl *D);
494 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
495 ExpectedDecl VisitVarDecl(VarDecl *D);
496 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
497 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
498 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
499 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
500 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
501 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
502 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
503 ExpectedDecl VisitUsingDecl(UsingDecl *D);
504 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
505 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
506 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
507 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000508
Balazs Keri3b30d652018-10-19 13:32:20 +0000509 Expected<ObjCTypeParamList *>
510 ImportObjCTypeParamList(ObjCTypeParamList *list);
511
512 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
513 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
514 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
515 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
516 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
517 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
518 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
519 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
520 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
521 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000522 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000523 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
524 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
525 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000526
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000527 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000528 ExpectedStmt VisitStmt(Stmt *S);
529 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
530 ExpectedStmt VisitDeclStmt(DeclStmt *S);
531 ExpectedStmt VisitNullStmt(NullStmt *S);
532 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
533 ExpectedStmt VisitCaseStmt(CaseStmt *S);
534 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
535 ExpectedStmt VisitLabelStmt(LabelStmt *S);
536 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
537 ExpectedStmt VisitIfStmt(IfStmt *S);
538 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
539 ExpectedStmt VisitWhileStmt(WhileStmt *S);
540 ExpectedStmt VisitDoStmt(DoStmt *S);
541 ExpectedStmt VisitForStmt(ForStmt *S);
542 ExpectedStmt VisitGotoStmt(GotoStmt *S);
543 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
544 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
545 ExpectedStmt VisitBreakStmt(BreakStmt *S);
546 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000547 // FIXME: MSAsmStmt
548 // FIXME: SEHExceptStmt
549 // FIXME: SEHFinallyStmt
550 // FIXME: SEHTryStmt
551 // FIXME: SEHLeaveStmt
552 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000553 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
554 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
555 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000556 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000557 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
558 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
559 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
560 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
561 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
562 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
563 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000564
565 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000566 ExpectedStmt VisitExpr(Expr *E);
567 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
568 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
569 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
570 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
571 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
572 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
573 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
574 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
575 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
576 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
577 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
578 ExpectedStmt VisitStringLiteral(StringLiteral *E);
579 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
580 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
581 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
582 ExpectedStmt VisitParenExpr(ParenExpr *E);
583 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
584 ExpectedStmt VisitStmtExpr(StmtExpr *E);
585 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
586 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
587 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
588 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
589 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
590 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
591 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
592 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
593 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
594 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
595 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
596 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
597 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
598 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
599 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
600 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
601 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
602 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
603 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
604 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
605 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
606 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
607 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
608 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
609 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
610 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
611 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
612 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
613 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
614 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
615 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
616 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
617 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
618 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
619 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
620 ExpectedStmt VisitMemberExpr(MemberExpr *E);
621 ExpectedStmt VisitCallExpr(CallExpr *E);
622 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
623 ExpectedStmt VisitInitListExpr(InitListExpr *E);
624 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
625 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
626 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
627 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
628 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
629 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
630 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
631 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
632 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000633
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000634 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000635 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000636 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000637 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
638 Expected<ItemT> ToOrErr = import(*Ibegin);
639 if (!ToOrErr)
640 return ToOrErr.takeError();
641 *Obegin = *ToOrErr;
642 }
643 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000644 }
645
Balazs Keri3b30d652018-10-19 13:32:20 +0000646 // Import every item from a container structure into an output container.
647 // If error occurs, stops at first error and returns the error.
648 // The output container should have space for all needed elements (it is not
649 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000650 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000651 Error ImportContainerChecked(
652 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
653 return ImportArrayChecked(
654 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000655 }
656
657 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000658 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000659 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
660 }
Lang Hames19e07e12017-06-20 21:06:00 +0000661
Lang Hames19e07e12017-06-20 21:06:00 +0000662 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000663
Balazs Keri3b30d652018-10-19 13:32:20 +0000664 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
665 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000666 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000667
Balazs Keri3b30d652018-10-19 13:32:20 +0000668// FIXME: Temporary until every import returns Expected.
669template <>
670Expected<TemplateName> ASTNodeImporter::import(const TemplateName &From) {
671 TemplateName To = Importer.Import(From);
672 if (To.isNull() && !From.isNull())
673 return make_error<ImportError>();
674 return To;
675}
676
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000677template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000678Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000679 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
680 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000681 auto ToLAngleLocOrErr = import(FromLAngleLoc);
682 if (!ToLAngleLocOrErr)
683 return ToLAngleLocOrErr.takeError();
684 auto ToRAngleLocOrErr = import(FromRAngleLoc);
685 if (!ToRAngleLocOrErr)
686 return ToRAngleLocOrErr.takeError();
687
688 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
689 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
690 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000691 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000692 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000693}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000694
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000695template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000696Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000697 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
698 return ImportTemplateArgumentListInfo(
699 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
700}
701
702template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000703Error ASTNodeImporter::ImportTemplateArgumentListInfo<
704 ASTTemplateArgumentListInfo>(
705 const ASTTemplateArgumentListInfo &From,
706 TemplateArgumentListInfo &Result) {
707 return ImportTemplateArgumentListInfo(
708 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000709}
710
Balazs Keri3b30d652018-10-19 13:32:20 +0000711Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000712ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
713 FunctionDecl *FromFD) {
714 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000715 FunctionDecl::TK_FunctionTemplateSpecialization);
716
717 FunctionTemplateAndArgsTy Result;
718
Gabor Marton5254e642018-06-27 13:32:50 +0000719 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000720 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
721 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000722
723 // Import template arguments.
724 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000725 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
726 std::get<1>(Result)))
727 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000728
Balazs Keri3b30d652018-10-19 13:32:20 +0000729 return Result;
730}
731
732template <>
733Expected<TemplateParameterList *>
734ASTNodeImporter::import(TemplateParameterList *From) {
735 SmallVector<NamedDecl *, 4> To(From->size());
736 if (Error Err = ImportContainerChecked(*From, To))
737 return std::move(Err);
738
739 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
740 if (!ToRequiresClause)
741 return ToRequiresClause.takeError();
742
743 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
744 if (!ToTemplateLocOrErr)
745 return ToTemplateLocOrErr.takeError();
746 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
747 if (!ToLAngleLocOrErr)
748 return ToLAngleLocOrErr.takeError();
749 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
750 if (!ToRAngleLocOrErr)
751 return ToRAngleLocOrErr.takeError();
752
753 return TemplateParameterList::Create(
754 Importer.getToContext(),
755 *ToTemplateLocOrErr,
756 *ToLAngleLocOrErr,
757 To,
758 *ToRAngleLocOrErr,
759 *ToRequiresClause);
760}
761
762template <>
763Expected<TemplateArgument>
764ASTNodeImporter::import(const TemplateArgument &From) {
765 switch (From.getKind()) {
766 case TemplateArgument::Null:
767 return TemplateArgument();
768
769 case TemplateArgument::Type: {
770 ExpectedType ToTypeOrErr = import(From.getAsType());
771 if (!ToTypeOrErr)
772 return ToTypeOrErr.takeError();
773 return TemplateArgument(*ToTypeOrErr);
774 }
775
776 case TemplateArgument::Integral: {
777 ExpectedType ToTypeOrErr = import(From.getIntegralType());
778 if (!ToTypeOrErr)
779 return ToTypeOrErr.takeError();
780 return TemplateArgument(From, *ToTypeOrErr);
781 }
782
783 case TemplateArgument::Declaration: {
784 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
785 if (!ToOrErr)
786 return ToOrErr.takeError();
787 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
788 if (!ToTypeOrErr)
789 return ToTypeOrErr.takeError();
790 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
791 }
792
793 case TemplateArgument::NullPtr: {
794 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
795 if (!ToTypeOrErr)
796 return ToTypeOrErr.takeError();
797 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
798 }
799
800 case TemplateArgument::Template: {
801 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
802 if (!ToTemplateOrErr)
803 return ToTemplateOrErr.takeError();
804
805 return TemplateArgument(*ToTemplateOrErr);
806 }
807
808 case TemplateArgument::TemplateExpansion: {
809 Expected<TemplateName> ToTemplateOrErr =
810 import(From.getAsTemplateOrTemplatePattern());
811 if (!ToTemplateOrErr)
812 return ToTemplateOrErr.takeError();
813
814 return TemplateArgument(
815 *ToTemplateOrErr, From.getNumTemplateExpansions());
816 }
817
818 case TemplateArgument::Expression:
819 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
820 return TemplateArgument(*ToExpr);
821 else
822 return ToExpr.takeError();
823
824 case TemplateArgument::Pack: {
825 SmallVector<TemplateArgument, 2> ToPack;
826 ToPack.reserve(From.pack_size());
827 if (Error Err = ImportTemplateArguments(
828 From.pack_begin(), From.pack_size(), ToPack))
829 return std::move(Err);
830
831 return TemplateArgument(
832 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
833 }
834 }
835
836 llvm_unreachable("Invalid template argument kind");
837}
838
839template <>
840Expected<TemplateArgumentLoc>
841ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
842 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
843 if (!ArgOrErr)
844 return ArgOrErr.takeError();
845 TemplateArgument Arg = *ArgOrErr;
846
847 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
848
849 TemplateArgumentLocInfo ToInfo;
850 if (Arg.getKind() == TemplateArgument::Expression) {
851 ExpectedExpr E = import(FromInfo.getAsExpr());
852 if (!E)
853 return E.takeError();
854 ToInfo = TemplateArgumentLocInfo(*E);
855 } else if (Arg.getKind() == TemplateArgument::Type) {
856 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
857 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
858 else
859 return TSIOrErr.takeError();
860 } else {
861 auto ToTemplateQualifierLocOrErr =
862 import(FromInfo.getTemplateQualifierLoc());
863 if (!ToTemplateQualifierLocOrErr)
864 return ToTemplateQualifierLocOrErr.takeError();
865 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
866 if (!ToTemplateNameLocOrErr)
867 return ToTemplateNameLocOrErr.takeError();
868 auto ToTemplateEllipsisLocOrErr =
869 import(FromInfo.getTemplateEllipsisLoc());
870 if (!ToTemplateEllipsisLocOrErr)
871 return ToTemplateEllipsisLocOrErr.takeError();
872
873 ToInfo = TemplateArgumentLocInfo(
874 *ToTemplateQualifierLocOrErr,
875 *ToTemplateNameLocOrErr,
876 *ToTemplateEllipsisLocOrErr);
877 }
878
879 return TemplateArgumentLoc(Arg, ToInfo);
880}
881
882template <>
883Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
884 if (DG.isNull())
885 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
886 size_t NumDecls = DG.end() - DG.begin();
887 SmallVector<Decl *, 1> ToDecls;
888 ToDecls.reserve(NumDecls);
889 for (Decl *FromD : DG) {
890 if (auto ToDOrErr = import(FromD))
891 ToDecls.push_back(*ToDOrErr);
892 else
893 return ToDOrErr.takeError();
894 }
895 return DeclGroupRef::Create(Importer.getToContext(),
896 ToDecls.begin(),
897 NumDecls);
898}
899
900template <>
901Expected<ASTNodeImporter::Designator>
902ASTNodeImporter::import(const Designator &D) {
903 if (D.isFieldDesignator()) {
904 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
905
906 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
907 if (!ToDotLocOrErr)
908 return ToDotLocOrErr.takeError();
909
910 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
911 if (!ToFieldLocOrErr)
912 return ToFieldLocOrErr.takeError();
913
914 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
915 }
916
917 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
918 if (!ToLBracketLocOrErr)
919 return ToLBracketLocOrErr.takeError();
920
921 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
922 if (!ToRBracketLocOrErr)
923 return ToRBracketLocOrErr.takeError();
924
925 if (D.isArrayDesignator())
926 return Designator(D.getFirstExprIndex(),
927 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
928
929 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
930 if (!ToEllipsisLocOrErr)
931 return ToEllipsisLocOrErr.takeError();
932
933 assert(D.isArrayRangeDesignator());
934 return Designator(
935 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
936 *ToRBracketLocOrErr);
937}
938
939template <>
940Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
941 VarDecl *Var = nullptr;
942 if (From.capturesVariable()) {
943 if (auto VarOrErr = import(From.getCapturedVar()))
944 Var = *VarOrErr;
945 else
946 return VarOrErr.takeError();
947 }
948
949 auto LocationOrErr = import(From.getLocation());
950 if (!LocationOrErr)
951 return LocationOrErr.takeError();
952
953 SourceLocation EllipsisLoc;
954 if (From.isPackExpansion())
955 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
956 return std::move(Err);
957
958 return LambdaCapture(
959 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
960 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000961}
962
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000963} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000964
Douglas Gregor3996e242010-02-15 22:01:00 +0000965//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000966// Import Types
967//----------------------------------------------------------------------------
968
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000969using namespace clang;
970
Balazs Keri3b30d652018-10-19 13:32:20 +0000971ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000972 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
973 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000974 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000975}
976
Balazs Keri3b30d652018-10-19 13:32:20 +0000977ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
978 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
979 if (!UnderlyingTypeOrErr)
980 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000981
Balazs Keri3b30d652018-10-19 13:32:20 +0000982 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000983}
984
Balazs Keri3b30d652018-10-19 13:32:20 +0000985ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000986 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000987#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
988 case BuiltinType::Id: \
989 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000990#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000991#define SHARED_SINGLETON_TYPE(Expansion)
992#define BUILTIN_TYPE(Id, SingletonId) \
993 case BuiltinType::Id: return Importer.getToContext().SingletonId;
994#include "clang/AST/BuiltinTypes.def"
995
996 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
997 // context supports C++.
998
999 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1000 // context supports ObjC.
1001
Douglas Gregor96e578d2010-02-05 17:54:41 +00001002 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +00001003 // The context we're importing from has an unsigned 'char'. If we're
1004 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001005 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001006 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001007 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001008
Douglas Gregor96e578d2010-02-05 17:54:41 +00001009 return Importer.getToContext().CharTy;
1010
Douglas Gregor96e578d2010-02-05 17:54:41 +00001011 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +00001012 // The context we're importing from has an unsigned 'char'. If we're
1013 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001015 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001016 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001017
Douglas Gregor96e578d2010-02-05 17:54:41 +00001018 return Importer.getToContext().CharTy;
1019
Chris Lattnerad3467e2010-12-25 23:25:43 +00001020 case BuiltinType::WChar_S:
1021 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001022 // FIXME: If not in C++, shall we translate to the C equivalent of
1023 // wchar_t?
1024 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001025 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001026
1027 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001028}
1029
Balazs Keri3b30d652018-10-19 13:32:20 +00001030ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1031 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1032 if (!ToOriginalTypeOrErr)
1033 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001036}
1037
Balazs Keri3b30d652018-10-19 13:32:20 +00001038ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1039 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1040 if (!ToElementTypeOrErr)
1041 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001042
Balazs Keri3b30d652018-10-19 13:32:20 +00001043 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001044}
1045
Balazs Keri3b30d652018-10-19 13:32:20 +00001046ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1047 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
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().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001052}
1053
Balazs Keri3b30d652018-10-19 13:32:20 +00001054ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001055 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001056 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1057 if (!ToPointeeTypeOrErr)
1058 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001059
Balazs Keri3b30d652018-10-19 13:32:20 +00001060 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001061}
1062
Balazs Keri3b30d652018-10-19 13:32:20 +00001063ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001064ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001065 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001066 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1067 if (!ToPointeeTypeOrErr)
1068 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001069
Balazs Keri3b30d652018-10-19 13:32:20 +00001070 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001071}
1072
Balazs Keri3b30d652018-10-19 13:32:20 +00001073ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001074ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001075 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001076 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1077 if (!ToPointeeTypeOrErr)
1078 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001079
Balazs Keri3b30d652018-10-19 13:32:20 +00001080 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001081}
1082
Balazs Keri3b30d652018-10-19 13:32:20 +00001083ExpectedType
1084ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001085 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001086 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1087 if (!ToPointeeTypeOrErr)
1088 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001089
Balazs Keri3b30d652018-10-19 13:32:20 +00001090 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1091 if (!ClassTypeOrErr)
1092 return ClassTypeOrErr.takeError();
1093
1094 return Importer.getToContext().getMemberPointerType(
1095 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001096}
1097
Balazs Keri3b30d652018-10-19 13:32:20 +00001098ExpectedType
1099ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1100 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1101 if (!ToElementTypeOrErr)
1102 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001103
Balazs Keri3b30d652018-10-19 13:32:20 +00001104 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001105 T->getSize(),
1106 T->getSizeModifier(),
1107 T->getIndexTypeCVRQualifiers());
1108}
1109
Balazs Keri3b30d652018-10-19 13:32:20 +00001110ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001111ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001112 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1113 if (!ToElementTypeOrErr)
1114 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001115
Balazs Keri3b30d652018-10-19 13:32:20 +00001116 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001117 T->getSizeModifier(),
1118 T->getIndexTypeCVRQualifiers());
1119}
1120
Balazs Keri3b30d652018-10-19 13:32:20 +00001121ExpectedType
1122ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1123 QualType ToElementType;
1124 Expr *ToSizeExpr;
1125 SourceRange ToBracketsRange;
1126 if (auto Imp = importSeq(
1127 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1128 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1129 else
1130 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001131
Balazs Keri3b30d652018-10-19 13:32:20 +00001132 return Importer.getToContext().getVariableArrayType(
1133 ToElementType, ToSizeExpr, T->getSizeModifier(),
1134 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001135}
1136
Balazs Keri3b30d652018-10-19 13:32:20 +00001137ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001138 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001139 QualType ToElementType;
1140 Expr *ToSizeExpr;
1141 SourceRange ToBracketsRange;
1142 if (auto Imp = importSeq(
1143 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1144 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1145 else
1146 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001147 // SizeExpr may be null if size is not specified directly.
1148 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001149
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001150 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001151 ToElementType, ToSizeExpr, T->getSizeModifier(),
1152 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001153}
1154
Balazs Keri3b30d652018-10-19 13:32:20 +00001155ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1156 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1157 if (!ToElementTypeOrErr)
1158 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001159
Balazs Keri3b30d652018-10-19 13:32:20 +00001160 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001161 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001162 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001163}
1164
Balazs Keri3b30d652018-10-19 13:32:20 +00001165ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1166 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1167 if (!ToElementTypeOrErr)
1168 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001169
Balazs Keri3b30d652018-10-19 13:32:20 +00001170 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001171 T->getNumElements());
1172}
1173
Balazs Keri3b30d652018-10-19 13:32:20 +00001174ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001175ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001176 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001177 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001178 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1179 if (!ToReturnTypeOrErr)
1180 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001181
Balazs Keri3b30d652018-10-19 13:32:20 +00001182 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001183 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001184}
1185
Balazs Keri3b30d652018-10-19 13:32:20 +00001186ExpectedType
1187ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1188 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1189 if (!ToReturnTypeOrErr)
1190 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001191
Douglas Gregor96e578d2010-02-05 17:54:41 +00001192 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001193 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001194 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001195 ExpectedType TyOrErr = import(A);
1196 if (!TyOrErr)
1197 return TyOrErr.takeError();
1198 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001199 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001200
Douglas Gregor96e578d2010-02-05 17:54:41 +00001201 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001202 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001203 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001204 ExpectedType TyOrErr = import(E);
1205 if (!TyOrErr)
1206 return TyOrErr.takeError();
1207 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001208 }
John McCalldb40c7f2010-12-14 08:05:40 +00001209
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001210 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1211 FunctionProtoType::ExtProtoInfo ToEPI;
1212
Balazs Keri3b30d652018-10-19 13:32:20 +00001213 auto Imp = importSeq(
1214 FromEPI.ExceptionSpec.NoexceptExpr,
1215 FromEPI.ExceptionSpec.SourceDecl,
1216 FromEPI.ExceptionSpec.SourceTemplate);
1217 if (!Imp)
1218 return Imp.takeError();
1219
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001220 ToEPI.ExtInfo = FromEPI.ExtInfo;
1221 ToEPI.Variadic = FromEPI.Variadic;
1222 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1223 ToEPI.TypeQuals = FromEPI.TypeQuals;
1224 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001225 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1226 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001227 std::tie(
1228 ToEPI.ExceptionSpec.NoexceptExpr,
1229 ToEPI.ExceptionSpec.SourceDecl,
1230 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001231
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 return Importer.getToContext().getFunctionType(
1233 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001234}
1235
Balazs Keri3b30d652018-10-19 13:32:20 +00001236ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001237 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001238 UnresolvedUsingTypenameDecl *ToD;
1239 Decl *ToPrevD;
1240 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1241 std::tie(ToD, ToPrevD) = *Imp;
1242 else
1243 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001244
Balazs Keri3b30d652018-10-19 13:32:20 +00001245 return Importer.getToContext().getTypeDeclType(
1246 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001247}
1248
Balazs Keri3b30d652018-10-19 13:32:20 +00001249ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1250 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1251 if (!ToInnerTypeOrErr)
1252 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001253
Balazs Keri3b30d652018-10-19 13:32:20 +00001254 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001255}
1256
Balazs Keri3b30d652018-10-19 13:32:20 +00001257ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1258 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1259 if (!ToDeclOrErr)
1260 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001263}
1264
Balazs Keri3b30d652018-10-19 13:32:20 +00001265ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1266 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1267 if (!ToExprOrErr)
1268 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001269
Balazs Keri3b30d652018-10-19 13:32:20 +00001270 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001271}
1272
Balazs Keri3b30d652018-10-19 13:32:20 +00001273ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1274 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1275 if (!ToUnderlyingTypeOrErr)
1276 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001277
Balazs Keri3b30d652018-10-19 13:32:20 +00001278 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001279}
1280
Balazs Keri3b30d652018-10-19 13:32:20 +00001281ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001282 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001283 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1284 if (!ToExprOrErr)
1285 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001286
Balazs Keri3b30d652018-10-19 13:32:20 +00001287 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1288 if (!ToUnderlyingTypeOrErr)
1289 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001290
Balazs Keri3b30d652018-10-19 13:32:20 +00001291 return Importer.getToContext().getDecltypeType(
1292 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001293}
1294
Balazs Keri3b30d652018-10-19 13:32:20 +00001295ExpectedType
1296ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1297 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1298 if (!ToBaseTypeOrErr)
1299 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001300
Balazs Keri3b30d652018-10-19 13:32:20 +00001301 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1302 if (!ToUnderlyingTypeOrErr)
1303 return ToUnderlyingTypeOrErr.takeError();
1304
1305 return Importer.getToContext().getUnaryTransformType(
1306 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001307}
1308
Balazs Keri3b30d652018-10-19 13:32:20 +00001309ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001310 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001311 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1312 if (!ToDeducedTypeOrErr)
1313 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001314
Balazs Keri3b30d652018-10-19 13:32:20 +00001315 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1316 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001317 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001318}
1319
Balazs Keri3b30d652018-10-19 13:32:20 +00001320ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001321 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001322 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1323 if (!ToDeclOrErr)
1324 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001325
Balazs Keri3b30d652018-10-19 13:32:20 +00001326 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1327 if (!ToInjTypeOrErr)
1328 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001329
1330 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1331 // See comments in InjectedClassNameType definition for details
1332 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1333 enum {
1334 TypeAlignmentInBits = 4,
1335 TypeAlignment = 1 << TypeAlignmentInBits
1336 };
1337
1338 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001339 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001340}
1341
Balazs Keri3b30d652018-10-19 13:32:20 +00001342ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1343 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1344 if (!ToDeclOrErr)
1345 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001346
Balazs Keri3b30d652018-10-19 13:32:20 +00001347 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001348}
1349
Balazs Keri3b30d652018-10-19 13:32:20 +00001350ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1351 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1352 if (!ToDeclOrErr)
1353 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001354
Balazs Keri3b30d652018-10-19 13:32:20 +00001355 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001356}
1357
Balazs Keri3b30d652018-10-19 13:32:20 +00001358ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1359 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1360 if (!ToModifiedTypeOrErr)
1361 return ToModifiedTypeOrErr.takeError();
1362 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1363 if (!ToEquivalentTypeOrErr)
1364 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001365
1366 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001367 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001368}
1369
Balazs Keri3b30d652018-10-19 13:32:20 +00001370ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001371 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001372 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1373 if (!ToDeclOrErr)
1374 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001375
1376 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001377 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001378}
1379
Balazs Keri3b30d652018-10-19 13:32:20 +00001380ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001381 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001382 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1383 if (!ReplacedOrErr)
1384 return ReplacedOrErr.takeError();
1385 const TemplateTypeParmType *Replaced =
1386 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001387
Balazs Keri3b30d652018-10-19 13:32:20 +00001388 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1389 if (!ToReplacementTypeOrErr)
1390 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001391
1392 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001394}
1395
Balazs Keri3b30d652018-10-19 13:32:20 +00001396ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001397 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 auto ToTemplateOrErr = import(T->getTemplateName());
1399 if (!ToTemplateOrErr)
1400 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001401
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001402 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001403 if (Error Err = ImportTemplateArguments(
1404 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1405 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Douglas Gregore2e50d332010-12-01 01:36:18 +00001407 QualType ToCanonType;
1408 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001409 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001410 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001411 if (ExpectedType TyOrErr = import(FromCanonType))
1412 ToCanonType = *TyOrErr;
1413 else
1414 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001415 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001416 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001417 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001418 ToCanonType);
1419}
1420
Balazs Keri3b30d652018-10-19 13:32:20 +00001421ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001422 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001423 auto ToQualifierOrErr = import(T->getQualifier());
1424 if (!ToQualifierOrErr)
1425 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001426
Balazs Keri3b30d652018-10-19 13:32:20 +00001427 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1428 if (!ToNamedTypeOrErr)
1429 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001430
Balazs Keri3b30d652018-10-19 13:32:20 +00001431 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1432 if (!ToOwnedTagDeclOrErr)
1433 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001434
Abramo Bagnara6150c882010-05-11 21:36:43 +00001435 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 *ToQualifierOrErr,
1437 *ToNamedTypeOrErr,
1438 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001439}
1440
Balazs Keri3b30d652018-10-19 13:32:20 +00001441ExpectedType
1442ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1443 ExpectedType ToPatternOrErr = import(T->getPattern());
1444 if (!ToPatternOrErr)
1445 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001446
Balazs Keri3b30d652018-10-19 13:32:20 +00001447 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001448 T->getNumExpansions());
1449}
1450
Balazs Keri3b30d652018-10-19 13:32:20 +00001451ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001452 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001453 auto ToQualifierOrErr = import(T->getQualifier());
1454 if (!ToQualifierOrErr)
1455 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001456
Balazs Keri3b30d652018-10-19 13:32:20 +00001457 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001458
1459 SmallVector<TemplateArgument, 2> ToPack;
1460 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001461 if (Error Err = ImportTemplateArguments(
1462 T->getArgs(), T->getNumArgs(), ToPack))
1463 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001464
1465 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001466 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001467}
1468
Balazs Keri3b30d652018-10-19 13:32:20 +00001469ExpectedType
1470ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1471 auto ToQualifierOrErr = import(T->getQualifier());
1472 if (!ToQualifierOrErr)
1473 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001474
1475 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001476
Balazs Keri3b30d652018-10-19 13:32:20 +00001477 QualType Canon;
1478 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1479 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1480 Canon = (*TyOrErr).getCanonicalType();
1481 else
1482 return TyOrErr.takeError();
1483 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001484
Balazs Keri3b30d652018-10-19 13:32:20 +00001485 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1486 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001487 Name, Canon);
1488}
1489
Balazs Keri3b30d652018-10-19 13:32:20 +00001490ExpectedType
1491ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1492 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1493 if (!ToDeclOrErr)
1494 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495
Balazs Keri3b30d652018-10-19 13:32:20 +00001496 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001497}
1498
Balazs Keri3b30d652018-10-19 13:32:20 +00001499ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1500 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1501 if (!ToBaseTypeOrErr)
1502 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001503
Douglas Gregore9d95f12015-07-07 03:57:35 +00001504 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001505 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001506 if (ExpectedType TyOrErr = import(TypeArg))
1507 TypeArgs.push_back(*TyOrErr);
1508 else
1509 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001510 }
1511
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001512 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001513 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001514 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1515 Protocols.push_back(*ProtocolOrErr);
1516 else
1517 return ProtocolOrErr.takeError();
1518
Douglas Gregor96e578d2010-02-05 17:54:41 +00001519 }
1520
Balazs Keri3b30d652018-10-19 13:32:20 +00001521 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001522 Protocols,
1523 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524}
1525
Balazs Keri3b30d652018-10-19 13:32:20 +00001526ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001527ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001528 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1529 if (!ToPointeeTypeOrErr)
1530 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001531
Balazs Keri3b30d652018-10-19 13:32:20 +00001532 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001533}
1534
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001535//----------------------------------------------------------------------------
1536// Import Declarations
1537//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001538Error ASTNodeImporter::ImportDeclParts(
1539 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1540 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001541 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1542 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1543 DeclContext *OrigDC = D->getDeclContext();
1544 FunctionDecl *FunDecl;
1545 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1546 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001547 auto getLeafPointeeType = [](const Type *T) {
1548 while (T->isPointerType() || T->isArrayType()) {
1549 T = T->getPointeeOrArrayElementType();
1550 }
1551 return T;
1552 };
1553 for (const ParmVarDecl *P : FunDecl->parameters()) {
1554 const Type *LeafT =
1555 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1556 auto *RT = dyn_cast<RecordType>(LeafT);
1557 if (RT && RT->getDecl() == D) {
1558 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1559 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001560 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001561 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001562 }
1563 }
1564
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001565 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001566 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1567 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001568
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001569 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001570 if (Error Err = importInto(Name, D->getDeclName()))
1571 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001572
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001573 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001574 if (Error Err = importInto(Loc, D->getLocation()))
1575 return Err;
1576
Sean Callanan59721b32015-04-28 18:41:46 +00001577 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Balazs Keri3b30d652018-10-19 13:32:20 +00001578
1579 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001580}
1581
Balazs Keri3b30d652018-10-19 13:32:20 +00001582Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001583 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001584 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001585
Balazs Keri3b30d652018-10-19 13:32:20 +00001586 if (!ToD)
1587 if (Error Err = importInto(ToD, FromD))
1588 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001589
Balazs Keri3b30d652018-10-19 13:32:20 +00001590 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1591 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1592 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1593 !ToRecord->getDefinition()) {
1594 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1595 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001596 }
1597 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001598 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001599 }
1600
Balazs Keri3b30d652018-10-19 13:32:20 +00001601 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1602 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001603 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001604 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1605 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001606 }
1607 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001608 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001609 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001610
1611 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001612}
1613
Balazs Keri3b30d652018-10-19 13:32:20 +00001614Error
1615ASTNodeImporter::ImportDeclarationNameLoc(
1616 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001617 // NOTE: To.Name and To.Loc are already imported.
1618 // We only have to import To.LocInfo.
1619 switch (To.getName().getNameKind()) {
1620 case DeclarationName::Identifier:
1621 case DeclarationName::ObjCZeroArgSelector:
1622 case DeclarationName::ObjCOneArgSelector:
1623 case DeclarationName::ObjCMultiArgSelector:
1624 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001625 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001626 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001627
1628 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001629 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1630 To.setCXXOperatorNameRange(*ToRangeOrErr);
1631 else
1632 return ToRangeOrErr.takeError();
1633 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001634 }
1635 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001636 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1637 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1638 else
1639 return LocOrErr.takeError();
1640 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001641 }
1642 case DeclarationName::CXXConstructorName:
1643 case DeclarationName::CXXDestructorName:
1644 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001645 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1646 To.setNamedTypeInfo(*ToTInfoOrErr);
1647 else
1648 return ToTInfoOrErr.takeError();
1649 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001650 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001651 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001652 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001653}
1654
Balazs Keri3b30d652018-10-19 13:32:20 +00001655Error
1656ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001657 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001658 auto ToDCOrErr = Importer.ImportContext(FromDC);
1659 return ToDCOrErr.takeError();
1660 }
Aleksei Sidorin89c1ac72018-10-29 21:46:18 +00001661
1662 const auto *FromRD = dyn_cast<RecordDecl>(FromDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00001663 for (auto *From : FromDC->decls()) {
1664 ExpectedDecl ImportedOrErr = import(From);
Aleksei Sidorin89c1ac72018-10-29 21:46:18 +00001665 if (!ImportedOrErr) {
1666 // For RecordDecls, failed import of a field will break the layout of the
1667 // structure. Handle it as an error.
1668 if (FromRD)
1669 return ImportedOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00001670 // Ignore the error, continue with next Decl.
1671 // FIXME: Handle this case somehow better.
Aleksei Sidorin89c1ac72018-10-29 21:46:18 +00001672 else
1673 consumeError(ImportedOrErr.takeError());
1674 }
1675 }
1676
1677 // Reorder declarations in RecordDecls because they may have another
1678 // order. Keeping field order is vitable because it determines structure
1679 // layout.
1680 // FIXME: This is an ugly fix. Unfortunately, I cannot come with better
1681 // solution for this issue. We cannot defer expression import here because
1682 // type import can depend on them.
1683 if (!FromRD)
1684 return Error::success();
1685
1686 auto ImportedDC = import(cast<Decl>(FromDC));
1687 assert(ImportedDC);
1688 auto *ToRD = cast<RecordDecl>(*ImportedDC);
1689
1690 for (auto *D : FromRD->decls()) {
1691 if (isa<FieldDecl>(D) || isa<FriendDecl>(D)) {
1692 Decl *ToD = Importer.GetAlreadyImportedOrNull(D);
1693 assert(ToRD == ToD->getDeclContext() && ToRD->containsDecl(ToD));
1694 ToRD->removeDecl(ToD);
1695 }
1696 }
1697
1698 assert(ToRD->field_empty());
1699
1700 for (auto *D : FromRD->decls()) {
1701 if (isa<FieldDecl>(D) || isa<FriendDecl>(D)) {
1702 Decl *ToD = Importer.GetAlreadyImportedOrNull(D);
1703 assert(ToRD == ToD->getDeclContext());
1704 assert(ToRD == ToD->getLexicalDeclContext());
1705 assert(!ToRD->containsDecl(ToD));
1706 ToRD->addDeclInternal(ToD);
1707 }
Douglas Gregor0a791672011-01-18 03:11:38 +00001708 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001709
Balazs Keri3b30d652018-10-19 13:32:20 +00001710 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001711}
1712
Balazs Keri3b30d652018-10-19 13:32:20 +00001713Error ASTNodeImporter::ImportDeclContext(
1714 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1715 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1716 if (!ToDCOrErr)
1717 return ToDCOrErr.takeError();
1718 ToDC = *ToDCOrErr;
1719
1720 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1721 auto ToLexicalDCOrErr = Importer.ImportContext(
1722 FromD->getLexicalDeclContext());
1723 if (!ToLexicalDCOrErr)
1724 return ToLexicalDCOrErr.takeError();
1725 ToLexicalDC = *ToLexicalDCOrErr;
1726 } else
1727 ToLexicalDC = ToDC;
1728
1729 return Error::success();
1730}
1731
1732Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001733 const CXXRecordDecl *From, CXXRecordDecl *To) {
1734 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1735 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001736
Balazs Keri1d20cc22018-07-16 12:16:39 +00001737 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001738 if (FromM->isImplicit()) {
1739 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1740 if (!ToMOrErr)
1741 return ToMOrErr.takeError();
1742 }
1743
1744 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001745}
1746
Balazs Keri3b30d652018-10-19 13:32:20 +00001747static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1748 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001749 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001750 Decl *ToTypedef = Importer.Import(FromTypedef);
1751 if (!ToTypedef)
1752 return make_error<ImportError>();
1753 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToTypedef));
1754 // FIXME: This should be the final code.
1755 //if (Expected<Decl *> ToTypedefOrErr = Importer.Import(FromTypedef))
1756 // To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1757 //else
1758 // return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001759 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001760 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001761}
1762
Balazs Keri3b30d652018-10-19 13:32:20 +00001763Error ASTNodeImporter::ImportDefinition(
1764 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001765 if (To->getDefinition() || To->isBeingDefined()) {
1766 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001767 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001768
Balazs Keri3b30d652018-10-19 13:32:20 +00001769 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001770 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001771
Douglas Gregore2e50d332010-12-01 01:36:18 +00001772 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001773
Balazs Keri3b30d652018-10-19 13:32:20 +00001774 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1775 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001776
Douglas Gregore2e50d332010-12-01 01:36:18 +00001777 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001778 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1779 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001780
1781 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1782 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1783 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001784 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001785 ToData.Aggregate = FromData.Aggregate;
1786 ToData.PlainOldData = FromData.PlainOldData;
1787 ToData.Empty = FromData.Empty;
1788 ToData.Polymorphic = FromData.Polymorphic;
1789 ToData.Abstract = FromData.Abstract;
1790 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001791 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1792 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1793 ToData.HasBasesWithNonStaticDataMembers =
1794 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001795 ToData.HasPrivateFields = FromData.HasPrivateFields;
1796 ToData.HasProtectedFields = FromData.HasProtectedFields;
1797 ToData.HasPublicFields = FromData.HasPublicFields;
1798 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001799 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001800 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001801 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001802 ToData.HasUninitializedReferenceMember
1803 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001804 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001805 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1806 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001807 ToData.NeedOverloadResolutionForCopyConstructor
1808 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001809 ToData.NeedOverloadResolutionForMoveConstructor
1810 = FromData.NeedOverloadResolutionForMoveConstructor;
1811 ToData.NeedOverloadResolutionForMoveAssignment
1812 = FromData.NeedOverloadResolutionForMoveAssignment;
1813 ToData.NeedOverloadResolutionForDestructor
1814 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001815 ToData.DefaultedCopyConstructorIsDeleted
1816 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001817 ToData.DefaultedMoveConstructorIsDeleted
1818 = FromData.DefaultedMoveConstructorIsDeleted;
1819 ToData.DefaultedMoveAssignmentIsDeleted
1820 = FromData.DefaultedMoveAssignmentIsDeleted;
1821 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001822 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1823 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001824 ToData.HasConstexprNonCopyMoveConstructor
1825 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001826 ToData.HasDefaultedDefaultConstructor
1827 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001828 ToData.DefaultedDefaultConstructorIsConstexpr
1829 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001830 ToData.HasConstexprDefaultConstructor
1831 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001832 ToData.HasNonLiteralTypeFieldsOrBases
1833 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001834 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001835 ToData.UserProvidedDefaultConstructor
1836 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001837 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001838 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1839 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1840 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1841 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001842 ToData.ImplicitCopyAssignmentHasConstParam
1843 = FromData.ImplicitCopyAssignmentHasConstParam;
1844 ToData.HasDeclaredCopyConstructorWithConstParam
1845 = FromData.HasDeclaredCopyConstructorWithConstParam;
1846 ToData.HasDeclaredCopyAssignmentWithConstParam
1847 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001848
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001849 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001850 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001851 ExpectedType TyOrErr = import(Base1.getType());
1852 if (!TyOrErr)
1853 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001854
1855 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001856 if (Base1.isPackExpansion()) {
1857 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1858 EllipsisLoc = *LocOrErr;
1859 else
1860 return LocOrErr.takeError();
1861 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001862
1863 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001864 if (Error Err =
1865 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1866 return Err;
1867
1868 auto RangeOrErr = import(Base1.getSourceRange());
1869 if (!RangeOrErr)
1870 return RangeOrErr.takeError();
1871
1872 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1873 if (!TSIOrErr)
1874 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001875
Douglas Gregore2e50d332010-12-01 01:36:18 +00001876 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001877 new (Importer.getToContext()) CXXBaseSpecifier(
1878 *RangeOrErr,
1879 Base1.isVirtual(),
1880 Base1.isBaseOfClass(),
1881 Base1.getAccessSpecifierAsWritten(),
1882 *TSIOrErr,
1883 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001884 }
1885 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001886 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001887 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001888
Douglas Gregor2e15c842012-02-01 21:00:38 +00001889 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001890 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1891 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001892
Douglas Gregore2e50d332010-12-01 01:36:18 +00001893 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001894 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001895}
1896
Balazs Keri3b30d652018-10-19 13:32:20 +00001897Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001898 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001899 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001900
Gabor Martonac3a5d62018-09-17 12:04:52 +00001901 Expr *FromInit = From->getInit();
1902 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001903 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001904
Balazs Keri3b30d652018-10-19 13:32:20 +00001905 ExpectedExpr ToInitOrErr = import(FromInit);
1906 if (!ToInitOrErr)
1907 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001908
Balazs Keri3b30d652018-10-19 13:32:20 +00001909 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001910 if (From->isInitKnownICE()) {
1911 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1912 Eval->CheckedICE = true;
1913 Eval->IsICE = From->isInitICE();
1914 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001915
1916 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001917 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001918}
1919
Balazs Keri3b30d652018-10-19 13:32:20 +00001920Error ASTNodeImporter::ImportDefinition(
1921 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001922 if (To->getDefinition() || To->isBeingDefined()) {
1923 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001924 return ImportDeclContext(From, /*ForceImport=*/true);
1925 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001926 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001927
Douglas Gregord451ea92011-07-29 23:31:30 +00001928 To->startDefinition();
1929
Balazs Keri3b30d652018-10-19 13:32:20 +00001930 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1931 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001932
Balazs Keri3b30d652018-10-19 13:32:20 +00001933 ExpectedType ToTypeOrErr =
1934 import(Importer.getFromContext().getTypeDeclType(From));
1935 if (!ToTypeOrErr)
1936 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001937
Balazs Keri3b30d652018-10-19 13:32:20 +00001938 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1939 if (!ToPromotionTypeOrErr)
1940 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001941
1942 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001943 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1944 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001945
Douglas Gregord451ea92011-07-29 23:31:30 +00001946 // FIXME: we might need to merge the number of positive or negative bits
1947 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001948 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001949 From->getNumPositiveBits(),
1950 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001951 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001952}
1953
Balazs Keri3b30d652018-10-19 13:32:20 +00001954// FIXME: Remove this, use `import` instead.
1955Expected<TemplateParameterList *> ASTNodeImporter::ImportTemplateParameterList(
1956 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001957 SmallVector<NamedDecl *, 4> ToParams(Params->size());
Balazs Keri3b30d652018-10-19 13:32:20 +00001958 if (Error Err = ImportContainerChecked(*Params, ToParams))
1959 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001960
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001961 Expr *ToRequiresClause;
1962 if (Expr *const R = Params->getRequiresClause()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001963 if (Error Err = importInto(ToRequiresClause, R))
1964 return std::move(Err);
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001965 } else {
1966 ToRequiresClause = nullptr;
1967 }
1968
Balazs Keri3b30d652018-10-19 13:32:20 +00001969 auto ToTemplateLocOrErr = import(Params->getTemplateLoc());
1970 if (!ToTemplateLocOrErr)
1971 return ToTemplateLocOrErr.takeError();
1972 auto ToLAngleLocOrErr = import(Params->getLAngleLoc());
1973 if (!ToLAngleLocOrErr)
1974 return ToLAngleLocOrErr.takeError();
1975 auto ToRAngleLocOrErr = import(Params->getRAngleLoc());
1976 if (!ToRAngleLocOrErr)
1977 return ToRAngleLocOrErr.takeError();
1978
1979 return TemplateParameterList::Create(
1980 Importer.getToContext(),
1981 *ToTemplateLocOrErr,
1982 *ToLAngleLocOrErr,
1983 ToParams,
1984 *ToRAngleLocOrErr,
1985 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001986}
1987
Balazs Keri3b30d652018-10-19 13:32:20 +00001988Error ASTNodeImporter::ImportTemplateArguments(
1989 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1990 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001991 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001992 if (auto ToOrErr = import(FromArgs[I]))
1993 ToArgs.push_back(*ToOrErr);
1994 else
1995 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001996 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001997
Balazs Keri3b30d652018-10-19 13:32:20 +00001998 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001999}
2000
Balazs Keri3b30d652018-10-19 13:32:20 +00002001// FIXME: Do not forget to remove this and use only 'import'.
2002Expected<TemplateArgument>
2003ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2004 return import(From);
2005}
2006
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002007template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00002008Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002009 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
2010 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002011 if (auto ToLocOrErr = import(FromLoc))
2012 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002013 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002014 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002015 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002016 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002017}
2018
Gabor Marton26f72a92018-07-12 09:42:05 +00002019static StructuralEquivalenceKind
2020getStructuralEquivalenceKind(const ASTImporter &Importer) {
2021 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
2022 : StructuralEquivalenceKind::Default;
2023}
2024
Gabor Marton950fb572018-07-17 12:39:27 +00002025bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
2026 StructuralEquivalenceContext Ctx(
2027 Importer.getFromContext(), Importer.getToContext(),
2028 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2029 false, Complain);
2030 return Ctx.IsEquivalent(From, To);
2031}
2032
2033bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002034 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002035 // Eliminate a potential failure point where we attempt to re-import
2036 // something we're trying to import while completing ToRecord.
2037 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2038 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002039 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002040 if (ToOriginRecord)
2041 ToRecord = ToOriginRecord;
2042 }
2043
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002044 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002045 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002046 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002047 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002048 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002049 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002050}
2051
Larisse Voufo39a1e502013-08-06 01:03:05 +00002052bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2053 bool Complain) {
2054 StructuralEquivalenceContext Ctx(
2055 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002056 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2057 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002058 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002059}
2060
Douglas Gregor98c10182010-02-12 22:17:39 +00002061bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002062 StructuralEquivalenceContext Ctx(
2063 Importer.getFromContext(), Importer.getToContext(),
2064 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002065 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002066}
2067
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002068bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2069 FunctionTemplateDecl *To) {
2070 StructuralEquivalenceContext Ctx(
2071 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002072 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2073 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002074 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002075}
2076
Balazs Keric7797c42018-07-11 09:37:24 +00002077bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2078 StructuralEquivalenceContext Ctx(
2079 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002080 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2081 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002082 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002083}
2084
Douglas Gregor91155082012-11-14 22:29:20 +00002085bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002086 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002087 const llvm::APSInt &FromVal = FromEC->getInitVal();
2088 const llvm::APSInt &ToVal = ToEC->getInitVal();
2089
2090 return FromVal.isSigned() == ToVal.isSigned() &&
2091 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2092 FromVal == ToVal;
2093}
2094
2095bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002096 ClassTemplateDecl *To) {
2097 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2098 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002099 Importer.getNonEquivalentDecls(),
2100 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002101 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002102}
2103
Larisse Voufo39a1e502013-08-06 01:03:05 +00002104bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2105 VarTemplateDecl *To) {
2106 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2107 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002108 Importer.getNonEquivalentDecls(),
2109 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002110 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002111}
2112
Balazs Keri3b30d652018-10-19 13:32:20 +00002113ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002114 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002115 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002116 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002117}
2118
Balazs Keri3b30d652018-10-19 13:32:20 +00002119ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2120 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2121 << D->getDeclKindName();
2122 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2123}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002124
Balazs Keri3b30d652018-10-19 13:32:20 +00002125ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2126 // Import the context of this declaration.
2127 DeclContext *DC, *LexicalDC;
2128 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2129 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002130
2131 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002132 ExpectedSLoc LocOrErr = import(D->getLocation());
2133 if (!LocOrErr)
2134 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002135
Gabor Marton26f72a92018-07-12 09:42:05 +00002136 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002137 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002138 return ToD;
2139
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002140 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002141 LexicalDC->addDeclInternal(ToD);
2142 return ToD;
2143}
2144
Balazs Keri3b30d652018-10-19 13:32:20 +00002145ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002146 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002147 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002148
Gabor Marton26f72a92018-07-12 09:42:05 +00002149 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002150
Sean Callanan65198272011-11-17 23:20:56 +00002151 return ToD;
2152}
2153
Balazs Keri3b30d652018-10-19 13:32:20 +00002154ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2155 ExpectedSLoc LocOrErr = import(D->getLocation());
2156 if (!LocOrErr)
2157 return LocOrErr.takeError();
2158 auto ColonLocOrErr = import(D->getColonLoc());
2159 if (!ColonLocOrErr)
2160 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002161
2162 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002163 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2164 if (!DCOrErr)
2165 return DCOrErr.takeError();
2166 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002167
Gabor Marton26f72a92018-07-12 09:42:05 +00002168 AccessSpecDecl *ToD;
2169 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002170 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002171 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002172
2173 // Lexical DeclContext and Semantic DeclContext
2174 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002175 ToD->setLexicalDeclContext(DC);
2176 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002177
Gabor Marton26f72a92018-07-12 09:42:05 +00002178 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002179}
2180
Balazs Keri3b30d652018-10-19 13:32:20 +00002181ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2182 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2183 if (!DCOrErr)
2184 return DCOrErr.takeError();
2185 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002186 DeclContext *LexicalDC = DC;
2187
Balazs Keri3b30d652018-10-19 13:32:20 +00002188 SourceLocation ToLocation, ToRParenLoc;
2189 Expr *ToAssertExpr;
2190 StringLiteral *ToMessage;
2191 if (auto Imp = importSeq(
2192 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2193 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2194 else
2195 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002196
Gabor Marton26f72a92018-07-12 09:42:05 +00002197 StaticAssertDecl *ToD;
2198 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002199 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2200 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002201 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002202
2203 ToD->setLexicalDeclContext(LexicalDC);
2204 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002205 return ToD;
2206}
2207
Balazs Keri3b30d652018-10-19 13:32:20 +00002208ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002209 // Import the major distinguishing characteristics of this namespace.
2210 DeclContext *DC, *LexicalDC;
2211 DeclarationName Name;
2212 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002213 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002214 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2215 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002216 if (ToD)
2217 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002218
2219 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002220 if (!Name) {
2221 // This is an anonymous namespace. Adopt an existing anonymous
2222 // namespace if we can.
2223 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002224 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002225 MergeWithNamespace = TU->getAnonymousNamespace();
2226 else
2227 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2228 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002229 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002230 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002231 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002232 for (auto *FoundDecl : FoundDecls) {
2233 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002234 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002235
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002236 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002237 MergeWithNamespace = FoundNS;
2238 ConflictingDecls.clear();
2239 break;
2240 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002241
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002242 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002243 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002244
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002245 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002246 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002247 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002248 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002249 if (!Name)
2250 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002251 }
2252 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002253
Balazs Keri3b30d652018-10-19 13:32:20 +00002254 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2255 if (!BeginLocOrErr)
2256 return BeginLocOrErr.takeError();
2257
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002258 // Create the "to" namespace, if needed.
2259 NamespaceDecl *ToNamespace = MergeWithNamespace;
2260 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002261 if (GetImportedOrCreateDecl(
2262 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002263 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002264 /*PrevDecl=*/nullptr))
2265 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002266 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002267 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002268
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002269 // If this is an anonymous namespace, register it as the anonymous
2270 // namespace within its context.
2271 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002272 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002273 TU->setAnonymousNamespace(ToNamespace);
2274 else
2275 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2276 }
2277 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002278 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002279
Balazs Keri3b30d652018-10-19 13:32:20 +00002280 if (Error Err = ImportDeclContext(D))
2281 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002282
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002283 return ToNamespace;
2284}
2285
Balazs Keri3b30d652018-10-19 13:32:20 +00002286ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002287 // Import the major distinguishing characteristics of this namespace.
2288 DeclContext *DC, *LexicalDC;
2289 DeclarationName Name;
2290 SourceLocation Loc;
2291 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002292 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2293 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002294 if (LookupD)
2295 return LookupD;
2296
2297 // NOTE: No conflict resolution is done for namespace aliases now.
2298
Balazs Keri3b30d652018-10-19 13:32:20 +00002299 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2300 NestedNameSpecifierLoc ToQualifierLoc;
2301 NamespaceDecl *ToNamespace;
2302 if (auto Imp = importSeq(
2303 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2304 D->getTargetNameLoc(), D->getNamespace()))
2305 std::tie(
2306 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2307 ToNamespace) = *Imp;
2308 else
2309 return Imp.takeError();
2310 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002311
Gabor Marton26f72a92018-07-12 09:42:05 +00002312 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002313 if (GetImportedOrCreateDecl(
2314 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2315 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002316 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002317
2318 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002319 LexicalDC->addDeclInternal(ToD);
2320
2321 return ToD;
2322}
2323
Balazs Keri3b30d652018-10-19 13:32:20 +00002324ExpectedDecl
2325ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002326 // Import the major distinguishing characteristics of this typedef.
2327 DeclContext *DC, *LexicalDC;
2328 DeclarationName Name;
2329 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002330 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002331 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2332 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002333 if (ToD)
2334 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002335
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002336 // If this typedef is not in block scope, determine whether we've
2337 // seen a typedef with the same name (that we can merge with) or any
2338 // other entity by that name (which name lookup could conflict with).
2339 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002340 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002341 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002342 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002343 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002344 for (auto *FoundDecl : FoundDecls) {
2345 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002346 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002347 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002348 if (Importer.IsStructurallyEquivalent(
2349 D->getUnderlyingType(), FoundTypedef->getUnderlyingType()))
2350 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002351 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002352
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002353 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002354 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002355
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002356 if (!ConflictingDecls.empty()) {
2357 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002358 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002359 ConflictingDecls.size());
2360 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002361 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002362 }
2363 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002364
Balazs Keri3b30d652018-10-19 13:32:20 +00002365 QualType ToUnderlyingType;
2366 TypeSourceInfo *ToTypeSourceInfo;
2367 SourceLocation ToBeginLoc;
2368 if (auto Imp = importSeq(
2369 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2370 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2371 else
2372 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002373
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002374 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002375 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002376 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002377 if (IsAlias) {
2378 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002379 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2380 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002381 return ToTypedef;
2382 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002383 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2384 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002385 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002386
Douglas Gregordd483172010-02-22 17:42:47 +00002387 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002388 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002389
2390 // Templated declarations should not appear in DeclContext.
2391 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2392 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2393 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002394
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002395 return ToTypedef;
2396}
2397
Balazs Keri3b30d652018-10-19 13:32:20 +00002398ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002399 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2400}
2401
Balazs Keri3b30d652018-10-19 13:32:20 +00002402ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002403 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2404}
2405
Balazs Keri3b30d652018-10-19 13:32:20 +00002406ExpectedDecl
2407ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002408 // Import the major distinguishing characteristics of this typedef.
2409 DeclContext *DC, *LexicalDC;
2410 DeclarationName Name;
2411 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002412 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002413 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2414 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002415 if (FoundD)
2416 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002417
2418 // If this typedef is not in block scope, determine whether we've
2419 // seen a typedef with the same name (that we can merge with) or any
2420 // other entity by that name (which name lookup could conflict with).
2421 if (!DC->isFunctionOrMethod()) {
2422 SmallVector<NamedDecl *, 4> ConflictingDecls;
2423 unsigned IDNS = Decl::IDNS_Ordinary;
2424 SmallVector<NamedDecl *, 2> FoundDecls;
2425 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002426 for (auto *FoundDecl : FoundDecls) {
2427 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002428 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002429 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002430 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002431 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002432 }
2433
2434 if (!ConflictingDecls.empty()) {
2435 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2436 ConflictingDecls.data(),
2437 ConflictingDecls.size());
2438 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002439 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002440 }
2441 }
2442
Balazs Keri3b30d652018-10-19 13:32:20 +00002443 TemplateParameterList *ToTemplateParameters;
2444 TypeAliasDecl *ToTemplatedDecl;
2445 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2446 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2447 else
2448 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002449
Gabor Marton26f72a92018-07-12 09:42:05 +00002450 TypeAliasTemplateDecl *ToAlias;
2451 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002452 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002453 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002454
Balazs Keri3b30d652018-10-19 13:32:20 +00002455 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002456
Gabor Horvath7a91c082017-11-14 11:30:38 +00002457 ToAlias->setAccess(D->getAccess());
2458 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002459 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002460 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002461}
2462
Balazs Keri3b30d652018-10-19 13:32:20 +00002463ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002464 // Import the major distinguishing characteristics of this label.
2465 DeclContext *DC, *LexicalDC;
2466 DeclarationName Name;
2467 SourceLocation Loc;
2468 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002469 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2470 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002471 if (ToD)
2472 return ToD;
2473
2474 assert(LexicalDC->isFunctionOrMethod());
2475
Gabor Marton26f72a92018-07-12 09:42:05 +00002476 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002477 if (D->isGnuLocal()) {
2478 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2479 if (!BeginLocOrErr)
2480 return BeginLocOrErr.takeError();
2481 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2482 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2483 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002484
Balazs Keri3b30d652018-10-19 13:32:20 +00002485 } else {
2486 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2487 Name.getAsIdentifierInfo()))
2488 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002489
Balazs Keri3b30d652018-10-19 13:32:20 +00002490 }
2491
2492 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2493 if (!ToStmtOrErr)
2494 return ToStmtOrErr.takeError();
2495
2496 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002497 ToLabel->setLexicalDeclContext(LexicalDC);
2498 LexicalDC->addDeclInternal(ToLabel);
2499 return ToLabel;
2500}
2501
Balazs Keri3b30d652018-10-19 13:32:20 +00002502ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002503 // Import the major distinguishing characteristics of this enum.
2504 DeclContext *DC, *LexicalDC;
2505 DeclarationName Name;
2506 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002507 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002508 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2509 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002510 if (ToD)
2511 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002512
Douglas Gregor98c10182010-02-12 22:17:39 +00002513 // Figure out what enum name we're looking for.
2514 unsigned IDNS = Decl::IDNS_Tag;
2515 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002516 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002517 if (Error Err = importInto(
2518 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2519 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002520 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002521 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002522 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002523
Douglas Gregor98c10182010-02-12 22:17:39 +00002524 // We may already have an enum of the same name; try to find and match it.
2525 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002526 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002527 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002528 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002529 for (auto *FoundDecl : FoundDecls) {
2530 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002531 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002532
Balazs Keri3b30d652018-10-19 13:32:20 +00002533 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002534 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002535 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002536 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002537
Balazs Keri3b30d652018-10-19 13:32:20 +00002538 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002539 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002540 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002541 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002542
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002543 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002544 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002545
Douglas Gregor98c10182010-02-12 22:17:39 +00002546 if (!ConflictingDecls.empty()) {
2547 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002548 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002549 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002550 if (!Name)
2551 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002552 }
2553 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002554
Balazs Keri3b30d652018-10-19 13:32:20 +00002555 SourceLocation ToBeginLoc;
2556 NestedNameSpecifierLoc ToQualifierLoc;
2557 QualType ToIntegerType;
2558 if (auto Imp = importSeq(
2559 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2560 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2561 else
2562 return Imp.takeError();
2563
Douglas Gregor98c10182010-02-12 22:17:39 +00002564 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002565 EnumDecl *D2;
2566 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002567 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002568 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2569 D->isScopedUsingClassTag(), D->isFixed()))
2570 return D2;
2571
Balazs Keri3b30d652018-10-19 13:32:20 +00002572 D2->setQualifierInfo(ToQualifierLoc);
2573 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002574 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002575 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002576 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002577
Douglas Gregor98c10182010-02-12 22:17:39 +00002578 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002579 if (D->isCompleteDefinition())
2580 if (Error Err = ImportDefinition(D, D2))
2581 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002582
Douglas Gregor3996e242010-02-15 22:01:00 +00002583 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002584}
2585
Balazs Keri3b30d652018-10-19 13:32:20 +00002586ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002587 bool IsFriendTemplate = false;
2588 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2589 IsFriendTemplate =
2590 DCXX->getDescribedClassTemplate() &&
2591 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2592 Decl::FOK_None;
2593 }
2594
Douglas Gregor5c73e912010-02-11 00:48:18 +00002595 // If this record has a definition in the translation unit we're coming from,
2596 // but this particular declaration is not that definition, import the
2597 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002598 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002599 if (Definition && Definition != D &&
Balazs Keri0c23dc52018-08-13 13:08:37 +00002600 // Friend template declaration must be imported on its own.
2601 !IsFriendTemplate &&
Gabor Martona3af5672018-05-23 14:24:02 +00002602 // In contrast to a normal CXXRecordDecl, the implicit
2603 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2604 // The definition of the implicit CXXRecordDecl in this case is the
2605 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2606 // condition in order to be able to import the implict Decl.
2607 !D->isImplicit()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002608 ExpectedDecl ImportedDefOrErr = import(Definition);
2609 if (!ImportedDefOrErr)
2610 return ImportedDefOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002611
Balazs Keri3b30d652018-10-19 13:32:20 +00002612 return Importer.MapImported(D, *ImportedDefOrErr);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002613 }
Gabor Martona3af5672018-05-23 14:24:02 +00002614
Douglas Gregor5c73e912010-02-11 00:48:18 +00002615 // Import the major distinguishing characteristics of this record.
2616 DeclContext *DC, *LexicalDC;
2617 DeclarationName Name;
2618 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002619 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002620 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2621 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002622 if (ToD)
2623 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002624
Douglas Gregor5c73e912010-02-11 00:48:18 +00002625 // Figure out what structure name we're looking for.
2626 unsigned IDNS = Decl::IDNS_Tag;
2627 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002628 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002629 if (Error Err = importInto(
2630 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2631 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002632 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002633 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002634 IDNS |= Decl::IDNS_Ordinary;
2635
2636 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002637 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002638 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002639 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002640 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002641 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002642 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002643
2644 if (!FoundDecls.empty()) {
2645 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2646 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2647 D->getASTContext().getExternalSource()->CompleteType(D);
2648 }
2649
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002650 for (auto *FoundDecl : FoundDecls) {
2651 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002652 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002653
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002654 Decl *Found = FoundDecl;
2655 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2656 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002657 Found = Tag->getDecl();
2658 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002659
2660 if (D->getDescribedTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002661 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Martona0df7a92018-05-30 09:19:26 +00002662 Found = Template->getTemplatedDecl();
Balazs Keri3b30d652018-10-19 13:32:20 +00002663 } else {
2664 ConflictingDecls.push_back(FoundDecl);
Gabor Martona0df7a92018-05-30 09:19:26 +00002665 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002666 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002667 }
2668
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002669 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002670 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002671 if (!IsStructuralMatch(D, FoundRecord, false))
2672 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002673 } else {
2674 if (!IsStructuralMatch(D, FoundRecord)) {
2675 ConflictingDecls.push_back(FoundDecl);
2676 continue;
2677 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002678 }
2679
Sean Callanan9092d472017-05-13 00:46:33 +00002680 PrevDecl = FoundRecord;
2681
Douglas Gregor25791052010-02-12 00:09:27 +00002682 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002683 if ((SearchName && !D->isCompleteDefinition() && !IsFriendTemplate)
Douglas Gregordd6006f2012-07-17 21:16:27 +00002684 || (D->isCompleteDefinition() &&
2685 D->isAnonymousStructOrUnion()
Balazs Keri3b30d652018-10-19 13:32:20 +00002686 == FoundDef->isAnonymousStructOrUnion())) {
Douglas Gregor25791052010-02-12 00:09:27 +00002687 // The record types structurally match, or the "from" translation
2688 // unit only had a forward declaration anyway; call it the same
2689 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002690 // FIXME: Structural equivalence check should check for same
2691 // user-defined methods.
2692 Importer.MapImported(D, FoundDef);
2693 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2694 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2695 assert(FoundCXX && "Record type mismatch");
2696
2697 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2698 // FoundDef may not have every implicit method that D has
2699 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002700 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2701 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002702 }
2703 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002704 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002705 if (IsFriendTemplate)
2706 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002707 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002708 // We have a forward declaration of this type, so adopt that forward
2709 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002710
Sean Callananc94711c2014-03-04 18:11:50 +00002711 // If one or both can be completed from external storage then try one
2712 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002713
Sean Callananc94711c2014-03-04 18:11:50 +00002714 if (FoundRecord->hasExternalLexicalStorage() &&
2715 !FoundRecord->isCompleteDefinition())
2716 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2717 if (D->hasExternalLexicalStorage())
2718 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002719
Sean Callananc94711c2014-03-04 18:11:50 +00002720 if (FoundRecord->isCompleteDefinition() &&
2721 D->isCompleteDefinition() &&
Balazs Keri3b30d652018-10-19 13:32:20 +00002722 !IsStructuralMatch(D, FoundRecord)) {
2723 ConflictingDecls.push_back(FoundDecl);
Sean Callananc94711c2014-03-04 18:11:50 +00002724 continue;
Balazs Keri3b30d652018-10-19 13:32:20 +00002725 }
Balazs Keri0c23dc52018-08-13 13:08:37 +00002726
Douglas Gregor25791052010-02-12 00:09:27 +00002727 AdoptDecl = FoundRecord;
2728 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002729 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002730
2731 continue;
2732 } else if (isa<ValueDecl>(Found))
2733 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002734
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002735 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002736 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002737
Douglas Gregordd6006f2012-07-17 21:16:27 +00002738 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002739 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002740 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002741 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002742 if (!Name)
2743 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002744 }
2745 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002746
Balazs Keri3b30d652018-10-19 13:32:20 +00002747 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2748 if (!BeginLocOrErr)
2749 return BeginLocOrErr.takeError();
2750
Douglas Gregor5c73e912010-02-11 00:48:18 +00002751 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002752 RecordDecl *D2 = AdoptDecl;
2753 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002754 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002755 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002756 if (DCXX->isLambda()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002757 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2758 if (!TInfoOrErr)
2759 return TInfoOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00002760 if (GetImportedOrCreateSpecialDecl(
2761 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002762 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002763 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2764 return D2CXX;
Balazs Keri3b30d652018-10-19 13:32:20 +00002765 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2766 if (!CDeclOrErr)
2767 return CDeclOrErr.takeError();
2768 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002769 } else if (DCXX->isInjectedClassName()) {
2770 // We have to be careful to do a similar dance to the one in
2771 // Sema::ActOnStartCXXMemberDeclarations
2772 CXXRecordDecl *const PrevDecl = nullptr;
2773 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002774 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002775 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002776 Name.getAsIdentifierInfo(), PrevDecl,
2777 DelayTypeCreation))
2778 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002779 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002780 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002781 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002782 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002783 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002784 Name.getAsIdentifierInfo(),
2785 cast_or_null<CXXRecordDecl>(PrevDecl)))
2786 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002787 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002788
Douglas Gregor3996e242010-02-15 22:01:00 +00002789 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002790 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002791 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002792 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002793 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002794
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002795 if (ClassTemplateDecl *FromDescribed =
2796 DCXX->getDescribedClassTemplate()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002797 ClassTemplateDecl *ToDescribed;
2798 if (Error Err = importInto(ToDescribed, FromDescribed))
2799 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002800 D2CXX->setDescribedClassTemplate(ToDescribed);
Balazs Keri0c23dc52018-08-13 13:08:37 +00002801 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
Gabor Marton5915777e2018-06-26 13:44:24 +00002802 // In a record describing a template the type should be an
2803 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2804 // previously set type to the correct value here (ToDescribed is not
2805 // available at record create).
2806 // FIXME: The previous type is cleared but not removed from
2807 // ASTContext's internal storage.
2808 CXXRecordDecl *Injected = nullptr;
2809 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2810 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2811 if (Record && Record->isInjectedClassName()) {
2812 Injected = Record;
2813 break;
2814 }
2815 }
2816 D2CXX->setTypeForDecl(nullptr);
2817 Importer.getToContext().getInjectedClassNameType(D2CXX,
2818 ToDescribed->getInjectedClassNameSpecialization());
2819 if (Injected) {
2820 Injected->setTypeForDecl(nullptr);
2821 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2822 }
2823 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002824 } else if (MemberSpecializationInfo *MemberInfo =
2825 DCXX->getMemberSpecializationInfo()) {
2826 TemplateSpecializationKind SK =
2827 MemberInfo->getTemplateSpecializationKind();
2828 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002829
2830 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2831 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2832 else
2833 return ToInstOrErr.takeError();
2834
2835 if (ExpectedSLoc POIOrErr =
2836 import(MemberInfo->getPointOfInstantiation()))
2837 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2838 *POIOrErr);
2839 else
2840 return POIOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002841 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002842
Douglas Gregor25791052010-02-12 00:09:27 +00002843 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002844 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002845 D->getTagKind(), DC, *BeginLocOrErr, Loc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002846 Name.getAsIdentifierInfo(), PrevDecl))
2847 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002848 D2->setLexicalDeclContext(LexicalDC);
2849 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002850 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002851
Balazs Keri3b30d652018-10-19 13:32:20 +00002852 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2853 D2->setQualifierInfo(*QualifierLocOrErr);
2854 else
2855 return QualifierLocOrErr.takeError();
2856
Douglas Gregordd6006f2012-07-17 21:16:27 +00002857 if (D->isAnonymousStructOrUnion())
2858 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002859 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002860
2861 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002862
Balazs Keri3b30d652018-10-19 13:32:20 +00002863 if (D->isCompleteDefinition())
2864 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2865 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002866
Douglas Gregor3996e242010-02-15 22:01:00 +00002867 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002868}
2869
Balazs Keri3b30d652018-10-19 13:32:20 +00002870ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002871 // Import the major distinguishing characteristics of this enumerator.
2872 DeclContext *DC, *LexicalDC;
2873 DeclarationName Name;
2874 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002875 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002876 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2877 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002878 if (ToD)
2879 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002880
Fangrui Song6907ce22018-07-30 19:24:48 +00002881 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002882 // in the same context.
2883 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002884 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002885 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002886 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002887 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002888 for (auto *FoundDecl : FoundDecls) {
2889 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002890 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002891
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002892 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002893 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002894 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002895 }
2896
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002897 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002898 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002899
Douglas Gregor98c10182010-02-12 22:17:39 +00002900 if (!ConflictingDecls.empty()) {
2901 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002902 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002903 ConflictingDecls.size());
2904 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002905 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002906 }
2907 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002908
Balazs Keri3b30d652018-10-19 13:32:20 +00002909 ExpectedType TypeOrErr = import(D->getType());
2910 if (!TypeOrErr)
2911 return TypeOrErr.takeError();
2912
2913 ExpectedExpr InitOrErr = import(D->getInitExpr());
2914 if (!InitOrErr)
2915 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002916
Gabor Marton26f72a92018-07-12 09:42:05 +00002917 EnumConstantDecl *ToEnumerator;
2918 if (GetImportedOrCreateDecl(
2919 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002920 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002921 return ToEnumerator;
2922
Douglas Gregordd483172010-02-22 17:42:47 +00002923 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002924 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002925 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002926 return ToEnumerator;
2927}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002928
Balazs Keri3b30d652018-10-19 13:32:20 +00002929Error ASTNodeImporter::ImportTemplateInformation(
2930 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002931 switch (FromFD->getTemplatedKind()) {
2932 case FunctionDecl::TK_NonTemplate:
2933 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002934 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002935
2936 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002937 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002938
2939 if (Expected<FunctionDecl *> InstFDOrErr =
2940 import(FromFD->getInstantiatedFromMemberFunction()))
2941 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2942 else
2943 return InstFDOrErr.takeError();
2944
2945 if (ExpectedSLoc POIOrErr = import(
2946 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2947 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2948 else
2949 return POIOrErr.takeError();
2950
2951 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002952 }
2953
2954 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002955 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002956 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002957 if (!FunctionAndArgsOrErr)
2958 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002959
2960 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002961 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002962
Gabor Marton5254e642018-06-27 13:32:50 +00002963 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002964 TemplateArgumentListInfo ToTAInfo;
2965 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002966 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002967 if (Error Err = ImportTemplateArgumentListInfo(
2968 *FromTAArgsAsWritten, ToTAInfo))
2969 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002970
Balazs Keri3b30d652018-10-19 13:32:20 +00002971 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2972 if (!POIOrErr)
2973 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002974
Gabor Marton5254e642018-06-27 13:32:50 +00002975 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002976 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002977 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2978 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2979 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002980 }
2981
2982 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2983 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2984 UnresolvedSet<8> TemplDecls;
2985 unsigned NumTemplates = FromInfo->getNumTemplates();
2986 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002987 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2988 import(FromInfo->getTemplate(I)))
2989 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002990 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002991 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002992 }
2993
2994 // Import TemplateArgumentListInfo.
2995 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002996 if (Error Err = ImportTemplateArgumentListInfo(
2997 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2998 llvm::makeArrayRef(
2999 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
3000 ToTAInfo))
3001 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003002
3003 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
3004 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00003005 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003006 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003007 }
Sam McCallfdc32072018-01-26 12:06:44 +00003008 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003009}
3010
Balazs Keri3b30d652018-10-19 13:32:20 +00003011Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00003012ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003013 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00003014 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00003015 if (!FunctionAndArgsOrErr)
3016 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00003017
Balazs Keri3b30d652018-10-19 13:32:20 +00003018 FunctionTemplateDecl *Template;
3019 TemplateArgsTy ToTemplArgs;
3020 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00003021 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003022 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00003023 return FoundSpec;
3024}
3025
Balazs Keri3b30d652018-10-19 13:32:20 +00003026ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00003027
Balazs Keri3b30d652018-10-19 13:32:20 +00003028 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00003029 auto RedeclIt = Redecls.begin();
3030 // Import the first part of the decl chain. I.e. import all previous
3031 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003032 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3033 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3034 if (!ToRedeclOrErr)
3035 return ToRedeclOrErr.takeError();
3036 }
Gabor Marton5254e642018-06-27 13:32:50 +00003037 assert(*RedeclIt == D);
3038
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003039 // Import the major distinguishing characteristics of this function.
3040 DeclContext *DC, *LexicalDC;
3041 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003042 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003043 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003044 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3045 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003046 if (ToD)
3047 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003048
Gabor Marton5254e642018-06-27 13:32:50 +00003049 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003050 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003051
Gabor Marton5254e642018-06-27 13:32:50 +00003052 // If this is a function template specialization, then try to find the same
3053 // existing specialization in the "to" context. The localUncachedLookup
3054 // below will not find any specialization, but would find the primary
3055 // template; thus, we have to skip normal lookup in case of specializations.
3056 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3057 if (D->getTemplatedKind() ==
3058 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003059 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3060 if (!FoundFunctionOrErr)
3061 return FoundFunctionOrErr.takeError();
3062 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
3063 if (D->doesThisDeclarationHaveABody() && FoundFunction->hasBody())
3064 return Importer.MapImported(D, FoundFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003065 FoundByLookup = FoundFunction;
3066 }
3067 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003068 // Try to find a function in our own ("to") context with the same name, same
3069 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003070 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003071 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003072 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003073 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003074 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003075 for (auto *FoundDecl : FoundDecls) {
3076 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003077 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003078
Balazs Keria35798d2018-07-17 09:52:41 +00003079 // If template was found, look at the templated function.
3080 if (FromFT) {
3081 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
3082 FoundDecl = Template->getTemplatedDecl();
3083 else
3084 continue;
3085 }
3086
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003087 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00003088 if (FoundFunction->hasExternalFormalLinkage() &&
3089 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00003090 if (IsStructuralMatch(D, FoundFunction)) {
3091 const FunctionDecl *Definition = nullptr;
3092 if (D->doesThisDeclarationHaveABody() &&
3093 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003094 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00003095 D, const_cast<FunctionDecl *>(Definition));
3096 }
3097 FoundByLookup = FoundFunction;
3098 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003099 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003100
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003101 // FIXME: Check for overloading more carefully, e.g., by boosting
3102 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003103
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003104 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003105 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003106 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003107
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003108 // Complain about inconsistent function types.
3109 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003110 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003111 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003112 diag::note_odr_value_here)
3113 << FoundFunction->getType();
3114 }
3115 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003116
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003117 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003118 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003119
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003120 if (!ConflictingDecls.empty()) {
3121 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003122 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003123 ConflictingDecls.size());
3124 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003125 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003126 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003127 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003128
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003129 DeclarationNameInfo NameInfo(Name, Loc);
3130 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003131 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3132 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003133
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003134 QualType FromTy = D->getType();
3135 bool usedDifferentExceptionSpec = false;
3136
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003137 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003138 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3139 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3140 // FunctionDecl that we are importing the FunctionProtoType for.
3141 // To avoid an infinite recursion when importing, create the FunctionDecl
3142 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003143 if (FromEPI.ExceptionSpec.SourceDecl ||
3144 FromEPI.ExceptionSpec.SourceTemplate ||
3145 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003146 FunctionProtoType::ExtProtoInfo DefaultEPI;
3147 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003148 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003149 usedDifferentExceptionSpec = true;
3150 }
3151 }
3152
Balazs Keri3b30d652018-10-19 13:32:20 +00003153 QualType T;
3154 TypeSourceInfo *TInfo;
3155 SourceLocation ToInnerLocStart, ToEndLoc;
3156 NestedNameSpecifierLoc ToQualifierLoc;
3157 if (auto Imp = importSeq(
3158 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3159 D->getQualifierLoc(), D->getEndLoc()))
3160 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3161 else
3162 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003163
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003164 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003165 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003166 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003167 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3168 Parameters.push_back(*ToPOrErr);
3169 else
3170 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003171 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003172
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003173 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003174 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003175 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003176 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003177 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3178 ToInnerLocStart, NameInfo, T, TInfo,
3179 FromConstructor->isExplicit(),
3180 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003181 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00003182 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003183 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3184 // Import first, then allocate memory and copy if there was no error.
3185 if (Error Err = ImportContainerChecked(
3186 FromConstructor->inits(), CtorInitializers))
3187 return std::move(Err);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003188 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00003189 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3190 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003191 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00003192 ToCtor->setCtorInitializers(Memory);
3193 ToCtor->setNumCtorInitializers(NumInitializers);
3194 }
Douglas Gregor00eace12010-02-21 18:29:16 +00003195 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003196 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003197 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3198 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3199 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003200 return ToFunction;
3201 } else if (CXXConversionDecl *FromConversion =
3202 dyn_cast<CXXConversionDecl>(D)) {
3203 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3204 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003205 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003206 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3207 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003208 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003209 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3210 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003211 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003212 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3213 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003214 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003215 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003216 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003217 D->getStorageClass(), D->isInlineSpecified(),
3218 D->hasWrittenPrototype(), D->isConstexpr()))
3219 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003220 }
John McCall3e11ebe2010-03-15 10:12:16 +00003221
Balazs Keri3b30d652018-10-19 13:32:20 +00003222 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003223 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003224 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003225 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3226 ToFunction->setTrivial(D->isTrivial());
3227 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003228 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003229
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003230 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003231 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003232 Param->setOwningFunction(ToFunction);
3233 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003234 }
David Blaikie9c70e042011-09-21 18:16:56 +00003235 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003236
Gabor Marton5254e642018-06-27 13:32:50 +00003237 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003238 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00003239 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003240 ToFunction->setPreviousDecl(Recent);
3241 }
3242
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003243 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3244 // params it refers to.
3245 if (TInfo) {
3246 if (auto ProtoLoc =
3247 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3248 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3249 ProtoLoc.setParam(I, Parameters[I]);
3250 }
3251 }
3252
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003253 if (usedDifferentExceptionSpec) {
3254 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003255 if (ExpectedType TyOrErr = import(D->getType()))
3256 ToFunction->setType(*TyOrErr);
3257 else
3258 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003259 }
3260
Balazs Keria35798d2018-07-17 09:52:41 +00003261 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003262 if (FromFT) {
3263 auto ToFTOrErr = import(FromFT);
3264 if (!ToFTOrErr)
3265 return ToFTOrErr.takeError();
3266 }
Balazs Keria35798d2018-07-17 09:52:41 +00003267
Gabor Marton5254e642018-06-27 13:32:50 +00003268 if (D->doesThisDeclarationHaveABody()) {
3269 if (Stmt *FromBody = D->getBody()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003270 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3271 ToFunction->setBody(*ToBodyOrErr);
3272 else
3273 return ToBodyOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00003274 }
3275 }
3276
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003277 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003278
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003279 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003280 if (Error Err = ImportTemplateInformation(D, ToFunction))
3281 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003282
Gabor Marton5254e642018-06-27 13:32:50 +00003283 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3284
3285 // TODO Can we generalize this approach to other AST nodes as well?
3286 if (D->getDeclContext()->containsDeclAndLoad(D))
3287 DC->addDeclInternal(ToFunction);
3288 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003289 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003290
Gabor Marton5254e642018-06-27 13:32:50 +00003291 // Friend declaration's lexical context is the befriending class, but the
3292 // semantic context is the enclosing scope of the befriending class.
3293 // We want the friend functions to be found in the semantic context by lookup.
3294 // FIXME should we handle this generically in VisitFriendDecl?
3295 // In Other cases when LexicalDC != DC we don't want it to be added,
3296 // e.g out-of-class definitions like void B::f() {} .
3297 if (LexicalDC != DC && IsFriend) {
3298 DC->makeDeclVisibleInContext(ToFunction);
3299 }
3300
Gabor Marton7a0841e2018-10-29 10:18:28 +00003301 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3302 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3303
Gabor Marton5254e642018-06-27 13:32:50 +00003304 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003305 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3306 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3307 if (!ToRedeclOrErr)
3308 return ToRedeclOrErr.takeError();
3309 }
Gabor Marton5254e642018-06-27 13:32:50 +00003310
Douglas Gregor43f54792010-02-17 02:12:47 +00003311 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003312}
3313
Balazs Keri3b30d652018-10-19 13:32:20 +00003314ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003315 return VisitFunctionDecl(D);
3316}
3317
Balazs Keri3b30d652018-10-19 13:32:20 +00003318ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003319 return VisitCXXMethodDecl(D);
3320}
3321
Balazs Keri3b30d652018-10-19 13:32:20 +00003322ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003323 return VisitCXXMethodDecl(D);
3324}
3325
Balazs Keri3b30d652018-10-19 13:32:20 +00003326ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003327 return VisitCXXMethodDecl(D);
3328}
3329
Balazs Keri3b30d652018-10-19 13:32:20 +00003330ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003331 // Import the major distinguishing characteristics of a variable.
3332 DeclContext *DC, *LexicalDC;
3333 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003334 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003335 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003336 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3337 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003338 if (ToD)
3339 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003340
Fangrui Song6907ce22018-07-30 19:24:48 +00003341 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003342 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003343 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003344 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003345 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003346 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003347 if (!Name &&
3348 ASTImporter::getFieldIndex(D) !=
3349 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003350 continue;
3351
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003352 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003353 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003354 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003355 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3356 // initializer of a FieldDecl might not had been instantiated in the
3357 // "To" context. However, the "From" context might instantiated that,
3358 // thus we have to merge that.
3359 if (Expr *FromInitializer = D->getInClassInitializer()) {
3360 // We don't have yet the initializer set.
3361 if (FoundField->hasInClassInitializer() &&
3362 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003363 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3364 FoundField->setInClassInitializer(*ToInitializerOrErr);
3365 else {
3366 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003367 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003368 // FIXME: warning message?
3369 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003370 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003371 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003372 }
3373 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003374 return FoundField;
3375 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003376
Balazs Keri3b30d652018-10-19 13:32:20 +00003377 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003378 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3379 << Name << D->getType() << FoundField->getType();
3380 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3381 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003382
3383 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003384 }
3385 }
3386
Balazs Keri3b30d652018-10-19 13:32:20 +00003387 QualType ToType;
3388 TypeSourceInfo *ToTInfo;
3389 Expr *ToBitWidth;
3390 SourceLocation ToInnerLocStart;
3391 Expr *ToInitializer;
3392 if (auto Imp = importSeq(
3393 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3394 D->getInnerLocStart(), D->getInClassInitializer()))
3395 std::tie(
3396 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3397 else
3398 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003399
Gabor Marton26f72a92018-07-12 09:42:05 +00003400 FieldDecl *ToField;
3401 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003402 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3403 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3404 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003405 return ToField;
3406
Douglas Gregordd483172010-02-22 17:42:47 +00003407 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003408 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003409 if (ToInitializer)
3410 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003411 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003412 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003413 return ToField;
3414}
3415
Balazs Keri3b30d652018-10-19 13:32:20 +00003416ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003417 // Import the major distinguishing characteristics of a variable.
3418 DeclContext *DC, *LexicalDC;
3419 DeclarationName Name;
3420 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003421 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003422 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3423 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003424 if (ToD)
3425 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003426
Fangrui Song6907ce22018-07-30 19:24:48 +00003427 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003428 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003429 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003430 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003431 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003432 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003433 if (!Name &&
3434 ASTImporter::getFieldIndex(D) !=
3435 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003436 continue;
3437
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003438 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003439 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003440 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003441 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003442 return FoundField;
3443 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003444
3445 // If there are more anonymous fields to check, continue.
3446 if (!Name && I < N-1)
3447 continue;
3448
Balazs Keri3b30d652018-10-19 13:32:20 +00003449 // FIXME: Why is this case not handled with calling HandleNameConflict?
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003450 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3451 << Name << D->getType() << FoundField->getType();
3452 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3453 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003454
3455 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003456 }
3457 }
3458
Francois Pichet783dd6e2010-11-21 06:08:52 +00003459 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003460 auto TypeOrErr = import(D->getType());
3461 if (!TypeOrErr)
3462 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003463
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003464 auto **NamedChain =
3465 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003466
3467 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003468 for (auto *PI : D->chain())
3469 if (Expected<NamedDecl *> ToD = import(PI))
3470 NamedChain[i++] = *ToD;
3471 else
3472 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003473
Gabor Marton26f72a92018-07-12 09:42:05 +00003474 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3475 IndirectFieldDecl *ToIndirectField;
3476 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003477 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003478 // FIXME here we leak `NamedChain` which is allocated before
3479 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003480
Balazs Keri3b30d652018-10-19 13:32:20 +00003481 for (const auto *Attr : D->attrs())
3482 ToIndirectField->addAttr(Importer.Import(Attr));
Aaron Ballman260995b2014-10-15 16:58:18 +00003483
Francois Pichet783dd6e2010-11-21 06:08:52 +00003484 ToIndirectField->setAccess(D->getAccess());
3485 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003486 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003487 return ToIndirectField;
3488}
3489
Balazs Keri3b30d652018-10-19 13:32:20 +00003490ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003491 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003492 DeclContext *DC, *LexicalDC;
3493 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3494 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003495
3496 // Determine whether we've already imported this decl.
3497 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3498 auto *RD = cast<CXXRecordDecl>(DC);
3499 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003500
3501 while (ImportedFriend) {
3502 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003503 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3504 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003505 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003506
3507 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3508 if (Importer.IsStructurallyEquivalent(
3509 D->getFriendType()->getType(),
3510 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003511 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003512 }
3513 ImportedFriend = ImportedFriend->getNextFriend();
3514 }
3515
3516 // Not found. Create it.
3517 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003518 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003519 NamedDecl *ToFriendD;
3520 if (Error Err = importInto(ToFriendD, FriendD))
3521 return std::move(Err);
3522
3523 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003524 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3525 ToFriendD->setObjectOfFriendDecl(false);
3526
3527 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003528 } else { // The friend is a type, not a decl.
3529 if (auto TSIOrErr = import(D->getFriendType()))
3530 ToFU = *TSIOrErr;
3531 else
3532 return TSIOrErr.takeError();
3533 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003534
3535 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003536 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003537 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003538 if (auto ListOrErr = ImportTemplateParameterList(FromTPLists[I]))
3539 ToTPLists[I] = *ListOrErr;
3540 else
3541 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003542 }
3543
Balazs Keri3b30d652018-10-19 13:32:20 +00003544 auto LocationOrErr = import(D->getLocation());
3545 if (!LocationOrErr)
3546 return LocationOrErr.takeError();
3547 auto FriendLocOrErr = import(D->getFriendLoc());
3548 if (!FriendLocOrErr)
3549 return FriendLocOrErr.takeError();
3550
Gabor Marton26f72a92018-07-12 09:42:05 +00003551 FriendDecl *FrD;
3552 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003553 *LocationOrErr, ToFU,
3554 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003555 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003556
3557 FrD->setAccess(D->getAccess());
3558 FrD->setLexicalDeclContext(LexicalDC);
3559 LexicalDC->addDeclInternal(FrD);
3560 return FrD;
3561}
3562
Balazs Keri3b30d652018-10-19 13:32:20 +00003563ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003564 // Import the major distinguishing characteristics of an ivar.
3565 DeclContext *DC, *LexicalDC;
3566 DeclarationName Name;
3567 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003568 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003569 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3570 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003571 if (ToD)
3572 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003573
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003574 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003575 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003576 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003577 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003578 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003579 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003580 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003581 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003582 return FoundIvar;
3583 }
3584
3585 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3586 << Name << D->getType() << FoundIvar->getType();
3587 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3588 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003589
3590 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003591 }
3592 }
3593
Balazs Keri3b30d652018-10-19 13:32:20 +00003594 QualType ToType;
3595 TypeSourceInfo *ToTypeSourceInfo;
3596 Expr *ToBitWidth;
3597 SourceLocation ToInnerLocStart;
3598 if (auto Imp = importSeq(
3599 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3600 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3601 else
3602 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003603
Gabor Marton26f72a92018-07-12 09:42:05 +00003604 ObjCIvarDecl *ToIvar;
3605 if (GetImportedOrCreateDecl(
3606 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003607 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3608 ToType, ToTypeSourceInfo,
3609 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003610 return ToIvar;
3611
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003612 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003613 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003614 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003615}
3616
Balazs Keri3b30d652018-10-19 13:32:20 +00003617ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003618
3619 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3620 auto RedeclIt = Redecls.begin();
3621 // Import the first part of the decl chain. I.e. import all previous
3622 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003623 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3624 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3625 if (!RedeclOrErr)
3626 return RedeclOrErr.takeError();
3627 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003628 assert(*RedeclIt == D);
3629
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003630 // Import the major distinguishing characteristics of a variable.
3631 DeclContext *DC, *LexicalDC;
3632 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003633 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003634 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003635 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3636 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003637 if (ToD)
3638 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003639
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003640 // Try to find a variable in our own ("to") context with the same name and
3641 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003642 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003643 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003644 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003645 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003646 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003647 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003648 for (auto *FoundDecl : FoundDecls) {
3649 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003650 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003651
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003652 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003653 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003654 if (FoundVar->hasExternalFormalLinkage() &&
3655 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003656 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003657 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003658
3659 // The VarDecl in the "From" context has a definition, but in the
3660 // "To" context we already have a definition.
3661 VarDecl *FoundDef = FoundVar->getDefinition();
3662 if (D->isThisDeclarationADefinition() && FoundDef)
3663 // FIXME Check for ODR error if the two definitions have
3664 // different initializers?
3665 return Importer.MapImported(D, FoundDef);
3666
3667 // The VarDecl in the "From" context has an initializer, but in the
3668 // "To" context we already have an initializer.
3669 const VarDecl *FoundDInit = nullptr;
3670 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3671 // FIXME Diagnose ODR error if the two initializers are different?
3672 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3673
3674 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003675 break;
3676 }
3677
Douglas Gregor56521c52010-02-12 17:23:39 +00003678 const ArrayType *FoundArray
3679 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3680 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003681 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003682 if (FoundArray && TArray) {
3683 if (isa<IncompleteArrayType>(FoundArray) &&
3684 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003685 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003686 if (auto TyOrErr = import(D->getType()))
3687 FoundVar->setType(*TyOrErr);
3688 else
3689 return TyOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003690
Gabor Martonac3a5d62018-09-17 12:04:52 +00003691 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003692 break;
3693 } else if (isa<IncompleteArrayType>(TArray) &&
3694 isa<ConstantArrayType>(FoundArray)) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003695 FoundByLookup = FoundVar;
Douglas Gregor56521c52010-02-12 17:23:39 +00003696 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003697 }
3698 }
3699
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003700 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003701 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003702 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3703 << FoundVar->getType();
3704 }
3705 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003706
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003707 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003708 }
3709
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003710 if (!ConflictingDecls.empty()) {
3711 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003712 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003713 ConflictingDecls.size());
3714 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003715 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003716 }
3717 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003718
Balazs Keri3b30d652018-10-19 13:32:20 +00003719 QualType ToType;
3720 TypeSourceInfo *ToTypeSourceInfo;
3721 SourceLocation ToInnerLocStart;
3722 NestedNameSpecifierLoc ToQualifierLoc;
3723 if (auto Imp = importSeq(
3724 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3725 D->getQualifierLoc()))
3726 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3727 else
3728 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003729
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003730 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003731 VarDecl *ToVar;
3732 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003733 ToInnerLocStart, Loc,
3734 Name.getAsIdentifierInfo(),
3735 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003736 D->getStorageClass()))
3737 return ToVar;
3738
Balazs Keri3b30d652018-10-19 13:32:20 +00003739 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003740 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003741 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003742
Gabor Martonac3a5d62018-09-17 12:04:52 +00003743 if (FoundByLookup) {
3744 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3745 ToVar->setPreviousDecl(Recent);
3746 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003747
Balazs Keri3b30d652018-10-19 13:32:20 +00003748 if (Error Err = ImportInitializer(D, ToVar))
3749 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003750
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003751 if (D->isConstexpr())
3752 ToVar->setConstexpr(true);
3753
Gabor Martonac3a5d62018-09-17 12:04:52 +00003754 if (D->getDeclContext()->containsDeclAndLoad(D))
3755 DC->addDeclInternal(ToVar);
3756 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3757 LexicalDC->addDeclInternal(ToVar);
3758
3759 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003760 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3761 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3762 if (!RedeclOrErr)
3763 return RedeclOrErr.takeError();
3764 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003765
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003766 return ToVar;
3767}
3768
Balazs Keri3b30d652018-10-19 13:32:20 +00003769ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003770 // Parameters are created in the translation unit's context, then moved
3771 // into the function declaration's context afterward.
3772 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003773
Balazs Keri3b30d652018-10-19 13:32:20 +00003774 DeclarationName ToDeclName;
3775 SourceLocation ToLocation;
3776 QualType ToType;
3777 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3778 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3779 else
3780 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003781
Douglas Gregor8b228d72010-02-17 21:22:52 +00003782 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003783 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003784 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3785 ToLocation, ToDeclName.getAsIdentifierInfo(),
3786 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003787 return ToParm;
3788 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003789}
3790
Balazs Keri3b30d652018-10-19 13:32:20 +00003791ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003792 // Parameters are created in the translation unit's context, then moved
3793 // into the function declaration's context afterward.
3794 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003795
Balazs Keri3b30d652018-10-19 13:32:20 +00003796 DeclarationName ToDeclName;
3797 SourceLocation ToLocation, ToInnerLocStart;
3798 QualType ToType;
3799 TypeSourceInfo *ToTypeSourceInfo;
3800 if (auto Imp = importSeq(
3801 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3802 D->getTypeSourceInfo()))
3803 std::tie(
3804 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3805 ToTypeSourceInfo) = *Imp;
3806 else
3807 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003808
Gabor Marton26f72a92018-07-12 09:42:05 +00003809 ParmVarDecl *ToParm;
3810 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003811 ToInnerLocStart, ToLocation,
3812 ToDeclName.getAsIdentifierInfo(), ToType,
3813 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003814 /*DefaultArg*/ nullptr))
3815 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003816
3817 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003818 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003819 ToParm->setKNRPromoted(D->isKNRPromoted());
3820
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003821 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003822 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3823 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3824 else
3825 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003826 } else if (D->hasUnparsedDefaultArg()) {
3827 ToParm->setUnparsedDefaultArg();
3828 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003829 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3830 ToParm->setDefaultArg(*ToDefArgOrErr);
3831 else
3832 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003833 }
Sean Callanan59721b32015-04-28 18:41:46 +00003834
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003835 if (D->isObjCMethodParameter()) {
3836 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3837 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3838 } else {
3839 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3840 D->getFunctionScopeIndex());
3841 }
3842
Gabor Marton26f72a92018-07-12 09:42:05 +00003843 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003844}
3845
Balazs Keri3b30d652018-10-19 13:32:20 +00003846ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003847 // Import the major distinguishing characteristics of a method.
3848 DeclContext *DC, *LexicalDC;
3849 DeclarationName Name;
3850 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003851 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003852 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3853 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003854 if (ToD)
3855 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003856
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003857 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003858 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003859 for (auto *FoundDecl : FoundDecls) {
3860 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003861 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3862 continue;
3863
3864 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003865 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3866 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003867 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003868 << D->isInstanceMethod() << Name << D->getReturnType()
3869 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003870 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003871 diag::note_odr_objc_method_here)
3872 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003873
3874 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003875 }
3876
3877 // Check the number of parameters.
3878 if (D->param_size() != FoundMethod->param_size()) {
3879 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3880 << D->isInstanceMethod() << Name
3881 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003882 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003883 diag::note_odr_objc_method_here)
3884 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003885
3886 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003887 }
3888
3889 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003890 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003891 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3892 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003893 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003894 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003895 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003896 diag::err_odr_objc_method_param_type_inconsistent)
3897 << D->isInstanceMethod() << Name
3898 << (*P)->getType() << (*FoundP)->getType();
3899 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3900 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003901
3902 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003903 }
3904 }
3905
3906 // Check variadic/non-variadic.
3907 // Check the number of parameters.
3908 if (D->isVariadic() != FoundMethod->isVariadic()) {
3909 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3910 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003911 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003912 diag::note_odr_objc_method_here)
3913 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003914
3915 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003916 }
3917
3918 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003919 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003920 }
3921 }
3922
Balazs Keri3b30d652018-10-19 13:32:20 +00003923 SourceLocation ToEndLoc;
3924 QualType ToReturnType;
3925 TypeSourceInfo *ToReturnTypeSourceInfo;
3926 if (auto Imp = importSeq(
3927 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3928 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3929 else
3930 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003931
Gabor Marton26f72a92018-07-12 09:42:05 +00003932 ObjCMethodDecl *ToMethod;
3933 if (GetImportedOrCreateDecl(
3934 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003935 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3936 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003937 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3938 D->getImplementationControl(), D->hasRelatedResultType()))
3939 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003940
3941 // FIXME: When we decide to merge method definitions, we'll need to
3942 // deal with implicit parameters.
3943
3944 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003945 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003946 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003947 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3948 ToParams.push_back(*ToPOrErr);
3949 else
3950 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003951 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003952
Douglas Gregor43f54792010-02-17 02:12:47 +00003953 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003954 for (auto *ToParam : ToParams) {
3955 ToParam->setOwningFunction(ToMethod);
3956 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003957 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003958
Balazs Keri3b30d652018-10-19 13:32:20 +00003959 SmallVector<SourceLocation, 12> FromSelLocs;
3960 D->getSelectorLocs(FromSelLocs);
3961 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3962 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3963 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003964
Balazs Keri3b30d652018-10-19 13:32:20 +00003965 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003966
3967 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003968 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003969 return ToMethod;
3970}
3971
Balazs Keri3b30d652018-10-19 13:32:20 +00003972ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003973 // Import the major distinguishing characteristics of a category.
3974 DeclContext *DC, *LexicalDC;
3975 DeclarationName Name;
3976 SourceLocation Loc;
3977 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003978 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3979 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003980 if (ToD)
3981 return ToD;
3982
Balazs Keri3b30d652018-10-19 13:32:20 +00003983 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3984 TypeSourceInfo *ToTypeSourceInfo;
3985 if (auto Imp = importSeq(
3986 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3987 D->getTypeSourceInfo()))
3988 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3989 else
3990 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003991
Gabor Marton26f72a92018-07-12 09:42:05 +00003992 ObjCTypeParamDecl *Result;
3993 if (GetImportedOrCreateDecl(
3994 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003995 ToVarianceLoc, D->getIndex(),
3996 ToLocation, Name.getAsIdentifierInfo(),
3997 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003998 return Result;
3999
Douglas Gregor85f3f952015-07-07 03:57:15 +00004000 Result->setLexicalDeclContext(LexicalDC);
4001 return Result;
4002}
4003
Balazs Keri3b30d652018-10-19 13:32:20 +00004004ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00004005 // Import the major distinguishing characteristics of a category.
4006 DeclContext *DC, *LexicalDC;
4007 DeclarationName Name;
4008 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004009 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004010 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4011 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004012 if (ToD)
4013 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004014
Balazs Keri3b30d652018-10-19 13:32:20 +00004015 ObjCInterfaceDecl *ToInterface;
4016 if (Error Err = importInto(ToInterface, D->getClassInterface()))
4017 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004018
Douglas Gregor84c51c32010-02-18 01:47:50 +00004019 // Determine if we've already encountered this category.
4020 ObjCCategoryDecl *MergeWithCategory
4021 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
4022 ObjCCategoryDecl *ToCategory = MergeWithCategory;
4023 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004024 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
4025 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4026 if (auto Imp = importSeq(
4027 D->getAtStartLoc(), D->getCategoryNameLoc(),
4028 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4029 std::tie(
4030 ToAtStartLoc, ToCategoryNameLoc,
4031 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4032 else
4033 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004034
4035 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004036 ToAtStartLoc, Loc,
4037 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004038 Name.getAsIdentifierInfo(), ToInterface,
4039 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004040 ToIvarLBraceLoc,
4041 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004042 return ToCategory;
4043
Douglas Gregor84c51c32010-02-18 01:47:50 +00004044 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004045 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004046 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004047 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004048 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4049 ToCategory->setTypeParamList(*PListOrErr);
4050 else
4051 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004052
Douglas Gregor84c51c32010-02-18 01:47:50 +00004053 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004054 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4055 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004056 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4057 = D->protocol_loc_begin();
4058 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4059 FromProtoEnd = D->protocol_end();
4060 FromProto != FromProtoEnd;
4061 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004062 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4063 Protocols.push_back(*ToProtoOrErr);
4064 else
4065 return ToProtoOrErr.takeError();
4066
4067 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4068 ProtocolLocs.push_back(*ToProtoLocOrErr);
4069 else
4070 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004071 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004072
Douglas Gregor84c51c32010-02-18 01:47:50 +00004073 // FIXME: If we're merging, make sure that the protocol list is the same.
4074 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4075 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004076
Douglas Gregor84c51c32010-02-18 01:47:50 +00004077 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004078 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004079 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004080
Douglas Gregor84c51c32010-02-18 01:47:50 +00004081 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004082 if (Error Err = ImportDeclContext(D))
4083 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004084
Douglas Gregor84c51c32010-02-18 01:47:50 +00004085 // If we have an implementation, import it as well.
4086 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004087 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4088 import(D->getImplementation()))
4089 ToCategory->setImplementation(*ToImplOrErr);
4090 else
4091 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004092 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004093
Douglas Gregor84c51c32010-02-18 01:47:50 +00004094 return ToCategory;
4095}
4096
Balazs Keri3b30d652018-10-19 13:32:20 +00004097Error ASTNodeImporter::ImportDefinition(
4098 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004099 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004100 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004101 if (Error Err = ImportDeclContext(From))
4102 return Err;
4103 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004104 }
4105
4106 // Start the protocol definition
4107 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004108
Douglas Gregor2aa53772012-01-24 17:42:07 +00004109 // Import protocols
4110 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4111 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004112 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4113 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004114 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4115 FromProtoEnd = From->protocol_end();
4116 FromProto != FromProtoEnd;
4117 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004118 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4119 Protocols.push_back(*ToProtoOrErr);
4120 else
4121 return ToProtoOrErr.takeError();
4122
4123 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4124 ProtocolLocs.push_back(*ToProtoLocOrErr);
4125 else
4126 return ToProtoLocOrErr.takeError();
4127
Douglas Gregor2aa53772012-01-24 17:42:07 +00004128 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004129
Douglas Gregor2aa53772012-01-24 17:42:07 +00004130 // FIXME: If we're merging, make sure that the protocol list is the same.
4131 To->setProtocolList(Protocols.data(), Protocols.size(),
4132 ProtocolLocs.data(), Importer.getToContext());
4133
Douglas Gregor2e15c842012-02-01 21:00:38 +00004134 if (shouldForceImportDeclContext(Kind)) {
4135 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004136 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4137 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004138 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004139 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004140}
4141
Balazs Keri3b30d652018-10-19 13:32:20 +00004142ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004143 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004144 // from, but this particular declaration is not that definition, import the
4145 // definition and map to that.
4146 ObjCProtocolDecl *Definition = D->getDefinition();
4147 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004148 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4149 return Importer.MapImported(D, *ImportedDefOrErr);
4150 else
4151 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004152 }
4153
Douglas Gregor84c51c32010-02-18 01:47:50 +00004154 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004155 DeclContext *DC, *LexicalDC;
4156 DeclarationName Name;
4157 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004158 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004159 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4160 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004161 if (ToD)
4162 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004163
Craig Topper36250ad2014-05-12 05:36:57 +00004164 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004165 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004166 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004167 for (auto *FoundDecl : FoundDecls) {
4168 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004169 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004170
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004171 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004172 break;
4173 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004174
Douglas Gregor98d156a2010-02-17 16:12:00 +00004175 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004176 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004177 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4178 if (!ToAtBeginLocOrErr)
4179 return ToAtBeginLocOrErr.takeError();
4180
Gabor Marton26f72a92018-07-12 09:42:05 +00004181 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4182 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004183 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004184 /*PrevDecl=*/nullptr))
4185 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004186 ToProto->setLexicalDeclContext(LexicalDC);
4187 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004188 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004189
4190 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004191
Balazs Keri3b30d652018-10-19 13:32:20 +00004192 if (D->isThisDeclarationADefinition())
4193 if (Error Err = ImportDefinition(D, ToProto))
4194 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004195
Douglas Gregor98d156a2010-02-17 16:12:00 +00004196 return ToProto;
4197}
4198
Balazs Keri3b30d652018-10-19 13:32:20 +00004199ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4200 DeclContext *DC, *LexicalDC;
4201 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4202 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004203
Balazs Keri3b30d652018-10-19 13:32:20 +00004204 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4205 if (!ExternLocOrErr)
4206 return ExternLocOrErr.takeError();
4207
4208 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4209 if (!LangLocOrErr)
4210 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004211
4212 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004213
4214 LinkageSpecDecl *ToLinkageSpec;
4215 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004216 *ExternLocOrErr, *LangLocOrErr,
4217 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004218 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004219
4220 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004221 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4222 if (!RBraceLocOrErr)
4223 return RBraceLocOrErr.takeError();
4224 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004225 }
4226
4227 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4228 LexicalDC->addDeclInternal(ToLinkageSpec);
4229
Sean Callanan0aae0412014-12-10 00:00:37 +00004230 return ToLinkageSpec;
4231}
4232
Balazs Keri3b30d652018-10-19 13:32:20 +00004233ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004234 DeclContext *DC, *LexicalDC;
4235 DeclarationName Name;
4236 SourceLocation Loc;
4237 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004238 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4239 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004240 if (ToD)
4241 return ToD;
4242
Balazs Keri3b30d652018-10-19 13:32:20 +00004243 SourceLocation ToLoc, ToUsingLoc;
4244 NestedNameSpecifierLoc ToQualifierLoc;
4245 if (auto Imp = importSeq(
4246 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4247 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4248 else
4249 return Imp.takeError();
4250
4251 DeclarationNameInfo NameInfo(Name, ToLoc);
4252 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4253 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004254
Gabor Marton26f72a92018-07-12 09:42:05 +00004255 UsingDecl *ToUsing;
4256 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004257 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004258 D->hasTypename()))
4259 return ToUsing;
4260
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004261 ToUsing->setLexicalDeclContext(LexicalDC);
4262 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004263
4264 if (NamedDecl *FromPattern =
4265 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004266 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4267 Importer.getToContext().setInstantiatedFromUsingDecl(
4268 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004269 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004270 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004271 }
4272
Balazs Keri3b30d652018-10-19 13:32:20 +00004273 for (UsingShadowDecl *FromShadow : D->shadows()) {
4274 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4275 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004276 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004277 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004278 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004279 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004280 }
4281 return ToUsing;
4282}
4283
Balazs Keri3b30d652018-10-19 13:32:20 +00004284ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004285 DeclContext *DC, *LexicalDC;
4286 DeclarationName Name;
4287 SourceLocation Loc;
4288 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004289 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4290 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004291 if (ToD)
4292 return ToD;
4293
Balazs Keri3b30d652018-10-19 13:32:20 +00004294 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4295 if (!ToUsingOrErr)
4296 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004297
Balazs Keri3b30d652018-10-19 13:32:20 +00004298 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4299 if (!ToTargetOrErr)
4300 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004301
Gabor Marton26f72a92018-07-12 09:42:05 +00004302 UsingShadowDecl *ToShadow;
4303 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004304 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004305 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004306
4307 ToShadow->setLexicalDeclContext(LexicalDC);
4308 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004309
4310 if (UsingShadowDecl *FromPattern =
4311 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004312 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4313 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4314 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004315 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004316 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004317 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004318 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004319 }
4320
4321 LexicalDC->addDeclInternal(ToShadow);
4322
4323 return ToShadow;
4324}
4325
Balazs Keri3b30d652018-10-19 13:32:20 +00004326ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004327 DeclContext *DC, *LexicalDC;
4328 DeclarationName Name;
4329 SourceLocation Loc;
4330 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004331 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4332 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004333 if (ToD)
4334 return ToD;
4335
Balazs Keri3b30d652018-10-19 13:32:20 +00004336 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4337 if (!ToComAncestorOrErr)
4338 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004339
Balazs Keri3b30d652018-10-19 13:32:20 +00004340 NamespaceDecl *ToNominatedNamespace;
4341 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4342 NestedNameSpecifierLoc ToQualifierLoc;
4343 if (auto Imp = importSeq(
4344 D->getNominatedNamespace(), D->getUsingLoc(),
4345 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4346 D->getIdentLocation()))
4347 std::tie(
4348 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4349 ToQualifierLoc, ToIdentLocation) = *Imp;
4350 else
4351 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004352
Gabor Marton26f72a92018-07-12 09:42:05 +00004353 UsingDirectiveDecl *ToUsingDir;
4354 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004355 ToUsingLoc,
4356 ToNamespaceKeyLocation,
4357 ToQualifierLoc,
4358 ToIdentLocation,
4359 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004360 return ToUsingDir;
4361
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004362 ToUsingDir->setLexicalDeclContext(LexicalDC);
4363 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004364
4365 return ToUsingDir;
4366}
4367
Balazs Keri3b30d652018-10-19 13:32:20 +00004368ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004369 UnresolvedUsingValueDecl *D) {
4370 DeclContext *DC, *LexicalDC;
4371 DeclarationName Name;
4372 SourceLocation Loc;
4373 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004374 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4375 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004376 if (ToD)
4377 return ToD;
4378
Balazs Keri3b30d652018-10-19 13:32:20 +00004379 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4380 NestedNameSpecifierLoc ToQualifierLoc;
4381 if (auto Imp = importSeq(
4382 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4383 D->getEllipsisLoc()))
4384 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4385 else
4386 return Imp.takeError();
4387
4388 DeclarationNameInfo NameInfo(Name, ToLoc);
4389 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4390 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004391
Gabor Marton26f72a92018-07-12 09:42:05 +00004392 UnresolvedUsingValueDecl *ToUsingValue;
4393 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004394 ToUsingLoc, ToQualifierLoc, NameInfo,
4395 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004396 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004397
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004398 ToUsingValue->setAccess(D->getAccess());
4399 ToUsingValue->setLexicalDeclContext(LexicalDC);
4400 LexicalDC->addDeclInternal(ToUsingValue);
4401
4402 return ToUsingValue;
4403}
4404
Balazs Keri3b30d652018-10-19 13:32:20 +00004405ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004406 UnresolvedUsingTypenameDecl *D) {
4407 DeclContext *DC, *LexicalDC;
4408 DeclarationName Name;
4409 SourceLocation Loc;
4410 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004411 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4412 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004413 if (ToD)
4414 return ToD;
4415
Balazs Keri3b30d652018-10-19 13:32:20 +00004416 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4417 NestedNameSpecifierLoc ToQualifierLoc;
4418 if (auto Imp = importSeq(
4419 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4420 D->getEllipsisLoc()))
4421 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4422 else
4423 return Imp.takeError();
4424
Gabor Marton26f72a92018-07-12 09:42:05 +00004425 UnresolvedUsingTypenameDecl *ToUsing;
4426 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004427 ToUsingLoc, ToTypenameLoc,
4428 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004429 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004430
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004431 ToUsing->setAccess(D->getAccess());
4432 ToUsing->setLexicalDeclContext(LexicalDC);
4433 LexicalDC->addDeclInternal(ToUsing);
4434
4435 return ToUsing;
4436}
4437
Balazs Keri3b30d652018-10-19 13:32:20 +00004438
4439Error ASTNodeImporter::ImportDefinition(
4440 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004441 if (To->getDefinition()) {
4442 // Check consistency of superclass.
4443 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4444 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004445 if (auto FromSuperOrErr = import(FromSuper))
4446 FromSuper = *FromSuperOrErr;
4447 else
4448 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004449 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004450
4451 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004452 if ((bool)FromSuper != (bool)ToSuper ||
4453 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004454 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004455 diag::err_odr_objc_superclass_inconsistent)
4456 << To->getDeclName();
4457 if (ToSuper)
4458 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4459 << To->getSuperClass()->getDeclName();
4460 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004461 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004462 diag::note_odr_objc_missing_superclass);
4463 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004464 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004465 diag::note_odr_objc_superclass)
4466 << From->getSuperClass()->getDeclName();
4467 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004468 Importer.FromDiag(From->getLocation(),
4469 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004470 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004471
Douglas Gregor2e15c842012-02-01 21:00:38 +00004472 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004473 if (Error Err = ImportDeclContext(From))
4474 return Err;
4475 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004476 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004477
Douglas Gregor2aa53772012-01-24 17:42:07 +00004478 // Start the definition.
4479 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004480
Douglas Gregor2aa53772012-01-24 17:42:07 +00004481 // If this class has a superclass, import it.
4482 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004483 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4484 To->setSuperClass(*SuperTInfoOrErr);
4485 else
4486 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004487 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004488
Douglas Gregor2aa53772012-01-24 17:42:07 +00004489 // Import protocols
4490 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4491 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004492 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4493 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004494
Douglas Gregor2aa53772012-01-24 17:42:07 +00004495 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4496 FromProtoEnd = From->protocol_end();
4497 FromProto != FromProtoEnd;
4498 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004499 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4500 Protocols.push_back(*ToProtoOrErr);
4501 else
4502 return ToProtoOrErr.takeError();
4503
4504 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4505 ProtocolLocs.push_back(*ToProtoLocOrErr);
4506 else
4507 return ToProtoLocOrErr.takeError();
4508
Douglas Gregor2aa53772012-01-24 17:42:07 +00004509 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004510
Douglas Gregor2aa53772012-01-24 17:42:07 +00004511 // FIXME: If we're merging, make sure that the protocol list is the same.
4512 To->setProtocolList(Protocols.data(), Protocols.size(),
4513 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004514
Douglas Gregor2aa53772012-01-24 17:42:07 +00004515 // Import categories. When the categories themselves are imported, they'll
4516 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004517 for (auto *Cat : From->known_categories()) {
4518 auto ToCatOrErr = import(Cat);
4519 if (!ToCatOrErr)
4520 return ToCatOrErr.takeError();
4521 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004522
Douglas Gregor2aa53772012-01-24 17:42:07 +00004523 // If we have an @implementation, import it as well.
4524 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004525 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4526 import(From->getImplementation()))
4527 To->setImplementation(*ToImplOrErr);
4528 else
4529 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004530 }
4531
Douglas Gregor2e15c842012-02-01 21:00:38 +00004532 if (shouldForceImportDeclContext(Kind)) {
4533 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004534 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4535 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004536 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004537 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004538}
4539
Balazs Keri3b30d652018-10-19 13:32:20 +00004540Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004541ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4542 if (!list)
4543 return nullptr;
4544
4545 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004546 for (auto *fromTypeParam : *list) {
4547 if (auto toTypeParamOrErr = import(fromTypeParam))
4548 toTypeParams.push_back(*toTypeParamOrErr);
4549 else
4550 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004551 }
4552
Balazs Keri3b30d652018-10-19 13:32:20 +00004553 auto LAngleLocOrErr = import(list->getLAngleLoc());
4554 if (!LAngleLocOrErr)
4555 return LAngleLocOrErr.takeError();
4556
4557 auto RAngleLocOrErr = import(list->getRAngleLoc());
4558 if (!RAngleLocOrErr)
4559 return RAngleLocOrErr.takeError();
4560
Douglas Gregor85f3f952015-07-07 03:57:15 +00004561 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004562 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004563 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004564 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004565}
4566
Balazs Keri3b30d652018-10-19 13:32:20 +00004567ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004568 // If this class has a definition in the translation unit we're coming from,
4569 // but this particular declaration is not that definition, import the
4570 // definition and map to that.
4571 ObjCInterfaceDecl *Definition = D->getDefinition();
4572 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004573 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4574 return Importer.MapImported(D, *ImportedDefOrErr);
4575 else
4576 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004577 }
4578
Douglas Gregor45635322010-02-16 01:20:57 +00004579 // Import the major distinguishing characteristics of an @interface.
4580 DeclContext *DC, *LexicalDC;
4581 DeclarationName Name;
4582 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004583 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004584 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4585 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004586 if (ToD)
4587 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004588
Douglas Gregor2aa53772012-01-24 17:42:07 +00004589 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004590 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004591 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004592 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004593 for (auto *FoundDecl : FoundDecls) {
4594 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004595 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004596
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004597 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004598 break;
4599 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004600
Douglas Gregor2aa53772012-01-24 17:42:07 +00004601 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004602 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004603 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004604 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4605 if (!AtBeginLocOrErr)
4606 return AtBeginLocOrErr.takeError();
4607
Gabor Marton26f72a92018-07-12 09:42:05 +00004608 if (GetImportedOrCreateDecl(
4609 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004610 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004611 /*TypeParamList=*/nullptr,
4612 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4613 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004614 ToIface->setLexicalDeclContext(LexicalDC);
4615 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004616 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004617 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004618 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004619 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004620 if (auto ToPListOrErr =
4621 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4622 ToIface->setTypeParamList(*ToPListOrErr);
4623 else
4624 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004625
Balazs Keri3b30d652018-10-19 13:32:20 +00004626 if (D->isThisDeclarationADefinition())
4627 if (Error Err = ImportDefinition(D, ToIface))
4628 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004629
Douglas Gregor98d156a2010-02-17 16:12:00 +00004630 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004631}
4632
Balazs Keri3b30d652018-10-19 13:32:20 +00004633ExpectedDecl
4634ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4635 ObjCCategoryDecl *Category;
4636 if (Error Err = importInto(Category, D->getCategoryDecl()))
4637 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004638
Douglas Gregor4da9d682010-12-07 15:32:12 +00004639 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4640 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004641 DeclContext *DC, *LexicalDC;
4642 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4643 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004644
Balazs Keri3b30d652018-10-19 13:32:20 +00004645 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4646 if (auto Imp = importSeq(
4647 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4648 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4649 else
4650 return Imp.takeError();
4651
Gabor Marton26f72a92018-07-12 09:42:05 +00004652 if (GetImportedOrCreateDecl(
4653 ToImpl, D, Importer.getToContext(), DC,
4654 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004655 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004656 return ToImpl;
4657
Balazs Keri3b30d652018-10-19 13:32:20 +00004658 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004659 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004660 Category->setImplementation(ToImpl);
4661 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004662
Gabor Marton26f72a92018-07-12 09:42:05 +00004663 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004664 if (Error Err = ImportDeclContext(D))
4665 return std::move(Err);
4666
Douglas Gregor4da9d682010-12-07 15:32:12 +00004667 return ToImpl;
4668}
4669
Balazs Keri3b30d652018-10-19 13:32:20 +00004670ExpectedDecl
4671ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004672 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004673 ObjCInterfaceDecl *Iface;
4674 if (Error Err = importInto(Iface, D->getClassInterface()))
4675 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004676
4677 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004678 ObjCInterfaceDecl *Super;
4679 if (Error Err = importInto(Super, D->getSuperClass()))
4680 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004681
4682 ObjCImplementationDecl *Impl = Iface->getImplementation();
4683 if (!Impl) {
4684 // We haven't imported an implementation yet. Create a new @implementation
4685 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004686 DeclContext *DC, *LexicalDC;
4687 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4688 return std::move(Err);
4689
4690 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4691 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4692 if (auto Imp = importSeq(
4693 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4694 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4695 std::tie(
4696 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4697 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4698 else
4699 return Imp.takeError();
4700
Gabor Marton26f72a92018-07-12 09:42:05 +00004701 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004702 DC, Iface, Super,
4703 ToLocation,
4704 ToAtStartLoc,
4705 ToSuperClassLoc,
4706 ToIvarLBraceLoc,
4707 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004708 return Impl;
4709
Balazs Keri3b30d652018-10-19 13:32:20 +00004710 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004711
Douglas Gregorda8025c2010-12-07 01:26:03 +00004712 // Associate the implementation with the class it implements.
4713 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004714 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004715 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004716 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004717
4718 // Verify that the existing @implementation has the same superclass.
4719 if ((Super && !Impl->getSuperClass()) ||
4720 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004721 (Super && Impl->getSuperClass() &&
4722 !declaresSameEntity(Super->getCanonicalDecl(),
4723 Impl->getSuperClass()))) {
4724 Importer.ToDiag(Impl->getLocation(),
4725 diag::err_odr_objc_superclass_inconsistent)
4726 << Iface->getDeclName();
4727 // FIXME: It would be nice to have the location of the superclass
4728 // below.
4729 if (Impl->getSuperClass())
4730 Importer.ToDiag(Impl->getLocation(),
4731 diag::note_odr_objc_superclass)
4732 << Impl->getSuperClass()->getDeclName();
4733 else
4734 Importer.ToDiag(Impl->getLocation(),
4735 diag::note_odr_objc_missing_superclass);
4736 if (D->getSuperClass())
4737 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004738 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004739 << D->getSuperClass()->getDeclName();
4740 else
4741 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004742 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004743
4744 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004745 }
4746 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004747
Douglas Gregorda8025c2010-12-07 01:26:03 +00004748 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004749 if (Error Err = ImportDeclContext(D))
4750 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004751
4752 return Impl;
4753}
4754
Balazs Keri3b30d652018-10-19 13:32:20 +00004755ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004756 // Import the major distinguishing characteristics of an @property.
4757 DeclContext *DC, *LexicalDC;
4758 DeclarationName Name;
4759 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004760 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004761 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4762 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004763 if (ToD)
4764 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004765
4766 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004767 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004768 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004769 for (auto *FoundDecl : FoundDecls) {
4770 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004771 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004772 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004773 FoundProp->getType())) {
4774 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4775 << Name << D->getType() << FoundProp->getType();
4776 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4777 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004778
4779 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004780 }
4781
4782 // FIXME: Check property attributes, getters, setters, etc.?
4783
4784 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004785 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004786 return FoundProp;
4787 }
4788 }
4789
Balazs Keri3b30d652018-10-19 13:32:20 +00004790 QualType ToType;
4791 TypeSourceInfo *ToTypeSourceInfo;
4792 SourceLocation ToAtLoc, ToLParenLoc;
4793 if (auto Imp = importSeq(
4794 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4795 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4796 else
4797 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004798
4799 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004800 ObjCPropertyDecl *ToProperty;
4801 if (GetImportedOrCreateDecl(
4802 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004803 Name.getAsIdentifierInfo(), ToAtLoc,
4804 ToLParenLoc, ToType,
4805 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004806 return ToProperty;
4807
Balazs Keri3b30d652018-10-19 13:32:20 +00004808 Selector ToGetterName, ToSetterName;
4809 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4810 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4811 ObjCIvarDecl *ToPropertyIvarDecl;
4812 if (auto Imp = importSeq(
4813 D->getGetterName(), D->getSetterName(),
4814 D->getGetterNameLoc(), D->getSetterNameLoc(),
4815 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4816 D->getPropertyIvarDecl()))
4817 std::tie(
4818 ToGetterName, ToSetterName,
4819 ToGetterNameLoc, ToSetterNameLoc,
4820 ToGetterMethodDecl, ToSetterMethodDecl,
4821 ToPropertyIvarDecl) = *Imp;
4822 else
4823 return Imp.takeError();
4824
Douglas Gregora11c4582010-02-17 18:02:10 +00004825 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004826 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004827
4828 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004829 ToProperty->setPropertyAttributesAsWritten(
4830 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004831 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4832 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4833 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4834 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4835 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004836 return ToProperty;
4837}
4838
Balazs Keri3b30d652018-10-19 13:32:20 +00004839ExpectedDecl
4840ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4841 ObjCPropertyDecl *Property;
4842 if (Error Err = importInto(Property, D->getPropertyDecl()))
4843 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004844
Balazs Keri3b30d652018-10-19 13:32:20 +00004845 DeclContext *DC, *LexicalDC;
4846 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4847 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004848
Balazs Keri3b30d652018-10-19 13:32:20 +00004849 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004850
4851 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004852 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004853 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4854 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004855
4856 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004857 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4858 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004859 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004860 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4861 if (auto Imp = importSeq(
4862 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4863 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4864 else
4865 return Imp.takeError();
4866
Gabor Marton26f72a92018-07-12 09:42:05 +00004867 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004868 ToBeginLoc,
4869 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004870 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004871 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004872 return ToImpl;
4873
Douglas Gregor14a49e22010-12-07 18:32:03 +00004874 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004875 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004876 } else {
4877 // Check that we have the same kind of property implementation (@synthesize
4878 // vs. @dynamic).
4879 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004880 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004881 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004882 << Property->getDeclName()
4883 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004884 == ObjCPropertyImplDecl::Dynamic);
4885 Importer.FromDiag(D->getLocation(),
4886 diag::note_odr_objc_property_impl_kind)
4887 << D->getPropertyDecl()->getDeclName()
4888 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004889
4890 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004891 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004892
4893 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004894 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4895 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004896 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004897 diag::err_odr_objc_synthesize_ivar_inconsistent)
4898 << Property->getDeclName()
4899 << ToImpl->getPropertyIvarDecl()->getDeclName()
4900 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004901 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004902 diag::note_odr_objc_synthesize_ivar_here)
4903 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004904
4905 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004906 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004907
Douglas Gregor14a49e22010-12-07 18:32:03 +00004908 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004909 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004910 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004911
Douglas Gregor14a49e22010-12-07 18:32:03 +00004912 return ToImpl;
4913}
4914
Balazs Keri3b30d652018-10-19 13:32:20 +00004915ExpectedDecl
4916ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004917 // For template arguments, we adopt the translation unit as our declaration
4918 // context. This context will be fixed when the actual template declaration
4919 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004920
Douglas Gregora082a492010-11-30 19:14:50 +00004921 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004922
4923 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4924 if (!BeginLocOrErr)
4925 return BeginLocOrErr.takeError();
4926
4927 ExpectedSLoc LocationOrErr = import(D->getLocation());
4928 if (!LocationOrErr)
4929 return LocationOrErr.takeError();
4930
Gabor Marton26f72a92018-07-12 09:42:05 +00004931 TemplateTypeParmDecl *ToD = nullptr;
4932 (void)GetImportedOrCreateDecl(
4933 ToD, D, Importer.getToContext(),
4934 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004935 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004936 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4937 D->wasDeclaredWithTypename(), D->isParameterPack());
4938 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004939}
4940
Balazs Keri3b30d652018-10-19 13:32:20 +00004941ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004942ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004943 DeclarationName ToDeclName;
4944 SourceLocation ToLocation, ToInnerLocStart;
4945 QualType ToType;
4946 TypeSourceInfo *ToTypeSourceInfo;
4947 if (auto Imp = importSeq(
4948 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4949 D->getInnerLocStart()))
4950 std::tie(
4951 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4952 ToInnerLocStart) = *Imp;
4953 else
4954 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004955
Douglas Gregora082a492010-11-30 19:14:50 +00004956 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004957
4958 NonTypeTemplateParmDecl *ToD = nullptr;
4959 (void)GetImportedOrCreateDecl(
4960 ToD, D, Importer.getToContext(),
4961 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004962 ToInnerLocStart, ToLocation, D->getDepth(),
4963 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4964 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004965 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004966}
4967
Balazs Keri3b30d652018-10-19 13:32:20 +00004968ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004969ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4970 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004971 auto NameOrErr = import(D->getDeclName());
4972 if (!NameOrErr)
4973 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004974
Douglas Gregora082a492010-11-30 19:14:50 +00004975 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004976 ExpectedSLoc LocationOrErr = import(D->getLocation());
4977 if (!LocationOrErr)
4978 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004979
Douglas Gregora082a492010-11-30 19:14:50 +00004980 // Import template parameters.
Balazs Keri3b30d652018-10-19 13:32:20 +00004981 auto TemplateParamsOrErr = ImportTemplateParameterList(
4982 D->getTemplateParameters());
4983 if (!TemplateParamsOrErr)
4984 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004985
Douglas Gregora082a492010-11-30 19:14:50 +00004986 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004987
4988 TemplateTemplateParmDecl *ToD = nullptr;
4989 (void)GetImportedOrCreateDecl(
4990 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004991 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4992 D->getDepth(), D->getPosition(), D->isParameterPack(),
4993 (*NameOrErr).getAsIdentifierInfo(),
4994 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004995 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004996}
4997
Gabor Marton9581c332018-05-23 13:53:36 +00004998// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4999// it has any definition in the redecl chain.
5000static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
5001 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
5002 if (!ToTemplatedDef)
5003 return nullptr;
5004 ClassTemplateDecl *TemplateWithDef =
5005 ToTemplatedDef->getDescribedClassTemplate();
5006 return TemplateWithDef;
5007}
5008
Balazs Keri3b30d652018-10-19 13:32:20 +00005009ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005010 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
5011
Douglas Gregora082a492010-11-30 19:14:50 +00005012 // If this record has a definition in the translation unit we're coming from,
5013 // but this particular declaration is not that definition, import the
5014 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005015 auto *Definition =
5016 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Balazs Keri0c23dc52018-08-13 13:08:37 +00005017 if (Definition && Definition != D->getTemplatedDecl() && !IsFriend) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005018 if (ExpectedDecl ImportedDefOrErr = import(
5019 Definition->getDescribedClassTemplate()))
5020 return Importer.MapImported(D, *ImportedDefOrErr);
5021 else
5022 return ImportedDefOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00005023 }
Gabor Marton9581c332018-05-23 13:53:36 +00005024
Douglas Gregora082a492010-11-30 19:14:50 +00005025 // Import the major distinguishing characteristics of this class template.
5026 DeclContext *DC, *LexicalDC;
5027 DeclarationName Name;
5028 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005029 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005030 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5031 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005032 if (ToD)
5033 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00005034
Douglas Gregora082a492010-11-30 19:14:50 +00005035 // We may already have a template of the same name; try to find and match it.
5036 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005037 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005038 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005039 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005040 for (auto *FoundDecl : FoundDecls) {
5041 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00005042 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005043
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005044 Decl *Found = FoundDecl;
5045 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00005046
5047 // The class to be imported is a definition.
5048 if (D->isThisDeclarationADefinition()) {
5049 // Lookup will find the fwd decl only if that is more recent than the
5050 // definition. So, try to get the definition if that is available in
5051 // the redecl chain.
5052 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005053 if (TemplateWithDef)
5054 FoundTemplate = TemplateWithDef;
5055 else
Gabor Marton9581c332018-05-23 13:53:36 +00005056 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005057 }
5058
Douglas Gregora082a492010-11-30 19:14:50 +00005059 if (IsStructuralMatch(D, FoundTemplate)) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005060 if (!IsFriend) {
5061 Importer.MapImported(D->getTemplatedDecl(),
5062 FoundTemplate->getTemplatedDecl());
5063 return Importer.MapImported(D, FoundTemplate);
5064 }
Aleksei Sidorin761c2242018-05-15 11:09:07 +00005065
Balazs Keri0c23dc52018-08-13 13:08:37 +00005066 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005067 }
Douglas Gregora082a492010-11-30 19:14:50 +00005068 }
Gabor Marton9581c332018-05-23 13:53:36 +00005069
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005070 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005071 }
Gabor Marton9581c332018-05-23 13:53:36 +00005072
Douglas Gregora082a492010-11-30 19:14:50 +00005073 if (!ConflictingDecls.empty()) {
5074 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00005075 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00005076 ConflictingDecls.size());
5077 }
Gabor Marton9581c332018-05-23 13:53:36 +00005078
Douglas Gregora082a492010-11-30 19:14:50 +00005079 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005080 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00005081 }
5082
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005083 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5084
Douglas Gregora082a492010-11-30 19:14:50 +00005085 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005086 CXXRecordDecl *ToTemplated;
5087 if (Error Err = importInto(ToTemplated, FromTemplated))
5088 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005089
Douglas Gregora082a492010-11-30 19:14:50 +00005090 // Create the class template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005091 auto TemplateParamsOrErr = ImportTemplateParameterList(
5092 D->getTemplateParameters());
5093 if (!TemplateParamsOrErr)
5094 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005095
Gabor Marton26f72a92018-07-12 09:42:05 +00005096 ClassTemplateDecl *D2;
5097 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005098 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005099 return D2;
5100
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005101 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005102
Balazs Keri0c23dc52018-08-13 13:08:37 +00005103 if (ToTemplated->getPreviousDecl()) {
5104 assert(
5105 ToTemplated->getPreviousDecl()->getDescribedClassTemplate() &&
5106 "Missing described template");
5107 D2->setPreviousDecl(
5108 ToTemplated->getPreviousDecl()->getDescribedClassTemplate());
5109 }
Douglas Gregora082a492010-11-30 19:14:50 +00005110 D2->setAccess(D->getAccess());
5111 D2->setLexicalDeclContext(LexicalDC);
Balazs Keri0c23dc52018-08-13 13:08:37 +00005112 if (!IsFriend)
5113 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005114
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005115 if (FromTemplated->isCompleteDefinition() &&
5116 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005117 // FIXME: Import definition!
5118 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005119
Douglas Gregora082a492010-11-30 19:14:50 +00005120 return D2;
5121}
5122
Balazs Keri3b30d652018-10-19 13:32:20 +00005123ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005124 ClassTemplateSpecializationDecl *D) {
5125 // If this record has a definition in the translation unit we're coming from,
5126 // but this particular declaration is not that definition, import the
5127 // definition and map to that.
5128 TagDecl *Definition = D->getDefinition();
5129 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005130 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5131 return Importer.MapImported(D, *ImportedDefOrErr);
5132 else
5133 return ImportedDefOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005134 }
5135
Balazs Keri3b30d652018-10-19 13:32:20 +00005136 ClassTemplateDecl *ClassTemplate;
5137 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5138 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005139
Douglas Gregore2e50d332010-12-01 01:36:18 +00005140 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005141 DeclContext *DC, *LexicalDC;
5142 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5143 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005144
5145 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005146 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005147 if (Error Err = ImportTemplateArguments(
5148 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5149 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005150
Douglas Gregore2e50d332010-12-01 01:36:18 +00005151 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005152 void *InsertPos = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005153 ClassTemplateSpecializationDecl *D2 = nullptr;
5154 ClassTemplatePartialSpecializationDecl *PartialSpec =
5155 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5156 if (PartialSpec)
5157 D2 = ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
5158 else
5159 D2 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
5160 ClassTemplateSpecializationDecl * const PrevDecl = D2;
5161 RecordDecl *FoundDef = D2 ? D2->getDefinition() : nullptr;
5162 if (FoundDef) {
5163 if (!D->isCompleteDefinition()) {
5164 // The "From" translation unit only had a forward declaration; call it
5165 // the same declaration.
5166 // TODO Handle the redecl chain properly!
5167 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005168 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005169
5170 if (IsStructuralMatch(D, FoundDef)) {
5171
5172 Importer.MapImported(D, FoundDef);
5173
5174 // Import those those default field initializers which have been
5175 // instantiated in the "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005176 for (auto *FromField : D->fields()) {
5177 auto ToOrErr = import(FromField);
5178 if (!ToOrErr)
5179 // FIXME: return the error?
5180 consumeError(ToOrErr.takeError());
5181 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005182
5183 // Import those methods which have been instantiated in the
5184 // "From" context, but not in the "To" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00005185 for (CXXMethodDecl *FromM : D->methods()) {
5186 auto ToOrErr = import(FromM);
5187 if (!ToOrErr)
5188 // FIXME: return the error?
5189 consumeError(ToOrErr.takeError());
5190 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005191
5192 // TODO Import instantiated default arguments.
5193 // TODO Import instantiated exception specifications.
5194 //
5195 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint what
5196 // else could be fused during an AST merge.
5197
5198 return FoundDef;
5199 }
5200 } else { // We either couldn't find any previous specialization in the "To"
5201 // context, or we found one but without definition. Let's create a
5202 // new specialization and register that at the class template.
Balazs Keri3b30d652018-10-19 13:32:20 +00005203
5204 // Import the location of this declaration.
5205 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5206 if (!BeginLocOrErr)
5207 return BeginLocOrErr.takeError();
5208 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5209 if (!IdLocOrErr)
5210 return IdLocOrErr.takeError();
5211
Gabor Marton42e15de2018-08-22 11:52:14 +00005212 if (PartialSpec) {
5213 // Import TemplateArgumentListInfo.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005214 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005215 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Balazs Keri3b30d652018-10-19 13:32:20 +00005216 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5217 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005218
Balazs Keri3b30d652018-10-19 13:32:20 +00005219 QualType CanonInjType;
5220 if (Error Err = importInto(
5221 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5222 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005223 CanonInjType = CanonInjType.getCanonicalType();
5224
Balazs Keri3b30d652018-10-19 13:32:20 +00005225 auto ToTPListOrErr = ImportTemplateParameterList(
5226 PartialSpec->getTemplateParameters());
5227 if (!ToTPListOrErr)
5228 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005229
Gabor Marton26f72a92018-07-12 09:42:05 +00005230 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00005231 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5232 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
Gabor Marton26f72a92018-07-12 09:42:05 +00005233 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
Gabor Marton42e15de2018-08-22 11:52:14 +00005234 ToTAInfo, CanonInjType,
5235 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
Gabor Marton26f72a92018-07-12 09:42:05 +00005236 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005237
Gabor Marton42e15de2018-08-22 11:52:14 +00005238 // Update InsertPos, because preceding import calls may have invalidated
5239 // it by adding new specializations.
5240 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5241 // Add this partial specialization to the class template.
5242 ClassTemplate->AddPartialSpecialization(
5243 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
5244
5245 } else { // Not a partial specialization.
Gabor Marton26f72a92018-07-12 09:42:05 +00005246 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00005247 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5248 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5249 PrevDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00005250 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005251
5252 // Update InsertPos, because preceding import calls may have invalidated
5253 // it by adding new specializations.
5254 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5255 // Add this specialization to the class template.
5256 ClassTemplate->AddSpecialization(D2, InsertPos);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005257 }
5258
Douglas Gregore2e50d332010-12-01 01:36:18 +00005259 D2->setSpecializationKind(D->getSpecializationKind());
5260
Douglas Gregore2e50d332010-12-01 01:36:18 +00005261 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005262 if (auto LocOrErr = import(D->getQualifierLoc()))
5263 D2->setQualifierInfo(*LocOrErr);
5264 else
5265 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005266
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005267 if (auto *TSI = D->getTypeAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005268 if (auto TInfoOrErr = import(TSI))
5269 D2->setTypeAsWritten(*TInfoOrErr);
5270 else
5271 return TInfoOrErr.takeError();
5272
5273 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5274 D2->setTemplateKeywordLoc(*LocOrErr);
5275 else
5276 return LocOrErr.takeError();
5277
5278 if (auto LocOrErr = import(D->getExternLoc()))
5279 D2->setExternLoc(*LocOrErr);
5280 else
5281 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005282 }
5283
Balazs Keri3b30d652018-10-19 13:32:20 +00005284 if (D->getPointOfInstantiation().isValid()) {
5285 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5286 D2->setPointOfInstantiation(*POIOrErr);
5287 else
5288 return POIOrErr.takeError();
5289 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005290
5291 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5292
Gabor Martonb14056b2018-05-25 11:21:24 +00005293 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00005294 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00005295
5296 // Add to the DC only if it was an explicit specialization/instantiation.
5297 if (D2->isExplicitInstantiationOrSpecialization()) {
5298 LexicalDC->addDeclInternal(D2);
5299 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005300 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005301 if (D->isCompleteDefinition())
5302 if (Error Err = ImportDefinition(D, D2))
5303 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005304
Douglas Gregore2e50d332010-12-01 01:36:18 +00005305 return D2;
5306}
5307
Balazs Keri3b30d652018-10-19 13:32:20 +00005308ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005309 // If this variable has a definition in the translation unit we're coming
5310 // from,
5311 // but this particular declaration is not that definition, import the
5312 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005313 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005314 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5315 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005316 if (ExpectedDecl ImportedDefOrErr = import(
5317 Definition->getDescribedVarTemplate()))
5318 return Importer.MapImported(D, *ImportedDefOrErr);
5319 else
5320 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005321 }
5322
5323 // Import the major distinguishing characteristics of this variable template.
5324 DeclContext *DC, *LexicalDC;
5325 DeclarationName Name;
5326 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005327 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005328 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5329 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005330 if (ToD)
5331 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005332
5333 // We may already have a template of the same name; try to find and match it.
5334 assert(!DC->isFunctionOrMethod() &&
5335 "Variable templates cannot be declared at function scope");
5336 SmallVector<NamedDecl *, 4> ConflictingDecls;
5337 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00005338 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005339 for (auto *FoundDecl : FoundDecls) {
5340 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005341 continue;
5342
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005343 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005344 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005345 if (IsStructuralMatch(D, FoundTemplate)) {
5346 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005347 Importer.MapImported(D->getTemplatedDecl(),
5348 FoundTemplate->getTemplatedDecl());
5349 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005350 }
5351 }
5352
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005353 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005354 }
5355
5356 if (!ConflictingDecls.empty()) {
5357 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5358 ConflictingDecls.data(),
5359 ConflictingDecls.size());
5360 }
5361
5362 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005363 // FIXME: Is it possible to get other error than name conflict?
5364 // (Put this `if` into the previous `if`?)
5365 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005366
5367 VarDecl *DTemplated = D->getTemplatedDecl();
5368
5369 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005370 // FIXME: Value not used?
5371 ExpectedType TypeOrErr = import(DTemplated->getType());
5372 if (!TypeOrErr)
5373 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005374
5375 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005376 VarDecl *ToTemplated;
5377 if (Error Err = importInto(ToTemplated, DTemplated))
5378 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005379
5380 // Create the variable template declaration itself.
Balazs Keri3b30d652018-10-19 13:32:20 +00005381 auto TemplateParamsOrErr = ImportTemplateParameterList(
5382 D->getTemplateParameters());
5383 if (!TemplateParamsOrErr)
5384 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005385
Gabor Marton26f72a92018-07-12 09:42:05 +00005386 VarTemplateDecl *ToVarTD;
5387 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005388 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005389 return ToVarTD;
5390
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005391 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005392
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005393 ToVarTD->setAccess(D->getAccess());
5394 ToVarTD->setLexicalDeclContext(LexicalDC);
5395 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005396
Larisse Voufo39a1e502013-08-06 01:03:05 +00005397 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005398 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005399 // FIXME: Import definition!
5400 }
5401
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005402 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005403}
5404
Balazs Keri3b30d652018-10-19 13:32:20 +00005405ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005406 VarTemplateSpecializationDecl *D) {
5407 // If this record has a definition in the translation unit we're coming from,
5408 // but this particular declaration is not that definition, import the
5409 // definition and map to that.
5410 VarDecl *Definition = D->getDefinition();
5411 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005412 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5413 return Importer.MapImported(D, *ImportedDefOrErr);
5414 else
5415 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005416 }
5417
Balazs Keri3b30d652018-10-19 13:32:20 +00005418 VarTemplateDecl *VarTemplate;
5419 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5420 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005421
5422 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005423 DeclContext *DC, *LexicalDC;
5424 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5425 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005426
5427 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005428 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5429 if (!BeginLocOrErr)
5430 return BeginLocOrErr.takeError();
5431
5432 auto IdLocOrErr = import(D->getLocation());
5433 if (!IdLocOrErr)
5434 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005435
5436 // Import template arguments.
5437 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005438 if (Error Err = ImportTemplateArguments(
5439 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5440 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005441
5442 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005443 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005444 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005445 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005446 if (D2) {
5447 // We already have a variable template specialization with these template
5448 // arguments.
5449
5450 // FIXME: Check for specialization vs. instantiation errors.
5451
5452 if (VarDecl *FoundDef = D2->getDefinition()) {
5453 if (!D->isThisDeclarationADefinition() ||
5454 IsStructuralMatch(D, FoundDef)) {
5455 // The record types structurally match, or the "from" translation
5456 // unit only had a forward declaration anyway; call it the same
5457 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005458 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005459 }
5460 }
5461 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005462 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005463 QualType T;
5464 if (Error Err = importInto(T, D->getType()))
5465 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005466
Balazs Keri3b30d652018-10-19 13:32:20 +00005467 auto TInfoOrErr = import(D->getTypeSourceInfo());
5468 if (!TInfoOrErr)
5469 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005470
5471 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005472 if (Error Err = ImportTemplateArgumentListInfo(
5473 D->getTemplateArgsInfo(), ToTAInfo))
5474 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005475
5476 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005477 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005478 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5479 // Import TemplateArgumentListInfo
5480 TemplateArgumentListInfo ArgInfos;
5481 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5482 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005483 if (Error Err = ImportTemplateArgumentListInfo(
5484 *FromTAArgsAsWritten, ArgInfos))
5485 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005486
Balazs Keri3b30d652018-10-19 13:32:20 +00005487 auto ToTPListOrErr = ImportTemplateParameterList(
5488 FromPartial->getTemplateParameters());
5489 if (!ToTPListOrErr)
5490 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005491
Gabor Marton26f72a92018-07-12 09:42:05 +00005492 PartVarSpecDecl *ToPartial;
5493 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005494 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5495 VarTemplate, T, *TInfoOrErr,
5496 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005497 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005498
Balazs Keri3b30d652018-10-19 13:32:20 +00005499 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5500 FromPartial->getInstantiatedFromMember()))
5501 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5502 else
5503 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005504
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005505 if (FromPartial->isMemberSpecialization())
5506 ToPartial->setMemberSpecialization();
5507
5508 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005509
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005510 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005511 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5512 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5513 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005514 D->getStorageClass(), TemplateArgs))
5515 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005516 }
5517
Balazs Keri3b30d652018-10-19 13:32:20 +00005518 if (D->getPointOfInstantiation().isValid()) {
5519 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5520 D2->setPointOfInstantiation(*POIOrErr);
5521 else
5522 return POIOrErr.takeError();
5523 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005524
Larisse Voufo39a1e502013-08-06 01:03:05 +00005525 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005526 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005527
5528 // Add this specialization to the class template.
5529 VarTemplate->AddSpecialization(D2, InsertPos);
5530
5531 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005532 if (auto LocOrErr = import(D->getQualifierLoc()))
5533 D2->setQualifierInfo(*LocOrErr);
5534 else
5535 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005536
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005537 if (D->isConstexpr())
5538 D2->setConstexpr(true);
5539
Larisse Voufo39a1e502013-08-06 01:03:05 +00005540 // Add the specialization to this context.
5541 D2->setLexicalDeclContext(LexicalDC);
5542 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005543
5544 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005545 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005546
Balazs Keri3b30d652018-10-19 13:32:20 +00005547 if (Error Err = ImportInitializer(D, D2))
5548 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005549
5550 return D2;
5551}
5552
Balazs Keri3b30d652018-10-19 13:32:20 +00005553ExpectedDecl
5554ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005555 DeclContext *DC, *LexicalDC;
5556 DeclarationName Name;
5557 SourceLocation Loc;
5558 NamedDecl *ToD;
5559
Balazs Keri3b30d652018-10-19 13:32:20 +00005560 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5561 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005562
5563 if (ToD)
5564 return ToD;
5565
5566 // Try to find a function in our own ("to") context with the same name, same
5567 // type, and in the same context as the function we're importing.
5568 if (!LexicalDC->isFunctionOrMethod()) {
5569 unsigned IDNS = Decl::IDNS_Ordinary;
5570 SmallVector<NamedDecl *, 2> FoundDecls;
5571 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005572 for (auto *FoundDecl : FoundDecls) {
5573 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005574 continue;
5575
Balazs Keri3b30d652018-10-19 13:32:20 +00005576 if (auto *FoundFunction =
5577 dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005578 if (FoundFunction->hasExternalFormalLinkage() &&
5579 D->hasExternalFormalLinkage()) {
5580 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00005581 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005582 // FIXME: Actually try to merge the body and other attributes.
5583 return FoundFunction;
5584 }
5585 }
5586 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005587 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005588 }
5589 }
5590
Balazs Keri3b30d652018-10-19 13:32:20 +00005591 auto ParamsOrErr = ImportTemplateParameterList(
5592 D->getTemplateParameters());
5593 if (!ParamsOrErr)
5594 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005595
Balazs Keri3b30d652018-10-19 13:32:20 +00005596 FunctionDecl *TemplatedFD;
5597 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5598 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005599
Gabor Marton26f72a92018-07-12 09:42:05 +00005600 FunctionTemplateDecl *ToFunc;
5601 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005602 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005603 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005604
5605 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
5606 ToFunc->setAccess(D->getAccess());
5607 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005608
5609 LexicalDC->addDeclInternal(ToFunc);
5610 return ToFunc;
5611}
5612
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005613//----------------------------------------------------------------------------
5614// Import Statements
5615//----------------------------------------------------------------------------
5616
Balazs Keri3b30d652018-10-19 13:32:20 +00005617ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005618 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5619 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005620 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005621}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005622
Balazs Keri3b30d652018-10-19 13:32:20 +00005623
5624ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005625 SmallVector<IdentifierInfo *, 4> Names;
5626 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5627 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005628 // ToII is nullptr when no symbolic name is given for output operand
5629 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005630 Names.push_back(ToII);
5631 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005632
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005633 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5634 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005635 // ToII is nullptr when no symbolic name is given for input operand
5636 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005637 Names.push_back(ToII);
5638 }
5639
5640 SmallVector<StringLiteral *, 4> Clobbers;
5641 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005642 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5643 Clobbers.push_back(*ClobberOrErr);
5644 else
5645 return ClobberOrErr.takeError();
5646
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005647 }
5648
5649 SmallVector<StringLiteral *, 4> Constraints;
5650 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005651 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5652 Constraints.push_back(*OutputOrErr);
5653 else
5654 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005655 }
5656
5657 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005658 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5659 Constraints.push_back(*InputOrErr);
5660 else
5661 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005662 }
5663
5664 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005665 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5666 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005667
Balazs Keri3b30d652018-10-19 13:32:20 +00005668 if (Error Err = ImportArrayChecked(
5669 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5670 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005671
Balazs Keri3b30d652018-10-19 13:32:20 +00005672 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5673 if (!AsmLocOrErr)
5674 return AsmLocOrErr.takeError();
5675 auto AsmStrOrErr = import(S->getAsmString());
5676 if (!AsmStrOrErr)
5677 return AsmStrOrErr.takeError();
5678 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5679 if (!RParenLocOrErr)
5680 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005681
5682 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005683 Importer.getToContext(),
5684 *AsmLocOrErr,
5685 S->isSimple(),
5686 S->isVolatile(),
5687 S->getNumOutputs(),
5688 S->getNumInputs(),
5689 Names.data(),
5690 Constraints.data(),
5691 Exprs.data(),
5692 *AsmStrOrErr,
5693 S->getNumClobbers(),
5694 Clobbers.data(),
5695 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005696}
5697
Balazs Keri3b30d652018-10-19 13:32:20 +00005698ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5699 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5700 if (!Imp)
5701 return Imp.takeError();
5702
5703 DeclGroupRef ToDG;
5704 SourceLocation ToBeginLoc, ToEndLoc;
5705 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5706
5707 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005708}
5709
Balazs Keri3b30d652018-10-19 13:32:20 +00005710ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5711 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5712 if (!ToSemiLocOrErr)
5713 return ToSemiLocOrErr.takeError();
5714 return new (Importer.getToContext()) NullStmt(
5715 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005716}
5717
Balazs Keri3b30d652018-10-19 13:32:20 +00005718ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005719 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005720
Balazs Keri3b30d652018-10-19 13:32:20 +00005721 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5722 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005723
Balazs Keri3b30d652018-10-19 13:32:20 +00005724 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5725 if (!ToLBracLocOrErr)
5726 return ToLBracLocOrErr.takeError();
5727
5728 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5729 if (!ToRBracLocOrErr)
5730 return ToRBracLocOrErr.takeError();
5731
5732 return CompoundStmt::Create(
5733 Importer.getToContext(), ToStmts,
5734 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005735}
5736
Balazs Keri3b30d652018-10-19 13:32:20 +00005737ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5738 auto Imp = importSeq(
5739 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5740 S->getEllipsisLoc(), S->getColonLoc());
5741 if (!Imp)
5742 return Imp.takeError();
5743
5744 Expr *ToLHS, *ToRHS;
5745 Stmt *ToSubStmt;
5746 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5747 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5748 *Imp;
5749
Bruno Ricci5b30571752018-10-28 12:30:53 +00005750 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5751 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005752 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005753
Gabor Horvath480892b2017-10-18 09:25:18 +00005754 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005755}
5756
Balazs Keri3b30d652018-10-19 13:32:20 +00005757ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5758 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5759 if (!Imp)
5760 return Imp.takeError();
5761
5762 SourceLocation ToDefaultLoc, ToColonLoc;
5763 Stmt *ToSubStmt;
5764 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5765
5766 return new (Importer.getToContext()) DefaultStmt(
5767 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005768}
5769
Balazs Keri3b30d652018-10-19 13:32:20 +00005770ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5771 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5772 if (!Imp)
5773 return Imp.takeError();
5774
5775 SourceLocation ToIdentLoc;
5776 LabelDecl *ToLabelDecl;
5777 Stmt *ToSubStmt;
5778 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5779
5780 return new (Importer.getToContext()) LabelStmt(
5781 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005782}
5783
Balazs Keri3b30d652018-10-19 13:32:20 +00005784ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5785 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5786 if (!ToAttrLocOrErr)
5787 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005788 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5789 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005790 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5791 return std::move(Err);
5792 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5793 if (!ToSubStmtOrErr)
5794 return ToSubStmtOrErr.takeError();
5795
5796 return AttributedStmt::Create(
5797 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005798}
5799
Balazs Keri3b30d652018-10-19 13:32:20 +00005800ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5801 auto Imp = importSeq(
5802 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5803 S->getThen(), S->getElseLoc(), S->getElse());
5804 if (!Imp)
5805 return Imp.takeError();
5806
5807 SourceLocation ToIfLoc, ToElseLoc;
5808 Stmt *ToInit, *ToThen, *ToElse;
5809 VarDecl *ToConditionVariable;
5810 Expr *ToCond;
5811 std::tie(
5812 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5813 *Imp;
5814
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005815 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5816 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5817 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005818}
5819
Balazs Keri3b30d652018-10-19 13:32:20 +00005820ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5821 auto Imp = importSeq(
5822 S->getInit(), S->getConditionVariable(), S->getCond(),
5823 S->getBody(), S->getSwitchLoc());
5824 if (!Imp)
5825 return Imp.takeError();
5826
5827 Stmt *ToInit, *ToBody;
5828 VarDecl *ToConditionVariable;
5829 Expr *ToCond;
5830 SourceLocation ToSwitchLoc;
5831 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5832
Bruno Riccie2806f82018-10-29 16:12:37 +00005833 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5834 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005835 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005836 ToStmt->setSwitchLoc(ToSwitchLoc);
5837
Sean Callanan59721b32015-04-28 18:41:46 +00005838 // Now we have to re-chain the cases.
5839 SwitchCase *LastChainedSwitchCase = nullptr;
5840 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5841 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005842 Expected<SwitchCase *> ToSCOrErr = import(SC);
5843 if (!ToSCOrErr)
5844 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005845 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005846 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005847 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005848 ToStmt->setSwitchCaseList(*ToSCOrErr);
5849 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005850 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005851
Sean Callanan59721b32015-04-28 18:41:46 +00005852 return ToStmt;
5853}
5854
Balazs Keri3b30d652018-10-19 13:32:20 +00005855ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5856 auto Imp = importSeq(
5857 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5858 if (!Imp)
5859 return Imp.takeError();
5860
5861 VarDecl *ToConditionVariable;
5862 Expr *ToCond;
5863 Stmt *ToBody;
5864 SourceLocation ToWhileLoc;
5865 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5866
Bruno Riccibacf7512018-10-30 13:42:41 +00005867 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5868 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005869}
5870
Balazs Keri3b30d652018-10-19 13:32:20 +00005871ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5872 auto Imp = importSeq(
5873 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5874 S->getRParenLoc());
5875 if (!Imp)
5876 return Imp.takeError();
5877
5878 Stmt *ToBody;
5879 Expr *ToCond;
5880 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5881 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5882
5883 return new (Importer.getToContext()) DoStmt(
5884 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005885}
5886
Balazs Keri3b30d652018-10-19 13:32:20 +00005887ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5888 auto Imp = importSeq(
5889 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5890 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5891 if (!Imp)
5892 return Imp.takeError();
5893
5894 Stmt *ToInit;
5895 Expr *ToCond, *ToInc;
5896 VarDecl *ToConditionVariable;
5897 Stmt *ToBody;
5898 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5899 std::tie(
5900 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5901 ToLParenLoc, ToRParenLoc) = *Imp;
5902
5903 return new (Importer.getToContext()) ForStmt(
5904 Importer.getToContext(),
5905 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5906 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005907}
5908
Balazs Keri3b30d652018-10-19 13:32:20 +00005909ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5910 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5911 if (!Imp)
5912 return Imp.takeError();
5913
5914 LabelDecl *ToLabel;
5915 SourceLocation ToGotoLoc, ToLabelLoc;
5916 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5917
5918 return new (Importer.getToContext()) GotoStmt(
5919 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005920}
5921
Balazs Keri3b30d652018-10-19 13:32:20 +00005922ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5923 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5924 if (!Imp)
5925 return Imp.takeError();
5926
5927 SourceLocation ToGotoLoc, ToStarLoc;
5928 Expr *ToTarget;
5929 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5930
5931 return new (Importer.getToContext()) IndirectGotoStmt(
5932 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005933}
5934
Balazs Keri3b30d652018-10-19 13:32:20 +00005935ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5936 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5937 if (!ToContinueLocOrErr)
5938 return ToContinueLocOrErr.takeError();
5939 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005940}
5941
Balazs Keri3b30d652018-10-19 13:32:20 +00005942ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5943 auto ToBreakLocOrErr = import(S->getBreakLoc());
5944 if (!ToBreakLocOrErr)
5945 return ToBreakLocOrErr.takeError();
5946 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005947}
5948
Balazs Keri3b30d652018-10-19 13:32:20 +00005949ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5950 auto Imp = importSeq(
5951 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5952 if (!Imp)
5953 return Imp.takeError();
5954
5955 SourceLocation ToReturnLoc;
5956 Expr *ToRetValue;
5957 const VarDecl *ToNRVOCandidate;
5958 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5959
5960 return new (Importer.getToContext()) ReturnStmt(
5961 ToReturnLoc, ToRetValue, ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005962}
5963
Balazs Keri3b30d652018-10-19 13:32:20 +00005964ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5965 auto Imp = importSeq(
5966 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5967 if (!Imp)
5968 return Imp.takeError();
5969
5970 SourceLocation ToCatchLoc;
5971 VarDecl *ToExceptionDecl;
5972 Stmt *ToHandlerBlock;
5973 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5974
5975 return new (Importer.getToContext()) CXXCatchStmt (
5976 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005977}
5978
Balazs Keri3b30d652018-10-19 13:32:20 +00005979ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5980 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5981 if (!ToTryLocOrErr)
5982 return ToTryLocOrErr.takeError();
5983
5984 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5985 if (!ToTryBlockOrErr)
5986 return ToTryBlockOrErr.takeError();
5987
Sean Callanan59721b32015-04-28 18:41:46 +00005988 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5989 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5990 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005991 if (auto ToHandlerOrErr = import(FromHandler))
5992 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005993 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005994 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005995 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005996
5997 return CXXTryStmt::Create(
5998 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005999}
6000
Balazs Keri3b30d652018-10-19 13:32:20 +00006001ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
6002 auto Imp1 = importSeq(
6003 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
6004 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
6005 if (!Imp1)
6006 return Imp1.takeError();
6007 auto Imp2 = importSeq(
6008 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
6009 if (!Imp2)
6010 return Imp2.takeError();
6011
6012 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
6013 Expr *ToCond, *ToInc;
6014 Stmt *ToInit, *ToBody;
6015 std::tie(
6016 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6017 ToBody) = *Imp1;
6018 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
6019 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
6020
6021 return new (Importer.getToContext()) CXXForRangeStmt(
6022 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6023 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006024}
6025
Balazs Keri3b30d652018-10-19 13:32:20 +00006026ExpectedStmt
6027ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
6028 auto Imp = importSeq(
6029 S->getElement(), S->getCollection(), S->getBody(),
6030 S->getForLoc(), S->getRParenLoc());
6031 if (!Imp)
6032 return Imp.takeError();
6033
6034 Stmt *ToElement, *ToBody;
6035 Expr *ToCollection;
6036 SourceLocation ToForLoc, ToRParenLoc;
6037 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6038
6039 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6040 ToCollection,
6041 ToBody,
6042 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006043 ToRParenLoc);
6044}
6045
Balazs Keri3b30d652018-10-19 13:32:20 +00006046ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6047 auto Imp = importSeq(
6048 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6049 S->getCatchBody());
6050 if (!Imp)
6051 return Imp.takeError();
6052
6053 SourceLocation ToAtCatchLoc, ToRParenLoc;
6054 VarDecl *ToCatchParamDecl;
6055 Stmt *ToCatchBody;
6056 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6057
6058 return new (Importer.getToContext()) ObjCAtCatchStmt (
6059 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006060}
6061
Balazs Keri3b30d652018-10-19 13:32:20 +00006062ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6063 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6064 if (!ToAtFinallyLocOrErr)
6065 return ToAtFinallyLocOrErr.takeError();
6066 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6067 if (!ToAtFinallyStmtOrErr)
6068 return ToAtFinallyStmtOrErr.takeError();
6069 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6070 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006071}
6072
Balazs Keri3b30d652018-10-19 13:32:20 +00006073ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6074 auto Imp = importSeq(
6075 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6076 if (!Imp)
6077 return Imp.takeError();
6078
6079 SourceLocation ToAtTryLoc;
6080 Stmt *ToTryBody, *ToFinallyStmt;
6081 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6082
Sean Callanan59721b32015-04-28 18:41:46 +00006083 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6084 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6085 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006086 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6087 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006088 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006089 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006090 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006091
Sean Callanan59721b32015-04-28 18:41:46 +00006092 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006093 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006094 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006095 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006096}
6097
Balazs Keri3b30d652018-10-19 13:32:20 +00006098ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006099 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006100 auto Imp = importSeq(
6101 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6102 if (!Imp)
6103 return Imp.takeError();
6104
6105 SourceLocation ToAtSynchronizedLoc;
6106 Expr *ToSynchExpr;
6107 Stmt *ToSynchBody;
6108 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6109
Sean Callanan59721b32015-04-28 18:41:46 +00006110 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6111 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6112}
6113
Balazs Keri3b30d652018-10-19 13:32:20 +00006114ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6115 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6116 if (!ToThrowLocOrErr)
6117 return ToThrowLocOrErr.takeError();
6118 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6119 if (!ToThrowExprOrErr)
6120 return ToThrowExprOrErr.takeError();
6121 return new (Importer.getToContext()) ObjCAtThrowStmt(
6122 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006123}
6124
Balazs Keri3b30d652018-10-19 13:32:20 +00006125ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6126 ObjCAutoreleasePoolStmt *S) {
6127 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6128 if (!ToAtLocOrErr)
6129 return ToAtLocOrErr.takeError();
6130 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6131 if (!ToSubStmtOrErr)
6132 return ToSubStmtOrErr.takeError();
6133 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6134 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006135}
6136
6137//----------------------------------------------------------------------------
6138// Import Expressions
6139//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006140ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006141 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6142 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006143 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006144}
6145
Balazs Keri3b30d652018-10-19 13:32:20 +00006146ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6147 auto Imp = importSeq(
6148 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6149 E->getRParenLoc(), E->getType());
6150 if (!Imp)
6151 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006152
Balazs Keri3b30d652018-10-19 13:32:20 +00006153 SourceLocation ToBuiltinLoc, ToRParenLoc;
6154 Expr *ToSubExpr;
6155 TypeSourceInfo *ToWrittenTypeInfo;
6156 QualType ToType;
6157 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6158 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006159
6160 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006161 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6162 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006163}
6164
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006165
Balazs Keri3b30d652018-10-19 13:32:20 +00006166ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6167 ExpectedType TypeOrErr = import(E->getType());
6168 if (!TypeOrErr)
6169 return TypeOrErr.takeError();
6170
6171 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6172 if (!BeginLocOrErr)
6173 return BeginLocOrErr.takeError();
6174
6175 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006176}
6177
Balazs Keri3b30d652018-10-19 13:32:20 +00006178ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6179 auto Imp = importSeq(
6180 E->getBeginLoc(), E->getType(), E->getFunctionName());
6181 if (!Imp)
6182 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006183
Balazs Keri3b30d652018-10-19 13:32:20 +00006184 SourceLocation ToBeginLoc;
6185 QualType ToType;
6186 StringLiteral *ToFunctionName;
6187 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006188
Bruno Ricci17ff0262018-10-27 19:21:19 +00006189 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6190 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006191}
6192
Balazs Keri3b30d652018-10-19 13:32:20 +00006193ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6194 auto Imp = importSeq(
6195 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6196 E->getLocation(), E->getType());
6197 if (!Imp)
6198 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006199
Balazs Keri3b30d652018-10-19 13:32:20 +00006200 NestedNameSpecifierLoc ToQualifierLoc;
6201 SourceLocation ToTemplateKeywordLoc, ToLocation;
6202 ValueDecl *ToDecl;
6203 QualType ToType;
6204 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6205 *Imp;
6206
6207 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006208 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006209 auto FoundDOrErr = import(E->getFoundDecl());
6210 if (!FoundDOrErr)
6211 return FoundDOrErr.takeError();
6212 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006213 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006214
Aleksei Sidorina693b372016-09-28 10:16:56 +00006215 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006216 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006217 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006218 if (Error Err =
6219 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6220 return std::move(Err);
6221 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006222 }
6223
Balazs Keri3b30d652018-10-19 13:32:20 +00006224 auto *ToE = DeclRefExpr::Create(
6225 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6226 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6227 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006228 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006229 ToE->setHadMultipleCandidates(true);
6230 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006231}
6232
Balazs Keri3b30d652018-10-19 13:32:20 +00006233ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6234 ExpectedType TypeOrErr = import(E->getType());
6235 if (!TypeOrErr)
6236 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006237
Balazs Keri3b30d652018-10-19 13:32:20 +00006238 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006239}
6240
Balazs Keri3b30d652018-10-19 13:32:20 +00006241ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6242 ExpectedExpr ToInitOrErr = import(E->getInit());
6243 if (!ToInitOrErr)
6244 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006245
Balazs Keri3b30d652018-10-19 13:32:20 +00006246 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6247 if (!ToEqualOrColonLocOrErr)
6248 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006249
Balazs Keri3b30d652018-10-19 13:32:20 +00006250 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006251 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006252 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6253 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6254 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006255 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006256 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006257 }
6258
Balazs Keri3b30d652018-10-19 13:32:20 +00006259 SmallVector<Designator, 4> ToDesignators(E->size());
6260 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6261 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006262
6263 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006264 Importer.getToContext(), ToDesignators,
6265 ToIndexExprs, *ToEqualOrColonLocOrErr,
6266 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006267}
6268
Balazs Keri3b30d652018-10-19 13:32:20 +00006269ExpectedStmt
6270ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6271 ExpectedType ToTypeOrErr = import(E->getType());
6272 if (!ToTypeOrErr)
6273 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006274
Balazs Keri3b30d652018-10-19 13:32:20 +00006275 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6276 if (!ToLocationOrErr)
6277 return ToLocationOrErr.takeError();
6278
6279 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6280 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006281}
6282
Balazs Keri3b30d652018-10-19 13:32:20 +00006283ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6284 ExpectedType ToTypeOrErr = import(E->getType());
6285 if (!ToTypeOrErr)
6286 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006287
Balazs Keri3b30d652018-10-19 13:32:20 +00006288 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6289 if (!ToLocationOrErr)
6290 return ToLocationOrErr.takeError();
6291
6292 return IntegerLiteral::Create(
6293 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006294}
6295
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006296
Balazs Keri3b30d652018-10-19 13:32:20 +00006297ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6298 ExpectedType ToTypeOrErr = import(E->getType());
6299 if (!ToTypeOrErr)
6300 return ToTypeOrErr.takeError();
6301
6302 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6303 if (!ToLocationOrErr)
6304 return ToLocationOrErr.takeError();
6305
6306 return FloatingLiteral::Create(
6307 Importer.getToContext(), E->getValue(), E->isExact(),
6308 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006309}
6310
Balazs Keri3b30d652018-10-19 13:32:20 +00006311ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6312 auto ToTypeOrErr = import(E->getType());
6313 if (!ToTypeOrErr)
6314 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006315
Balazs Keri3b30d652018-10-19 13:32:20 +00006316 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6317 if (!ToSubExprOrErr)
6318 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006319
Balazs Keri3b30d652018-10-19 13:32:20 +00006320 return new (Importer.getToContext()) ImaginaryLiteral(
6321 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006322}
6323
Balazs Keri3b30d652018-10-19 13:32:20 +00006324ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6325 ExpectedType ToTypeOrErr = import(E->getType());
6326 if (!ToTypeOrErr)
6327 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006328
Balazs Keri3b30d652018-10-19 13:32:20 +00006329 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6330 if (!ToLocationOrErr)
6331 return ToLocationOrErr.takeError();
6332
6333 return new (Importer.getToContext()) CharacterLiteral(
6334 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006335}
6336
Balazs Keri3b30d652018-10-19 13:32:20 +00006337ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6338 ExpectedType ToTypeOrErr = import(E->getType());
6339 if (!ToTypeOrErr)
6340 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006341
Balazs Keri3b30d652018-10-19 13:32:20 +00006342 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6343 if (Error Err = ImportArrayChecked(
6344 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6345 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006346
Balazs Keri3b30d652018-10-19 13:32:20 +00006347 return StringLiteral::Create(
6348 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6349 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006350}
6351
Balazs Keri3b30d652018-10-19 13:32:20 +00006352ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6353 auto Imp = importSeq(
6354 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6355 E->getInitializer());
6356 if (!Imp)
6357 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006358
Balazs Keri3b30d652018-10-19 13:32:20 +00006359 SourceLocation ToLParenLoc;
6360 TypeSourceInfo *ToTypeSourceInfo;
6361 QualType ToType;
6362 Expr *ToInitializer;
6363 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006364
6365 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006366 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6367 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006368}
6369
Balazs Keri3b30d652018-10-19 13:32:20 +00006370ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6371 auto Imp = importSeq(
6372 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6373 if (!Imp)
6374 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006375
Balazs Keri3b30d652018-10-19 13:32:20 +00006376 SourceLocation ToBuiltinLoc, ToRParenLoc;
6377 QualType ToType;
6378 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6379
6380 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6381 if (Error Err = ImportArrayChecked(
6382 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6383 ToExprs.begin()))
6384 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006385
6386 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006387 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006388}
6389
Balazs Keri3b30d652018-10-19 13:32:20 +00006390ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6391 auto Imp = importSeq(
6392 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6393 if (!Imp)
6394 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006395
Balazs Keri3b30d652018-10-19 13:32:20 +00006396 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6397 LabelDecl *ToLabel;
6398 QualType ToType;
6399 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006400
6401 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006402 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006403}
6404
Balazs Keri3b30d652018-10-19 13:32:20 +00006405ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6406 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6407 if (!Imp)
6408 return Imp.takeError();
6409
6410 SourceLocation ToLParen, ToRParen;
6411 Expr *ToSubExpr;
6412 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006413
Fangrui Song6907ce22018-07-30 19:24:48 +00006414 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006415 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006416}
6417
Balazs Keri3b30d652018-10-19 13:32:20 +00006418ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6419 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6420 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6421 return std::move(Err);
6422
6423 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6424 if (!ToLParenLocOrErr)
6425 return ToLParenLocOrErr.takeError();
6426
6427 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6428 if (!ToRParenLocOrErr)
6429 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006430
6431 return new (Importer.getToContext()) ParenListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006432 Importer.getToContext(), *ToLParenLocOrErr, ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006433}
6434
Balazs Keri3b30d652018-10-19 13:32:20 +00006435ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6436 auto Imp = importSeq(
6437 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6438 if (!Imp)
6439 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006440
Balazs Keri3b30d652018-10-19 13:32:20 +00006441 CompoundStmt *ToSubStmt;
6442 QualType ToType;
6443 SourceLocation ToLParenLoc, ToRParenLoc;
6444 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006445
Balazs Keri3b30d652018-10-19 13:32:20 +00006446 return new (Importer.getToContext()) StmtExpr(
6447 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006448}
6449
Balazs Keri3b30d652018-10-19 13:32:20 +00006450ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6451 auto Imp = importSeq(
6452 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6453 if (!Imp)
6454 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006455
Balazs Keri3b30d652018-10-19 13:32:20 +00006456 Expr *ToSubExpr;
6457 QualType ToType;
6458 SourceLocation ToOperatorLoc;
6459 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006460
Aaron Ballmana5038552018-01-09 13:07:03 +00006461 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006462 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6463 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006464}
6465
Balazs Keri3b30d652018-10-19 13:32:20 +00006466ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006467ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006468 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6469 if (!Imp)
6470 return Imp.takeError();
6471
6472 QualType ToType;
6473 SourceLocation ToOperatorLoc, ToRParenLoc;
6474 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006475
Douglas Gregord8552cd2010-02-19 01:24:23 +00006476 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006477 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6478 import(E->getArgumentTypeInfo());
6479 if (!ToArgumentTypeInfoOrErr)
6480 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006481
Balazs Keri3b30d652018-10-19 13:32:20 +00006482 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6483 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6484 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006485 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006486
Balazs Keri3b30d652018-10-19 13:32:20 +00006487 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6488 if (!ToArgumentExprOrErr)
6489 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006490
Balazs Keri3b30d652018-10-19 13:32:20 +00006491 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6492 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006493}
6494
Balazs Keri3b30d652018-10-19 13:32:20 +00006495ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6496 auto Imp = importSeq(
6497 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6498 if (!Imp)
6499 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006500
Balazs Keri3b30d652018-10-19 13:32:20 +00006501 Expr *ToLHS, *ToRHS;
6502 QualType ToType;
6503 SourceLocation ToOperatorLoc;
6504 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006505
Balazs Keri3b30d652018-10-19 13:32:20 +00006506 return new (Importer.getToContext()) BinaryOperator(
6507 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6508 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006509}
6510
Balazs Keri3b30d652018-10-19 13:32:20 +00006511ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6512 auto Imp = importSeq(
6513 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6514 E->getRHS(), E->getType());
6515 if (!Imp)
6516 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006517
Balazs Keri3b30d652018-10-19 13:32:20 +00006518 Expr *ToCond, *ToLHS, *ToRHS;
6519 SourceLocation ToQuestionLoc, ToColonLoc;
6520 QualType ToType;
6521 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006522
6523 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006524 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6525 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006526}
6527
Balazs Keri3b30d652018-10-19 13:32:20 +00006528ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006529 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006530 auto Imp = importSeq(
6531 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6532 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6533 if (!Imp)
6534 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006535
Balazs Keri3b30d652018-10-19 13:32:20 +00006536 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6537 OpaqueValueExpr *ToOpaqueValue;
6538 SourceLocation ToQuestionLoc, ToColonLoc;
6539 QualType ToType;
6540 std::tie(
6541 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6542 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006543
6544 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006545 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6546 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6547 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006548}
6549
Balazs Keri3b30d652018-10-19 13:32:20 +00006550ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6551 auto Imp = importSeq(
6552 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6553 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6554 if (!Imp)
6555 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006556
Balazs Keri3b30d652018-10-19 13:32:20 +00006557 SourceLocation ToBeginLoc, ToEndLoc;
6558 TypeSourceInfo *ToQueriedTypeSourceInfo;
6559 Expr *ToDimensionExpression;
6560 QualType ToType;
6561 std::tie(
6562 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6563 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006564
6565 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006566 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6567 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006568}
6569
Balazs Keri3b30d652018-10-19 13:32:20 +00006570ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6571 auto Imp = importSeq(
6572 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6573 if (!Imp)
6574 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006575
Balazs Keri3b30d652018-10-19 13:32:20 +00006576 SourceLocation ToBeginLoc, ToEndLoc;
6577 Expr *ToQueriedExpression;
6578 QualType ToType;
6579 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006580
6581 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006582 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6583 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006584}
6585
Balazs Keri3b30d652018-10-19 13:32:20 +00006586ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6587 auto Imp = importSeq(
6588 E->getLocation(), E->getType(), E->getSourceExpr());
6589 if (!Imp)
6590 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006591
Balazs Keri3b30d652018-10-19 13:32:20 +00006592 SourceLocation ToLocation;
6593 QualType ToType;
6594 Expr *ToSourceExpr;
6595 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006596
6597 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006598 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006599}
6600
Balazs Keri3b30d652018-10-19 13:32:20 +00006601ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6602 auto Imp = importSeq(
6603 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6604 if (!Imp)
6605 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006606
Balazs Keri3b30d652018-10-19 13:32:20 +00006607 Expr *ToLHS, *ToRHS;
6608 SourceLocation ToRBracketLoc;
6609 QualType ToType;
6610 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006611
6612 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006613 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6614 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006615}
6616
Balazs Keri3b30d652018-10-19 13:32:20 +00006617ExpectedStmt
6618ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6619 auto Imp = importSeq(
6620 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6621 E->getComputationResultType(), E->getOperatorLoc());
6622 if (!Imp)
6623 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006624
Balazs Keri3b30d652018-10-19 13:32:20 +00006625 Expr *ToLHS, *ToRHS;
6626 QualType ToType, ToComputationLHSType, ToComputationResultType;
6627 SourceLocation ToOperatorLoc;
6628 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6629 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006630
Balazs Keri3b30d652018-10-19 13:32:20 +00006631 return new (Importer.getToContext()) CompoundAssignOperator(
6632 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6633 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6634 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006635}
6636
Balazs Keri3b30d652018-10-19 13:32:20 +00006637Expected<CXXCastPath>
6638ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6639 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006640 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006641 if (auto SpecOrErr = import(*I))
6642 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006643 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006644 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006645 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006646 return Path;
John McCallcf142162010-08-07 06:22:56 +00006647}
6648
Balazs Keri3b30d652018-10-19 13:32:20 +00006649ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6650 ExpectedType ToTypeOrErr = import(E->getType());
6651 if (!ToTypeOrErr)
6652 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006653
Balazs Keri3b30d652018-10-19 13:32:20 +00006654 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6655 if (!ToSubExprOrErr)
6656 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006657
Balazs Keri3b30d652018-10-19 13:32:20 +00006658 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6659 if (!ToBasePathOrErr)
6660 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006661
Balazs Keri3b30d652018-10-19 13:32:20 +00006662 return ImplicitCastExpr::Create(
6663 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6664 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006665}
6666
Balazs Keri3b30d652018-10-19 13:32:20 +00006667ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6668 auto Imp1 = importSeq(
6669 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6670 if (!Imp1)
6671 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006672
Balazs Keri3b30d652018-10-19 13:32:20 +00006673 QualType ToType;
6674 Expr *ToSubExpr;
6675 TypeSourceInfo *ToTypeInfoAsWritten;
6676 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006677
Balazs Keri3b30d652018-10-19 13:32:20 +00006678 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6679 if (!ToBasePathOrErr)
6680 return ToBasePathOrErr.takeError();
6681 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006682
Aleksei Sidorina693b372016-09-28 10:16:56 +00006683 switch (E->getStmtClass()) {
6684 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006685 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006686 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6687 if (!ToLParenLocOrErr)
6688 return ToLParenLocOrErr.takeError();
6689 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6690 if (!ToRParenLocOrErr)
6691 return ToRParenLocOrErr.takeError();
6692 return CStyleCastExpr::Create(
6693 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6694 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6695 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006696 }
6697
6698 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006699 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006700 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6701 if (!ToLParenLocOrErr)
6702 return ToLParenLocOrErr.takeError();
6703 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6704 if (!ToRParenLocOrErr)
6705 return ToRParenLocOrErr.takeError();
6706 return CXXFunctionalCastExpr::Create(
6707 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6708 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6709 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006710 }
6711
6712 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006713 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6714 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6715 if (!ToLParenLocOrErr)
6716 return ToLParenLocOrErr.takeError();
6717 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6718 if (!ToBridgeKeywordLocOrErr)
6719 return ToBridgeKeywordLocOrErr.takeError();
6720 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6721 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6722 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006723 }
6724 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006725 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006726 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006727 }
6728}
6729
Balazs Keri3b30d652018-10-19 13:32:20 +00006730ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6731 SmallVector<OffsetOfNode, 4> ToNodes;
6732 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6733 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006734
Balazs Keri3b30d652018-10-19 13:32:20 +00006735 SourceLocation ToBeginLoc, ToEndLoc;
6736 if (FromNode.getKind() != OffsetOfNode::Base) {
6737 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6738 if (!Imp)
6739 return Imp.takeError();
6740 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6741 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006742
Balazs Keri3b30d652018-10-19 13:32:20 +00006743 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006744 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006745 ToNodes.push_back(
6746 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006747 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006748 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006749 auto ToBSOrErr = import(FromNode.getBase());
6750 if (!ToBSOrErr)
6751 return ToBSOrErr.takeError();
6752 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006753 break;
6754 }
6755 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006756 auto ToFieldOrErr = import(FromNode.getField());
6757 if (!ToFieldOrErr)
6758 return ToFieldOrErr.takeError();
6759 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006760 break;
6761 }
6762 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006763 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6764 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006765 break;
6766 }
6767 }
6768 }
6769
Balazs Keri3b30d652018-10-19 13:32:20 +00006770 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6771 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6772 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6773 if (!ToIndexExprOrErr)
6774 return ToIndexExprOrErr.takeError();
6775 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006776 }
6777
Balazs Keri3b30d652018-10-19 13:32:20 +00006778 auto Imp = importSeq(
6779 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6780 E->getRParenLoc());
6781 if (!Imp)
6782 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006783
Balazs Keri3b30d652018-10-19 13:32:20 +00006784 QualType ToType;
6785 TypeSourceInfo *ToTypeSourceInfo;
6786 SourceLocation ToOperatorLoc, ToRParenLoc;
6787 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6788
6789 return OffsetOfExpr::Create(
6790 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6791 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006792}
6793
Balazs Keri3b30d652018-10-19 13:32:20 +00006794ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6795 auto Imp = importSeq(
6796 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6797 if (!Imp)
6798 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006799
Balazs Keri3b30d652018-10-19 13:32:20 +00006800 QualType ToType;
6801 Expr *ToOperand;
6802 SourceLocation ToBeginLoc, ToEndLoc;
6803 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006804
Balazs Keri3b30d652018-10-19 13:32:20 +00006805 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006806 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006807 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006808 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006809 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006810
Balazs Keri3b30d652018-10-19 13:32:20 +00006811 return new (Importer.getToContext()) CXXNoexceptExpr(
6812 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006813}
6814
Balazs Keri3b30d652018-10-19 13:32:20 +00006815ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6816 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6817 if (!Imp)
6818 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006819
Balazs Keri3b30d652018-10-19 13:32:20 +00006820 Expr *ToSubExpr;
6821 QualType ToType;
6822 SourceLocation ToThrowLoc;
6823 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006824
6825 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006826 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006827}
6828
Balazs Keri3b30d652018-10-19 13:32:20 +00006829ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6830 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6831 if (!ToUsedLocOrErr)
6832 return ToUsedLocOrErr.takeError();
6833
6834 auto ToParamOrErr = import(E->getParam());
6835 if (!ToParamOrErr)
6836 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006837
6838 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006839 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006840}
6841
Balazs Keri3b30d652018-10-19 13:32:20 +00006842ExpectedStmt
6843ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6844 auto Imp = importSeq(
6845 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6846 if (!Imp)
6847 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006848
Balazs Keri3b30d652018-10-19 13:32:20 +00006849 QualType ToType;
6850 TypeSourceInfo *ToTypeSourceInfo;
6851 SourceLocation ToRParenLoc;
6852 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006853
6854 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006855 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006856}
6857
Balazs Keri3b30d652018-10-19 13:32:20 +00006858ExpectedStmt
6859ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6860 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6861 if (!ToSubExprOrErr)
6862 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006863
Balazs Keri3b30d652018-10-19 13:32:20 +00006864 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6865 if (!ToDtorOrErr)
6866 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006867
6868 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006869 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6870 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006871}
6872
Balazs Keri3b30d652018-10-19 13:32:20 +00006873ExpectedStmt
6874ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6875 auto Imp = importSeq(
6876 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6877 E->getParenOrBraceRange());
6878 if (!Imp)
6879 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006880
Balazs Keri3b30d652018-10-19 13:32:20 +00006881 CXXConstructorDecl *ToConstructor;
6882 QualType ToType;
6883 TypeSourceInfo *ToTypeSourceInfo;
6884 SourceRange ToParenOrBraceRange;
6885 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006886
Balazs Keri3b30d652018-10-19 13:32:20 +00006887 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6888 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6889 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006890
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006891 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006892 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6893 ToParenOrBraceRange, E->hadMultipleCandidates(),
6894 E->isListInitialization(), E->isStdInitListInitialization(),
6895 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006896}
6897
Balazs Keri3b30d652018-10-19 13:32:20 +00006898ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006899ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006900 auto Imp = importSeq(
6901 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6902 if (!Imp)
6903 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006904
Balazs Keri3b30d652018-10-19 13:32:20 +00006905 QualType ToType;
6906 Expr *ToTemporaryExpr;
6907 const ValueDecl *ToExtendingDecl;
6908 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006909
6910 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006911 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006912
6913 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006914 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006915 return ToMTE;
6916}
6917
Balazs Keri3b30d652018-10-19 13:32:20 +00006918ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6919 auto Imp = importSeq(
6920 E->getType(), E->getPattern(), E->getEllipsisLoc());
6921 if (!Imp)
6922 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006923
Balazs Keri3b30d652018-10-19 13:32:20 +00006924 QualType ToType;
6925 Expr *ToPattern;
6926 SourceLocation ToEllipsisLoc;
6927 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006928
6929 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006930 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006931}
6932
Balazs Keri3b30d652018-10-19 13:32:20 +00006933ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6934 auto Imp = importSeq(
6935 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6936 if (!Imp)
6937 return Imp.takeError();
6938
6939 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6940 NamedDecl *ToPack;
6941 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006942
6943 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006944 if (!E->isValueDependent())
6945 Length = E->getPackLength();
6946
Balazs Keri3b30d652018-10-19 13:32:20 +00006947 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006948 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006949 if (Error Err = ImportTemplateArguments(
6950 E->getPartialArguments().data(),
6951 E->getPartialArguments().size(),
6952 ToPartialArguments))
6953 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006954 }
6955
6956 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006957 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6958 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006959}
6960
Aleksei Sidorina693b372016-09-28 10:16:56 +00006961
Balazs Keri3b30d652018-10-19 13:32:20 +00006962ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6963 auto Imp = importSeq(
6964 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6965 E->getArraySize(), E->getInitializer(), E->getType(),
6966 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6967 E->getDirectInitRange());
6968 if (!Imp)
6969 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006970
Balazs Keri3b30d652018-10-19 13:32:20 +00006971 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6972 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6973 Expr *ToArraySize, *ToInitializer;
6974 QualType ToType;
6975 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6976 std::tie(
6977 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6978 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006979
Balazs Keri3b30d652018-10-19 13:32:20 +00006980 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6981 if (Error Err =
6982 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6983 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006984
6985 return new (Importer.getToContext()) CXXNewExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006986 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6987 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6988 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6989 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6990 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006991}
6992
Balazs Keri3b30d652018-10-19 13:32:20 +00006993ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6994 auto Imp = importSeq(
6995 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6996 if (!Imp)
6997 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006998
Balazs Keri3b30d652018-10-19 13:32:20 +00006999 QualType ToType;
7000 FunctionDecl *ToOperatorDelete;
7001 Expr *ToArgument;
7002 SourceLocation ToBeginLoc;
7003 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007004
7005 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007006 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
7007 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
7008 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00007009}
7010
Balazs Keri3b30d652018-10-19 13:32:20 +00007011ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
7012 auto Imp = importSeq(
7013 E->getType(), E->getLocation(), E->getConstructor(),
7014 E->getParenOrBraceRange());
7015 if (!Imp)
7016 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007017
Balazs Keri3b30d652018-10-19 13:32:20 +00007018 QualType ToType;
7019 SourceLocation ToLocation;
7020 CXXConstructorDecl *ToConstructor;
7021 SourceRange ToParenOrBraceRange;
7022 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007023
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007024 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007025 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7026 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007027
Balazs Keri3b30d652018-10-19 13:32:20 +00007028 return CXXConstructExpr::Create(
7029 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7030 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7031 E->isListInitialization(), E->isStdInitListInitialization(),
7032 E->requiresZeroInitialization(), E->getConstructionKind(),
7033 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007034}
7035
Balazs Keri3b30d652018-10-19 13:32:20 +00007036ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7037 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7038 if (!ToSubExprOrErr)
7039 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007040
Balazs Keri3b30d652018-10-19 13:32:20 +00007041 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7042 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7043 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007044
Balazs Keri3b30d652018-10-19 13:32:20 +00007045 return ExprWithCleanups::Create(
7046 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7047 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007048}
7049
Balazs Keri3b30d652018-10-19 13:32:20 +00007050ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7051 auto Imp = importSeq(
7052 E->getCallee(), E->getType(), E->getRParenLoc());
7053 if (!Imp)
7054 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007055
Balazs Keri3b30d652018-10-19 13:32:20 +00007056 Expr *ToCallee;
7057 QualType ToType;
7058 SourceLocation ToRParenLoc;
7059 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007060
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007061 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007062 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7063 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007064
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007065 return new (Importer.getToContext()) CXXMemberCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007066 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7067 ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007068}
7069
Balazs Keri3b30d652018-10-19 13:32:20 +00007070ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7071 ExpectedType ToTypeOrErr = import(E->getType());
7072 if (!ToTypeOrErr)
7073 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007074
Balazs Keri3b30d652018-10-19 13:32:20 +00007075 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7076 if (!ToLocationOrErr)
7077 return ToLocationOrErr.takeError();
7078
7079 return new (Importer.getToContext()) CXXThisExpr(
7080 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007081}
7082
Balazs Keri3b30d652018-10-19 13:32:20 +00007083ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7084 ExpectedType ToTypeOrErr = import(E->getType());
7085 if (!ToTypeOrErr)
7086 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007087
Balazs Keri3b30d652018-10-19 13:32:20 +00007088 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7089 if (!ToLocationOrErr)
7090 return ToLocationOrErr.takeError();
7091
7092 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7093 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007094}
7095
Balazs Keri3b30d652018-10-19 13:32:20 +00007096ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7097 auto Imp1 = importSeq(
7098 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7099 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7100 if (!Imp1)
7101 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007102
Balazs Keri3b30d652018-10-19 13:32:20 +00007103 Expr *ToBase;
7104 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7105 NestedNameSpecifierLoc ToQualifierLoc;
7106 ValueDecl *ToMemberDecl;
7107 QualType ToType;
7108 std::tie(
7109 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7110 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007111
Balazs Keri3b30d652018-10-19 13:32:20 +00007112 auto Imp2 = importSeq(
7113 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7114 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7115 if (!Imp2)
7116 return Imp2.takeError();
7117 NamedDecl *ToDecl;
7118 DeclarationName ToName;
7119 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7120 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007121
7122 DeclAccessPair ToFoundDecl =
7123 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007124
Balazs Keri3b30d652018-10-19 13:32:20 +00007125 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007126
7127 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007128 // FIXME: handle template arguments
7129 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Sean Callanan59721b32015-04-28 18:41:46 +00007130 }
7131
Balazs Keri3b30d652018-10-19 13:32:20 +00007132 return MemberExpr::Create(
7133 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7134 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
7135 ToMemberNameInfo, nullptr, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007136}
7137
Balazs Keri3b30d652018-10-19 13:32:20 +00007138ExpectedStmt
7139ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7140 auto Imp = importSeq(
7141 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7142 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7143 if (!Imp)
7144 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007145
Balazs Keri3b30d652018-10-19 13:32:20 +00007146 Expr *ToBase;
7147 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7148 NestedNameSpecifierLoc ToQualifierLoc;
7149 TypeSourceInfo *ToScopeTypeInfo;
7150 std::tie(
7151 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7152 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007153
7154 PseudoDestructorTypeStorage Storage;
7155 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7156 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007157 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7158 if (!ToDestroyedTypeLocOrErr)
7159 return ToDestroyedTypeLocOrErr.takeError();
7160 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007161 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007162 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7163 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7164 else
7165 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007166 }
7167
7168 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007169 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7170 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007171}
7172
Balazs Keri3b30d652018-10-19 13:32:20 +00007173ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007174 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007175 auto Imp = importSeq(
7176 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7177 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7178 if (!Imp)
7179 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007180
Balazs Keri3b30d652018-10-19 13:32:20 +00007181 QualType ToType;
7182 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7183 NestedNameSpecifierLoc ToQualifierLoc;
7184 NamedDecl *ToFirstQualifierFoundInScope;
7185 std::tie(
7186 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7187 ToFirstQualifierFoundInScope) = *Imp;
7188
7189 Expr *ToBase = nullptr;
7190 if (!E->isImplicitAccess()) {
7191 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7192 ToBase = *ToBaseOrErr;
7193 else
7194 return ToBaseOrErr.takeError();
7195 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007196
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007197 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007198 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007199 if (Error Err = ImportTemplateArgumentListInfo(
7200 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7201 ToTAInfo))
7202 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007203 ResInfo = &ToTAInfo;
7204 }
7205
Balazs Keri3b30d652018-10-19 13:32:20 +00007206 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7207 if (!ToMemberNameInfoOrErr)
7208 return ToMemberNameInfoOrErr.takeError();
7209 DeclarationNameInfo ToMemberNameInfo(
7210 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007211 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007212 if (Error Err = ImportDeclarationNameLoc(
7213 E->getMemberNameInfo(), ToMemberNameInfo))
7214 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007215
7216 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007217 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7218 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7219 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007220}
7221
Balazs Keri3b30d652018-10-19 13:32:20 +00007222ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007223ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007224 auto Imp = importSeq(
7225 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7226 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7227 if (!Imp)
7228 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007229
Balazs Keri3b30d652018-10-19 13:32:20 +00007230 NestedNameSpecifierLoc ToQualifierLoc;
7231 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7232 DeclarationName ToDeclName;
7233 std::tie(
7234 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7235 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007236
Balazs Keri3b30d652018-10-19 13:32:20 +00007237 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7238 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7239 return std::move(Err);
7240
7241 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007242 TemplateArgumentListInfo *ResInfo = nullptr;
7243 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007244 if (Error Err =
7245 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7246 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007247 ResInfo = &ToTAInfo;
7248 }
7249
7250 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007251 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7252 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007253}
7254
Balazs Keri3b30d652018-10-19 13:32:20 +00007255ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7256 CXXUnresolvedConstructExpr *E) {
7257 auto Imp = importSeq(
7258 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7259 if (!Imp)
7260 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007261
Balazs Keri3b30d652018-10-19 13:32:20 +00007262 SourceLocation ToLParenLoc, ToRParenLoc;
7263 TypeSourceInfo *ToTypeSourceInfo;
7264 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7265
7266 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7267 if (Error Err =
7268 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7269 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007270
7271 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007272 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7273 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007274}
7275
Balazs Keri3b30d652018-10-19 13:32:20 +00007276ExpectedStmt
7277ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7278 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7279 if (!ToNamingClassOrErr)
7280 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007281
Balazs Keri3b30d652018-10-19 13:32:20 +00007282 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7283 if (!ToQualifierLocOrErr)
7284 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007285
Balazs Keri3b30d652018-10-19 13:32:20 +00007286 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7287 if (!ToNameInfoOrErr)
7288 return ToNameInfoOrErr.takeError();
7289 DeclarationNameInfo ToNameInfo(
7290 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007291 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007292 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7293 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007294
7295 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007296 for (auto *D : E->decls())
7297 if (auto ToDOrErr = import(D))
7298 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007299 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007300 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007301
Balazs Keri3b30d652018-10-19 13:32:20 +00007302 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7303 TemplateArgumentListInfo ToTAInfo;
7304 if (Error Err = ImportTemplateArgumentListInfo(
7305 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7306 ToTAInfo))
7307 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007308
Balazs Keri3b30d652018-10-19 13:32:20 +00007309 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7310 if (!ToTemplateKeywordLocOrErr)
7311 return ToTemplateKeywordLocOrErr.takeError();
7312
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007313 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007314 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7315 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7316 ToDecls.begin(), ToDecls.end());
7317 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007318
7319 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007320 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7321 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7322 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007323}
7324
Balazs Keri3b30d652018-10-19 13:32:20 +00007325ExpectedStmt
7326ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7327 auto Imp1 = importSeq(
7328 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7329 E->getTemplateKeywordLoc());
7330 if (!Imp1)
7331 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007332
Balazs Keri3b30d652018-10-19 13:32:20 +00007333 QualType ToType;
7334 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7335 NestedNameSpecifierLoc ToQualifierLoc;
7336 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7337
7338 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7339 if (!Imp2)
7340 return Imp2.takeError();
7341 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7342 // Import additional name location/type info.
7343 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7344 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007345
7346 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007347 for (Decl *D : E->decls())
7348 if (auto ToDOrErr = import(D))
7349 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007350 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007351 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007352
7353 TemplateArgumentListInfo ToTAInfo;
7354 TemplateArgumentListInfo *ResInfo = nullptr;
7355 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007356 if (Error Err =
7357 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7358 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007359 ResInfo = &ToTAInfo;
7360 }
7361
Balazs Keri3b30d652018-10-19 13:32:20 +00007362 Expr *ToBase = nullptr;
7363 if (!E->isImplicitAccess()) {
7364 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7365 ToBase = *ToBaseOrErr;
7366 else
7367 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007368 }
7369
7370 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007371 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7372 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7373 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007374}
7375
Balazs Keri3b30d652018-10-19 13:32:20 +00007376ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7377 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7378 if (!Imp)
7379 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007380
Balazs Keri3b30d652018-10-19 13:32:20 +00007381 Expr *ToCallee;
7382 QualType ToType;
7383 SourceLocation ToRParenLoc;
7384 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007385
7386 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007387 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7388 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7389 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007390
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007391 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
7392 return new (Importer.getToContext()) CXXOperatorCallExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007393 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
7394 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007395 }
7396
Balazs Keri3b30d652018-10-19 13:32:20 +00007397 return new (Importer.getToContext()) CallExpr(
7398 Importer.getToContext(), ToCallee, ToArgs, ToType, E->getValueKind(),
7399 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007400}
7401
Balazs Keri3b30d652018-10-19 13:32:20 +00007402ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7403 CXXRecordDecl *FromClass = E->getLambdaClass();
7404 auto ToClassOrErr = import(FromClass);
7405 if (!ToClassOrErr)
7406 return ToClassOrErr.takeError();
7407 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007408
7409 // NOTE: lambda classes are created with BeingDefined flag set up.
7410 // It means that ImportDefinition doesn't work for them and we should fill it
7411 // manually.
7412 if (ToClass->isBeingDefined()) {
7413 for (auto FromField : FromClass->fields()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007414 auto ToFieldOrErr = import(FromField);
7415 if (!ToFieldOrErr)
7416 return ToFieldOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007417 }
7418 }
7419
Balazs Keri3b30d652018-10-19 13:32:20 +00007420 auto ToCallOpOrErr = import(E->getCallOperator());
7421 if (!ToCallOpOrErr)
7422 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007423
7424 ToClass->completeDefinition();
7425
Balazs Keri3b30d652018-10-19 13:32:20 +00007426 SmallVector<LambdaCapture, 8> ToCaptures;
7427 ToCaptures.reserve(E->capture_size());
7428 for (const auto &FromCapture : E->captures()) {
7429 if (auto ToCaptureOrErr = import(FromCapture))
7430 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007431 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007432 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007433 }
7434
Balazs Keri3b30d652018-10-19 13:32:20 +00007435 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7436 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7437 return std::move(Err);
7438
7439 auto Imp = importSeq(
7440 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7441 if (!Imp)
7442 return Imp.takeError();
7443
7444 SourceRange ToIntroducerRange;
7445 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7446 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007447
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007448 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007449 Importer.getToContext(), ToClass, ToIntroducerRange,
7450 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7451 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7452 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007453}
7454
Sean Callanan8bca9962016-03-28 21:43:01 +00007455
Balazs Keri3b30d652018-10-19 13:32:20 +00007456ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7457 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7458 if (!Imp)
7459 return Imp.takeError();
7460
7461 SourceLocation ToLBraceLoc, ToRBraceLoc;
7462 QualType ToType;
7463 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7464
7465 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7466 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7467 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007468
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007469 ASTContext &ToCtx = Importer.getToContext();
7470 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007471 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7472 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007473
Balazs Keri3b30d652018-10-19 13:32:20 +00007474 if (E->hasArrayFiller()) {
7475 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7476 To->setArrayFiller(*ToFillerOrErr);
7477 else
7478 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007479 }
7480
Balazs Keri3b30d652018-10-19 13:32:20 +00007481 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7482 if (auto ToFDOrErr = import(FromFD))
7483 To->setInitializedFieldInUnion(*ToFDOrErr);
7484 else
7485 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007486 }
7487
Balazs Keri3b30d652018-10-19 13:32:20 +00007488 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7489 if (auto ToSyntFormOrErr = import(SyntForm))
7490 To->setSyntacticForm(*ToSyntFormOrErr);
7491 else
7492 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007493 }
7494
Gabor Martona20ce602018-09-03 13:10:53 +00007495 // Copy InitListExprBitfields, which are not handled in the ctor of
7496 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007497 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007498
7499 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007500}
7501
Balazs Keri3b30d652018-10-19 13:32:20 +00007502ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007503 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007504 ExpectedType ToTypeOrErr = import(E->getType());
7505 if (!ToTypeOrErr)
7506 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007507
Balazs Keri3b30d652018-10-19 13:32:20 +00007508 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7509 if (!ToSubExprOrErr)
7510 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007511
Balazs Keri3b30d652018-10-19 13:32:20 +00007512 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7513 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007514}
7515
Balazs Keri3b30d652018-10-19 13:32:20 +00007516ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007517 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007518 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7519 if (!Imp)
7520 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007521
Balazs Keri3b30d652018-10-19 13:32:20 +00007522 SourceLocation ToLocation;
7523 QualType ToType;
7524 CXXConstructorDecl *ToConstructor;
7525 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007526
7527 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007528 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7529 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007530}
7531
Balazs Keri3b30d652018-10-19 13:32:20 +00007532ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7533 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7534 if (!Imp)
7535 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007536
Balazs Keri3b30d652018-10-19 13:32:20 +00007537 QualType ToType;
7538 Expr *ToCommonExpr, *ToSubExpr;
7539 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007540
Balazs Keri3b30d652018-10-19 13:32:20 +00007541 return new (Importer.getToContext()) ArrayInitLoopExpr(
7542 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007543}
7544
Balazs Keri3b30d652018-10-19 13:32:20 +00007545ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7546 ExpectedType ToTypeOrErr = import(E->getType());
7547 if (!ToTypeOrErr)
7548 return ToTypeOrErr.takeError();
7549 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007550}
7551
Balazs Keri3b30d652018-10-19 13:32:20 +00007552ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7553 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7554 if (!ToBeginLocOrErr)
7555 return ToBeginLocOrErr.takeError();
7556
7557 auto ToFieldOrErr = import(E->getField());
7558 if (!ToFieldOrErr)
7559 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007560
7561 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007562 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007563}
7564
Balazs Keri3b30d652018-10-19 13:32:20 +00007565ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7566 auto Imp = importSeq(
7567 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7568 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7569 if (!Imp)
7570 return Imp.takeError();
7571
7572 QualType ToType;
7573 Expr *ToSubExpr;
7574 TypeSourceInfo *ToTypeInfoAsWritten;
7575 SourceLocation ToOperatorLoc, ToRParenLoc;
7576 SourceRange ToAngleBrackets;
7577 std::tie(
7578 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7579 ToAngleBrackets) = *Imp;
7580
Sean Callanandd2c1742016-05-16 20:48:03 +00007581 ExprValueKind VK = E->getValueKind();
7582 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007583 auto ToBasePathOrErr = ImportCastPath(E);
7584 if (!ToBasePathOrErr)
7585 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007586
Sean Callanandd2c1742016-05-16 20:48:03 +00007587 if (isa<CXXStaticCastExpr>(E)) {
7588 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007589 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7590 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007591 } else if (isa<CXXDynamicCastExpr>(E)) {
7592 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007593 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7594 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007595 } else if (isa<CXXReinterpretCastExpr>(E)) {
7596 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007597 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7598 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007599 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007600 return CXXConstCastExpr::Create(
7601 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7602 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007603 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007604 llvm_unreachable("Unknown cast type");
7605 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007606 }
7607}
7608
Balazs Keri3b30d652018-10-19 13:32:20 +00007609ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007610 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007611 auto Imp = importSeq(
7612 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7613 if (!Imp)
7614 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007615
Balazs Keri3b30d652018-10-19 13:32:20 +00007616 QualType ToType;
7617 SourceLocation ToExprLoc;
7618 NonTypeTemplateParmDecl *ToParameter;
7619 Expr *ToReplacement;
7620 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007621
7622 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007623 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007624}
7625
Balazs Keri3b30d652018-10-19 13:32:20 +00007626ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7627 auto Imp = importSeq(
7628 E->getType(), E->getBeginLoc(), E->getEndLoc());
7629 if (!Imp)
7630 return Imp.takeError();
7631
7632 QualType ToType;
7633 SourceLocation ToBeginLoc, ToEndLoc;
7634 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007635
7636 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007637 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7638 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007639
7640 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7641 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007642 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007643
7644 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007645 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7646 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007647}
7648
Balazs Keri3b30d652018-10-19 13:32:20 +00007649ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7650 ExpectedType ToTypeOrErr = import(E->getType());
7651 if (!ToTypeOrErr)
7652 return ToTypeOrErr.takeError();
7653
7654 auto ToSourceRangeOrErr = import(E->getSourceRange());
7655 if (!ToSourceRangeOrErr)
7656 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007657
7658 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007659 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7660 return new (Importer.getToContext()) CXXTypeidExpr(
7661 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7662 else
7663 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007664 }
7665
Balazs Keri3b30d652018-10-19 13:32:20 +00007666 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7667 if (!ToExprOperandOrErr)
7668 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007669
Balazs Keri3b30d652018-10-19 13:32:20 +00007670 return new (Importer.getToContext()) CXXTypeidExpr(
7671 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007672}
7673
Lang Hames19e07e12017-06-20 21:06:00 +00007674void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7675 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007676 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7677 if (auto ImportedOrErr = import(FromOverriddenMethod))
7678 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7679 (*ImportedOrErr)->getCanonicalDecl()));
7680 else
7681 consumeError(ImportedOrErr.takeError());
7682 }
Lang Hames19e07e12017-06-20 21:06:00 +00007683}
7684
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007685ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007686 ASTContext &FromContext, FileManager &FromFileManager,
7687 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007688 : ToContext(ToContext), FromContext(FromContext),
7689 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7690 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007691 ImportedDecls[FromContext.getTranslationUnitDecl()]
7692 = ToContext.getTranslationUnitDecl();
7693}
7694
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007695ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007696
7697QualType ASTImporter::Import(QualType FromT) {
7698 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007699 return {};
John McCall424cec92011-01-19 06:33:43 +00007700
Balazs Keri3b30d652018-10-19 13:32:20 +00007701 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007702
7703 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007704 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007705 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007706 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007707 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007708
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007709 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007710 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007711 ExpectedType ToTOrErr = Importer.Visit(FromTy);
7712 if (!ToTOrErr) {
7713 llvm::consumeError(ToTOrErr.takeError());
7714 return {};
7715 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007716
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007717 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007718 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007719
Balazs Keri3b30d652018-10-19 13:32:20 +00007720 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007721}
7722
Douglas Gregor62d311f2010-02-09 19:21:46 +00007723TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007724 if (!FromTSI)
7725 return FromTSI;
7726
7727 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007728 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007729 QualType T = Import(FromTSI->getType());
7730 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007731 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007732
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007733 return ToContext.getTrivialTypeSourceInfo(
7734 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007735}
7736
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007737Attr *ASTImporter::Import(const Attr *FromAttr) {
7738 Attr *ToAttr = FromAttr->clone(ToContext);
7739 ToAttr->setRange(Import(FromAttr->getRange()));
7740 return ToAttr;
7741}
7742
Sean Callanan59721b32015-04-28 18:41:46 +00007743Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7744 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7745 if (Pos != ImportedDecls.end()) {
7746 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007747 // FIXME: move this call to ImportDeclParts().
Balazs Keri3b30d652018-10-19 13:32:20 +00007748 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD))
7749 llvm::consumeError(std::move(Err));
Sean Callanan59721b32015-04-28 18:41:46 +00007750 return ToD;
7751 } else {
7752 return nullptr;
7753 }
7754}
7755
Douglas Gregor62d311f2010-02-09 19:21:46 +00007756Decl *ASTImporter::Import(Decl *FromD) {
7757 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007758 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007759
Douglas Gregord451ea92011-07-29 23:31:30 +00007760 ASTNodeImporter Importer(*this);
7761
Gabor Marton26f72a92018-07-12 09:42:05 +00007762 // Check whether we've already imported this declaration.
7763 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7764 if (ToD) {
7765 // If FromD has some updated flags after last import, apply it
7766 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007767 return ToD;
7768 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007769
7770 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007771 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
7772 if (!ToDOrErr) {
7773 llvm::consumeError(ToDOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007774 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007775 }
7776 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007777
Gabor Marton26f72a92018-07-12 09:42:05 +00007778 // Notify subclasses.
7779 Imported(FromD, ToD);
7780
Gabor Martonac3a5d62018-09-17 12:04:52 +00007781 updateFlags(FromD, ToD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007782 return ToD;
7783}
7784
Balazs Keri3b30d652018-10-19 13:32:20 +00007785Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007786 if (!FromDC)
7787 return FromDC;
7788
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007789 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007790 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007791 return nullptr;
7792
Fangrui Song6907ce22018-07-30 19:24:48 +00007793 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007794 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007795 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7796 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007797 if (ToRecord->isCompleteDefinition()) {
7798 // Do nothing.
7799 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007800 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7801 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7802 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007803 } else {
7804 CompleteDecl(ToRecord);
7805 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007806 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7807 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007808 if (ToEnum->isCompleteDefinition()) {
7809 // Do nothing.
7810 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007811 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7812 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7813 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007814 } else {
7815 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007816 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007817 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7818 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007819 if (ToClass->getDefinition()) {
7820 // Do nothing.
7821 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007822 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7823 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7824 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007825 } else {
7826 CompleteDecl(ToClass);
7827 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007828 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7829 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007830 if (ToProto->getDefinition()) {
7831 // Do nothing.
7832 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007833 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7834 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7835 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007836 } else {
7837 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007838 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007839 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007840
Douglas Gregor95d82832012-01-24 18:36:04 +00007841 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007842}
7843
7844Expr *ASTImporter::Import(Expr *FromE) {
7845 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007846 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007847
7848 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7849}
7850
7851Stmt *ASTImporter::Import(Stmt *FromS) {
7852 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007853 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007854
Fangrui Song6907ce22018-07-30 19:24:48 +00007855 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007856 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7857 if (Pos != ImportedStmts.end())
7858 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007859
Balazs Keri3b30d652018-10-19 13:32:20 +00007860 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007861 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007862 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
7863 if (!ToSOrErr) {
7864 llvm::consumeError(ToSOrErr.takeError());
Craig Topper36250ad2014-05-12 05:36:57 +00007865 return nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007866 }
Craig Topper36250ad2014-05-12 05:36:57 +00007867
Balazs Keri3b30d652018-10-19 13:32:20 +00007868 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007869 auto *FromE = cast<Expr>(FromS);
7870 // Copy ExprBitfields, which may not be handled in Expr subclasses
7871 // constructors.
7872 ToE->setValueKind(FromE->getValueKind());
7873 ToE->setObjectKind(FromE->getObjectKind());
7874 ToE->setTypeDependent(FromE->isTypeDependent());
7875 ToE->setValueDependent(FromE->isValueDependent());
7876 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7877 ToE->setContainsUnexpandedParameterPack(
7878 FromE->containsUnexpandedParameterPack());
7879 }
7880
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007881 // Record the imported declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007882 ImportedStmts[FromS] = *ToSOrErr;
7883 return *ToSOrErr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007884}
7885
7886NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7887 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007888 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007889
Douglas Gregor90ebf252011-04-27 16:48:40 +00007890 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7891
7892 switch (FromNNS->getKind()) {
7893 case NestedNameSpecifier::Identifier:
7894 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7895 return NestedNameSpecifier::Create(ToContext, prefix, II);
7896 }
Craig Topper36250ad2014-05-12 05:36:57 +00007897 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007898
7899 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007900 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007901 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007902 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7903 }
Craig Topper36250ad2014-05-12 05:36:57 +00007904 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007905
7906 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007907 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007908 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007909 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7910 }
Craig Topper36250ad2014-05-12 05:36:57 +00007911 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007912
7913 case NestedNameSpecifier::Global:
7914 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7915
Nikola Smiljanic67860242014-09-26 00:28:20 +00007916 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007917 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007918 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007919 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7920 }
7921 return nullptr;
7922
Douglas Gregor90ebf252011-04-27 16:48:40 +00007923 case NestedNameSpecifier::TypeSpec:
7924 case NestedNameSpecifier::TypeSpecWithTemplate: {
7925 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7926 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007927 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007928 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007929 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007930 bTemplate, T.getTypePtr());
7931 }
7932 }
Craig Topper36250ad2014-05-12 05:36:57 +00007933 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007934 }
7935
7936 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007937}
7938
Douglas Gregor14454802011-02-25 02:25:35 +00007939NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007940 // Copied from NestedNameSpecifier mostly.
7941 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7942 NestedNameSpecifierLoc NNS = FromNNS;
7943
7944 // Push each of the nested-name-specifiers's onto a stack for
7945 // serialization in reverse order.
7946 while (NNS) {
7947 NestedNames.push_back(NNS);
7948 NNS = NNS.getPrefix();
7949 }
7950
7951 NestedNameSpecifierLocBuilder Builder;
7952
7953 while (!NestedNames.empty()) {
7954 NNS = NestedNames.pop_back_val();
7955 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7956 if (!Spec)
7957 return NestedNameSpecifierLoc();
7958
7959 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7960 switch (Kind) {
7961 case NestedNameSpecifier::Identifier:
7962 Builder.Extend(getToContext(),
7963 Spec->getAsIdentifier(),
7964 Import(NNS.getLocalBeginLoc()),
7965 Import(NNS.getLocalEndLoc()));
7966 break;
7967
7968 case NestedNameSpecifier::Namespace:
7969 Builder.Extend(getToContext(),
7970 Spec->getAsNamespace(),
7971 Import(NNS.getLocalBeginLoc()),
7972 Import(NNS.getLocalEndLoc()));
7973 break;
7974
7975 case NestedNameSpecifier::NamespaceAlias:
7976 Builder.Extend(getToContext(),
7977 Spec->getAsNamespaceAlias(),
7978 Import(NNS.getLocalBeginLoc()),
7979 Import(NNS.getLocalEndLoc()));
7980 break;
7981
7982 case NestedNameSpecifier::TypeSpec:
7983 case NestedNameSpecifier::TypeSpecWithTemplate: {
7984 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7985 QualType(Spec->getAsType(), 0));
7986 Builder.Extend(getToContext(),
7987 Import(NNS.getLocalBeginLoc()),
7988 TSI->getTypeLoc(),
7989 Import(NNS.getLocalEndLoc()));
7990 break;
7991 }
7992
7993 case NestedNameSpecifier::Global:
7994 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7995 break;
7996
7997 case NestedNameSpecifier::Super: {
7998 SourceRange ToRange = Import(NNS.getSourceRange());
7999 Builder.MakeSuper(getToContext(),
8000 Spec->getAsRecordDecl(),
8001 ToRange.getBegin(),
8002 ToRange.getEnd());
8003 }
8004 }
8005 }
8006
8007 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008008}
8009
Douglas Gregore2e50d332010-12-01 01:36:18 +00008010TemplateName ASTImporter::Import(TemplateName From) {
8011 switch (From.getKind()) {
8012 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008013 if (auto *ToTemplate =
8014 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00008015 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00008016
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008017 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008018
Douglas Gregore2e50d332010-12-01 01:36:18 +00008019 case TemplateName::OverloadedTemplate: {
8020 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8021 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008022 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008023 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00008024 ToTemplates.addDecl(To);
8025 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008026 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008027 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008028 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008029 ToTemplates.end());
8030 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008031
Douglas Gregore2e50d332010-12-01 01:36:18 +00008032 case TemplateName::QualifiedTemplate: {
8033 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
8034 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
8035 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008036 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008037
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008038 if (auto *ToTemplate =
8039 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00008040 return ToContext.getQualifiedTemplateName(Qualifier,
8041 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008042 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008043
8044 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00008045 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008046
Douglas Gregore2e50d332010-12-01 01:36:18 +00008047 case TemplateName::DependentTemplate: {
8048 DependentTemplateName *DTN = From.getAsDependentTemplateName();
8049 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
8050 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008051 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008052
Douglas Gregore2e50d332010-12-01 01:36:18 +00008053 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00008054 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008055 Import(DTN->getIdentifier()));
8056 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008057
Douglas Gregore2e50d332010-12-01 01:36:18 +00008058 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
8059 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008060
8061 case TemplateName::SubstTemplateTemplateParm: {
8062 SubstTemplateTemplateParmStorage *subst
8063 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008064 auto *param =
8065 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00008066 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008067 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00008068
8069 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008070 if (replacement.isNull())
8071 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008072
John McCalld9dfe3a2011-06-30 08:33:18 +00008073 return ToContext.getSubstTemplateTemplateParm(param, replacement);
8074 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008075
Douglas Gregor5590be02011-01-15 06:45:20 +00008076 case TemplateName::SubstTemplateTemplateParmPack: {
8077 SubstTemplateTemplateParmPackStorage *SubstPack
8078 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008079 auto *Param =
8080 cast_or_null<TemplateTemplateParmDecl>(
8081 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00008082 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008083 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00008084
Douglas Gregor5590be02011-01-15 06:45:20 +00008085 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008086 Expected<TemplateArgument> ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00008087 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
Balazs Keri3b30d652018-10-19 13:32:20 +00008088 if (!ArgPack) {
8089 llvm::consumeError(ArgPack.takeError());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008090 return {};
Balazs Keri3b30d652018-10-19 13:32:20 +00008091 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008092
Balazs Keri3b30d652018-10-19 13:32:20 +00008093 return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00008094 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008095 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008096
Douglas Gregore2e50d332010-12-01 01:36:18 +00008097 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008098}
8099
Douglas Gregor62d311f2010-02-09 19:21:46 +00008100SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
8101 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008102 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008103
Douglas Gregor811663e2010-02-10 00:15:17 +00008104 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008105
Douglas Gregor811663e2010-02-10 00:15:17 +00008106 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00008107 FileID ToFileID = Import(Decomposed.first);
8108 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008109 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008110 SourceManager &ToSM = ToContext.getSourceManager();
8111 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00008112}
8113
8114SourceRange ASTImporter::Import(SourceRange FromRange) {
8115 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
8116}
8117
Douglas Gregor811663e2010-02-10 00:15:17 +00008118FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008119 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008120 if (Pos != ImportedFileIDs.end())
8121 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008122
Douglas Gregor811663e2010-02-10 00:15:17 +00008123 SourceManager &FromSM = FromContext.getSourceManager();
8124 SourceManager &ToSM = ToContext.getSourceManager();
8125 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008126
8127 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008128 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008129 if (FromSLoc.isExpansion()) {
8130 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
8131 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
8132 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
8133 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8134 SourceLocation MLoc;
8135 if (FromEx.isMacroArgExpansion()) {
8136 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
8137 } else {
8138 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
8139 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
8140 FromEx.isExpansionTokenRange());
8141 }
8142 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008143 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008144 // Include location of this file.
8145 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
8146
8147 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
8148 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8149 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8150 // disk again
8151 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8152 // than mmap the files several times.
8153 const FileEntry *Entry =
8154 ToFileManager.getFile(Cache->OrigEntry->getName());
8155 if (!Entry)
8156 return {};
8157 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
8158 FromSLoc.getFile().getFileCharacteristic());
8159 } else {
8160 // FIXME: We want to re-use the existing MemoryBuffer!
8161 const llvm::MemoryBuffer *FromBuf =
8162 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
8163 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8164 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8165 FromBuf->getBufferIdentifier());
8166 ToID = ToSM.createFileID(std::move(ToBuf),
8167 FromSLoc.getFile().getFileCharacteristic());
8168 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008169 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008170
Sebastian Redl99219f12010-09-30 01:03:06 +00008171 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008172 return ToID;
8173}
8174
Sean Callanandd2c1742016-05-16 20:48:03 +00008175CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8176 Expr *ToExpr = Import(From->getInit());
8177 if (!ToExpr && From->getInit())
8178 return nullptr;
8179
8180 if (From->isBaseInitializer()) {
8181 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8182 if (!ToTInfo && From->getTypeSourceInfo())
8183 return nullptr;
8184
8185 return new (ToContext) CXXCtorInitializer(
8186 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
8187 ToExpr, Import(From->getRParenLoc()),
8188 From->isPackExpansion() ? Import(From->getEllipsisLoc())
8189 : SourceLocation());
8190 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008191 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008192 if (!ToField && From->getMember())
8193 return nullptr;
8194
8195 return new (ToContext) CXXCtorInitializer(
8196 ToContext, ToField, Import(From->getMemberLocation()),
8197 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8198 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008199 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00008200 Import(From->getIndirectMember()));
8201 if (!ToIField && From->getIndirectMember())
8202 return nullptr;
8203
8204 return new (ToContext) CXXCtorInitializer(
8205 ToContext, ToIField, Import(From->getMemberLocation()),
8206 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
8207 } else if (From->isDelegatingInitializer()) {
8208 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
8209 if (!ToTInfo && From->getTypeSourceInfo())
8210 return nullptr;
8211
8212 return new (ToContext)
8213 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
8214 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00008215 } else {
8216 return nullptr;
8217 }
8218}
8219
Aleksei Sidorina693b372016-09-28 10:16:56 +00008220CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
8221 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8222 if (Pos != ImportedCXXBaseSpecifiers.end())
8223 return Pos->second;
8224
8225 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
8226 Import(BaseSpec->getSourceRange()),
8227 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8228 BaseSpec->getAccessSpecifierAsWritten(),
8229 Import(BaseSpec->getTypeSourceInfo()),
8230 Import(BaseSpec->getEllipsisLoc()));
8231 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8232 return Imported;
8233}
8234
Balazs Keri3b30d652018-10-19 13:32:20 +00008235Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008236 Decl *To = Import(From);
8237 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008238 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008239
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008240 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008241 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008242
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008243 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00008244 if (!ToRecord->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008245 return Importer.ImportDefinition(
8246 cast<RecordDecl>(FromDC), ToRecord,
8247 ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008248 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00008249 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008250
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008251 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00008252 if (!ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008253 return Importer.ImportDefinition(
8254 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
Fangrui Song6907ce22018-07-30 19:24:48 +00008255 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008256 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008257
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008258 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008259 if (!ToIFace->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008260 return Importer.ImportDefinition(
8261 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8262 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008263 }
8264 }
Douglas Gregord451ea92011-07-29 23:31:30 +00008265
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008266 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00008267 if (!ToProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008268 return Importer.ImportDefinition(
8269 cast<ObjCProtocolDecl>(FromDC), ToProto,
8270 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00008271 }
8272 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008273
Balazs Keri3b30d652018-10-19 13:32:20 +00008274 return Importer.ImportDeclContext(FromDC, true);
Douglas Gregor0a791672011-01-18 03:11:38 +00008275 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008276
8277 return Error::success();
8278}
8279
8280void ASTImporter::ImportDefinition(Decl *From) {
8281 Error Err = ImportDefinition_New(From);
8282 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008283}
8284
Douglas Gregor96e578d2010-02-05 17:54:41 +00008285DeclarationName ASTImporter::Import(DeclarationName FromName) {
8286 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008287 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008288
8289 switch (FromName.getNameKind()) {
8290 case DeclarationName::Identifier:
8291 return Import(FromName.getAsIdentifierInfo());
8292
8293 case DeclarationName::ObjCZeroArgSelector:
8294 case DeclarationName::ObjCOneArgSelector:
8295 case DeclarationName::ObjCMultiArgSelector:
8296 return Import(FromName.getObjCSelector());
8297
8298 case DeclarationName::CXXConstructorName: {
8299 QualType T = Import(FromName.getCXXNameType());
8300 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008301 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008302
8303 return ToContext.DeclarationNames.getCXXConstructorName(
8304 ToContext.getCanonicalType(T));
8305 }
8306
8307 case DeclarationName::CXXDestructorName: {
8308 QualType T = Import(FromName.getCXXNameType());
8309 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008310 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008311
8312 return ToContext.DeclarationNames.getCXXDestructorName(
8313 ToContext.getCanonicalType(T));
8314 }
8315
Richard Smith35845152017-02-07 01:37:30 +00008316 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008317 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00008318 Import(FromName.getCXXDeductionGuideTemplate()));
8319 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008320 return {};
Richard Smith35845152017-02-07 01:37:30 +00008321 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
8322 }
8323
Douglas Gregor96e578d2010-02-05 17:54:41 +00008324 case DeclarationName::CXXConversionFunctionName: {
8325 QualType T = Import(FromName.getCXXNameType());
8326 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008327 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008328
8329 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8330 ToContext.getCanonicalType(T));
8331 }
8332
8333 case DeclarationName::CXXOperatorName:
8334 return ToContext.DeclarationNames.getCXXOperatorName(
8335 FromName.getCXXOverloadedOperator());
8336
8337 case DeclarationName::CXXLiteralOperatorName:
8338 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
8339 Import(FromName.getCXXLiteralIdentifier()));
8340
8341 case DeclarationName::CXXUsingDirective:
8342 // FIXME: STATICS!
8343 return DeclarationName::getUsingDirectiveName();
8344 }
8345
David Blaikiee4d798f2012-01-20 21:50:17 +00008346 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008347}
8348
Douglas Gregore2e50d332010-12-01 01:36:18 +00008349IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008350 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008351 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008352
Sean Callananf94ef1d2016-05-14 06:11:19 +00008353 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8354
8355 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8356 ToId->setBuiltinID(FromId->getBuiltinID());
8357
8358 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008359}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008360
Douglas Gregor43f54792010-02-17 02:12:47 +00008361Selector ASTImporter::Import(Selector FromSel) {
8362 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008363 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00008364
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008365 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008366 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8367 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8368 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8369 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8370}
8371
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008372DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8373 DeclContext *DC,
8374 unsigned IDNS,
8375 NamedDecl **Decls,
8376 unsigned NumDecls) {
8377 return Name;
8378}
8379
8380DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008381 if (LastDiagFromFrom)
8382 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8383 FromContext.getDiagnostics());
8384 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008385 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008386}
8387
8388DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008389 if (!LastDiagFromFrom)
8390 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8391 ToContext.getDiagnostics());
8392 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008393 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008394}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008395
Douglas Gregor2e15c842012-02-01 21:00:38 +00008396void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008397 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008398 if (!ID->getDefinition())
8399 ID->startDefinition();
8400 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008401 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008402 if (!PD->getDefinition())
8403 PD->startDefinition();
8404 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008405 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008406 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8407 TD->startDefinition();
8408 TD->setCompleteDefinition(true);
8409 }
8410 }
8411 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008412 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008413 }
8414}
8415
Gabor Marton26f72a92018-07-12 09:42:05 +00008416Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8417 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8418 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8419 "Try to import an already imported Decl");
8420 if (Pos != ImportedDecls.end())
8421 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008422 ImportedDecls[From] = To;
8423 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008424}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008425
Douglas Gregordd6006f2012-07-17 21:16:27 +00008426bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8427 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00008428 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00008429 = ImportedTypes.find(From.getTypePtr());
8430 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8431 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008432
Douglas Gregordd6006f2012-07-17 21:16:27 +00008433 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008434 getStructuralEquivalenceKind(*this), false,
8435 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008436 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008437}