blob: 38ad4e28931427d1e0044bccf0d9c5cd1aab9ffb [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
Gabor Marton5254e642018-06-27 13:32:50 +000074 template <class T>
75 SmallVector<Decl*, 2>
76 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
77 SmallVector<Decl*, 2> Redecls;
78 for (auto *R : D->getFirstDecl()->redecls()) {
79 if (R != D->getFirstDecl())
80 Redecls.push_back(R);
81 }
82 Redecls.push_back(D->getFirstDecl());
83 std::reverse(Redecls.begin(), Redecls.end());
84 return Redecls;
85 }
86
87 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
88 // Currently only FunctionDecl is supported
89 auto FD = cast<FunctionDecl>(D);
90 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
91 }
92
Gabor Marton26f72a92018-07-12 09:42:05 +000093 void updateFlags(const Decl *From, Decl *To) {
94 // Check if some flags or attrs are new in 'From' and copy into 'To'.
95 // FIXME: Other flags or attrs?
96 if (From->isUsed(false) && !To->isUsed(false))
97 To->setIsUsed();
98 }
99
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000100 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000101 public DeclVisitor<ASTNodeImporter, Decl *>,
102 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000103 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000104
Gabor Marton26f72a92018-07-12 09:42:05 +0000105 // Wrapper for an overload set.
106 template <typename ToDeclT> struct CallOverloadedCreateFun {
107 template <typename... Args>
108 auto operator()(Args &&... args)
109 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
110 return ToDeclT::Create(std::forward<Args>(args)...);
111 }
112 };
113
114 // Always use these functions to create a Decl during import. There are
115 // certain tasks which must be done after the Decl was created, e.g. we
116 // must immediately register that as an imported Decl. The parameter `ToD`
117 // will be set to the newly created Decl or if had been imported before
118 // then to the already imported Decl. Returns a bool value set to true if
119 // the `FromD` had been imported before.
120 template <typename ToDeclT, typename FromDeclT, typename... Args>
121 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
122 Args &&... args) {
123 // There may be several overloads of ToDeclT::Create. We must make sure
124 // to call the one which would be chosen by the arguments, thus we use a
125 // wrapper for the overload set.
126 CallOverloadedCreateFun<ToDeclT> OC;
127 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
128 std::forward<Args>(args)...);
129 }
130 // Use this overload if a special Type is needed to be created. E.g if we
131 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
132 // then:
133 // TypedefNameDecl *ToTypedef;
134 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
135 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
136 typename... Args>
137 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
138 Args &&... args) {
139 CallOverloadedCreateFun<NewDeclT> OC;
140 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
141 std::forward<Args>(args)...);
142 }
143 // Use this version if a special create function must be
144 // used, e.g. CXXRecordDecl::CreateLambda .
145 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
146 typename... Args>
147 LLVM_NODISCARD bool
148 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
149 FromDeclT *FromD, Args &&... args) {
150 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
151 if (ToD)
152 return true; // Already imported.
153 ToD = CreateFun(std::forward<Args>(args)...);
154 InitializeImportedDecl(FromD, ToD);
155 return false; // A new Decl is created.
156 }
157
158 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
159 Importer.MapImported(FromD, ToD);
160 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
161 if (FromD->hasAttrs())
162 for (const Attr *FromAttr : FromD->getAttrs())
163 ToD->addAttr(Importer.Import(FromAttr));
164 if (FromD->isUsed())
165 ToD->setIsUsed();
166 if (FromD->isImplicit())
167 ToD->setImplicit();
168 }
169
Douglas Gregor96e578d2010-02-05 17:54:41 +0000170 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000171 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000172
Douglas Gregor96e578d2010-02-05 17:54:41 +0000173 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +0000174 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000175 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000176
177 // Importing types
John McCall424cec92011-01-19 06:33:43 +0000178 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000179 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +0000180 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000181 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +0000182 QualType VisitComplexType(const ComplexType *T);
183 QualType VisitPointerType(const PointerType *T);
184 QualType VisitBlockPointerType(const BlockPointerType *T);
185 QualType VisitLValueReferenceType(const LValueReferenceType *T);
186 QualType VisitRValueReferenceType(const RValueReferenceType *T);
187 QualType VisitMemberPointerType(const MemberPointerType *T);
188 QualType VisitConstantArrayType(const ConstantArrayType *T);
189 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
190 QualType VisitVariableArrayType(const VariableArrayType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000191 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000192 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +0000193 QualType VisitVectorType(const VectorType *T);
194 QualType VisitExtVectorType(const ExtVectorType *T);
195 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
196 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000197 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +0000198 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +0000199 QualType VisitTypedefType(const TypedefType *T);
200 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000201 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +0000202 QualType VisitTypeOfType(const TypeOfType *T);
203 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +0000204 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +0000205 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000206 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000207 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +0000208 QualType VisitRecordType(const RecordType *T);
209 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +0000210 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000211 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000212 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +0000213 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
214 QualType VisitElaboratedType(const ElaboratedType *T);
Peter Szecsice7f3182018-05-07 12:08:27 +0000215 QualType VisitDependentNameType(const DependentNameType *T);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000216 QualType VisitPackExpansionType(const PackExpansionType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000217 QualType VisitDependentTemplateSpecializationType(
218 const DependentTemplateSpecializationType *T);
John McCall424cec92011-01-19 06:33:43 +0000219 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
220 QualType VisitObjCObjectType(const ObjCObjectType *T);
221 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000222
223 // Importing declarations
Fangrui Song6907ce22018-07-30 19:24:48 +0000224 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
225 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000226 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +0000227 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000228 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
229 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +0000230 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Balazs Keri1d20cc22018-07-16 12:16:39 +0000231 void ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000232
Aleksei Sidorina693b372016-09-28 10:16:56 +0000233 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
234
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000235 using Designator = DesignatedInitExpr::Designator;
236
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000237 Designator ImportDesignator(const Designator &D);
238
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000239 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
Fangrui Song6907ce22018-07-30 19:24:48 +0000240
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000241 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000242 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000243 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000244 /// nothing (if minimal import is set) or might be everything (if minimal
245 /// import is not set).
246 IDK_Default,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000247
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000248 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000249 IDK_Everything,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000251 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000252 /// DeclContext.
253 IDK_Basic
254 };
255
Douglas Gregor2e15c842012-02-01 21:00:38 +0000256 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
257 return IDK == IDK_Everything ||
258 (IDK == IDK_Default && !Importer.isMinimalImport());
259 }
260
Fangrui Song6907ce22018-07-30 19:24:48 +0000261 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000262 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000263 bool ImportDefinition(VarDecl *From, VarDecl *To,
264 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000265 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000266 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000267 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000268 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000269 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000270 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000271 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000272 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000273 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000274 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
275 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000276 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
277 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000278 SmallVectorImpl<TemplateArgument> &ToArgs);
279
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000280 template <typename InContainerTy>
281 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
282 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000283
284 template<typename InContainerTy>
285 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
286 SourceLocation FromRAngleLoc,
287 const InContainerTy &Container,
288 TemplateArgumentListInfo &Result);
289
Gabor Marton5254e642018-06-27 13:32:50 +0000290 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
291 using OptionalTemplateArgsTy = Optional<TemplateArgsTy>;
292 std::tuple<FunctionTemplateDecl *, OptionalTemplateArgsTy>
293 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
294 FunctionDecl *FromFD);
295
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000296 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
297
Gabor Marton950fb572018-07-17 12:39:27 +0000298 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000299 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
300 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000301 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
302 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000303 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000304 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000305 bool IsStructuralMatch(FunctionTemplateDecl *From,
306 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000307 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000308 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000309 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000310 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000311 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000312 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000313 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000314 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000315 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000316 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000317 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000318 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000319 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000320 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000321 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000322 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000323 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000324 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000325 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000326 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
327 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
328 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
329 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000330 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000331 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000332 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000333 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000334 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000335 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000336 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000337 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000338 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000339 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000340 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000341 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000342 Decl *VisitUsingDecl(UsingDecl *D);
343 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
344 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
345 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
346 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
347
Douglas Gregor85f3f952015-07-07 03:57:15 +0000348 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000349 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000350 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000351 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000352 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000353 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000354 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
355 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
356 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
357 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000358 Decl *VisitClassTemplateSpecializationDecl(
359 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000360 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
361 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000362 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000363
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000364 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000365 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
366
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000367 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000368 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000369 Stmt *VisitDeclStmt(DeclStmt *S);
370 Stmt *VisitNullStmt(NullStmt *S);
371 Stmt *VisitCompoundStmt(CompoundStmt *S);
372 Stmt *VisitCaseStmt(CaseStmt *S);
373 Stmt *VisitDefaultStmt(DefaultStmt *S);
374 Stmt *VisitLabelStmt(LabelStmt *S);
375 Stmt *VisitAttributedStmt(AttributedStmt *S);
376 Stmt *VisitIfStmt(IfStmt *S);
377 Stmt *VisitSwitchStmt(SwitchStmt *S);
378 Stmt *VisitWhileStmt(WhileStmt *S);
379 Stmt *VisitDoStmt(DoStmt *S);
380 Stmt *VisitForStmt(ForStmt *S);
381 Stmt *VisitGotoStmt(GotoStmt *S);
382 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
383 Stmt *VisitContinueStmt(ContinueStmt *S);
384 Stmt *VisitBreakStmt(BreakStmt *S);
385 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000386 // FIXME: MSAsmStmt
387 // FIXME: SEHExceptStmt
388 // FIXME: SEHFinallyStmt
389 // FIXME: SEHTryStmt
390 // FIXME: SEHLeaveStmt
391 // FIXME: CapturedStmt
392 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
393 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
394 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
395 // FIXME: MSDependentExistsStmt
396 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
397 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
398 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
399 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
400 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
401 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
402 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000403
404 // Importing expressions
405 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000406 Expr *VisitVAArgExpr(VAArgExpr *E);
407 Expr *VisitGNUNullExpr(GNUNullExpr *E);
408 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000409 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000410 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
411 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
412 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000413 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000414 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000415 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000416 Expr *VisitStringLiteral(StringLiteral *E);
417 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
418 Expr *VisitAtomicExpr(AtomicExpr *E);
419 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000420 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000421 Expr *VisitParenListExpr(ParenListExpr *E);
422 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000423 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000424 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000425 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000426 Expr *VisitConditionalOperator(ConditionalOperator *E);
427 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
428 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000429 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
430 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
431 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000432 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000433 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000434 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
435 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
436 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
437 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
438 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
439 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
440 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
441 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
442 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000443 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000444 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000445 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
446 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000447 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000448 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000449 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000450 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000451 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
452 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000453 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000454 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000455 Expr *VisitCXXThisExpr(CXXThisExpr *E);
456 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000457 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000458 Expr *VisitMemberExpr(MemberExpr *E);
459 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000460 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000461 Expr *VisitInitListExpr(InitListExpr *E);
Gabor Marton07b01ff2018-06-29 12:17:34 +0000462 Expr *VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
Balazs Keri95baa842018-07-25 10:21:06 +0000463 Expr *VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000464 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
465 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000466 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
467 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000468 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000469 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000470 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000471
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000472 template<typename IIter, typename OIter>
473 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000474 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
475
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000476 ASTImporter &ImporterRef = Importer;
477 std::transform(Ibegin, Iend, Obegin,
478 [&ImporterRef](ItemT From) -> ItemT {
479 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000480 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000481 }
482
483 template<typename IIter, typename OIter>
484 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000485 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
486
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000487 ASTImporter &ImporterRef = Importer;
488 bool Failed = false;
489 std::transform(Ibegin, Iend, Obegin,
490 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000491 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000492 if (!To && From)
493 Failed = true;
494 return To;
495 });
496 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000497 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000498
499 template<typename InContainerTy, typename OutContainerTy>
500 bool ImportContainerChecked(const InContainerTy &InContainer,
501 OutContainerTy &OutContainer) {
502 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
503 OutContainer.begin());
504 }
505
506 template<typename InContainerTy, typename OIter>
507 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
508 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
509 }
Lang Hames19e07e12017-06-20 21:06:00 +0000510
511 // Importing overrides.
512 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000513
514 FunctionDecl *FindFunctionTemplateSpecialization(FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000515 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000516
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000517template <typename InContainerTy>
518bool ASTNodeImporter::ImportTemplateArgumentListInfo(
519 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
520 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
521 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
522 Importer.Import(FromRAngleLoc));
523 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
524 return true;
525 Result = ToTAInfo;
526 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000527}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000528
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000529template <>
530bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
531 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
532 return ImportTemplateArgumentListInfo(
533 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
534}
535
536template <>
537bool ASTNodeImporter::ImportTemplateArgumentListInfo<
538 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
539 TemplateArgumentListInfo &Result) {
540 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
541 From.arguments(), Result);
542}
543
Gabor Marton5254e642018-06-27 13:32:50 +0000544std::tuple<FunctionTemplateDecl *, ASTNodeImporter::OptionalTemplateArgsTy>
545ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
546 FunctionDecl *FromFD) {
547 assert(FromFD->getTemplatedKind() ==
548 FunctionDecl::TK_FunctionTemplateSpecialization);
549 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
550 auto *Template = cast_or_null<FunctionTemplateDecl>(
551 Importer.Import(FTSInfo->getTemplate()));
552
553 // Import template arguments.
554 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
555 TemplateArgsTy ToTemplArgs;
556 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
557 ToTemplArgs)) // Error during import.
558 return std::make_tuple(Template, OptionalTemplateArgsTy());
559
560 return std::make_tuple(Template, ToTemplArgs);
561}
562
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000563} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000564
Douglas Gregor3996e242010-02-15 22:01:00 +0000565//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000566// Import Types
567//----------------------------------------------------------------------------
568
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000569using namespace clang;
570
John McCall424cec92011-01-19 06:33:43 +0000571QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000572 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
573 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000574 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000575}
576
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000577QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
578 QualType UnderlyingType = Importer.Import(T->getValueType());
579 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000580 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000581
582 return Importer.getToContext().getAtomicType(UnderlyingType);
583}
584
John McCall424cec92011-01-19 06:33:43 +0000585QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000586 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000587#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
588 case BuiltinType::Id: \
589 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000590#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000591#define SHARED_SINGLETON_TYPE(Expansion)
592#define BUILTIN_TYPE(Id, SingletonId) \
593 case BuiltinType::Id: return Importer.getToContext().SingletonId;
594#include "clang/AST/BuiltinTypes.def"
595
596 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
597 // context supports C++.
598
599 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
600 // context supports ObjC.
601
Douglas Gregor96e578d2010-02-05 17:54:41 +0000602 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000603 // The context we're importing from has an unsigned 'char'. If we're
604 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000605 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000606 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000607 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000608
Douglas Gregor96e578d2010-02-05 17:54:41 +0000609 return Importer.getToContext().CharTy;
610
Douglas Gregor96e578d2010-02-05 17:54:41 +0000611 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000612 // The context we're importing from has an unsigned 'char'. If we're
613 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000614 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000615 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000616 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000617
Douglas Gregor96e578d2010-02-05 17:54:41 +0000618 return Importer.getToContext().CharTy;
619
Chris Lattnerad3467e2010-12-25 23:25:43 +0000620 case BuiltinType::WChar_S:
621 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000622 // FIXME: If not in C++, shall we translate to the C equivalent of
623 // wchar_t?
624 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000625 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000626
627 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000628}
629
Aleksei Sidorina693b372016-09-28 10:16:56 +0000630QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
631 QualType OrigT = Importer.Import(T->getOriginalType());
632 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000633 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000634
635 return Importer.getToContext().getDecayedType(OrigT);
636}
637
John McCall424cec92011-01-19 06:33:43 +0000638QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000639 QualType ToElementType = Importer.Import(T->getElementType());
640 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000641 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000642
Douglas Gregor96e578d2010-02-05 17:54:41 +0000643 return Importer.getToContext().getComplexType(ToElementType);
644}
645
John McCall424cec92011-01-19 06:33:43 +0000646QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000647 QualType ToPointeeType = Importer.Import(T->getPointeeType());
648 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000649 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000650
Douglas Gregor96e578d2010-02-05 17:54:41 +0000651 return Importer.getToContext().getPointerType(ToPointeeType);
652}
653
John McCall424cec92011-01-19 06:33:43 +0000654QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000655 // FIXME: Check for blocks support in "to" context.
656 QualType ToPointeeType = Importer.Import(T->getPointeeType());
657 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000658 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000659
Douglas Gregor96e578d2010-02-05 17:54:41 +0000660 return Importer.getToContext().getBlockPointerType(ToPointeeType);
661}
662
John McCall424cec92011-01-19 06:33:43 +0000663QualType
664ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000665 // FIXME: Check for C++ support in "to" context.
666 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
667 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000668 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000669
Douglas Gregor96e578d2010-02-05 17:54:41 +0000670 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
671}
672
John McCall424cec92011-01-19 06:33:43 +0000673QualType
674ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000675 // FIXME: Check for C++0x support in "to" context.
676 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
677 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000678 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000679
680 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000681}
682
John McCall424cec92011-01-19 06:33:43 +0000683QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000684 // FIXME: Check for C++ support in "to" context.
685 QualType ToPointeeType = Importer.Import(T->getPointeeType());
686 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000687 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000688
Douglas Gregor96e578d2010-02-05 17:54:41 +0000689 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
Fangrui Song6907ce22018-07-30 19:24:48 +0000690 return Importer.getToContext().getMemberPointerType(ToPointeeType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000691 ClassType.getTypePtr());
692}
693
John McCall424cec92011-01-19 06:33:43 +0000694QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000695 QualType ToElementType = Importer.Import(T->getElementType());
696 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000697 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000698
699 return Importer.getToContext().getConstantArrayType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000700 T->getSize(),
701 T->getSizeModifier(),
702 T->getIndexTypeCVRQualifiers());
703}
704
John McCall424cec92011-01-19 06:33:43 +0000705QualType
706ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000707 QualType ToElementType = Importer.Import(T->getElementType());
708 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000709 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000710
711 return Importer.getToContext().getIncompleteArrayType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000712 T->getSizeModifier(),
713 T->getIndexTypeCVRQualifiers());
714}
715
John McCall424cec92011-01-19 06:33:43 +0000716QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000717 QualType ToElementType = Importer.Import(T->getElementType());
718 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000719 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000720
721 Expr *Size = Importer.Import(T->getSizeExpr());
722 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000723 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000724
Douglas Gregor96e578d2010-02-05 17:54:41 +0000725 SourceRange Brackets = Importer.Import(T->getBracketsRange());
726 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
727 T->getSizeModifier(),
728 T->getIndexTypeCVRQualifiers(),
729 Brackets);
730}
731
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000732QualType ASTNodeImporter::VisitDependentSizedArrayType(
733 const DependentSizedArrayType *T) {
734 QualType ToElementType = Importer.Import(T->getElementType());
735 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000736 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000737
738 // SizeExpr may be null if size is not specified directly.
739 // For example, 'int a[]'.
740 Expr *Size = Importer.Import(T->getSizeExpr());
741 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000742 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000743
744 SourceRange Brackets = Importer.Import(T->getBracketsRange());
745 return Importer.getToContext().getDependentSizedArrayType(
746 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
747 Brackets);
748}
749
John McCall424cec92011-01-19 06:33:43 +0000750QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000751 QualType ToElementType = Importer.Import(T->getElementType());
752 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000753 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000754
755 return Importer.getToContext().getVectorType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000756 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000757 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000758}
759
John McCall424cec92011-01-19 06:33:43 +0000760QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000761 QualType ToElementType = Importer.Import(T->getElementType());
762 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000763 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000764
765 return Importer.getToContext().getExtVectorType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000766 T->getNumElements());
767}
768
John McCall424cec92011-01-19 06:33:43 +0000769QualType
770ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000771 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +0000772 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000773 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000774 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000775 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000776
Douglas Gregor96e578d2010-02-05 17:54:41 +0000777 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000778 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000779}
780
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000781QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000782 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000783 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000784 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000785
Douglas Gregor96e578d2010-02-05 17:54:41 +0000786 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000787 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000788 for (const auto &A : T->param_types()) {
789 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000790 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000791 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000792 ArgTypes.push_back(ArgType);
793 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000794
Douglas Gregor96e578d2010-02-05 17:54:41 +0000795 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000796 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000797 for (const auto &E : T->exceptions()) {
798 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000799 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000800 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000801 ExceptionTypes.push_back(ExceptionType);
802 }
John McCalldb40c7f2010-12-14 08:05:40 +0000803
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000804 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
805 FunctionProtoType::ExtProtoInfo ToEPI;
806
807 ToEPI.ExtInfo = FromEPI.ExtInfo;
808 ToEPI.Variadic = FromEPI.Variadic;
809 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
810 ToEPI.TypeQuals = FromEPI.TypeQuals;
811 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000812 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
813 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
814 ToEPI.ExceptionSpec.NoexceptExpr =
815 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
816 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
817 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
818 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
819 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000820
Jordan Rose5c382722013-03-08 21:51:21 +0000821 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000822}
823
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000824QualType ASTNodeImporter::VisitUnresolvedUsingType(
825 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000826 const auto *ToD =
827 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000828 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000829 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000830
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000831 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000832 cast_or_null<UnresolvedUsingTypenameDecl>(
833 Importer.Import(T->getDecl()->getPreviousDecl()));
834 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000835 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000836
837 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
838}
839
Sean Callananda6df8a2011-08-11 16:56:07 +0000840QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
841 QualType ToInnerType = Importer.Import(T->getInnerType());
842 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000843 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000844
Sean Callananda6df8a2011-08-11 16:56:07 +0000845 return Importer.getToContext().getParenType(ToInnerType);
846}
847
John McCall424cec92011-01-19 06:33:43 +0000848QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000849 auto *ToDecl =
850 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000851 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000852 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000853
Douglas Gregor96e578d2010-02-05 17:54:41 +0000854 return Importer.getToContext().getTypeDeclType(ToDecl);
855}
856
John McCall424cec92011-01-19 06:33:43 +0000857QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000858 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
859 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000860 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000861
Douglas Gregor96e578d2010-02-05 17:54:41 +0000862 return Importer.getToContext().getTypeOfExprType(ToExpr);
863}
864
John McCall424cec92011-01-19 06:33:43 +0000865QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000866 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
867 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000868 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000869
Douglas Gregor96e578d2010-02-05 17:54:41 +0000870 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
871}
872
John McCall424cec92011-01-19 06:33:43 +0000873QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000874 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000875 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
876 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000877 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000878
Douglas Gregor81495f32012-02-12 18:42:33 +0000879 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
880 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000881 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000882
883 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000884}
885
Alexis Hunte852b102011-05-24 22:41:36 +0000886QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
887 QualType ToBaseType = Importer.Import(T->getBaseType());
888 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
889 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000890 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000891
892 return Importer.getToContext().getUnaryTransformType(ToBaseType,
893 ToUnderlyingType,
894 T->getUTTKind());
895}
896
Richard Smith30482bc2011-02-20 03:19:35 +0000897QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000898 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000899 QualType FromDeduced = T->getDeducedType();
900 QualType ToDeduced;
901 if (!FromDeduced.isNull()) {
902 ToDeduced = Importer.Import(FromDeduced);
903 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000904 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000905 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000906
Richard Smithe301ba22015-11-11 02:02:15 +0000907 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000908 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000909}
910
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000911QualType ASTNodeImporter::VisitInjectedClassNameType(
912 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000913 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000914 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000915 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000916
917 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
918 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000919 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000920
921 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
922 // See comments in InjectedClassNameType definition for details
923 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
924 enum {
925 TypeAlignmentInBits = 4,
926 TypeAlignment = 1 << TypeAlignmentInBits
927 };
928
929 return QualType(new (Importer.getToContext(), TypeAlignment)
930 InjectedClassNameType(D, InjType), 0);
931}
932
John McCall424cec92011-01-19 06:33:43 +0000933QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000934 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000935 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000936 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000937
938 return Importer.getToContext().getTagDeclType(ToDecl);
939}
940
John McCall424cec92011-01-19 06:33:43 +0000941QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000942 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000943 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000944 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000945
946 return Importer.getToContext().getTagDeclType(ToDecl);
947}
948
Sean Callanan72fe0852015-04-02 23:50:08 +0000949QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
950 QualType FromModifiedType = T->getModifiedType();
951 QualType FromEquivalentType = T->getEquivalentType();
952 QualType ToModifiedType;
953 QualType ToEquivalentType;
954
955 if (!FromModifiedType.isNull()) {
956 ToModifiedType = Importer.Import(FromModifiedType);
957 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000958 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000959 }
960 if (!FromEquivalentType.isNull()) {
961 ToEquivalentType = Importer.Import(FromEquivalentType);
962 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000963 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000964 }
965
966 return Importer.getToContext().getAttributedType(T->getAttrKind(),
967 ToModifiedType, ToEquivalentType);
968}
969
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000970QualType ASTNodeImporter::VisitTemplateTypeParmType(
971 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000972 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000973 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
974 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000975 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000976
977 return Importer.getToContext().getTemplateTypeParmType(
978 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
979}
980
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000981QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
982 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000983 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000984 cast_or_null<TemplateTypeParmType>(Importer.Import(
985 QualType(T->getReplacedParameter(), 0)).getTypePtr());
986 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000987 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000988
989 QualType Replacement = Importer.Import(T->getReplacementType());
990 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000991 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000992 Replacement = Replacement.getCanonicalType();
993
994 return Importer.getToContext().getSubstTemplateTypeParmType(
995 Replaced, Replacement);
996}
997
Douglas Gregore2e50d332010-12-01 01:36:18 +0000998QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000999 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001000 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1001 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001002 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001003
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001004 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001005 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001006 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001007
Douglas Gregore2e50d332010-12-01 01:36:18 +00001008 QualType ToCanonType;
1009 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001010 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001011 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1012 ToCanonType =Importer.Import(FromCanonType);
1013 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001014 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001015 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001016 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00001017 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001018 ToCanonType);
1019}
1020
John McCall424cec92011-01-19 06:33:43 +00001021QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001022 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001023 // Note: the qualifier in an ElaboratedType is optional.
1024 if (T->getQualifier()) {
1025 ToQualifier = Importer.Import(T->getQualifier());
1026 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001027 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +00001028 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001029
1030 QualType ToNamedType = Importer.Import(T->getNamedType());
1031 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001032 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001033
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001034 TagDecl *OwnedTagDecl =
1035 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl()));
1036 if (!OwnedTagDecl && T->getOwnedTagDecl())
1037 return {};
1038
Abramo Bagnara6150c882010-05-11 21:36:43 +00001039 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001040 ToQualifier, ToNamedType,
1041 OwnedTagDecl);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001042}
1043
Gabor Horvath7a91c082017-11-14 11:30:38 +00001044QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1045 QualType Pattern = Importer.Import(T->getPattern());
1046 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001047 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +00001048
1049 return Importer.getToContext().getPackExpansionType(Pattern,
1050 T->getNumExpansions());
1051}
1052
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001053QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
1054 const DependentTemplateSpecializationType *T) {
1055 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
1056 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001057 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001058
1059 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1060 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001061 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001062
1063 SmallVector<TemplateArgument, 2> ToPack;
1064 ToPack.reserve(T->getNumArgs());
1065 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001066 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001067
1068 return Importer.getToContext().getDependentTemplateSpecializationType(
1069 T->getKeyword(), Qualifier, Name, ToPack);
1070}
1071
Peter Szecsice7f3182018-05-07 12:08:27 +00001072QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1073 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
1074 if (!NNS && T->getQualifier())
1075 return QualType();
1076
1077 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1078 if (!Name && T->getIdentifier())
1079 return QualType();
1080
1081 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
1082 ? QualType()
1083 : Importer.Import(T->getCanonicalTypeInternal());
1084 if (!Canon.isNull())
1085 Canon = Canon.getCanonicalType();
1086
1087 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
1088 Name, Canon);
1089}
1090
John McCall424cec92011-01-19 06:33:43 +00001091QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001092 auto *Class =
1093 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001094 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001095 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001096
John McCall8b07ec22010-05-15 11:32:37 +00001097 return Importer.getToContext().getObjCInterfaceType(Class);
1098}
1099
John McCall424cec92011-01-19 06:33:43 +00001100QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001101 QualType ToBaseType = Importer.Import(T->getBaseType());
1102 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001103 return {};
John McCall8b07ec22010-05-15 11:32:37 +00001104
Douglas Gregore9d95f12015-07-07 03:57:35 +00001105 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001106 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001107 QualType ImportedTypeArg = Importer.Import(TypeArg);
1108 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001109 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +00001110
1111 TypeArgs.push_back(ImportedTypeArg);
1112 }
1113
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001114 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001115 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001116 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001117 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001118 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001119 Protocols.push_back(Protocol);
1120 }
1121
Douglas Gregore9d95f12015-07-07 03:57:35 +00001122 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001123 Protocols,
1124 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001125}
1126
John McCall424cec92011-01-19 06:33:43 +00001127QualType
1128ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001129 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1130 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001131 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001132
John McCall8b07ec22010-05-15 11:32:37 +00001133 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001134}
1135
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001136//----------------------------------------------------------------------------
1137// Import Declarations
1138//----------------------------------------------------------------------------
Fangrui Song6907ce22018-07-30 19:24:48 +00001139bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1140 DeclContext *&LexicalDC,
1141 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001142 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001143 SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001144 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1145 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1146 DeclContext *OrigDC = D->getDeclContext();
1147 FunctionDecl *FunDecl;
1148 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1149 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001150 auto getLeafPointeeType = [](const Type *T) {
1151 while (T->isPointerType() || T->isArrayType()) {
1152 T = T->getPointeeOrArrayElementType();
1153 }
1154 return T;
1155 };
1156 for (const ParmVarDecl *P : FunDecl->parameters()) {
1157 const Type *LeafT =
1158 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1159 auto *RT = dyn_cast<RecordType>(LeafT);
1160 if (RT && RT->getDecl() == D) {
1161 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1162 << D->getDeclKindName();
1163 return true;
1164 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001165 }
1166 }
1167
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001168 // Import the context of this declaration.
Gabor Marton6e1510c2018-07-12 11:50:21 +00001169 DC = Importer.ImportContext(OrigDC);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001170 if (!DC)
1171 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001172
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001173 LexicalDC = DC;
1174 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1175 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1176 if (!LexicalDC)
1177 return true;
1178 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001179
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001180 // Import the name of this declaration.
1181 Name = Importer.Import(D->getDeclName());
1182 if (D->getDeclName() && !Name)
1183 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001184
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001185 // Import the location of this declaration.
1186 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001187 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001188 return false;
1189}
1190
Douglas Gregord451ea92011-07-29 23:31:30 +00001191void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1192 if (!FromD)
1193 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001194
Douglas Gregord451ea92011-07-29 23:31:30 +00001195 if (!ToD) {
1196 ToD = Importer.Import(FromD);
1197 if (!ToD)
1198 return;
1199 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001200
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001201 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1202 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001203 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001204 ImportDefinition(FromRecord, ToRecord);
1205 }
1206 }
1207 return;
1208 }
1209
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001210 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1211 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001212 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1213 ImportDefinition(FromEnum, ToEnum);
1214 }
1215 }
1216 return;
1217 }
1218}
1219
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001220void
1221ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1222 DeclarationNameInfo& To) {
1223 // NOTE: To.Name and To.Loc are already imported.
1224 // We only have to import To.LocInfo.
1225 switch (To.getName().getNameKind()) {
1226 case DeclarationName::Identifier:
1227 case DeclarationName::ObjCZeroArgSelector:
1228 case DeclarationName::ObjCOneArgSelector:
1229 case DeclarationName::ObjCMultiArgSelector:
1230 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001231 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001232 return;
1233
1234 case DeclarationName::CXXOperatorName: {
1235 SourceRange Range = From.getCXXOperatorNameRange();
1236 To.setCXXOperatorNameRange(Importer.Import(Range));
1237 return;
1238 }
1239 case DeclarationName::CXXLiteralOperatorName: {
1240 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1241 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1242 return;
1243 }
1244 case DeclarationName::CXXConstructorName:
1245 case DeclarationName::CXXDestructorName:
1246 case DeclarationName::CXXConversionFunctionName: {
1247 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1248 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1249 return;
1250 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001251 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001252 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001253}
1254
Fangrui Song6907ce22018-07-30 19:24:48 +00001255void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001256 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001257 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001258 return;
1259 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001260
Aaron Ballman629afae2014-03-07 19:56:05 +00001261 for (auto *From : FromDC->decls())
1262 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001263}
1264
Balazs Keri1d20cc22018-07-16 12:16:39 +00001265void ASTNodeImporter::ImportImplicitMethods(
1266 const CXXRecordDecl *From, CXXRecordDecl *To) {
1267 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1268 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001269
Balazs Keri1d20cc22018-07-16 12:16:39 +00001270 for (CXXMethodDecl *FromM : From->methods())
1271 if (FromM->isImplicit())
1272 Importer.Import(FromM);
1273}
1274
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001275static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1276 ASTImporter &Importer) {
1277 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1278 auto *ToTypedef =
1279 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1280 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1281
1282 To->setTypedefNameForAnonDecl(ToTypedef);
1283 }
1284}
1285
Fangrui Song6907ce22018-07-30 19:24:48 +00001286bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001287 ImportDefinitionKind Kind) {
1288 if (To->getDefinition() || To->isBeingDefined()) {
1289 if (Kind == IDK_Everything)
1290 ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001291
Douglas Gregore2e50d332010-12-01 01:36:18 +00001292 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001293 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001294
Douglas Gregore2e50d332010-12-01 01:36:18 +00001295 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001296
1297 setTypedefNameForAnonDecl(From, To, Importer);
Fangrui Song6907ce22018-07-30 19:24:48 +00001298
Douglas Gregore2e50d332010-12-01 01:36:18 +00001299 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001300 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1301 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001302
1303 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1304 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1305 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001306 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001307 ToData.Aggregate = FromData.Aggregate;
1308 ToData.PlainOldData = FromData.PlainOldData;
1309 ToData.Empty = FromData.Empty;
1310 ToData.Polymorphic = FromData.Polymorphic;
1311 ToData.Abstract = FromData.Abstract;
1312 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001313 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1314 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1315 ToData.HasBasesWithNonStaticDataMembers =
1316 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001317 ToData.HasPrivateFields = FromData.HasPrivateFields;
1318 ToData.HasProtectedFields = FromData.HasProtectedFields;
1319 ToData.HasPublicFields = FromData.HasPublicFields;
1320 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001321 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001322 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001323 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001324 ToData.HasUninitializedReferenceMember
1325 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001326 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001327 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1328 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001329 ToData.NeedOverloadResolutionForCopyConstructor
1330 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001331 ToData.NeedOverloadResolutionForMoveConstructor
1332 = FromData.NeedOverloadResolutionForMoveConstructor;
1333 ToData.NeedOverloadResolutionForMoveAssignment
1334 = FromData.NeedOverloadResolutionForMoveAssignment;
1335 ToData.NeedOverloadResolutionForDestructor
1336 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001337 ToData.DefaultedCopyConstructorIsDeleted
1338 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001339 ToData.DefaultedMoveConstructorIsDeleted
1340 = FromData.DefaultedMoveConstructorIsDeleted;
1341 ToData.DefaultedMoveAssignmentIsDeleted
1342 = FromData.DefaultedMoveAssignmentIsDeleted;
1343 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001344 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1345 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001346 ToData.HasConstexprNonCopyMoveConstructor
1347 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001348 ToData.HasDefaultedDefaultConstructor
1349 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001350 ToData.DefaultedDefaultConstructorIsConstexpr
1351 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001352 ToData.HasConstexprDefaultConstructor
1353 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001354 ToData.HasNonLiteralTypeFieldsOrBases
1355 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001356 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001357 ToData.UserProvidedDefaultConstructor
1358 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001359 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001360 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1361 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1362 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1363 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001364 ToData.ImplicitCopyAssignmentHasConstParam
1365 = FromData.ImplicitCopyAssignmentHasConstParam;
1366 ToData.HasDeclaredCopyConstructorWithConstParam
1367 = FromData.HasDeclaredCopyConstructorWithConstParam;
1368 ToData.HasDeclaredCopyAssignmentWithConstParam
1369 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001370
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001371 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001372 for (const auto &Base1 : FromCXX->bases()) {
1373 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001374 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001375 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001376
1377 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001378 if (Base1.isPackExpansion())
1379 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001380
1381 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001382 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001383
Douglas Gregore2e50d332010-12-01 01:36:18 +00001384 Bases.push_back(
Fangrui Song6907ce22018-07-30 19:24:48 +00001385 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001386 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1387 Base1.isVirtual(),
1388 Base1.isBaseOfClass(),
1389 Base1.getAccessSpecifierAsWritten(),
1390 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001391 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001392 }
1393 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001394 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001395 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001396
Douglas Gregor2e15c842012-02-01 21:00:38 +00001397 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001398 ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001399
Douglas Gregore2e50d332010-12-01 01:36:18 +00001400 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001401 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001402}
1403
Larisse Voufo39a1e502013-08-06 01:03:05 +00001404bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1405 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001406 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001407 return false;
1408
1409 // FIXME: Can we really import any initializer? Alternatively, we could force
1410 // ourselves to import every declaration of a variable and then only use
1411 // getInit() here.
1412 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1413
1414 // FIXME: Other bits to merge?
1415
1416 return false;
1417}
1418
Fangrui Song6907ce22018-07-30 19:24:48 +00001419bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001420 ImportDefinitionKind Kind) {
1421 if (To->getDefinition() || To->isBeingDefined()) {
1422 if (Kind == IDK_Everything)
1423 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001424 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001425 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001426
Douglas Gregord451ea92011-07-29 23:31:30 +00001427 To->startDefinition();
1428
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001429 setTypedefNameForAnonDecl(From, To, Importer);
1430
Douglas Gregord451ea92011-07-29 23:31:30 +00001431 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1432 if (T.isNull())
1433 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001434
Douglas Gregord451ea92011-07-29 23:31:30 +00001435 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1436 if (ToPromotionType.isNull())
1437 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001438
1439 if (shouldForceImportDeclContext(Kind))
1440 ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001441
Douglas Gregord451ea92011-07-29 23:31:30 +00001442 // FIXME: we might need to merge the number of positive or negative bits
1443 // if the enumerator lists don't match.
1444 To->completeDefinition(T, ToPromotionType,
1445 From->getNumPositiveBits(),
1446 From->getNumNegativeBits());
1447 return false;
1448}
1449
Douglas Gregora082a492010-11-30 19:14:50 +00001450TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1451 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001452 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1453 if (ImportContainerChecked(*Params, ToParams))
1454 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001455
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001456 Expr *ToRequiresClause;
1457 if (Expr *const R = Params->getRequiresClause()) {
1458 ToRequiresClause = Importer.Import(R);
1459 if (!ToRequiresClause)
1460 return nullptr;
1461 } else {
1462 ToRequiresClause = nullptr;
1463 }
1464
Douglas Gregora082a492010-11-30 19:14:50 +00001465 return TemplateParameterList::Create(Importer.getToContext(),
1466 Importer.Import(Params->getTemplateLoc()),
1467 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001468 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001469 Importer.Import(Params->getRAngleLoc()),
1470 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001471}
1472
Fangrui Song6907ce22018-07-30 19:24:48 +00001473TemplateArgument
Douglas Gregore2e50d332010-12-01 01:36:18 +00001474ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1475 switch (From.getKind()) {
1476 case TemplateArgument::Null:
1477 return TemplateArgument();
Fangrui Song6907ce22018-07-30 19:24:48 +00001478
Douglas Gregore2e50d332010-12-01 01:36:18 +00001479 case TemplateArgument::Type: {
1480 QualType ToType = Importer.Import(From.getAsType());
1481 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001482 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001483 return TemplateArgument(ToType);
1484 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001485
Douglas Gregore2e50d332010-12-01 01:36:18 +00001486 case TemplateArgument::Integral: {
1487 QualType ToType = Importer.Import(From.getIntegralType());
1488 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001489 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001490 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001491 }
1492
Eli Friedmanb826a002012-09-26 02:36:12 +00001493 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001494 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001495 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1496 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001497 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001498 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001499 }
1500
1501 case TemplateArgument::NullPtr: {
1502 QualType ToType = Importer.Import(From.getNullPtrType());
1503 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001504 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001505 return TemplateArgument(ToType, /*isNullPtr*/true);
1506 }
1507
Douglas Gregore2e50d332010-12-01 01:36:18 +00001508 case TemplateArgument::Template: {
1509 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1510 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001511 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001512
Douglas Gregore2e50d332010-12-01 01:36:18 +00001513 return TemplateArgument(ToTemplate);
1514 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001515
1516 case TemplateArgument::TemplateExpansion: {
Fangrui Song6907ce22018-07-30 19:24:48 +00001517 TemplateName ToTemplate
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001518 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1519 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001520 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001521
Douglas Gregore1d60df2011-01-14 23:41:42 +00001522 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001523 }
1524
Douglas Gregore2e50d332010-12-01 01:36:18 +00001525 case TemplateArgument::Expression:
1526 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1527 return TemplateArgument(ToExpr);
1528 return TemplateArgument();
Fangrui Song6907ce22018-07-30 19:24:48 +00001529
Douglas Gregore2e50d332010-12-01 01:36:18 +00001530 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001531 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001532 ToPack.reserve(From.pack_size());
1533 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001534 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001535
1536 return TemplateArgument(
1537 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001538 }
1539 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001540
Douglas Gregore2e50d332010-12-01 01:36:18 +00001541 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001542}
1543
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001544Optional<TemplateArgumentLoc>
1545ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001546 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1547 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1548 TemplateArgumentLocInfo ToInfo;
1549 if (Arg.getKind() == TemplateArgument::Expression) {
1550 Expr *E = Importer.Import(FromInfo.getAsExpr());
1551 ToInfo = TemplateArgumentLocInfo(E);
1552 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001553 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001554 } else if (Arg.getKind() == TemplateArgument::Type) {
1555 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1556 ToInfo = TemplateArgumentLocInfo(TSI);
1557 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001558 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001559 } else {
1560 ToInfo = TemplateArgumentLocInfo(
1561 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1562 Importer.Import(FromInfo.getTemplateNameLoc()),
1563 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1564 }
1565 return TemplateArgumentLoc(Arg, ToInfo);
1566}
1567
Douglas Gregore2e50d332010-12-01 01:36:18 +00001568bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1569 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001570 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001571 for (unsigned I = 0; I != NumFromArgs; ++I) {
1572 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1573 if (To.isNull() && !FromArgs[I].isNull())
1574 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001575
Douglas Gregore2e50d332010-12-01 01:36:18 +00001576 ToArgs.push_back(To);
1577 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001578
Douglas Gregore2e50d332010-12-01 01:36:18 +00001579 return false;
1580}
1581
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001582// We cannot use Optional<> pattern here and below because
1583// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1584// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001585template <typename InContainerTy>
1586bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1587 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1588 for (const auto &FromLoc : Container) {
1589 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1590 ToTAInfo.addArgument(*ToLoc);
1591 else
1592 return true;
1593 }
1594 return false;
1595}
1596
Gabor Marton26f72a92018-07-12 09:42:05 +00001597static StructuralEquivalenceKind
1598getStructuralEquivalenceKind(const ASTImporter &Importer) {
1599 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1600 : StructuralEquivalenceKind::Default;
1601}
1602
Gabor Marton950fb572018-07-17 12:39:27 +00001603bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1604 StructuralEquivalenceContext Ctx(
1605 Importer.getFromContext(), Importer.getToContext(),
1606 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1607 false, Complain);
1608 return Ctx.IsEquivalent(From, To);
1609}
1610
1611bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001612 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001613 // Eliminate a potential failure point where we attempt to re-import
1614 // something we're trying to import while completing ToRecord.
1615 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1616 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001617 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001618 if (ToOriginRecord)
1619 ToRecord = ToOriginRecord;
1620 }
1621
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001622 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001623 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001624 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001625 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001626 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001627 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001628}
1629
Larisse Voufo39a1e502013-08-06 01:03:05 +00001630bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1631 bool Complain) {
1632 StructuralEquivalenceContext Ctx(
1633 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001634 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1635 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001636 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001637}
1638
Douglas Gregor98c10182010-02-12 22:17:39 +00001639bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001640 StructuralEquivalenceContext Ctx(
1641 Importer.getFromContext(), Importer.getToContext(),
1642 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001643 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001644}
1645
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001646bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1647 FunctionTemplateDecl *To) {
1648 StructuralEquivalenceContext Ctx(
1649 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001650 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1651 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001652 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001653}
1654
Balazs Keric7797c42018-07-11 09:37:24 +00001655bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1656 StructuralEquivalenceContext Ctx(
1657 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001658 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1659 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001660 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001661}
1662
Douglas Gregor91155082012-11-14 22:29:20 +00001663bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001664 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001665 const llvm::APSInt &FromVal = FromEC->getInitVal();
1666 const llvm::APSInt &ToVal = ToEC->getInitVal();
1667
1668 return FromVal.isSigned() == ToVal.isSigned() &&
1669 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1670 FromVal == ToVal;
1671}
1672
1673bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001674 ClassTemplateDecl *To) {
1675 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1676 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001677 Importer.getNonEquivalentDecls(),
1678 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001679 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001680}
1681
Larisse Voufo39a1e502013-08-06 01:03:05 +00001682bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1683 VarTemplateDecl *To) {
1684 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1685 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001686 Importer.getNonEquivalentDecls(),
1687 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001688 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001689}
1690
Douglas Gregore4c83e42010-02-09 22:48:33 +00001691Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001692 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001693 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001694 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001695}
1696
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001697Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1698 // Import the context of this declaration.
1699 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1700 if (!DC)
1701 return nullptr;
1702
1703 DeclContext *LexicalDC = DC;
1704 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1705 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1706 if (!LexicalDC)
1707 return nullptr;
1708 }
1709
1710 // Import the location of this declaration.
1711 SourceLocation Loc = Importer.Import(D->getLocation());
1712
Gabor Marton26f72a92018-07-12 09:42:05 +00001713 EmptyDecl *ToD;
1714 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc))
1715 return ToD;
1716
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001717 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001718 LexicalDC->addDeclInternal(ToD);
1719 return ToD;
1720}
1721
Sean Callanan65198272011-11-17 23:20:56 +00001722Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001723 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00001724 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00001725
Gabor Marton26f72a92018-07-12 09:42:05 +00001726 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00001727
Sean Callanan65198272011-11-17 23:20:56 +00001728 return ToD;
1729}
1730
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001731Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001732 SourceLocation Loc = Importer.Import(D->getLocation());
1733 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1734
1735 // Import the context of this declaration.
1736 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1737 if (!DC)
1738 return nullptr;
1739
Gabor Marton26f72a92018-07-12 09:42:05 +00001740 AccessSpecDecl *ToD;
1741 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
1742 DC, Loc, ColonLoc))
1743 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001744
1745 // Lexical DeclContext and Semantic DeclContext
1746 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00001747 ToD->setLexicalDeclContext(DC);
1748 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001749
Gabor Marton26f72a92018-07-12 09:42:05 +00001750 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001751}
1752
Aleksei Sidorina693b372016-09-28 10:16:56 +00001753Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1754 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1755 if (!DC)
1756 return nullptr;
1757
1758 DeclContext *LexicalDC = DC;
1759
1760 // Import the location of this declaration.
1761 SourceLocation Loc = Importer.Import(D->getLocation());
1762
1763 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1764 if (!AssertExpr)
1765 return nullptr;
1766
1767 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001768 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001769 if (!ToMsg && FromMsg)
1770 return nullptr;
1771
Gabor Marton26f72a92018-07-12 09:42:05 +00001772 StaticAssertDecl *ToD;
1773 if (GetImportedOrCreateDecl(
1774 ToD, D, Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1775 Importer.Import(D->getRParenLoc()), D->isFailed()))
1776 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001777
1778 ToD->setLexicalDeclContext(LexicalDC);
1779 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001780 return ToD;
1781}
1782
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001783Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1784 // Import the major distinguishing characteristics of this namespace.
1785 DeclContext *DC, *LexicalDC;
1786 DeclarationName Name;
1787 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001788 NamedDecl *ToD;
1789 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001790 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001791 if (ToD)
1792 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001793
1794 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001795 if (!Name) {
1796 // This is an anonymous namespace. Adopt an existing anonymous
1797 // namespace if we can.
1798 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001799 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001800 MergeWithNamespace = TU->getAnonymousNamespace();
1801 else
1802 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1803 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001804 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001805 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001806 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001807 for (auto *FoundDecl : FoundDecls) {
1808 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001809 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001810
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001811 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001812 MergeWithNamespace = FoundNS;
1813 ConflictingDecls.clear();
1814 break;
1815 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001816
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001817 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001818 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001819
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001820 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001821 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00001822 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001823 ConflictingDecls.size());
1824 }
1825 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001826
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001827 // Create the "to" namespace, if needed.
1828 NamespaceDecl *ToNamespace = MergeWithNamespace;
1829 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001830 if (GetImportedOrCreateDecl(
1831 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
1832 Importer.Import(D->getLocStart()), Loc, Name.getAsIdentifierInfo(),
1833 /*PrevDecl=*/nullptr))
1834 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001835 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001836 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00001837
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001838 // If this is an anonymous namespace, register it as the anonymous
1839 // namespace within its context.
1840 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001841 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001842 TU->setAnonymousNamespace(ToNamespace);
1843 else
1844 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1845 }
1846 }
Gabor Marton26f72a92018-07-12 09:42:05 +00001847 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00001848
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001849 ImportDeclContext(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00001850
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001851 return ToNamespace;
1852}
1853
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001854Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1855 // Import the major distinguishing characteristics of this namespace.
1856 DeclContext *DC, *LexicalDC;
1857 DeclarationName Name;
1858 SourceLocation Loc;
1859 NamedDecl *LookupD;
1860 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1861 return nullptr;
1862 if (LookupD)
1863 return LookupD;
1864
1865 // NOTE: No conflict resolution is done for namespace aliases now.
1866
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001867 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1868 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001869 if (!TargetDecl)
1870 return nullptr;
1871
1872 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1873 if (!ToII)
1874 return nullptr;
1875
1876 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1877 if (D->getQualifierLoc() && !ToQLoc)
1878 return nullptr;
1879
Gabor Marton26f72a92018-07-12 09:42:05 +00001880 NamespaceAliasDecl *ToD;
1881 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC,
1882 Importer.Import(D->getNamespaceLoc()),
1883 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1884 Importer.Import(D->getTargetNameLoc()),
1885 TargetDecl))
1886 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001887
1888 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001889 LexicalDC->addDeclInternal(ToD);
1890
1891 return ToD;
1892}
1893
Richard Smithdda56e42011-04-15 14:24:37 +00001894Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001895 // Import the major distinguishing characteristics of this typedef.
1896 DeclContext *DC, *LexicalDC;
1897 DeclarationName Name;
1898 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001899 NamedDecl *ToD;
1900 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001901 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001902 if (ToD)
1903 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001904
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001905 // If this typedef is not in block scope, determine whether we've
1906 // seen a typedef with the same name (that we can merge with) or any
1907 // other entity by that name (which name lookup could conflict with).
1908 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001909 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001910 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001911 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001912 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001913 for (auto *FoundDecl : FoundDecls) {
1914 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001915 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001916 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001917 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1918 FoundTypedef->getUnderlyingType()))
Gabor Marton26f72a92018-07-12 09:42:05 +00001919 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001920 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001921
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001922 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001923 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001924
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001925 if (!ConflictingDecls.empty()) {
1926 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00001927 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001928 ConflictingDecls.size());
1929 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001930 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001931 }
1932 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001933
Douglas Gregorb4964f72010-02-15 23:54:17 +00001934 // Import the underlying type of this typedef;
1935 QualType T = Importer.Import(D->getUnderlyingType());
1936 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001937 return nullptr;
1938
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001939 // Create the new typedef node.
1940 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001941 SourceLocation StartL = Importer.Import(D->getLocStart());
Gabor Marton26f72a92018-07-12 09:42:05 +00001942
Richard Smithdda56e42011-04-15 14:24:37 +00001943 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00001944 if (IsAlias) {
1945 if (GetImportedOrCreateDecl<TypeAliasDecl>(
1946 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1947 Name.getAsIdentifierInfo(), TInfo))
1948 return ToTypedef;
1949 } else if (GetImportedOrCreateDecl<TypedefDecl>(
1950 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1951 Name.getAsIdentifierInfo(), TInfo))
1952 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001953
Douglas Gregordd483172010-02-22 17:42:47 +00001954 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001955 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001956
1957 // Templated declarations should not appear in DeclContext.
1958 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1959 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1960 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001961
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001962 return ToTypedef;
1963}
1964
Richard Smithdda56e42011-04-15 14:24:37 +00001965Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1966 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1967}
1968
1969Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1970 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1971}
1972
Gabor Horvath7a91c082017-11-14 11:30:38 +00001973Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1974 // Import the major distinguishing characteristics of this typedef.
1975 DeclContext *DC, *LexicalDC;
1976 DeclarationName Name;
1977 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001978 NamedDecl *FoundD;
1979 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001980 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001981 if (FoundD)
1982 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001983
1984 // If this typedef is not in block scope, determine whether we've
1985 // seen a typedef with the same name (that we can merge with) or any
1986 // other entity by that name (which name lookup could conflict with).
1987 if (!DC->isFunctionOrMethod()) {
1988 SmallVector<NamedDecl *, 4> ConflictingDecls;
1989 unsigned IDNS = Decl::IDNS_Ordinary;
1990 SmallVector<NamedDecl *, 2> FoundDecls;
1991 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001992 for (auto *FoundDecl : FoundDecls) {
1993 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001994 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001995 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00001996 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001997 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001998 }
1999
2000 if (!ConflictingDecls.empty()) {
2001 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2002 ConflictingDecls.data(),
2003 ConflictingDecls.size());
2004 if (!Name)
2005 return nullptr;
2006 }
2007 }
2008
2009 TemplateParameterList *Params = ImportTemplateParameterList(
2010 D->getTemplateParameters());
2011 if (!Params)
2012 return nullptr;
2013
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002014 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00002015 Importer.Import(D->getTemplatedDecl()));
2016 if (!TemplDecl)
2017 return nullptr;
2018
Gabor Marton26f72a92018-07-12 09:42:05 +00002019 TypeAliasTemplateDecl *ToAlias;
2020 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
2021 Name, Params, TemplDecl))
2022 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002023
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002024 TemplDecl->setDescribedAliasTemplate(ToAlias);
2025
Gabor Horvath7a91c082017-11-14 11:30:38 +00002026 ToAlias->setAccess(D->getAccess());
2027 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002028 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002029 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002030}
2031
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002032Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2033 // Import the major distinguishing characteristics of this label.
2034 DeclContext *DC, *LexicalDC;
2035 DeclarationName Name;
2036 SourceLocation Loc;
2037 NamedDecl *ToD;
2038 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2039 return nullptr;
2040 if (ToD)
2041 return ToD;
2042
2043 assert(LexicalDC->isFunctionOrMethod());
2044
Gabor Marton26f72a92018-07-12 09:42:05 +00002045 LabelDecl *ToLabel;
2046 if (D->isGnuLocal()
2047 ? GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2048 Importer.Import(D->getLocation()),
2049 Name.getAsIdentifierInfo(),
2050 Importer.Import(D->getLocStart()))
2051 : GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2052 Importer.Import(D->getLocation()),
2053 Name.getAsIdentifierInfo()))
2054 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002055
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002056 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002057 if (!Label)
2058 return nullptr;
2059
2060 ToLabel->setStmt(Label);
2061 ToLabel->setLexicalDeclContext(LexicalDC);
2062 LexicalDC->addDeclInternal(ToLabel);
2063 return ToLabel;
2064}
2065
Douglas Gregor98c10182010-02-12 22:17:39 +00002066Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2067 // Import the major distinguishing characteristics of this enum.
2068 DeclContext *DC, *LexicalDC;
2069 DeclarationName Name;
2070 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002071 NamedDecl *ToD;
2072 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002073 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002074 if (ToD)
2075 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002076
Douglas Gregor98c10182010-02-12 22:17:39 +00002077 // Figure out what enum name we're looking for.
2078 unsigned IDNS = Decl::IDNS_Tag;
2079 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002080 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2081 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002082 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002083 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002084 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002085
Douglas Gregor98c10182010-02-12 22:17:39 +00002086 // We may already have an enum of the same name; try to find and match it.
2087 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002088 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002089 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002090 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002091 for (auto *FoundDecl : FoundDecls) {
2092 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002093 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002094
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002095 Decl *Found = FoundDecl;
2096 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2097 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00002098 Found = Tag->getDecl();
2099 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002100
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002101 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002102 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002103 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002104 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002105
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002106 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002107 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002108
Douglas Gregor98c10182010-02-12 22:17:39 +00002109 if (!ConflictingDecls.empty()) {
2110 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002111 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002112 ConflictingDecls.size());
2113 }
2114 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002115
Douglas Gregor98c10182010-02-12 22:17:39 +00002116 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002117 EnumDecl *D2;
2118 if (GetImportedOrCreateDecl(
2119 D2, D, Importer.getToContext(), DC, Importer.Import(D->getLocStart()),
2120 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2121 D->isScopedUsingClassTag(), D->isFixed()))
2122 return D2;
2123
John McCall3e11ebe2010-03-15 10:12:16 +00002124 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002125 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002126 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002127 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002128 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002129
2130 // Import the integer type.
2131 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2132 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002133 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002134 D2->setIntegerType(ToIntegerType);
Fangrui Song6907ce22018-07-30 19:24:48 +00002135
Douglas Gregor98c10182010-02-12 22:17:39 +00002136 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002137 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002138 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002139
Douglas Gregor3996e242010-02-15 22:01:00 +00002140 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002141}
2142
Douglas Gregor5c73e912010-02-11 00:48:18 +00002143Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2144 // If this record has a definition in the translation unit we're coming from,
2145 // but this particular declaration is not that definition, import the
2146 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002147 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002148 if (Definition && Definition != D &&
2149 // In contrast to a normal CXXRecordDecl, the implicit
2150 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2151 // The definition of the implicit CXXRecordDecl in this case is the
2152 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2153 // condition in order to be able to import the implict Decl.
2154 !D->isImplicit()) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002155 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002156 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002157 return nullptr;
2158
Gabor Marton26f72a92018-07-12 09:42:05 +00002159 return Importer.MapImported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002160 }
Gabor Martona3af5672018-05-23 14:24:02 +00002161
Douglas Gregor5c73e912010-02-11 00:48:18 +00002162 // Import the major distinguishing characteristics of this record.
2163 DeclContext *DC, *LexicalDC;
2164 DeclarationName Name;
2165 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002166 NamedDecl *ToD;
2167 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002168 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002169 if (ToD)
2170 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002171
Douglas Gregor5c73e912010-02-11 00:48:18 +00002172 // Figure out what structure name we're looking for.
2173 unsigned IDNS = Decl::IDNS_Tag;
2174 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002175 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2176 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002177 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002178 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002179 IDNS |= Decl::IDNS_Ordinary;
2180
2181 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002182 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002183 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002184 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002185 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002186 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002187 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002188
2189 if (!FoundDecls.empty()) {
2190 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2191 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2192 D->getASTContext().getExternalSource()->CompleteType(D);
2193 }
2194
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002195 for (auto *FoundDecl : FoundDecls) {
2196 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002197 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002198
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002199 Decl *Found = FoundDecl;
2200 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2201 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002202 Found = Tag->getDecl();
2203 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002204
2205 if (D->getDescribedTemplate()) {
2206 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found))
2207 Found = Template->getTemplatedDecl();
2208 else
2209 continue;
2210 }
2211
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002212 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002213 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002214 if (!IsStructuralMatch(D, FoundRecord, false))
2215 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002216 }
2217
Sean Callanan9092d472017-05-13 00:46:33 +00002218 PrevDecl = FoundRecord;
2219
Douglas Gregor25791052010-02-12 00:09:27 +00002220 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002221 if ((SearchName && !D->isCompleteDefinition())
2222 || (D->isCompleteDefinition() &&
2223 D->isAnonymousStructOrUnion()
2224 == FoundDef->isAnonymousStructOrUnion() &&
2225 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002226 // The record types structurally match, or the "from" translation
2227 // unit only had a forward declaration anyway; call it the same
2228 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002229 // FIXME: Structural equivalence check should check for same
2230 // user-defined methods.
2231 Importer.MapImported(D, FoundDef);
2232 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2233 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2234 assert(FoundCXX && "Record type mismatch");
2235
2236 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2237 // FoundDef may not have every implicit method that D has
2238 // because implicit methods are created only if they are used.
2239 ImportImplicitMethods(DCXX, FoundCXX);
2240 }
2241 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002242 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002243 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002244 // We have a forward declaration of this type, so adopt that forward
2245 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002246
Sean Callananc94711c2014-03-04 18:11:50 +00002247 // If one or both can be completed from external storage then try one
2248 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002249
Sean Callananc94711c2014-03-04 18:11:50 +00002250 if (FoundRecord->hasExternalLexicalStorage() &&
2251 !FoundRecord->isCompleteDefinition())
2252 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2253 if (D->hasExternalLexicalStorage())
2254 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002255
Sean Callananc94711c2014-03-04 18:11:50 +00002256 if (FoundRecord->isCompleteDefinition() &&
2257 D->isCompleteDefinition() &&
2258 !IsStructuralMatch(D, FoundRecord))
2259 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002260
Douglas Gregor25791052010-02-12 00:09:27 +00002261 AdoptDecl = FoundRecord;
2262 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002263 } else if (!SearchName) {
2264 continue;
2265 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002266 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002267
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002268 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002269 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002270
Douglas Gregordd6006f2012-07-17 21:16:27 +00002271 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002272 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002273 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002274 ConflictingDecls.size());
2275 }
2276 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002277
Douglas Gregor5c73e912010-02-11 00:48:18 +00002278 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002279 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002280 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002281 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002282 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002283 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002284 if (DCXX->isLambda()) {
2285 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00002286 if (GetImportedOrCreateSpecialDecl(
2287 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2288 DC, TInfo, Loc, DCXX->isDependentLambda(),
2289 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2290 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002291 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2292 if (DCXX->getLambdaContextDecl() && !CDecl)
2293 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002294 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002295 } else if (DCXX->isInjectedClassName()) {
2296 // We have to be careful to do a similar dance to the one in
2297 // Sema::ActOnStartCXXMemberDeclarations
2298 CXXRecordDecl *const PrevDecl = nullptr;
2299 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002300 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2301 D->getTagKind(), DC, StartLoc, Loc,
2302 Name.getAsIdentifierInfo(), PrevDecl,
2303 DelayTypeCreation))
2304 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002305 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002306 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002307 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002308 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2309 D->getTagKind(), DC, StartLoc, Loc,
2310 Name.getAsIdentifierInfo(),
2311 cast_or_null<CXXRecordDecl>(PrevDecl)))
2312 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002313 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002314
Douglas Gregor3996e242010-02-15 22:01:00 +00002315 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002316 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002317 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002318 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002319 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002320
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002321 if (ClassTemplateDecl *FromDescribed =
2322 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002323 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2324 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002325 if (!ToDescribed)
2326 return nullptr;
2327 D2CXX->setDescribedClassTemplate(ToDescribed);
Gabor Marton5915777e2018-06-26 13:44:24 +00002328 if (!DCXX->isInjectedClassName()) {
2329 // In a record describing a template the type should be an
2330 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2331 // previously set type to the correct value here (ToDescribed is not
2332 // available at record create).
2333 // FIXME: The previous type is cleared but not removed from
2334 // ASTContext's internal storage.
2335 CXXRecordDecl *Injected = nullptr;
2336 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2337 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2338 if (Record && Record->isInjectedClassName()) {
2339 Injected = Record;
2340 break;
2341 }
2342 }
2343 D2CXX->setTypeForDecl(nullptr);
2344 Importer.getToContext().getInjectedClassNameType(D2CXX,
2345 ToDescribed->getInjectedClassNameSpecialization());
2346 if (Injected) {
2347 Injected->setTypeForDecl(nullptr);
2348 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2349 }
2350 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002351 } else if (MemberSpecializationInfo *MemberInfo =
2352 DCXX->getMemberSpecializationInfo()) {
2353 TemplateSpecializationKind SK =
2354 MemberInfo->getTemplateSpecializationKind();
2355 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002356 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002357 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2358 if (FromInst && !ToInst)
2359 return nullptr;
2360 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2361 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2362 Importer.Import(MemberInfo->getPointOfInstantiation()));
2363 }
Douglas Gregor25791052010-02-12 00:09:27 +00002364 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002365 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2366 D->getTagKind(), DC, StartLoc, Loc,
2367 Name.getAsIdentifierInfo(), PrevDecl))
2368 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002369 D2->setLexicalDeclContext(LexicalDC);
2370 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002371 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002372
Douglas Gregor14454802011-02-25 02:25:35 +00002373 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002374 if (D->isAnonymousStructOrUnion())
2375 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002376 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002377
2378 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002379
Douglas Gregor95d82832012-01-24 18:36:04 +00002380 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002381 return nullptr;
2382
Douglas Gregor3996e242010-02-15 22:01:00 +00002383 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002384}
2385
Douglas Gregor98c10182010-02-12 22:17:39 +00002386Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2387 // Import the major distinguishing characteristics of this enumerator.
2388 DeclContext *DC, *LexicalDC;
2389 DeclarationName Name;
2390 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002391 NamedDecl *ToD;
2392 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002393 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002394 if (ToD)
2395 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002396
2397 QualType T = Importer.Import(D->getType());
2398 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002399 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002400
Fangrui Song6907ce22018-07-30 19:24:48 +00002401 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002402 // in the same context.
2403 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002404 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002405 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002406 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002407 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002408 for (auto *FoundDecl : FoundDecls) {
2409 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002410 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002411
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002412 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002413 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002414 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002415 }
2416
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002417 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002418 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002419
Douglas Gregor98c10182010-02-12 22:17:39 +00002420 if (!ConflictingDecls.empty()) {
2421 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002422 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002423 ConflictingDecls.size());
2424 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002425 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002426 }
2427 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002428
Douglas Gregor98c10182010-02-12 22:17:39 +00002429 Expr *Init = Importer.Import(D->getInitExpr());
2430 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002431 return nullptr;
2432
Gabor Marton26f72a92018-07-12 09:42:05 +00002433 EnumConstantDecl *ToEnumerator;
2434 if (GetImportedOrCreateDecl(
2435 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2436 Name.getAsIdentifierInfo(), T, Init, D->getInitVal()))
2437 return ToEnumerator;
2438
Douglas Gregordd483172010-02-22 17:42:47 +00002439 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002440 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002441 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002442 return ToEnumerator;
2443}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002444
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002445bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2446 FunctionDecl *ToFD) {
2447 switch (FromFD->getTemplatedKind()) {
2448 case FunctionDecl::TK_NonTemplate:
2449 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002450 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002451
2452 case FunctionDecl::TK_MemberSpecialization: {
2453 auto *InstFD = cast_or_null<FunctionDecl>(
2454 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2455 if (!InstFD)
2456 return true;
2457
2458 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2459 SourceLocation POI = Importer.Import(
2460 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2461 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2462 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002463 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002464 }
2465
2466 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Gabor Marton5254e642018-06-27 13:32:50 +00002467 FunctionTemplateDecl* Template;
2468 OptionalTemplateArgsTy ToTemplArgs;
2469 std::tie(Template, ToTemplArgs) =
2470 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2471 if (!Template || !ToTemplArgs)
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002472 return true;
2473
2474 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Gabor Marton5254e642018-06-27 13:32:50 +00002475 Importer.getToContext(), *ToTemplArgs);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002476
Gabor Marton5254e642018-06-27 13:32:50 +00002477 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002478 TemplateArgumentListInfo ToTAInfo;
2479 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002480 if (FromTAArgsAsWritten)
2481 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002482 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002483
2484 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2485
Gabor Marton5254e642018-06-27 13:32:50 +00002486 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002487 ToFD->setFunctionTemplateSpecialization(
2488 Template, ToTAList, /* InsertPos= */ nullptr,
2489 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002490 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002491 }
2492
2493 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2494 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2495 UnresolvedSet<8> TemplDecls;
2496 unsigned NumTemplates = FromInfo->getNumTemplates();
2497 for (unsigned I = 0; I < NumTemplates; I++) {
2498 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2499 Importer.Import(FromInfo->getTemplate(I))))
2500 TemplDecls.addDecl(ToFTD);
2501 else
2502 return true;
2503 }
2504
2505 // Import TemplateArgumentListInfo.
2506 TemplateArgumentListInfo ToTAInfo;
2507 if (ImportTemplateArgumentListInfo(
2508 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2509 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2510 FromInfo->getNumTemplateArgs()),
2511 ToTAInfo))
2512 return true;
2513
2514 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2515 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002516 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002517 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002518 }
Sam McCallfdc32072018-01-26 12:06:44 +00002519 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002520}
2521
Gabor Marton5254e642018-06-27 13:32:50 +00002522FunctionDecl *
2523ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
2524 FunctionTemplateDecl* Template;
2525 OptionalTemplateArgsTy ToTemplArgs;
2526 std::tie(Template, ToTemplArgs) =
2527 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2528 if (!Template || !ToTemplArgs)
2529 return nullptr;
2530
2531 void *InsertPos = nullptr;
2532 auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos);
2533 return FoundSpec;
2534}
2535
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002536Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002537
2538 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
2539 auto RedeclIt = Redecls.begin();
2540 // Import the first part of the decl chain. I.e. import all previous
2541 // declarations starting from the canonical decl.
2542 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt)
2543 if (!Importer.Import(*RedeclIt))
2544 return nullptr;
2545 assert(*RedeclIt == D);
2546
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002547 // Import the major distinguishing characteristics of this function.
2548 DeclContext *DC, *LexicalDC;
2549 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002550 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002551 NamedDecl *ToD;
2552 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002553 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002554 if (ToD)
2555 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002556
Gabor Marton5254e642018-06-27 13:32:50 +00002557 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002558 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002559
Gabor Marton5254e642018-06-27 13:32:50 +00002560 // If this is a function template specialization, then try to find the same
2561 // existing specialization in the "to" context. The localUncachedLookup
2562 // below will not find any specialization, but would find the primary
2563 // template; thus, we have to skip normal lookup in case of specializations.
2564 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2565 if (D->getTemplatedKind() ==
2566 FunctionDecl::TK_FunctionTemplateSpecialization) {
2567 if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) {
2568 if (D->doesThisDeclarationHaveABody() &&
2569 FoundFunction->hasBody())
2570 return Importer.Imported(D, FoundFunction);
2571 FoundByLookup = FoundFunction;
2572 }
2573 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002574 // Try to find a function in our own ("to") context with the same name, same
2575 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002576 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002577 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002578 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002579 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002580 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002581 for (auto *FoundDecl : FoundDecls) {
2582 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002583 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002584
Balazs Keria35798d2018-07-17 09:52:41 +00002585 // If template was found, look at the templated function.
2586 if (FromFT) {
2587 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
2588 FoundDecl = Template->getTemplatedDecl();
2589 else
2590 continue;
2591 }
2592
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002593 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002594 if (FoundFunction->hasExternalFormalLinkage() &&
2595 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00002596 if (IsStructuralMatch(D, FoundFunction)) {
2597 const FunctionDecl *Definition = nullptr;
2598 if (D->doesThisDeclarationHaveABody() &&
2599 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002600 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00002601 D, const_cast<FunctionDecl *>(Definition));
2602 }
2603 FoundByLookup = FoundFunction;
2604 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002605 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002606
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002607 // FIXME: Check for overloading more carefully, e.g., by boosting
2608 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002609
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002610 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002611 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002612 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002613
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002614 // Complain about inconsistent function types.
2615 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002616 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002617 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002618 diag::note_odr_value_here)
2619 << FoundFunction->getType();
2620 }
2621 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002622
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002623 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002624 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002625
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002626 if (!ConflictingDecls.empty()) {
2627 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002628 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002629 ConflictingDecls.size());
2630 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002631 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00002632 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002633 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002634
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002635 DeclarationNameInfo NameInfo(Name, Loc);
2636 // Import additional name location/type info.
2637 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2638
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002639 QualType FromTy = D->getType();
2640 bool usedDifferentExceptionSpec = false;
2641
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002642 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002643 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2644 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2645 // FunctionDecl that we are importing the FunctionProtoType for.
2646 // To avoid an infinite recursion when importing, create the FunctionDecl
2647 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002648 if (FromEPI.ExceptionSpec.SourceDecl ||
2649 FromEPI.ExceptionSpec.SourceTemplate ||
2650 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002651 FunctionProtoType::ExtProtoInfo DefaultEPI;
2652 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002653 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002654 usedDifferentExceptionSpec = true;
2655 }
2656 }
2657
Douglas Gregorb4964f72010-02-15 23:54:17 +00002658 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002659 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002660 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002661 return nullptr;
2662
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002663 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002664 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002665 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002666 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002667 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002668 return nullptr;
2669
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002670 Parameters.push_back(ToP);
2671 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002672
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002673 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002674 if (D->getTypeSourceInfo() && !TInfo)
2675 return nullptr;
2676
2677 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002678 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002679 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002680 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002681 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
2682 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2683 InnerLocStart, NameInfo, T, TInfo, FromConstructor->isExplicit(),
2684 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
2685 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00002686 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2687 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002688 for (auto *I : FromConstructor->inits()) {
2689 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002690 if (!ToI && I)
2691 return nullptr;
2692 CtorInitializers.push_back(ToI);
2693 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002694 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002695 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2696 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002697 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002698 ToCtor->setCtorInitializers(Memory);
2699 ToCtor->setNumCtorInitializers(NumInitializers);
2700 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002701 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002702 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
2703 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2704 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2705 D->isImplicit()))
2706 return ToFunction;
2707 } else if (CXXConversionDecl *FromConversion =
2708 dyn_cast<CXXConversionDecl>(D)) {
2709 if (GetImportedOrCreateDecl<CXXConversionDecl>(
2710 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2711 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2712 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
2713 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002714 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002715 if (GetImportedOrCreateDecl<CXXMethodDecl>(
2716 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2717 InnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
2718 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
2719 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002720 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002721 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
2722 InnerLocStart, NameInfo, T, TInfo,
2723 D->getStorageClass(), D->isInlineSpecified(),
2724 D->hasWrittenPrototype(), D->isConstexpr()))
2725 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002726 }
John McCall3e11ebe2010-03-15 10:12:16 +00002727
2728 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002729 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002730 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002731 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002732 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2733 ToFunction->setTrivial(D->isTrivial());
2734 ToFunction->setPure(D->isPure());
Rafael Stahla0010472018-07-09 08:40:17 +00002735 ToFunction->setRangeEnd(Importer.Import(D->getLocEnd()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00002736
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002737 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002738 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002739 Param->setOwningFunction(ToFunction);
2740 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002741 }
David Blaikie9c70e042011-09-21 18:16:56 +00002742 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002743
Gabor Marton5254e642018-06-27 13:32:50 +00002744 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002745 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00002746 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002747 ToFunction->setPreviousDecl(Recent);
2748 }
2749
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002750 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2751 // params it refers to.
2752 if (TInfo) {
2753 if (auto ProtoLoc =
2754 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2755 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2756 ProtoLoc.setParam(I, Parameters[I]);
2757 }
2758 }
2759
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002760 if (usedDifferentExceptionSpec) {
2761 // Update FunctionProtoType::ExtProtoInfo.
2762 QualType T = Importer.Import(D->getType());
2763 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002764 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002765 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002766 }
2767
Balazs Keria35798d2018-07-17 09:52:41 +00002768 // Import the describing template function, if any.
2769 if (FromFT)
2770 if (!Importer.Import(FromFT))
2771 return nullptr;
2772
Gabor Marton5254e642018-06-27 13:32:50 +00002773 if (D->doesThisDeclarationHaveABody()) {
2774 if (Stmt *FromBody = D->getBody()) {
2775 if (Stmt *ToBody = Importer.Import(FromBody)) {
2776 ToFunction->setBody(ToBody);
2777 }
Sean Callanan59721b32015-04-28 18:41:46 +00002778 }
2779 }
2780
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002781 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002782
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002783 // If it is a template, import all related things.
2784 if (ImportTemplateInformation(D, ToFunction))
2785 return nullptr;
2786
Gabor Marton5254e642018-06-27 13:32:50 +00002787 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
2788
2789 // TODO Can we generalize this approach to other AST nodes as well?
2790 if (D->getDeclContext()->containsDeclAndLoad(D))
2791 DC->addDeclInternal(ToFunction);
2792 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002793 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002794
Gabor Marton5254e642018-06-27 13:32:50 +00002795 // Friend declaration's lexical context is the befriending class, but the
2796 // semantic context is the enclosing scope of the befriending class.
2797 // We want the friend functions to be found in the semantic context by lookup.
2798 // FIXME should we handle this generically in VisitFriendDecl?
2799 // In Other cases when LexicalDC != DC we don't want it to be added,
2800 // e.g out-of-class definitions like void B::f() {} .
2801 if (LexicalDC != DC && IsFriend) {
2802 DC->makeDeclVisibleInContext(ToFunction);
2803 }
2804
2805 // Import the rest of the chain. I.e. import all subsequent declarations.
2806 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt)
2807 if (!Importer.Import(*RedeclIt))
2808 return nullptr;
2809
Lang Hames19e07e12017-06-20 21:06:00 +00002810 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2811 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2812
Douglas Gregor43f54792010-02-17 02:12:47 +00002813 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002814}
2815
Douglas Gregor00eace12010-02-21 18:29:16 +00002816Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2817 return VisitFunctionDecl(D);
2818}
2819
2820Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2821 return VisitCXXMethodDecl(D);
2822}
2823
2824Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2825 return VisitCXXMethodDecl(D);
2826}
2827
2828Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2829 return VisitCXXMethodDecl(D);
2830}
2831
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002832static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002833 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002834 if (!Owner)
2835 return 0;
2836
2837 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002838 for (const auto *D : Owner->noload_decls()) {
2839 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002840 return Index;
2841
2842 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2843 ++Index;
2844 }
2845
2846 return Index;
2847}
2848
Douglas Gregor5c73e912010-02-11 00:48:18 +00002849Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2850 // Import the major distinguishing characteristics of a variable.
2851 DeclContext *DC, *LexicalDC;
2852 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002853 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002854 NamedDecl *ToD;
2855 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002856 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002857 if (ToD)
2858 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002859
Fangrui Song6907ce22018-07-30 19:24:48 +00002860 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002861 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002862 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002863 for (auto *FoundDecl : FoundDecls) {
2864 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002865 // For anonymous fields, match up by index.
2866 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2867 continue;
2868
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002869 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002870 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002871 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002872 return FoundField;
2873 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002874
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002875 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2876 << Name << D->getType() << FoundField->getType();
2877 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2878 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002879 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002880 }
2881 }
2882
Douglas Gregorb4964f72010-02-15 23:54:17 +00002883 // Import the type.
2884 QualType T = Importer.Import(D->getType());
2885 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002886 return nullptr;
2887
Douglas Gregor5c73e912010-02-11 00:48:18 +00002888 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2889 Expr *BitWidth = Importer.Import(D->getBitWidth());
2890 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002891 return nullptr;
2892
Gabor Marton26f72a92018-07-12 09:42:05 +00002893 FieldDecl *ToField;
2894 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
2895 Importer.Import(D->getInnerLocStart()), Loc,
2896 Name.getAsIdentifierInfo(), T, TInfo, BitWidth,
2897 D->isMutable(), D->getInClassInitStyle()))
2898 return ToField;
2899
Douglas Gregordd483172010-02-22 17:42:47 +00002900 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002901 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002902 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002903 Expr *ToInitializer = Importer.Import(FromInitializer);
2904 if (ToInitializer)
2905 ToField->setInClassInitializer(ToInitializer);
2906 else
2907 return nullptr;
2908 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002909 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00002910 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002911 return ToField;
2912}
2913
Francois Pichet783dd6e2010-11-21 06:08:52 +00002914Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2915 // Import the major distinguishing characteristics of a variable.
2916 DeclContext *DC, *LexicalDC;
2917 DeclarationName Name;
2918 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002919 NamedDecl *ToD;
2920 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002921 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002922 if (ToD)
2923 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002924
Fangrui Song6907ce22018-07-30 19:24:48 +00002925 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002926 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002927 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002928 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002929 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002930 // For anonymous indirect fields, match up by index.
2931 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2932 continue;
2933
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002934 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002935 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002936 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002937 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002938 return FoundField;
2939 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002940
2941 // If there are more anonymous fields to check, continue.
2942 if (!Name && I < N-1)
2943 continue;
2944
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002945 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2946 << Name << D->getType() << FoundField->getType();
2947 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2948 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002949 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002950 }
2951 }
2952
Francois Pichet783dd6e2010-11-21 06:08:52 +00002953 // Import the type.
2954 QualType T = Importer.Import(D->getType());
2955 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002956 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002957
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002958 auto **NamedChain =
2959 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002960
2961 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002962 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002963 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002964 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002965 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002966 NamedChain[i++] = cast<NamedDecl>(D);
2967 }
2968
Gabor Marton26f72a92018-07-12 09:42:05 +00002969 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
2970 IndirectFieldDecl *ToIndirectField;
2971 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
2972 Loc, Name.getAsIdentifierInfo(), T, CH))
2973 // FIXME here we leak `NamedChain` which is allocated before
2974 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00002975
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002976 for (const auto *A : D->attrs())
2977 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00002978
Francois Pichet783dd6e2010-11-21 06:08:52 +00002979 ToIndirectField->setAccess(D->getAccess());
2980 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002981 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002982 return ToIndirectField;
2983}
2984
Aleksei Sidorina693b372016-09-28 10:16:56 +00002985Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2986 // Import the major distinguishing characteristics of a declaration.
2987 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2988 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2989 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2990 if (!DC || !LexicalDC)
2991 return nullptr;
2992
2993 // Determine whether we've already imported this decl.
2994 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2995 auto *RD = cast<CXXRecordDecl>(DC);
2996 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002997
2998 while (ImportedFriend) {
2999 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003000 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3001 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003002 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003003
3004 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3005 if (Importer.IsStructurallyEquivalent(
3006 D->getFriendType()->getType(),
3007 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003008 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003009 }
3010 ImportedFriend = ImportedFriend->getNextFriend();
3011 }
3012
3013 // Not found. Create it.
3014 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003015 if (NamedDecl *FriendD = D->getFriendDecl()) {
3016 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
3017 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
3018 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3019 ToFriendD->setObjectOfFriendDecl(false);
3020
3021 ToFU = ToFriendD;
3022 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003023 ToFU = Importer.Import(D->getFriendType());
3024 if (!ToFU)
3025 return nullptr;
3026
3027 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003028 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003029 for (unsigned I = 0; I < D->NumTPLists; I++) {
3030 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
3031 if (!List)
3032 return nullptr;
3033 ToTPLists[I] = List;
3034 }
3035
Gabor Marton26f72a92018-07-12 09:42:05 +00003036 FriendDecl *FrD;
3037 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
3038 Importer.Import(D->getLocation()), ToFU,
3039 Importer.Import(D->getFriendLoc()), ToTPLists))
3040 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003041
3042 FrD->setAccess(D->getAccess());
3043 FrD->setLexicalDeclContext(LexicalDC);
3044 LexicalDC->addDeclInternal(FrD);
3045 return FrD;
3046}
3047
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003048Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3049 // Import the major distinguishing characteristics of an ivar.
3050 DeclContext *DC, *LexicalDC;
3051 DeclarationName Name;
3052 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003053 NamedDecl *ToD;
3054 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003055 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003056 if (ToD)
3057 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003058
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003059 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003060 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003061 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003062 for (auto *FoundDecl : FoundDecls) {
3063 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003064 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003065 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003066 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003067 return FoundIvar;
3068 }
3069
3070 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3071 << Name << D->getType() << FoundIvar->getType();
3072 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3073 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003074 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003075 }
3076 }
3077
3078 // Import the type.
3079 QualType T = Importer.Import(D->getType());
3080 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003081 return nullptr;
3082
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003083 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3084 Expr *BitWidth = Importer.Import(D->getBitWidth());
3085 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003086 return nullptr;
3087
Gabor Marton26f72a92018-07-12 09:42:05 +00003088 ObjCIvarDecl *ToIvar;
3089 if (GetImportedOrCreateDecl(
3090 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
3091 Importer.Import(D->getInnerLocStart()), Loc,
3092 Name.getAsIdentifierInfo(), T, TInfo, D->getAccessControl(), BitWidth,
3093 D->getSynthesize()))
3094 return ToIvar;
3095
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003096 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003097 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003098 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003099}
3100
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003101Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3102 // Import the major distinguishing characteristics of a variable.
3103 DeclContext *DC, *LexicalDC;
3104 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003105 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003106 NamedDecl *ToD;
3107 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003108 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003109 if (ToD)
3110 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003111
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003112 // Try to find a variable in our own ("to") context with the same name and
3113 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003114 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003115 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003116 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003117 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003118 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003119 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003120 for (auto *FoundDecl : FoundDecls) {
3121 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003122 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003123
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003124 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003125 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003126 if (FoundVar->hasExternalFormalLinkage() &&
3127 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003128 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003129 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003130 MergeWithVar = FoundVar;
3131 break;
3132 }
3133
Douglas Gregor56521c52010-02-12 17:23:39 +00003134 const ArrayType *FoundArray
3135 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3136 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003137 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003138 if (FoundArray && TArray) {
3139 if (isa<IncompleteArrayType>(FoundArray) &&
3140 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003141 // Import the type.
3142 QualType T = Importer.Import(D->getType());
3143 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003144 return nullptr;
3145
Douglas Gregor56521c52010-02-12 17:23:39 +00003146 FoundVar->setType(T);
3147 MergeWithVar = FoundVar;
3148 break;
3149 } else if (isa<IncompleteArrayType>(TArray) &&
3150 isa<ConstantArrayType>(FoundArray)) {
3151 MergeWithVar = FoundVar;
3152 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003153 }
3154 }
3155
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003156 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003157 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003158 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3159 << FoundVar->getType();
3160 }
3161 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003162
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003163 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003164 }
3165
3166 if (MergeWithVar) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003167 // An equivalent variable with external linkage has been found. Link
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003168 // the two declarations, then merge them.
Gabor Marton26f72a92018-07-12 09:42:05 +00003169 Importer.MapImported(D, MergeWithVar);
3170 updateFlags(D, MergeWithVar);
3171
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003172 if (VarDecl *DDef = D->getDefinition()) {
3173 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00003174 Importer.ToDiag(ExistingDef->getLocation(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003175 diag::err_odr_variable_multiple_def)
3176 << Name;
3177 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3178 } else {
3179 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003180 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003181 if (DDef->isInitKnownICE()) {
3182 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3183 Eval->CheckedICE = true;
3184 Eval->IsICE = DDef->isInitICE();
3185 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003186 }
3187 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003188
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003189 return MergeWithVar;
3190 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003191
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003192 if (!ConflictingDecls.empty()) {
3193 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003194 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003195 ConflictingDecls.size());
3196 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003197 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003198 }
3199 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003200
Douglas Gregorb4964f72010-02-15 23:54:17 +00003201 // Import the type.
3202 QualType T = Importer.Import(D->getType());
3203 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003204 return nullptr;
3205
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003206 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003207 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003208 VarDecl *ToVar;
3209 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
3210 Importer.Import(D->getInnerLocStart()), Loc,
3211 Name.getAsIdentifierInfo(), T, TInfo,
3212 D->getStorageClass()))
3213 return ToVar;
3214
Douglas Gregor14454802011-02-25 02:25:35 +00003215 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003216 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003217 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003218
3219 // Templated declarations should never appear in the enclosing DeclContext.
3220 if (!D->getDescribedVarTemplate())
3221 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003222
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003223 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003224 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003225 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003226
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003227 if (D->isConstexpr())
3228 ToVar->setConstexpr(true);
3229
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003230 return ToVar;
3231}
3232
Douglas Gregor8b228d72010-02-17 21:22:52 +00003233Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3234 // Parameters are created in the translation unit's context, then moved
3235 // into the function declaration's context afterward.
3236 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003237
Douglas Gregor8b228d72010-02-17 21:22:52 +00003238 // Import the name of this declaration.
3239 DeclarationName Name = Importer.Import(D->getDeclName());
3240 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003241 return nullptr;
3242
Douglas Gregor8b228d72010-02-17 21:22:52 +00003243 // Import the location of this declaration.
3244 SourceLocation Loc = Importer.Import(D->getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00003245
Douglas Gregor8b228d72010-02-17 21:22:52 +00003246 // Import the parameter's type.
3247 QualType T = Importer.Import(D->getType());
3248 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003249 return nullptr;
3250
Douglas Gregor8b228d72010-02-17 21:22:52 +00003251 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003252 ImplicitParamDecl *ToParm = nullptr;
3253 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, Loc,
3254 Name.getAsIdentifierInfo(), T,
3255 D->getParameterKind()))
3256 return ToParm;
3257 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003258}
3259
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003260Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3261 // Parameters are created in the translation unit's context, then moved
3262 // into the function declaration's context afterward.
3263 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003264
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003265 // Import the name of this declaration.
3266 DeclarationName Name = Importer.Import(D->getDeclName());
3267 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003268 return nullptr;
3269
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003270 // Import the location of this declaration.
3271 SourceLocation Loc = Importer.Import(D->getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00003272
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003273 // Import the parameter's type.
3274 QualType T = Importer.Import(D->getType());
3275 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003276 return nullptr;
3277
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003278 // Create the imported parameter.
3279 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003280 ParmVarDecl *ToParm;
3281 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3282 Importer.Import(D->getInnerLocStart()), Loc,
3283 Name.getAsIdentifierInfo(), T, TInfo,
3284 D->getStorageClass(),
3285 /*DefaultArg*/ nullptr))
3286 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003287
3288 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003289 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003290 ToParm->setKNRPromoted(D->isKNRPromoted());
3291
3292 Expr *ToDefArg = nullptr;
3293 Expr *FromDefArg = nullptr;
3294 if (D->hasUninstantiatedDefaultArg()) {
3295 FromDefArg = D->getUninstantiatedDefaultArg();
3296 ToDefArg = Importer.Import(FromDefArg);
3297 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3298 } else if (D->hasUnparsedDefaultArg()) {
3299 ToParm->setUnparsedDefaultArg();
3300 } else if (D->hasDefaultArg()) {
3301 FromDefArg = D->getDefaultArg();
3302 ToDefArg = Importer.Import(FromDefArg);
3303 ToParm->setDefaultArg(ToDefArg);
3304 }
3305 if (FromDefArg && !ToDefArg)
3306 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003307
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003308 if (D->isObjCMethodParameter()) {
3309 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3310 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3311 } else {
3312 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3313 D->getFunctionScopeIndex());
3314 }
3315
Gabor Marton26f72a92018-07-12 09:42:05 +00003316 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003317}
3318
Douglas Gregor43f54792010-02-17 02:12:47 +00003319Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3320 // Import the major distinguishing characteristics of a method.
3321 DeclContext *DC, *LexicalDC;
3322 DeclarationName Name;
3323 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003324 NamedDecl *ToD;
3325 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003326 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003327 if (ToD)
3328 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003329
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003330 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003331 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003332 for (auto *FoundDecl : FoundDecls) {
3333 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003334 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3335 continue;
3336
3337 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003338 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3339 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003340 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003341 << D->isInstanceMethod() << Name << D->getReturnType()
3342 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003343 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003344 diag::note_odr_objc_method_here)
3345 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003346 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003347 }
3348
3349 // Check the number of parameters.
3350 if (D->param_size() != FoundMethod->param_size()) {
3351 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3352 << D->isInstanceMethod() << Name
3353 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003354 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003355 diag::note_odr_objc_method_here)
3356 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003357 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003358 }
3359
3360 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003361 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003362 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3363 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003364 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003365 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003366 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003367 diag::err_odr_objc_method_param_type_inconsistent)
3368 << D->isInstanceMethod() << Name
3369 << (*P)->getType() << (*FoundP)->getType();
3370 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3371 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003372 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003373 }
3374 }
3375
3376 // Check variadic/non-variadic.
3377 // Check the number of parameters.
3378 if (D->isVariadic() != FoundMethod->isVariadic()) {
3379 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3380 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003381 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003382 diag::note_odr_objc_method_here)
3383 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003384 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003385 }
3386
3387 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003388 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003389 }
3390 }
3391
3392 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003393 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003394 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003395 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003396
Alp Toker314cc812014-01-25 16:55:45 +00003397 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003398
Gabor Marton26f72a92018-07-12 09:42:05 +00003399 ObjCMethodDecl *ToMethod;
3400 if (GetImportedOrCreateDecl(
3401 ToMethod, D, Importer.getToContext(), Loc,
3402 Importer.Import(D->getLocEnd()), Name.getObjCSelector(), ResultTy,
3403 ReturnTInfo, DC, D->isInstanceMethod(), D->isVariadic(),
3404 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3405 D->getImplementationControl(), D->hasRelatedResultType()))
3406 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003407
3408 // FIXME: When we decide to merge method definitions, we'll need to
3409 // deal with implicit parameters.
3410
3411 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003412 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003413 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003414 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003415 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003416 return nullptr;
3417
Douglas Gregor43f54792010-02-17 02:12:47 +00003418 ToParams.push_back(ToP);
3419 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003420
Douglas Gregor43f54792010-02-17 02:12:47 +00003421 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003422 for (auto *ToParam : ToParams) {
3423 ToParam->setOwningFunction(ToMethod);
3424 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003425 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003426
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003427 SmallVector<SourceLocation, 12> SelLocs;
3428 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003429 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003430 Loc = Importer.Import(Loc);
3431
3432 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003433
3434 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003435 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003436 return ToMethod;
3437}
3438
Douglas Gregor85f3f952015-07-07 03:57:15 +00003439Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3440 // Import the major distinguishing characteristics of a category.
3441 DeclContext *DC, *LexicalDC;
3442 DeclarationName Name;
3443 SourceLocation Loc;
3444 NamedDecl *ToD;
3445 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3446 return nullptr;
3447 if (ToD)
3448 return ToD;
3449
3450 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3451 if (!BoundInfo)
3452 return nullptr;
3453
Gabor Marton26f72a92018-07-12 09:42:05 +00003454 ObjCTypeParamDecl *Result;
3455 if (GetImportedOrCreateDecl(
3456 Result, D, Importer.getToContext(), DC, D->getVariance(),
3457 Importer.Import(D->getVarianceLoc()), D->getIndex(),
3458 Importer.Import(D->getLocation()), Name.getAsIdentifierInfo(),
3459 Importer.Import(D->getColonLoc()), BoundInfo))
3460 return Result;
3461
Douglas Gregor85f3f952015-07-07 03:57:15 +00003462 Result->setLexicalDeclContext(LexicalDC);
3463 return Result;
3464}
3465
Douglas Gregor84c51c32010-02-18 01:47:50 +00003466Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3467 // Import the major distinguishing characteristics of a category.
3468 DeclContext *DC, *LexicalDC;
3469 DeclarationName Name;
3470 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003471 NamedDecl *ToD;
3472 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003473 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003474 if (ToD)
3475 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003476
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003477 auto *ToInterface =
3478 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003479 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003480 return nullptr;
3481
Douglas Gregor84c51c32010-02-18 01:47:50 +00003482 // Determine if we've already encountered this category.
3483 ObjCCategoryDecl *MergeWithCategory
3484 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3485 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3486 if (!ToCategory) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003487
3488 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
3489 Importer.Import(D->getAtStartLoc()), Loc,
3490 Importer.Import(D->getCategoryNameLoc()),
3491 Name.getAsIdentifierInfo(), ToInterface,
3492 /*TypeParamList=*/nullptr,
3493 Importer.Import(D->getIvarLBraceLoc()),
3494 Importer.Import(D->getIvarRBraceLoc())))
3495 return ToCategory;
3496
Douglas Gregor84c51c32010-02-18 01:47:50 +00003497 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003498 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003499 // Import the type parameter list after calling Imported, to avoid
3500 // loops when bringing in their DeclContext.
3501 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3502 D->getTypeParamList()));
Fangrui Song6907ce22018-07-30 19:24:48 +00003503
Douglas Gregor84c51c32010-02-18 01:47:50 +00003504 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003505 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3506 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003507 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3508 = D->protocol_loc_begin();
3509 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3510 FromProtoEnd = D->protocol_end();
3511 FromProto != FromProtoEnd;
3512 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003513 auto *ToProto =
3514 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003515 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003516 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003517 Protocols.push_back(ToProto);
3518 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3519 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003520
Douglas Gregor84c51c32010-02-18 01:47:50 +00003521 // FIXME: If we're merging, make sure that the protocol list is the same.
3522 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3523 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003524 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003525 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003526 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003527
Douglas Gregor84c51c32010-02-18 01:47:50 +00003528 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003529 ImportDeclContext(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00003530
Douglas Gregor84c51c32010-02-18 01:47:50 +00003531 // If we have an implementation, import it as well.
3532 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003533 auto *Impl =
3534 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003535 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003536 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003537 return nullptr;
3538
Douglas Gregor84c51c32010-02-18 01:47:50 +00003539 ToCategory->setImplementation(Impl);
3540 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003541
Douglas Gregor84c51c32010-02-18 01:47:50 +00003542 return ToCategory;
3543}
3544
Fangrui Song6907ce22018-07-30 19:24:48 +00003545bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003546 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003547 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003548 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003549 if (shouldForceImportDeclContext(Kind))
3550 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003551 return false;
3552 }
3553
3554 // Start the protocol definition
3555 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00003556
Douglas Gregor2aa53772012-01-24 17:42:07 +00003557 // Import protocols
3558 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3559 SmallVector<SourceLocation, 4> ProtocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00003560 ObjCProtocolDecl::protocol_loc_iterator
Douglas Gregor2aa53772012-01-24 17:42:07 +00003561 FromProtoLoc = From->protocol_loc_begin();
3562 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3563 FromProtoEnd = From->protocol_end();
3564 FromProto != FromProtoEnd;
3565 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003566 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003567 if (!ToProto)
3568 return true;
3569 Protocols.push_back(ToProto);
3570 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3571 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003572
Douglas Gregor2aa53772012-01-24 17:42:07 +00003573 // FIXME: If we're merging, make sure that the protocol list is the same.
3574 To->setProtocolList(Protocols.data(), Protocols.size(),
3575 ProtocolLocs.data(), Importer.getToContext());
3576
Douglas Gregor2e15c842012-02-01 21:00:38 +00003577 if (shouldForceImportDeclContext(Kind)) {
3578 // Import all of the members of this protocol.
3579 ImportDeclContext(From, /*ForceImport=*/true);
3580 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003581 return false;
3582}
3583
Douglas Gregor98d156a2010-02-17 16:12:00 +00003584Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00003585 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00003586 // from, but this particular declaration is not that definition, import the
3587 // definition and map to that.
3588 ObjCProtocolDecl *Definition = D->getDefinition();
3589 if (Definition && Definition != D) {
3590 Decl *ImportedDef = Importer.Import(Definition);
3591 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003592 return nullptr;
3593
Gabor Marton26f72a92018-07-12 09:42:05 +00003594 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003595 }
3596
Douglas Gregor84c51c32010-02-18 01:47:50 +00003597 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003598 DeclContext *DC, *LexicalDC;
3599 DeclarationName Name;
3600 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003601 NamedDecl *ToD;
3602 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003603 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003604 if (ToD)
3605 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003606
Craig Topper36250ad2014-05-12 05:36:57 +00003607 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003608 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003609 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003610 for (auto *FoundDecl : FoundDecls) {
3611 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003612 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00003613
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003614 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003615 break;
3616 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003617
Douglas Gregor98d156a2010-02-17 16:12:00 +00003618 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003619 if (!ToProto) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003620 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
3621 Name.getAsIdentifierInfo(), Loc,
3622 Importer.Import(D->getAtStartLoc()),
3623 /*PrevDecl=*/nullptr))
3624 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003625 ToProto->setLexicalDeclContext(LexicalDC);
3626 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003627 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003628
3629 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003630
Douglas Gregor2aa53772012-01-24 17:42:07 +00003631 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003632 return nullptr;
3633
Douglas Gregor98d156a2010-02-17 16:12:00 +00003634 return ToProto;
3635}
3636
Sean Callanan0aae0412014-12-10 00:00:37 +00003637Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3638 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3639 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3640
3641 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3642 SourceLocation LangLoc = Importer.Import(D->getLocation());
3643
3644 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00003645
3646 LinkageSpecDecl *ToLinkageSpec;
3647 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
3648 ExternLoc, LangLoc, D->getLanguage(), HasBraces))
3649 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00003650
3651 if (HasBraces) {
3652 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3653 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3654 }
3655
3656 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3657 LexicalDC->addDeclInternal(ToLinkageSpec);
3658
Sean Callanan0aae0412014-12-10 00:00:37 +00003659 return ToLinkageSpec;
3660}
3661
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003662Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3663 DeclContext *DC, *LexicalDC;
3664 DeclarationName Name;
3665 SourceLocation Loc;
3666 NamedDecl *ToD = nullptr;
3667 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3668 return nullptr;
3669 if (ToD)
3670 return ToD;
3671
3672 DeclarationNameInfo NameInfo(Name,
3673 Importer.Import(D->getNameInfo().getLoc()));
3674 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3675
Gabor Marton26f72a92018-07-12 09:42:05 +00003676 UsingDecl *ToUsing;
3677 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3678 Importer.Import(D->getUsingLoc()),
3679 Importer.Import(D->getQualifierLoc()), NameInfo,
3680 D->hasTypename()))
3681 return ToUsing;
3682
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003683 ToUsing->setLexicalDeclContext(LexicalDC);
3684 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003685
3686 if (NamedDecl *FromPattern =
3687 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003688 if (auto *ToPattern =
3689 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003690 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3691 else
3692 return nullptr;
3693 }
3694
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003695 for (auto *FromShadow : D->shadows()) {
3696 if (auto *ToShadow =
3697 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003698 ToUsing->addShadowDecl(ToShadow);
3699 else
3700 // FIXME: We return a nullptr here but the definition is already created
3701 // and available with lookups. How to fix this?..
3702 return nullptr;
3703 }
3704 return ToUsing;
3705}
3706
3707Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3708 DeclContext *DC, *LexicalDC;
3709 DeclarationName Name;
3710 SourceLocation Loc;
3711 NamedDecl *ToD = nullptr;
3712 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3713 return nullptr;
3714 if (ToD)
3715 return ToD;
3716
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003717 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3718 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003719 if (!ToUsing)
3720 return nullptr;
3721
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003722 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3723 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003724 if (!ToTarget)
3725 return nullptr;
3726
Gabor Marton26f72a92018-07-12 09:42:05 +00003727 UsingShadowDecl *ToShadow;
3728 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
3729 ToUsing, ToTarget))
3730 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003731
3732 ToShadow->setLexicalDeclContext(LexicalDC);
3733 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003734
3735 if (UsingShadowDecl *FromPattern =
3736 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003737 if (auto *ToPattern =
3738 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003739 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3740 ToPattern);
3741 else
3742 // FIXME: We return a nullptr here but the definition is already created
3743 // and available with lookups. How to fix this?..
3744 return nullptr;
3745 }
3746
3747 LexicalDC->addDeclInternal(ToShadow);
3748
3749 return ToShadow;
3750}
3751
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003752Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3753 DeclContext *DC, *LexicalDC;
3754 DeclarationName Name;
3755 SourceLocation Loc;
3756 NamedDecl *ToD = nullptr;
3757 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3758 return nullptr;
3759 if (ToD)
3760 return ToD;
3761
3762 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3763 if (!ToComAncestor)
3764 return nullptr;
3765
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003766 auto *ToNominated = cast_or_null<NamespaceDecl>(
3767 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003768 if (!ToNominated)
3769 return nullptr;
3770
Gabor Marton26f72a92018-07-12 09:42:05 +00003771 UsingDirectiveDecl *ToUsingDir;
3772 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
3773 Importer.Import(D->getUsingLoc()),
3774 Importer.Import(D->getNamespaceKeyLocation()),
3775 Importer.Import(D->getQualifierLoc()),
3776 Importer.Import(D->getIdentLocation()),
3777 ToNominated, ToComAncestor))
3778 return ToUsingDir;
3779
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003780 ToUsingDir->setLexicalDeclContext(LexicalDC);
3781 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003782
3783 return ToUsingDir;
3784}
3785
3786Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3787 UnresolvedUsingValueDecl *D) {
3788 DeclContext *DC, *LexicalDC;
3789 DeclarationName Name;
3790 SourceLocation Loc;
3791 NamedDecl *ToD = nullptr;
3792 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3793 return nullptr;
3794 if (ToD)
3795 return ToD;
3796
3797 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3798 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3799
Gabor Marton26f72a92018-07-12 09:42:05 +00003800 UnresolvedUsingValueDecl *ToUsingValue;
3801 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
3802 Importer.Import(D->getUsingLoc()),
3803 Importer.Import(D->getQualifierLoc()), NameInfo,
3804 Importer.Import(D->getEllipsisLoc())))
3805 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003806
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003807 ToUsingValue->setAccess(D->getAccess());
3808 ToUsingValue->setLexicalDeclContext(LexicalDC);
3809 LexicalDC->addDeclInternal(ToUsingValue);
3810
3811 return ToUsingValue;
3812}
3813
3814Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3815 UnresolvedUsingTypenameDecl *D) {
3816 DeclContext *DC, *LexicalDC;
3817 DeclarationName Name;
3818 SourceLocation Loc;
3819 NamedDecl *ToD = nullptr;
3820 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3821 return nullptr;
3822 if (ToD)
3823 return ToD;
3824
Gabor Marton26f72a92018-07-12 09:42:05 +00003825 UnresolvedUsingTypenameDecl *ToUsing;
3826 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3827 Importer.Import(D->getUsingLoc()),
3828 Importer.Import(D->getTypenameLoc()),
3829 Importer.Import(D->getQualifierLoc()), Loc, Name,
3830 Importer.Import(D->getEllipsisLoc())))
3831 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003832
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003833 ToUsing->setAccess(D->getAccess());
3834 ToUsing->setLexicalDeclContext(LexicalDC);
3835 LexicalDC->addDeclInternal(ToUsing);
3836
3837 return ToUsing;
3838}
3839
Fangrui Song6907ce22018-07-30 19:24:48 +00003840bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003841 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003842 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003843 if (To->getDefinition()) {
3844 // Check consistency of superclass.
3845 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3846 if (FromSuper) {
3847 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3848 if (!FromSuper)
3849 return true;
3850 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003851
3852 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00003853 if ((bool)FromSuper != (bool)ToSuper ||
3854 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00003855 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00003856 diag::err_odr_objc_superclass_inconsistent)
3857 << To->getDeclName();
3858 if (ToSuper)
3859 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3860 << To->getSuperClass()->getDeclName();
3861 else
Fangrui Song6907ce22018-07-30 19:24:48 +00003862 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00003863 diag::note_odr_objc_missing_superclass);
3864 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00003865 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00003866 diag::note_odr_objc_superclass)
3867 << From->getSuperClass()->getDeclName();
3868 else
Fangrui Song6907ce22018-07-30 19:24:48 +00003869 Importer.FromDiag(From->getLocation(),
3870 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003871 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003872
Douglas Gregor2e15c842012-02-01 21:00:38 +00003873 if (shouldForceImportDeclContext(Kind))
3874 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003875 return false;
3876 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003877
Douglas Gregor2aa53772012-01-24 17:42:07 +00003878 // Start the definition.
3879 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00003880
Douglas Gregor2aa53772012-01-24 17:42:07 +00003881 // If this class has a superclass, import it.
3882 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003883 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3884 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003885 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003886
3887 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003888 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003889
Douglas Gregor2aa53772012-01-24 17:42:07 +00003890 // Import protocols
3891 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3892 SmallVector<SourceLocation, 4> ProtocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00003893 ObjCInterfaceDecl::protocol_loc_iterator
Douglas Gregor2aa53772012-01-24 17:42:07 +00003894 FromProtoLoc = From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00003895
Douglas Gregor2aa53772012-01-24 17:42:07 +00003896 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3897 FromProtoEnd = From->protocol_end();
3898 FromProto != FromProtoEnd;
3899 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003900 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003901 if (!ToProto)
3902 return true;
3903 Protocols.push_back(ToProto);
3904 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3905 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003906
Douglas Gregor2aa53772012-01-24 17:42:07 +00003907 // FIXME: If we're merging, make sure that the protocol list is the same.
3908 To->setProtocolList(Protocols.data(), Protocols.size(),
3909 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00003910
Douglas Gregor2aa53772012-01-24 17:42:07 +00003911 // Import categories. When the categories themselves are imported, they'll
3912 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003913 for (auto *Cat : From->known_categories())
3914 Importer.Import(Cat);
Fangrui Song6907ce22018-07-30 19:24:48 +00003915
Douglas Gregor2aa53772012-01-24 17:42:07 +00003916 // If we have an @implementation, import it as well.
3917 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003918 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3919 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003920 if (!Impl)
3921 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00003922
Douglas Gregor2aa53772012-01-24 17:42:07 +00003923 To->setImplementation(Impl);
3924 }
3925
Douglas Gregor2e15c842012-02-01 21:00:38 +00003926 if (shouldForceImportDeclContext(Kind)) {
3927 // Import all of the members of this class.
3928 ImportDeclContext(From, /*ForceImport=*/true);
3929 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003930 return false;
3931}
3932
Douglas Gregor85f3f952015-07-07 03:57:15 +00003933ObjCTypeParamList *
3934ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3935 if (!list)
3936 return nullptr;
3937
3938 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3939 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003940 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3941 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003942 if (!toTypeParam)
3943 return nullptr;
3944
3945 toTypeParams.push_back(toTypeParam);
3946 }
3947
3948 return ObjCTypeParamList::create(Importer.getToContext(),
3949 Importer.Import(list->getLAngleLoc()),
3950 toTypeParams,
3951 Importer.Import(list->getRAngleLoc()));
3952}
3953
Douglas Gregor45635322010-02-16 01:20:57 +00003954Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003955 // If this class has a definition in the translation unit we're coming from,
3956 // but this particular declaration is not that definition, import the
3957 // definition and map to that.
3958 ObjCInterfaceDecl *Definition = D->getDefinition();
3959 if (Definition && Definition != D) {
3960 Decl *ImportedDef = Importer.Import(Definition);
3961 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003962 return nullptr;
3963
Gabor Marton26f72a92018-07-12 09:42:05 +00003964 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003965 }
3966
Douglas Gregor45635322010-02-16 01:20:57 +00003967 // Import the major distinguishing characteristics of an @interface.
3968 DeclContext *DC, *LexicalDC;
3969 DeclarationName Name;
3970 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003971 NamedDecl *ToD;
3972 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003973 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003974 if (ToD)
3975 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003976
Douglas Gregor2aa53772012-01-24 17:42:07 +00003977 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003978 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003979 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003980 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003981 for (auto *FoundDecl : FoundDecls) {
3982 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003983 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00003984
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003985 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003986 break;
3987 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003988
Douglas Gregor2aa53772012-01-24 17:42:07 +00003989 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003990 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003991 if (!ToIface) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003992 if (GetImportedOrCreateDecl(
3993 ToIface, D, Importer.getToContext(), DC,
3994 Importer.Import(D->getAtStartLoc()), Name.getAsIdentifierInfo(),
3995 /*TypeParamList=*/nullptr,
3996 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
3997 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003998 ToIface->setLexicalDeclContext(LexicalDC);
3999 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004000 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004001 Importer.MapImported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004002 // Import the type parameter list after calling Imported, to avoid
4003 // loops when bringing in their DeclContext.
4004 ToIface->setTypeParamList(ImportObjCTypeParamList(
4005 D->getTypeParamListAsWritten()));
Fangrui Song6907ce22018-07-30 19:24:48 +00004006
Douglas Gregor2aa53772012-01-24 17:42:07 +00004007 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004008 return nullptr;
4009
Douglas Gregor98d156a2010-02-17 16:12:00 +00004010 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004011}
4012
Douglas Gregor4da9d682010-12-07 15:32:12 +00004013Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004014 auto *Category = cast_or_null<ObjCCategoryDecl>(
4015 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00004016 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004017 return nullptr;
4018
Douglas Gregor4da9d682010-12-07 15:32:12 +00004019 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4020 if (!ToImpl) {
4021 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4022 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004023 return nullptr;
4024
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004025 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Gabor Marton26f72a92018-07-12 09:42:05 +00004026 if (GetImportedOrCreateDecl(
4027 ToImpl, D, Importer.getToContext(), DC,
4028 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
4029 Importer.Import(D->getLocation()),
4030 Importer.Import(D->getAtStartLoc()), CategoryNameLoc))
4031 return ToImpl;
4032
Douglas Gregor4da9d682010-12-07 15:32:12 +00004033 DeclContext *LexicalDC = DC;
4034 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4035 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4036 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004037 return nullptr;
4038
Douglas Gregor4da9d682010-12-07 15:32:12 +00004039 ToImpl->setLexicalDeclContext(LexicalDC);
4040 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004041
Sean Callanan95e74be2011-10-21 02:57:43 +00004042 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004043 Category->setImplementation(ToImpl);
4044 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004045
Gabor Marton26f72a92018-07-12 09:42:05 +00004046 Importer.MapImported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004047 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004048 return ToImpl;
4049}
4050
Douglas Gregorda8025c2010-12-07 01:26:03 +00004051Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4052 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004053 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
4054 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004055 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004056 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004057
4058 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004059 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004060 if (D->getSuperClass()) {
4061 Super = cast_or_null<ObjCInterfaceDecl>(
4062 Importer.Import(D->getSuperClass()));
4063 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004064 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004065 }
4066
4067 ObjCImplementationDecl *Impl = Iface->getImplementation();
4068 if (!Impl) {
4069 // We haven't imported an implementation yet. Create a new @implementation
4070 // now.
Gabor Marton26f72a92018-07-12 09:42:05 +00004071 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
4072 Importer.ImportContext(D->getDeclContext()),
4073 Iface, Super, Importer.Import(D->getLocation()),
4074 Importer.Import(D->getAtStartLoc()),
4075 Importer.Import(D->getSuperClassLoc()),
4076 Importer.Import(D->getIvarLBraceLoc()),
4077 Importer.Import(D->getIvarRBraceLoc())))
4078 return Impl;
4079
Douglas Gregorda8025c2010-12-07 01:26:03 +00004080 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4081 DeclContext *LexicalDC
4082 = Importer.ImportContext(D->getLexicalDeclContext());
4083 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004084 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004085 Impl->setLexicalDeclContext(LexicalDC);
4086 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004087
Douglas Gregorda8025c2010-12-07 01:26:03 +00004088 // Associate the implementation with the class it implements.
4089 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004090 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004091 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004092 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004093
4094 // Verify that the existing @implementation has the same superclass.
4095 if ((Super && !Impl->getSuperClass()) ||
4096 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004097 (Super && Impl->getSuperClass() &&
4098 !declaresSameEntity(Super->getCanonicalDecl(),
4099 Impl->getSuperClass()))) {
4100 Importer.ToDiag(Impl->getLocation(),
4101 diag::err_odr_objc_superclass_inconsistent)
4102 << Iface->getDeclName();
4103 // FIXME: It would be nice to have the location of the superclass
4104 // below.
4105 if (Impl->getSuperClass())
4106 Importer.ToDiag(Impl->getLocation(),
4107 diag::note_odr_objc_superclass)
4108 << Impl->getSuperClass()->getDeclName();
4109 else
4110 Importer.ToDiag(Impl->getLocation(),
4111 diag::note_odr_objc_missing_superclass);
4112 if (D->getSuperClass())
4113 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004114 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004115 << D->getSuperClass()->getDeclName();
4116 else
4117 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004118 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004119 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004120 }
4121 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004122
Douglas Gregorda8025c2010-12-07 01:26:03 +00004123 // Import all of the members of this @implementation.
4124 ImportDeclContext(D);
4125
4126 return Impl;
4127}
4128
Douglas Gregora11c4582010-02-17 18:02:10 +00004129Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4130 // Import the major distinguishing characteristics of an @property.
4131 DeclContext *DC, *LexicalDC;
4132 DeclarationName Name;
4133 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004134 NamedDecl *ToD;
4135 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004136 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004137 if (ToD)
4138 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004139
4140 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004141 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004142 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004143 for (auto *FoundDecl : FoundDecls) {
4144 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004145 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004146 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004147 FoundProp->getType())) {
4148 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4149 << Name << D->getType() << FoundProp->getType();
4150 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4151 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004152 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004153 }
4154
4155 // FIXME: Check property attributes, getters, setters, etc.?
4156
4157 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004158 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004159 return FoundProp;
4160 }
4161 }
4162
4163 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004164 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4165 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004166 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004167
4168 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004169 ObjCPropertyDecl *ToProperty;
4170 if (GetImportedOrCreateDecl(
4171 ToProperty, D, Importer.getToContext(), DC, Loc,
4172 Name.getAsIdentifierInfo(), Importer.Import(D->getAtLoc()),
4173 Importer.Import(D->getLParenLoc()), Importer.Import(D->getType()),
4174 TSI, D->getPropertyImplementation()))
4175 return ToProperty;
4176
Douglas Gregora11c4582010-02-17 18:02:10 +00004177 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004178 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004179
4180 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004181 ToProperty->setPropertyAttributesAsWritten(
4182 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00004183 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
4184 Importer.Import(D->getGetterNameLoc()));
4185 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
4186 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00004187 ToProperty->setGetterMethodDecl(
4188 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4189 ToProperty->setSetterMethodDecl(
4190 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4191 ToProperty->setPropertyIvarDecl(
4192 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4193 return ToProperty;
4194}
4195
Douglas Gregor14a49e22010-12-07 18:32:03 +00004196Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004197 auto *Property = cast_or_null<ObjCPropertyDecl>(
4198 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00004199 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004200 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004201
4202 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4203 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004204 return nullptr;
4205
Douglas Gregor14a49e22010-12-07 18:32:03 +00004206 // Import the lexical declaration context.
4207 DeclContext *LexicalDC = DC;
4208 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4209 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4210 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004211 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004212 }
4213
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004214 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004215 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004216 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004217
4218 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004219 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004220 if (D->getPropertyIvarDecl()) {
4221 Ivar = cast_or_null<ObjCIvarDecl>(
4222 Importer.Import(D->getPropertyIvarDecl()));
4223 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004224 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004225 }
4226
4227 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004228 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4229 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004230 if (!ToImpl) {
4231 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
4232 Importer.Import(D->getLocStart()),
4233 Importer.Import(D->getLocation()), Property,
4234 D->getPropertyImplementation(), Ivar,
4235 Importer.Import(D->getPropertyIvarDeclLoc())))
4236 return ToImpl;
4237
Douglas Gregor14a49e22010-12-07 18:32:03 +00004238 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004239 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004240 } else {
4241 // Check that we have the same kind of property implementation (@synthesize
4242 // vs. @dynamic).
4243 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004244 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004245 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004246 << Property->getDeclName()
4247 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004248 == ObjCPropertyImplDecl::Dynamic);
4249 Importer.FromDiag(D->getLocation(),
4250 diag::note_odr_objc_property_impl_kind)
4251 << D->getPropertyDecl()->getDeclName()
4252 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004253 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004254 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004255
4256 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004257 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4258 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004259 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004260 diag::err_odr_objc_synthesize_ivar_inconsistent)
4261 << Property->getDeclName()
4262 << ToImpl->getPropertyIvarDecl()->getDeclName()
4263 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004264 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004265 diag::note_odr_objc_synthesize_ivar_here)
4266 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004267 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004268 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004269
Douglas Gregor14a49e22010-12-07 18:32:03 +00004270 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004271 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004272 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004273
Douglas Gregor14a49e22010-12-07 18:32:03 +00004274 return ToImpl;
4275}
4276
Douglas Gregora082a492010-11-30 19:14:50 +00004277Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4278 // For template arguments, we adopt the translation unit as our declaration
4279 // context. This context will be fixed when the actual template declaration
4280 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004281
Douglas Gregora082a492010-11-30 19:14:50 +00004282 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004283 TemplateTypeParmDecl *ToD = nullptr;
4284 (void)GetImportedOrCreateDecl(
4285 ToD, D, Importer.getToContext(),
4286 Importer.getToContext().getTranslationUnitDecl(),
4287 Importer.Import(D->getLocStart()), Importer.Import(D->getLocation()),
4288 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4289 D->wasDeclaredWithTypename(), D->isParameterPack());
4290 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004291}
4292
4293Decl *
4294ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4295 // Import the name of this declaration.
4296 DeclarationName Name = Importer.Import(D->getDeclName());
4297 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004298 return nullptr;
4299
Douglas Gregora082a492010-11-30 19:14:50 +00004300 // Import the location of this declaration.
4301 SourceLocation Loc = Importer.Import(D->getLocation());
4302
4303 // Import the type of this declaration.
4304 QualType T = Importer.Import(D->getType());
4305 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004306 return nullptr;
4307
Douglas Gregora082a492010-11-30 19:14:50 +00004308 // Import type-source information.
4309 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4310 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004311 return nullptr;
4312
Douglas Gregora082a492010-11-30 19:14:50 +00004313 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004314
4315 NonTypeTemplateParmDecl *ToD = nullptr;
4316 (void)GetImportedOrCreateDecl(
4317 ToD, D, Importer.getToContext(),
4318 Importer.getToContext().getTranslationUnitDecl(),
4319 Importer.Import(D->getInnerLocStart()), Loc, D->getDepth(),
4320 D->getPosition(), Name.getAsIdentifierInfo(), T, D->isParameterPack(),
4321 TInfo);
4322 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004323}
4324
4325Decl *
4326ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4327 // Import the name of this declaration.
4328 DeclarationName Name = Importer.Import(D->getDeclName());
4329 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004330 return nullptr;
4331
Douglas Gregora082a492010-11-30 19:14:50 +00004332 // Import the location of this declaration.
4333 SourceLocation Loc = Importer.Import(D->getLocation());
Gabor Marton26f72a92018-07-12 09:42:05 +00004334
Douglas Gregora082a492010-11-30 19:14:50 +00004335 // Import template parameters.
4336 TemplateParameterList *TemplateParams
4337 = ImportTemplateParameterList(D->getTemplateParameters());
4338 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004339 return nullptr;
4340
Douglas Gregora082a492010-11-30 19:14:50 +00004341 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004342
4343 TemplateTemplateParmDecl *ToD = nullptr;
4344 (void)GetImportedOrCreateDecl(
4345 ToD, D, Importer.getToContext(),
4346 Importer.getToContext().getTranslationUnitDecl(), Loc, D->getDepth(),
4347 D->getPosition(), D->isParameterPack(), Name.getAsIdentifierInfo(),
4348 TemplateParams);
4349 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004350}
4351
Gabor Marton9581c332018-05-23 13:53:36 +00004352// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4353// it has any definition in the redecl chain.
4354static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4355 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4356 if (!ToTemplatedDef)
4357 return nullptr;
4358 ClassTemplateDecl *TemplateWithDef =
4359 ToTemplatedDef->getDescribedClassTemplate();
4360 return TemplateWithDef;
4361}
4362
Douglas Gregora082a492010-11-30 19:14:50 +00004363Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4364 // If this record has a definition in the translation unit we're coming from,
4365 // but this particular declaration is not that definition, import the
4366 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004367 auto *Definition =
4368 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004369 if (Definition && Definition != D->getTemplatedDecl()) {
4370 Decl *ImportedDef
4371 = Importer.Import(Definition->getDescribedClassTemplate());
4372 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004373 return nullptr;
4374
Gabor Marton26f72a92018-07-12 09:42:05 +00004375 return Importer.MapImported(D, ImportedDef);
Douglas Gregora082a492010-11-30 19:14:50 +00004376 }
Gabor Marton9581c332018-05-23 13:53:36 +00004377
Douglas Gregora082a492010-11-30 19:14:50 +00004378 // Import the major distinguishing characteristics of this class template.
4379 DeclContext *DC, *LexicalDC;
4380 DeclarationName Name;
4381 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004382 NamedDecl *ToD;
4383 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004384 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004385 if (ToD)
4386 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004387
Douglas Gregora082a492010-11-30 19:14:50 +00004388 // We may already have a template of the same name; try to find and match it.
4389 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004390 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004391 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004392 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004393 for (auto *FoundDecl : FoundDecls) {
4394 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004395 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004396
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004397 Decl *Found = FoundDecl;
4398 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00004399
4400 // The class to be imported is a definition.
4401 if (D->isThisDeclarationADefinition()) {
4402 // Lookup will find the fwd decl only if that is more recent than the
4403 // definition. So, try to get the definition if that is available in
4404 // the redecl chain.
4405 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
4406 if (!TemplateWithDef)
4407 continue;
4408 FoundTemplate = TemplateWithDef; // Continue with the definition.
4409 }
4410
Douglas Gregora082a492010-11-30 19:14:50 +00004411 if (IsStructuralMatch(D, FoundTemplate)) {
4412 // The class templates structurally match; call it the same template.
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004413
Gabor Marton26f72a92018-07-12 09:42:05 +00004414 Importer.MapImported(D->getTemplatedDecl(),
4415 FoundTemplate->getTemplatedDecl());
4416 return Importer.MapImported(D, FoundTemplate);
Gabor Marton9581c332018-05-23 13:53:36 +00004417 }
Douglas Gregora082a492010-11-30 19:14:50 +00004418 }
Gabor Marton9581c332018-05-23 13:53:36 +00004419
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004420 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004421 }
Gabor Marton9581c332018-05-23 13:53:36 +00004422
Douglas Gregora082a492010-11-30 19:14:50 +00004423 if (!ConflictingDecls.empty()) {
4424 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004425 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004426 ConflictingDecls.size());
4427 }
Gabor Marton9581c332018-05-23 13:53:36 +00004428
Douglas Gregora082a492010-11-30 19:14:50 +00004429 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004430 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004431 }
4432
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004433 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4434
Douglas Gregora082a492010-11-30 19:14:50 +00004435 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004436 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4437 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004438 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004439 return nullptr;
4440
Douglas Gregora082a492010-11-30 19:14:50 +00004441 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004442 TemplateParameterList *TemplateParams =
4443 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004444 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004445 return nullptr;
4446
Gabor Marton26f72a92018-07-12 09:42:05 +00004447 ClassTemplateDecl *D2;
4448 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
4449 TemplateParams, ToTemplated))
4450 return D2;
4451
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004452 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004453
Douglas Gregora082a492010-11-30 19:14:50 +00004454 D2->setAccess(D->getAccess());
4455 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004456 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004457
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004458 if (FromTemplated->isCompleteDefinition() &&
4459 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004460 // FIXME: Import definition!
4461 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004462
Douglas Gregora082a492010-11-30 19:14:50 +00004463 return D2;
4464}
4465
Douglas Gregore2e50d332010-12-01 01:36:18 +00004466Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4467 ClassTemplateSpecializationDecl *D) {
4468 // If this record has a definition in the translation unit we're coming from,
4469 // but this particular declaration is not that definition, import the
4470 // definition and map to that.
4471 TagDecl *Definition = D->getDefinition();
4472 if (Definition && Definition != D) {
4473 Decl *ImportedDef = Importer.Import(Definition);
4474 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004475 return nullptr;
4476
Gabor Marton26f72a92018-07-12 09:42:05 +00004477 return Importer.MapImported(D, ImportedDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004478 }
4479
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004480 auto *ClassTemplate =
4481 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004482 D->getSpecializedTemplate()));
4483 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004484 return nullptr;
4485
Douglas Gregore2e50d332010-12-01 01:36:18 +00004486 // Import the context of this declaration.
4487 DeclContext *DC = ClassTemplate->getDeclContext();
4488 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004489 return nullptr;
4490
Douglas Gregore2e50d332010-12-01 01:36:18 +00004491 DeclContext *LexicalDC = DC;
4492 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4493 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4494 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004495 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004496 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004497
Douglas Gregore2e50d332010-12-01 01:36:18 +00004498 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004499 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4500 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004501
4502 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004503 SmallVector<TemplateArgument, 2> TemplateArgs;
Fangrui Song6907ce22018-07-30 19:24:48 +00004504 if (ImportTemplateArguments(D->getTemplateArgs().data(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00004505 D->getTemplateArgs().size(),
4506 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004507 return nullptr;
4508
Douglas Gregore2e50d332010-12-01 01:36:18 +00004509 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004510 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004511 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004512 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004513 if (D2) {
4514 // We already have a class template specialization with these template
4515 // arguments.
Fangrui Song6907ce22018-07-30 19:24:48 +00004516
Douglas Gregore2e50d332010-12-01 01:36:18 +00004517 // FIXME: Check for specialization vs. instantiation errors.
Fangrui Song6907ce22018-07-30 19:24:48 +00004518
Douglas Gregore2e50d332010-12-01 01:36:18 +00004519 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004520 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004521 // The record types structurally match, or the "from" translation
4522 // unit only had a forward declaration anyway; call it the same
4523 // function.
Gabor Marton26f72a92018-07-12 09:42:05 +00004524 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004525 }
4526 }
4527 } else {
4528 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004529 if (auto *PartialSpec =
4530 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004531 // Import TemplateArgumentListInfo
4532 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004533 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4534 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004535 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004536
4537 QualType CanonInjType = Importer.Import(
4538 PartialSpec->getInjectedSpecializationType());
4539 if (CanonInjType.isNull())
4540 return nullptr;
4541 CanonInjType = CanonInjType.getCanonicalType();
4542
4543 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4544 PartialSpec->getTemplateParameters());
4545 if (!ToTPList && PartialSpec->getTemplateParameters())
4546 return nullptr;
4547
Gabor Marton26f72a92018-07-12 09:42:05 +00004548 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
4549 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4550 IdLoc, ToTPList, ClassTemplate,
4551 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4552 ToTAInfo, CanonInjType, nullptr))
4553 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004554
4555 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004556 if (GetImportedOrCreateDecl(
4557 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4558 IdLoc, ClassTemplate, TemplateArgs, /*PrevDecl=*/nullptr))
4559 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004560 }
4561
Douglas Gregore2e50d332010-12-01 01:36:18 +00004562 D2->setSpecializationKind(D->getSpecializationKind());
4563
4564 // Add this specialization to the class template.
4565 ClassTemplate->AddSpecialization(D2, InsertPos);
Fangrui Song6907ce22018-07-30 19:24:48 +00004566
Douglas Gregore2e50d332010-12-01 01:36:18 +00004567 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004568 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004569
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004570 if (auto *TSI = D->getTypeAsWritten()) {
4571 TypeSourceInfo *TInfo = Importer.Import(TSI);
4572 if (!TInfo)
4573 return nullptr;
4574 D2->setTypeAsWritten(TInfo);
4575 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4576 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4577 }
4578
4579 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4580 if (POI.isValid())
4581 D2->setPointOfInstantiation(POI);
4582 else if (D->getPointOfInstantiation().isValid())
4583 return nullptr;
4584
4585 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4586
Gabor Martonb14056b2018-05-25 11:21:24 +00004587 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00004588 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00004589
4590 // Add to the DC only if it was an explicit specialization/instantiation.
4591 if (D2->isExplicitInstantiationOrSpecialization()) {
4592 LexicalDC->addDeclInternal(D2);
4593 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004594 }
John McCallf937c022011-10-07 06:10:15 +00004595 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004596 return nullptr;
4597
Douglas Gregore2e50d332010-12-01 01:36:18 +00004598 return D2;
4599}
4600
Larisse Voufo39a1e502013-08-06 01:03:05 +00004601Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4602 // If this variable has a definition in the translation unit we're coming
4603 // from,
4604 // but this particular declaration is not that definition, import the
4605 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004606 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004607 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4608 if (Definition && Definition != D->getTemplatedDecl()) {
4609 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4610 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004611 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004612
Gabor Marton26f72a92018-07-12 09:42:05 +00004613 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004614 }
4615
4616 // Import the major distinguishing characteristics of this variable template.
4617 DeclContext *DC, *LexicalDC;
4618 DeclarationName Name;
4619 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004620 NamedDecl *ToD;
4621 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004622 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004623 if (ToD)
4624 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004625
4626 // We may already have a template of the same name; try to find and match it.
4627 assert(!DC->isFunctionOrMethod() &&
4628 "Variable templates cannot be declared at function scope");
4629 SmallVector<NamedDecl *, 4> ConflictingDecls;
4630 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004631 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004632 for (auto *FoundDecl : FoundDecls) {
4633 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004634 continue;
4635
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004636 Decl *Found = FoundDecl;
4637 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004638 if (IsStructuralMatch(D, FoundTemplate)) {
4639 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00004640 Importer.MapImported(D->getTemplatedDecl(),
4641 FoundTemplate->getTemplatedDecl());
4642 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004643 }
4644 }
4645
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004646 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004647 }
4648
4649 if (!ConflictingDecls.empty()) {
4650 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4651 ConflictingDecls.data(),
4652 ConflictingDecls.size());
4653 }
4654
4655 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004656 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004657
4658 VarDecl *DTemplated = D->getTemplatedDecl();
4659
4660 // Import the type.
4661 QualType T = Importer.Import(DTemplated->getType());
4662 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004663 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004664
4665 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004666 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4667 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004668 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004669
4670 // Create the variable template declaration itself.
4671 TemplateParameterList *TemplateParams =
4672 ImportTemplateParameterList(D->getTemplateParameters());
4673 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004674 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004675
Gabor Marton26f72a92018-07-12 09:42:05 +00004676 VarTemplateDecl *ToVarTD;
4677 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
4678 Name, TemplateParams, ToTemplated))
4679 return ToVarTD;
4680
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004681 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004682
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004683 ToVarTD->setAccess(D->getAccess());
4684 ToVarTD->setLexicalDeclContext(LexicalDC);
4685 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004686
Larisse Voufo39a1e502013-08-06 01:03:05 +00004687 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004688 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004689 // FIXME: Import definition!
4690 }
4691
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004692 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004693}
4694
4695Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4696 VarTemplateSpecializationDecl *D) {
4697 // If this record has a definition in the translation unit we're coming from,
4698 // but this particular declaration is not that definition, import the
4699 // definition and map to that.
4700 VarDecl *Definition = D->getDefinition();
4701 if (Definition && Definition != D) {
4702 Decl *ImportedDef = Importer.Import(Definition);
4703 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004704 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004705
Gabor Marton26f72a92018-07-12 09:42:05 +00004706 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004707 }
4708
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004709 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004710 Importer.Import(D->getSpecializedTemplate()));
4711 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004712 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004713
4714 // Import the context of this declaration.
4715 DeclContext *DC = VarTemplate->getDeclContext();
4716 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004717 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004718
4719 DeclContext *LexicalDC = DC;
4720 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4721 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4722 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004723 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004724 }
4725
4726 // Import the location of this declaration.
4727 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4728 SourceLocation IdLoc = Importer.Import(D->getLocation());
4729
4730 // Import template arguments.
4731 SmallVector<TemplateArgument, 2> TemplateArgs;
4732 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4733 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004734 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004735
4736 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004737 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004738 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004739 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004740 if (D2) {
4741 // We already have a variable template specialization with these template
4742 // arguments.
4743
4744 // FIXME: Check for specialization vs. instantiation errors.
4745
4746 if (VarDecl *FoundDef = D2->getDefinition()) {
4747 if (!D->isThisDeclarationADefinition() ||
4748 IsStructuralMatch(D, FoundDef)) {
4749 // The record types structurally match, or the "from" translation
4750 // unit only had a forward declaration anyway; call it the same
4751 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00004752 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004753 }
4754 }
4755 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004756 // Import the type.
4757 QualType T = Importer.Import(D->getType());
4758 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004759 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004760
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004761 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4762 if (D->getTypeSourceInfo() && !TInfo)
4763 return nullptr;
4764
4765 TemplateArgumentListInfo ToTAInfo;
4766 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4767 return nullptr;
4768
4769 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004770 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004771 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4772 // Import TemplateArgumentListInfo
4773 TemplateArgumentListInfo ArgInfos;
4774 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4775 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4776 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4777 return nullptr;
4778
4779 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4780 FromPartial->getTemplateParameters());
4781 if (!ToTPList)
4782 return nullptr;
4783
Gabor Marton26f72a92018-07-12 09:42:05 +00004784 PartVarSpecDecl *ToPartial;
4785 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
4786 StartLoc, IdLoc, ToTPList, VarTemplate, T,
4787 TInfo, D->getStorageClass(), TemplateArgs,
4788 ArgInfos))
4789 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004790
4791 auto *FromInst = FromPartial->getInstantiatedFromMember();
4792 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4793 if (FromInst && !ToInst)
4794 return nullptr;
4795
4796 ToPartial->setInstantiatedFromMember(ToInst);
4797 if (FromPartial->isMemberSpecialization())
4798 ToPartial->setMemberSpecialization();
4799
4800 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004801 } else { // Full specialization
Gabor Marton26f72a92018-07-12 09:42:05 +00004802 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, StartLoc,
4803 IdLoc, VarTemplate, T, TInfo,
4804 D->getStorageClass(), TemplateArgs))
4805 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004806 }
4807
4808 SourceLocation POI = D->getPointOfInstantiation();
4809 if (POI.isValid())
4810 D2->setPointOfInstantiation(Importer.Import(POI));
4811
Larisse Voufo39a1e502013-08-06 01:03:05 +00004812 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004813 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004814
4815 // Add this specialization to the class template.
4816 VarTemplate->AddSpecialization(D2, InsertPos);
4817
4818 // Import the qualifier, if any.
4819 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4820
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004821 if (D->isConstexpr())
4822 D2->setConstexpr(true);
4823
Larisse Voufo39a1e502013-08-06 01:03:05 +00004824 // Add the specialization to this context.
4825 D2->setLexicalDeclContext(LexicalDC);
4826 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004827
4828 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004829 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004830
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004831 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4832 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4833 // we have to check the declaration for initializer - otherwise, it won't be
4834 // imported.
4835 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4836 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004837 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004838
4839 return D2;
4840}
4841
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004842Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4843 DeclContext *DC, *LexicalDC;
4844 DeclarationName Name;
4845 SourceLocation Loc;
4846 NamedDecl *ToD;
4847
4848 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4849 return nullptr;
4850
4851 if (ToD)
4852 return ToD;
4853
4854 // Try to find a function in our own ("to") context with the same name, same
4855 // type, and in the same context as the function we're importing.
4856 if (!LexicalDC->isFunctionOrMethod()) {
4857 unsigned IDNS = Decl::IDNS_Ordinary;
4858 SmallVector<NamedDecl *, 2> FoundDecls;
4859 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004860 for (auto *FoundDecl : FoundDecls) {
4861 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004862 continue;
4863
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004864 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004865 if (FoundFunction->hasExternalFormalLinkage() &&
4866 D->hasExternalFormalLinkage()) {
4867 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00004868 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004869 // FIXME: Actually try to merge the body and other attributes.
4870 return FoundFunction;
4871 }
4872 }
4873 }
4874 }
4875 }
4876
4877 TemplateParameterList *Params =
4878 ImportTemplateParameterList(D->getTemplateParameters());
4879 if (!Params)
4880 return nullptr;
4881
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004882 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004883 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4884 if (!TemplatedFD)
4885 return nullptr;
4886
Gabor Marton26f72a92018-07-12 09:42:05 +00004887 FunctionTemplateDecl *ToFunc;
4888 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
4889 Params, TemplatedFD))
4890 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004891
4892 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4893 ToFunc->setAccess(D->getAccess());
4894 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004895
4896 LexicalDC->addDeclInternal(ToFunc);
4897 return ToFunc;
4898}
4899
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004900//----------------------------------------------------------------------------
4901// Import Statements
4902//----------------------------------------------------------------------------
4903
Sean Callanan59721b32015-04-28 18:41:46 +00004904DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4905 if (DG.isNull())
4906 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4907 size_t NumDecls = DG.end() - DG.begin();
4908 SmallVector<Decl *, 1> ToDecls(NumDecls);
4909 auto &_Importer = this->Importer;
4910 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4911 [&_Importer](Decl *D) -> Decl * {
4912 return _Importer.Import(D);
4913 });
4914 return DeclGroupRef::Create(Importer.getToContext(),
4915 ToDecls.begin(),
4916 NumDecls);
4917}
4918
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004919Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4920 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4921 << S->getStmtClassName();
4922 return nullptr;
4923}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004924
4925Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4926 SmallVector<IdentifierInfo *, 4> Names;
4927 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4928 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004929 // ToII is nullptr when no symbolic name is given for output operand
4930 // see ParseStmtAsm::ParseAsmOperandsOpt
4931 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004932 return nullptr;
4933 Names.push_back(ToII);
4934 }
4935 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4936 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004937 // ToII is nullptr when no symbolic name is given for input operand
4938 // see ParseStmtAsm::ParseAsmOperandsOpt
4939 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004940 return nullptr;
4941 Names.push_back(ToII);
4942 }
4943
4944 SmallVector<StringLiteral *, 4> Clobbers;
4945 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004946 auto *Clobber = cast_or_null<StringLiteral>(
4947 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004948 if (!Clobber)
4949 return nullptr;
4950 Clobbers.push_back(Clobber);
4951 }
4952
4953 SmallVector<StringLiteral *, 4> Constraints;
4954 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004955 auto *Output = cast_or_null<StringLiteral>(
4956 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004957 if (!Output)
4958 return nullptr;
4959 Constraints.push_back(Output);
4960 }
4961
4962 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004963 auto *Input = cast_or_null<StringLiteral>(
4964 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004965 if (!Input)
4966 return nullptr;
4967 Constraints.push_back(Input);
4968 }
4969
4970 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004971 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004972 return nullptr;
4973
Aleksei Sidorina693b372016-09-28 10:16:56 +00004974 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004975 return nullptr;
4976
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004977 auto *AsmStr = cast_or_null<StringLiteral>(
4978 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004979 if (!AsmStr)
4980 return nullptr;
4981
4982 return new (Importer.getToContext()) GCCAsmStmt(
4983 Importer.getToContext(),
4984 Importer.Import(S->getAsmLoc()),
4985 S->isSimple(),
4986 S->isVolatile(),
4987 S->getNumOutputs(),
4988 S->getNumInputs(),
4989 Names.data(),
4990 Constraints.data(),
4991 Exprs.data(),
4992 AsmStr,
4993 S->getNumClobbers(),
4994 Clobbers.data(),
4995 Importer.Import(S->getRParenLoc()));
4996}
4997
Sean Callanan59721b32015-04-28 18:41:46 +00004998Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4999 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005000 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00005001 if (!ToD)
5002 return nullptr;
5003 }
5004 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
5005 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
5006 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
5007}
5008
5009Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5010 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
5011 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
5012 S->hasLeadingEmptyMacro());
5013}
5014
5015Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005016 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005017
5018 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00005019 return nullptr;
5020
Sean Callanan59721b32015-04-28 18:41:46 +00005021 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
5022 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00005023 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
5024 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005025}
5026
5027Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5028 Expr *ToLHS = Importer.Import(S->getLHS());
5029 if (!ToLHS)
5030 return nullptr;
5031 Expr *ToRHS = Importer.Import(S->getRHS());
5032 if (!ToRHS && S->getRHS())
5033 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00005034 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5035 if (!ToSubStmt && S->getSubStmt())
5036 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005037 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
5038 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
5039 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005040 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00005041 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
5042 ToStmt->setSubStmt(ToSubStmt);
5043 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005044}
5045
5046Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5047 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
5048 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5049 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5050 if (!ToSubStmt && S->getSubStmt())
5051 return nullptr;
5052 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
5053 ToSubStmt);
5054}
5055
5056Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5057 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005058 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00005059 if (!ToLabelDecl && S->getDecl())
5060 return nullptr;
5061 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5062 if (!ToSubStmt && S->getSubStmt())
5063 return nullptr;
5064 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
5065 ToSubStmt);
5066}
5067
5068Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5069 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
5070 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5071 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00005072 if (ImportContainerChecked(FromAttrs, ToAttrs))
5073 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005074 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5075 if (!ToSubStmt && S->getSubStmt())
5076 return nullptr;
5077 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
5078 ToAttrs, ToSubStmt);
5079}
5080
5081Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5082 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00005083 Stmt *ToInit = Importer.Import(S->getInit());
5084 if (!ToInit && S->getInit())
5085 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005086 VarDecl *ToConditionVariable = nullptr;
5087 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5088 ToConditionVariable =
5089 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5090 if (!ToConditionVariable)
5091 return nullptr;
5092 }
5093 Expr *ToCondition = Importer.Import(S->getCond());
5094 if (!ToCondition && S->getCond())
5095 return nullptr;
5096 Stmt *ToThenStmt = Importer.Import(S->getThen());
5097 if (!ToThenStmt && S->getThen())
5098 return nullptr;
5099 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
5100 Stmt *ToElseStmt = Importer.Import(S->getElse());
5101 if (!ToElseStmt && S->getElse())
5102 return nullptr;
5103 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00005104 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00005105 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00005106 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00005107 ToCondition, ToThenStmt,
5108 ToElseLoc, ToElseStmt);
5109}
5110
5111Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00005112 Stmt *ToInit = Importer.Import(S->getInit());
5113 if (!ToInit && S->getInit())
5114 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005115 VarDecl *ToConditionVariable = nullptr;
5116 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5117 ToConditionVariable =
5118 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5119 if (!ToConditionVariable)
5120 return nullptr;
5121 }
5122 Expr *ToCondition = Importer.Import(S->getCond());
5123 if (!ToCondition && S->getCond())
5124 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005125 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00005126 Importer.getToContext(), ToInit,
5127 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00005128 Stmt *ToBody = Importer.Import(S->getBody());
5129 if (!ToBody && S->getBody())
5130 return nullptr;
5131 ToStmt->setBody(ToBody);
5132 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5133 // Now we have to re-chain the cases.
5134 SwitchCase *LastChainedSwitchCase = nullptr;
5135 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5136 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005137 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00005138 if (!ToSC)
5139 return nullptr;
5140 if (LastChainedSwitchCase)
5141 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5142 else
5143 ToStmt->setSwitchCaseList(ToSC);
5144 LastChainedSwitchCase = ToSC;
5145 }
5146 return ToStmt;
5147}
5148
5149Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5150 VarDecl *ToConditionVariable = nullptr;
5151 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5152 ToConditionVariable =
5153 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5154 if (!ToConditionVariable)
5155 return nullptr;
5156 }
5157 Expr *ToCondition = Importer.Import(S->getCond());
5158 if (!ToCondition && S->getCond())
5159 return nullptr;
5160 Stmt *ToBody = Importer.Import(S->getBody());
5161 if (!ToBody && S->getBody())
5162 return nullptr;
5163 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5164 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5165 ToConditionVariable,
5166 ToCondition, ToBody,
5167 ToWhileLoc);
5168}
5169
5170Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5171 Stmt *ToBody = Importer.Import(S->getBody());
5172 if (!ToBody && S->getBody())
5173 return nullptr;
5174 Expr *ToCondition = Importer.Import(S->getCond());
5175 if (!ToCondition && S->getCond())
5176 return nullptr;
5177 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5178 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5179 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5180 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5181 ToDoLoc, ToWhileLoc,
5182 ToRParenLoc);
5183}
5184
5185Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5186 Stmt *ToInit = Importer.Import(S->getInit());
5187 if (!ToInit && S->getInit())
5188 return nullptr;
5189 Expr *ToCondition = Importer.Import(S->getCond());
5190 if (!ToCondition && S->getCond())
5191 return nullptr;
5192 VarDecl *ToConditionVariable = nullptr;
5193 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5194 ToConditionVariable =
5195 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5196 if (!ToConditionVariable)
5197 return nullptr;
5198 }
5199 Expr *ToInc = Importer.Import(S->getInc());
5200 if (!ToInc && S->getInc())
5201 return nullptr;
5202 Stmt *ToBody = Importer.Import(S->getBody());
5203 if (!ToBody && S->getBody())
5204 return nullptr;
5205 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5206 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5207 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5208 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5209 ToInit, ToCondition,
5210 ToConditionVariable,
5211 ToInc, ToBody,
5212 ToForLoc, ToLParenLoc,
5213 ToRParenLoc);
5214}
5215
5216Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5217 LabelDecl *ToLabel = nullptr;
5218 if (LabelDecl *FromLabel = S->getLabel()) {
5219 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5220 if (!ToLabel)
5221 return nullptr;
5222 }
5223 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5224 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5225 return new (Importer.getToContext()) GotoStmt(ToLabel,
5226 ToGotoLoc, ToLabelLoc);
5227}
5228
5229Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5230 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5231 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5232 Expr *ToTarget = Importer.Import(S->getTarget());
5233 if (!ToTarget && S->getTarget())
5234 return nullptr;
5235 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5236 ToTarget);
5237}
5238
5239Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5240 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5241 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5242}
5243
5244Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5245 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5246 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5247}
5248
5249Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5250 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5251 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5252 if (!ToRetExpr && S->getRetValue())
5253 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005254 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
5255 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00005256 if (!ToNRVOCandidate && NRVOCandidate)
5257 return nullptr;
5258 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5259 ToNRVOCandidate);
5260}
5261
5262Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5263 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5264 VarDecl *ToExceptionDecl = nullptr;
5265 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5266 ToExceptionDecl =
5267 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5268 if (!ToExceptionDecl)
5269 return nullptr;
5270 }
5271 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5272 if (!ToHandlerBlock && S->getHandlerBlock())
5273 return nullptr;
5274 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5275 ToExceptionDecl,
5276 ToHandlerBlock);
5277}
5278
5279Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5280 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5281 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5282 if (!ToTryBlock && S->getTryBlock())
5283 return nullptr;
5284 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5285 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5286 CXXCatchStmt *FromHandler = S->getHandler(HI);
5287 if (Stmt *ToHandler = Importer.Import(FromHandler))
5288 ToHandlers[HI] = ToHandler;
5289 else
5290 return nullptr;
5291 }
5292 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5293 ToHandlers);
5294}
5295
5296Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005297 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005298 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5299 if (!ToRange && S->getRangeStmt())
5300 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005301 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005302 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5303 if (!ToBegin && S->getBeginStmt())
5304 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005305 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005306 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5307 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005308 return nullptr;
5309 Expr *ToCond = Importer.Import(S->getCond());
5310 if (!ToCond && S->getCond())
5311 return nullptr;
5312 Expr *ToInc = Importer.Import(S->getInc());
5313 if (!ToInc && S->getInc())
5314 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005315 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005316 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5317 if (!ToLoopVar && S->getLoopVarStmt())
5318 return nullptr;
5319 Stmt *ToBody = Importer.Import(S->getBody());
5320 if (!ToBody && S->getBody())
5321 return nullptr;
5322 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005323 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005324 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5325 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005326 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005327 ToCond, ToInc,
5328 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005329 ToForLoc, ToCoawaitLoc,
5330 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005331}
5332
5333Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5334 Stmt *ToElem = Importer.Import(S->getElement());
5335 if (!ToElem && S->getElement())
5336 return nullptr;
5337 Expr *ToCollect = Importer.Import(S->getCollection());
5338 if (!ToCollect && S->getCollection())
5339 return nullptr;
5340 Stmt *ToBody = Importer.Import(S->getBody());
5341 if (!ToBody && S->getBody())
5342 return nullptr;
5343 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5344 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5345 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5346 ToCollect,
5347 ToBody, ToForLoc,
5348 ToRParenLoc);
5349}
5350
5351Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5352 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5353 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5354 VarDecl *ToExceptionDecl = nullptr;
5355 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5356 ToExceptionDecl =
5357 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5358 if (!ToExceptionDecl)
5359 return nullptr;
5360 }
5361 Stmt *ToBody = Importer.Import(S->getCatchBody());
5362 if (!ToBody && S->getCatchBody())
5363 return nullptr;
5364 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5365 ToRParenLoc,
5366 ToExceptionDecl,
5367 ToBody);
5368}
5369
5370Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5371 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5372 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5373 if (!ToAtFinallyStmt && S->getFinallyBody())
5374 return nullptr;
5375 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5376 ToAtFinallyStmt);
5377}
5378
5379Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5380 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5381 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5382 if (!ToAtTryStmt && S->getTryBody())
5383 return nullptr;
5384 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5385 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5386 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5387 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5388 ToCatchStmts[CI] = ToCatchStmt;
5389 else
5390 return nullptr;
5391 }
5392 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5393 if (!ToAtFinallyStmt && S->getFinallyStmt())
5394 return nullptr;
5395 return ObjCAtTryStmt::Create(Importer.getToContext(),
5396 ToAtTryLoc, ToAtTryStmt,
5397 ToCatchStmts.begin(), ToCatchStmts.size(),
5398 ToAtFinallyStmt);
5399}
5400
5401Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5402 (ObjCAtSynchronizedStmt *S) {
5403 SourceLocation ToAtSynchronizedLoc =
5404 Importer.Import(S->getAtSynchronizedLoc());
5405 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5406 if (!ToSynchExpr && S->getSynchExpr())
5407 return nullptr;
5408 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5409 if (!ToSynchBody && S->getSynchBody())
5410 return nullptr;
5411 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5412 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5413}
5414
5415Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5416 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5417 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5418 if (!ToThrow && S->getThrowExpr())
5419 return nullptr;
5420 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5421}
5422
5423Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5424 (ObjCAutoreleasePoolStmt *S) {
5425 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5426 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5427 if (!ToSubStmt && S->getSubStmt())
5428 return nullptr;
5429 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5430 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005431}
5432
5433//----------------------------------------------------------------------------
5434// Import Expressions
5435//----------------------------------------------------------------------------
5436Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5437 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5438 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005439 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005440}
5441
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005442Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5443 QualType T = Importer.Import(E->getType());
5444 if (T.isNull())
5445 return nullptr;
5446
5447 Expr *SubExpr = Importer.Import(E->getSubExpr());
5448 if (!SubExpr && E->getSubExpr())
5449 return nullptr;
5450
5451 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5452 if (!TInfo)
5453 return nullptr;
5454
5455 return new (Importer.getToContext()) VAArgExpr(
5456 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5457 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5458}
5459
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005460Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5461 QualType T = Importer.Import(E->getType());
5462 if (T.isNull())
5463 return nullptr;
5464
5465 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005466 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005467}
5468
5469Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5470 QualType T = Importer.Import(E->getType());
5471 if (T.isNull())
5472 return nullptr;
5473
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005474 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005475 if (!SL && E->getFunctionName())
5476 return nullptr;
5477
5478 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005479 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005480}
5481
Douglas Gregor52f820e2010-02-19 01:17:02 +00005482Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005483 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005484 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005485 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005486
Craig Topper36250ad2014-05-12 05:36:57 +00005487 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005488 if (E->getDecl() != E->getFoundDecl()) {
5489 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5490 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005491 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005492 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005493
Douglas Gregor52f820e2010-02-19 01:17:02 +00005494 QualType T = Importer.Import(E->getType());
5495 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005496 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005497
Aleksei Sidorina693b372016-09-28 10:16:56 +00005498 TemplateArgumentListInfo ToTAInfo;
5499 TemplateArgumentListInfo *ResInfo = nullptr;
5500 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005501 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5502 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005503 ResInfo = &ToTAInfo;
5504 }
5505
Fangrui Song6907ce22018-07-30 19:24:48 +00005506 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005507 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005508 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005509 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005510 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005511 Importer.Import(E->getLocation()),
5512 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005513 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005514 if (E->hadMultipleCandidates())
5515 DRE->setHadMultipleCandidates(true);
5516 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005517}
5518
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005519Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5520 QualType T = Importer.Import(E->getType());
5521 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005522 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005523
5524 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5525}
5526
5527ASTNodeImporter::Designator
5528ASTNodeImporter::ImportDesignator(const Designator &D) {
5529 if (D.isFieldDesignator()) {
5530 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5531 // Caller checks for import error
5532 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5533 Importer.Import(D.getFieldLoc()));
5534 }
5535 if (D.isArrayDesignator())
5536 return Designator(D.getFirstExprIndex(),
5537 Importer.Import(D.getLBracketLoc()),
5538 Importer.Import(D.getRBracketLoc()));
5539
5540 assert(D.isArrayRangeDesignator());
5541 return Designator(D.getFirstExprIndex(),
5542 Importer.Import(D.getLBracketLoc()),
5543 Importer.Import(D.getEllipsisLoc()),
5544 Importer.Import(D.getRBracketLoc()));
5545}
5546
5547
5548Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005549 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005550 if (!Init)
5551 return nullptr;
5552
5553 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5554 // List elements from the second, the first is Init itself
5555 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005556 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005557 IndexExprs[I - 1] = Arg;
5558 else
5559 return nullptr;
5560 }
5561
5562 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005563 llvm::transform(DIE->designators(), Designators.begin(),
5564 [this](const Designator &D) -> Designator {
5565 return ImportDesignator(D);
5566 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005567
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005568 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005569 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005570 return nullptr;
5571
5572 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005573 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005574 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5575 DIE->usesGNUSyntax(), Init);
5576}
5577
5578Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5579 QualType T = Importer.Import(E->getType());
5580 if (T.isNull())
5581 return nullptr;
5582
5583 return new (Importer.getToContext())
5584 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5585}
5586
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005587Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5588 QualType T = Importer.Import(E->getType());
5589 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005590 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005591
Fangrui Song6907ce22018-07-30 19:24:48 +00005592 return IntegerLiteral::Create(Importer.getToContext(),
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005593 E->getValue(), T,
5594 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005595}
5596
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005597Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5598 QualType T = Importer.Import(E->getType());
5599 if (T.isNull())
5600 return nullptr;
5601
5602 return FloatingLiteral::Create(Importer.getToContext(),
5603 E->getValue(), E->isExact(), T,
5604 Importer.Import(E->getLocation()));
5605}
5606
Douglas Gregor623421d2010-02-18 02:21:22 +00005607Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5608 QualType T = Importer.Import(E->getType());
5609 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005610 return nullptr;
5611
Douglas Gregorfb65e592011-07-27 05:40:30 +00005612 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5613 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005614 Importer.Import(E->getLocation()));
5615}
5616
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005617Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5618 QualType T = Importer.Import(E->getType());
5619 if (T.isNull())
5620 return nullptr;
5621
5622 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5623 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5624
5625 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5626 E->getKind(), E->isPascal(), T,
5627 Locations.data(), Locations.size());
5628}
5629
5630Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5631 QualType T = Importer.Import(E->getType());
5632 if (T.isNull())
5633 return nullptr;
5634
5635 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5636 if (!TInfo)
5637 return nullptr;
5638
5639 Expr *Init = Importer.Import(E->getInitializer());
5640 if (!Init)
5641 return nullptr;
5642
5643 return new (Importer.getToContext()) CompoundLiteralExpr(
5644 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5645 Init, E->isFileScope());
5646}
5647
5648Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5649 QualType T = Importer.Import(E->getType());
5650 if (T.isNull())
5651 return nullptr;
5652
5653 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5654 if (ImportArrayChecked(
5655 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5656 Exprs.begin()))
5657 return nullptr;
5658
5659 return new (Importer.getToContext()) AtomicExpr(
5660 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5661 Importer.Import(E->getRParenLoc()));
5662}
5663
5664Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5665 QualType T = Importer.Import(E->getType());
5666 if (T.isNull())
5667 return nullptr;
5668
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005669 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005670 if (!ToLabel)
5671 return nullptr;
5672
5673 return new (Importer.getToContext()) AddrLabelExpr(
5674 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5675 ToLabel, T);
5676}
5677
Douglas Gregorc74247e2010-02-19 01:07:06 +00005678Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5679 Expr *SubExpr = Importer.Import(E->getSubExpr());
5680 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005681 return nullptr;
5682
Fangrui Song6907ce22018-07-30 19:24:48 +00005683 return new (Importer.getToContext())
Douglas Gregorc74247e2010-02-19 01:07:06 +00005684 ParenExpr(Importer.Import(E->getLParen()),
5685 Importer.Import(E->getRParen()),
5686 SubExpr);
5687}
5688
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005689Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5690 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005691 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005692 return nullptr;
5693
5694 return new (Importer.getToContext()) ParenListExpr(
5695 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5696 Exprs, Importer.Import(E->getLParenLoc()));
5697}
5698
5699Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5700 QualType T = Importer.Import(E->getType());
5701 if (T.isNull())
5702 return nullptr;
5703
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005704 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5705 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005706 if (!ToSubStmt && E->getSubStmt())
5707 return nullptr;
5708
5709 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5710 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5711}
5712
Douglas Gregorc74247e2010-02-19 01:07:06 +00005713Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5714 QualType T = Importer.Import(E->getType());
5715 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005716 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005717
5718 Expr *SubExpr = Importer.Import(E->getSubExpr());
5719 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005720 return nullptr;
5721
Aaron Ballmana5038552018-01-09 13:07:03 +00005722 return new (Importer.getToContext()) UnaryOperator(
5723 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5724 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005725}
5726
Aaron Ballmana5038552018-01-09 13:07:03 +00005727Expr *
5728ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005729 QualType ResultType = Importer.Import(E->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00005730
Douglas Gregord8552cd2010-02-19 01:24:23 +00005731 if (E->isArgumentType()) {
5732 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5733 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005734 return nullptr;
5735
Peter Collingbournee190dee2011-03-11 19:24:49 +00005736 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5737 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005738 Importer.Import(E->getOperatorLoc()),
5739 Importer.Import(E->getRParenLoc()));
5740 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005741
Douglas Gregord8552cd2010-02-19 01:24:23 +00005742 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5743 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005744 return nullptr;
5745
Peter Collingbournee190dee2011-03-11 19:24:49 +00005746 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5747 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005748 Importer.Import(E->getOperatorLoc()),
5749 Importer.Import(E->getRParenLoc()));
5750}
5751
Douglas Gregorc74247e2010-02-19 01:07:06 +00005752Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5753 QualType T = Importer.Import(E->getType());
5754 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005755 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005756
5757 Expr *LHS = Importer.Import(E->getLHS());
5758 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005759 return nullptr;
5760
Douglas Gregorc74247e2010-02-19 01:07:06 +00005761 Expr *RHS = Importer.Import(E->getRHS());
5762 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005763 return nullptr;
5764
Douglas Gregorc74247e2010-02-19 01:07:06 +00005765 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005766 T, E->getValueKind(),
5767 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005768 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005769 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005770}
5771
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005772Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5773 QualType T = Importer.Import(E->getType());
5774 if (T.isNull())
5775 return nullptr;
5776
5777 Expr *ToLHS = Importer.Import(E->getLHS());
5778 if (!ToLHS)
5779 return nullptr;
5780
5781 Expr *ToRHS = Importer.Import(E->getRHS());
5782 if (!ToRHS)
5783 return nullptr;
5784
5785 Expr *ToCond = Importer.Import(E->getCond());
5786 if (!ToCond)
5787 return nullptr;
5788
5789 return new (Importer.getToContext()) ConditionalOperator(
5790 ToCond, Importer.Import(E->getQuestionLoc()),
5791 ToLHS, Importer.Import(E->getColonLoc()),
5792 ToRHS, T, E->getValueKind(), E->getObjectKind());
5793}
5794
5795Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5796 BinaryConditionalOperator *E) {
5797 QualType T = Importer.Import(E->getType());
5798 if (T.isNull())
5799 return nullptr;
5800
5801 Expr *Common = Importer.Import(E->getCommon());
5802 if (!Common)
5803 return nullptr;
5804
5805 Expr *Cond = Importer.Import(E->getCond());
5806 if (!Cond)
5807 return nullptr;
5808
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005809 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5810 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005811 if (!OpaqueValue)
5812 return nullptr;
5813
5814 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5815 if (!TrueExpr)
5816 return nullptr;
5817
5818 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5819 if (!FalseExpr)
5820 return nullptr;
5821
5822 return new (Importer.getToContext()) BinaryConditionalOperator(
5823 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5824 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5825 T, E->getValueKind(), E->getObjectKind());
5826}
5827
Aleksei Sidorina693b372016-09-28 10:16:56 +00005828Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5829 QualType T = Importer.Import(E->getType());
5830 if (T.isNull())
5831 return nullptr;
5832
5833 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5834 if (!ToQueried)
5835 return nullptr;
5836
5837 Expr *Dim = Importer.Import(E->getDimensionExpression());
5838 if (!Dim && E->getDimensionExpression())
5839 return nullptr;
5840
5841 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5842 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5843 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5844}
5845
5846Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5847 QualType T = Importer.Import(E->getType());
5848 if (T.isNull())
5849 return nullptr;
5850
5851 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5852 if (!ToQueried)
5853 return nullptr;
5854
5855 return new (Importer.getToContext()) ExpressionTraitExpr(
5856 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5857 E->getValue(), Importer.Import(E->getLocEnd()), T);
5858}
5859
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005860Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5861 QualType T = Importer.Import(E->getType());
5862 if (T.isNull())
5863 return nullptr;
5864
5865 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5866 if (!SourceExpr && E->getSourceExpr())
5867 return nullptr;
5868
5869 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005870 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005871 E->getObjectKind(), SourceExpr);
5872}
5873
Aleksei Sidorina693b372016-09-28 10:16:56 +00005874Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5875 QualType T = Importer.Import(E->getType());
5876 if (T.isNull())
5877 return nullptr;
5878
5879 Expr *ToLHS = Importer.Import(E->getLHS());
5880 if (!ToLHS)
5881 return nullptr;
5882
5883 Expr *ToRHS = Importer.Import(E->getRHS());
5884 if (!ToRHS)
5885 return nullptr;
5886
5887 return new (Importer.getToContext()) ArraySubscriptExpr(
5888 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5889 Importer.Import(E->getRBracketLoc()));
5890}
5891
Douglas Gregorc74247e2010-02-19 01:07:06 +00005892Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5893 QualType T = Importer.Import(E->getType());
5894 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005895 return nullptr;
5896
Douglas Gregorc74247e2010-02-19 01:07:06 +00005897 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5898 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005899 return nullptr;
5900
Douglas Gregorc74247e2010-02-19 01:07:06 +00005901 QualType CompResultType = Importer.Import(E->getComputationResultType());
5902 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005903 return nullptr;
5904
Douglas Gregorc74247e2010-02-19 01:07:06 +00005905 Expr *LHS = Importer.Import(E->getLHS());
5906 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005907 return nullptr;
5908
Douglas Gregorc74247e2010-02-19 01:07:06 +00005909 Expr *RHS = Importer.Import(E->getRHS());
5910 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005911 return nullptr;
5912
Fangrui Song6907ce22018-07-30 19:24:48 +00005913 return new (Importer.getToContext())
Douglas Gregorc74247e2010-02-19 01:07:06 +00005914 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005915 T, E->getValueKind(),
5916 E->getObjectKind(),
5917 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005918 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005919 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005920}
5921
Aleksei Sidorina693b372016-09-28 10:16:56 +00005922bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5923 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5924 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5925 Path.push_back(Spec);
5926 else
5927 return true;
5928 }
5929 return false;
John McCallcf142162010-08-07 06:22:56 +00005930}
5931
Douglas Gregor98c10182010-02-12 22:17:39 +00005932Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5933 QualType T = Importer.Import(E->getType());
5934 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005935 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005936
5937 Expr *SubExpr = Importer.Import(E->getSubExpr());
5938 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005939 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005940
5941 CXXCastPath BasePath;
5942 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005943 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005944
5945 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005946 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005947}
5948
Aleksei Sidorina693b372016-09-28 10:16:56 +00005949Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005950 QualType T = Importer.Import(E->getType());
5951 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005952 return nullptr;
5953
Douglas Gregor5481d322010-02-19 01:32:14 +00005954 Expr *SubExpr = Importer.Import(E->getSubExpr());
5955 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005956 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005957
5958 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5959 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005960 return nullptr;
5961
John McCallcf142162010-08-07 06:22:56 +00005962 CXXCastPath BasePath;
5963 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005964 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005965
Aleksei Sidorina693b372016-09-28 10:16:56 +00005966 switch (E->getStmtClass()) {
5967 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005968 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005969 return CStyleCastExpr::Create(Importer.getToContext(), T,
5970 E->getValueKind(), E->getCastKind(),
5971 SubExpr, &BasePath, TInfo,
5972 Importer.Import(CCE->getLParenLoc()),
5973 Importer.Import(CCE->getRParenLoc()));
5974 }
5975
5976 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005977 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005978 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5979 E->getValueKind(), TInfo,
5980 E->getCastKind(), SubExpr, &BasePath,
5981 Importer.Import(FCE->getLParenLoc()),
5982 Importer.Import(FCE->getRParenLoc()));
5983 }
5984
5985 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005986 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005987 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5988 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5989 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5990 TInfo, SubExpr);
5991 }
5992 default:
5993 break; // just fall through
5994 }
5995
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005996 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005997 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5998 RParenLoc = Importer.Import(Named->getRParenLoc());
5999 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
6000
6001 switch (E->getStmtClass()) {
6002 case Stmt::CXXStaticCastExprClass:
6003 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
6004 E->getValueKind(), E->getCastKind(),
6005 SubExpr, &BasePath, TInfo,
6006 ExprLoc, RParenLoc, Brackets);
6007
6008 case Stmt::CXXDynamicCastExprClass:
6009 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
6010 E->getValueKind(), E->getCastKind(),
6011 SubExpr, &BasePath, TInfo,
6012 ExprLoc, RParenLoc, Brackets);
6013
6014 case Stmt::CXXReinterpretCastExprClass:
6015 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
6016 E->getValueKind(), E->getCastKind(),
6017 SubExpr, &BasePath, TInfo,
6018 ExprLoc, RParenLoc, Brackets);
6019
6020 case Stmt::CXXConstCastExprClass:
6021 return CXXConstCastExpr::Create(Importer.getToContext(), T,
6022 E->getValueKind(), SubExpr, TInfo, ExprLoc,
6023 RParenLoc, Brackets);
6024 default:
6025 llvm_unreachable("Cast expression of unsupported type!");
6026 return nullptr;
6027 }
6028}
6029
6030Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
6031 QualType T = Importer.Import(OE->getType());
6032 if (T.isNull())
6033 return nullptr;
6034
6035 SmallVector<OffsetOfNode, 4> Nodes;
6036 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
6037 const OffsetOfNode &Node = OE->getComponent(I);
6038
6039 switch (Node.getKind()) {
6040 case OffsetOfNode::Array:
6041 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
6042 Node.getArrayExprIndex(),
6043 Importer.Import(Node.getLocEnd())));
6044 break;
6045
6046 case OffsetOfNode::Base: {
6047 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
6048 if (!BS && Node.getBase())
6049 return nullptr;
6050 Nodes.push_back(OffsetOfNode(BS));
6051 break;
6052 }
6053 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006054 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006055 if (!FD)
6056 return nullptr;
6057 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
6058 Importer.Import(Node.getLocEnd())));
6059 break;
6060 }
6061 case OffsetOfNode::Identifier: {
6062 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
6063 if (!ToII)
6064 return nullptr;
6065 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
6066 Importer.Import(Node.getLocEnd())));
6067 break;
6068 }
6069 }
6070 }
6071
6072 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
6073 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
6074 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
6075 if (!ToIndexExpr)
6076 return nullptr;
6077 Exprs[I] = ToIndexExpr;
6078 }
6079
6080 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
6081 if (!TInfo && OE->getTypeSourceInfo())
6082 return nullptr;
6083
6084 return OffsetOfExpr::Create(Importer.getToContext(), T,
6085 Importer.Import(OE->getOperatorLoc()),
6086 TInfo, Nodes, Exprs,
6087 Importer.Import(OE->getRParenLoc()));
6088}
6089
6090Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6091 QualType T = Importer.Import(E->getType());
6092 if (T.isNull())
6093 return nullptr;
6094
6095 Expr *Operand = Importer.Import(E->getOperand());
6096 if (!Operand)
6097 return nullptr;
6098
6099 CanThrowResult CanThrow;
6100 if (E->isValueDependent())
6101 CanThrow = CT_Dependent;
6102 else
6103 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
6104
6105 return new (Importer.getToContext()) CXXNoexceptExpr(
6106 T, Operand, CanThrow,
6107 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
6108}
6109
6110Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6111 QualType T = Importer.Import(E->getType());
6112 if (T.isNull())
6113 return nullptr;
6114
6115 Expr *SubExpr = Importer.Import(E->getSubExpr());
6116 if (!SubExpr && E->getSubExpr())
6117 return nullptr;
6118
6119 return new (Importer.getToContext()) CXXThrowExpr(
6120 SubExpr, T, Importer.Import(E->getThrowLoc()),
6121 E->isThrownVariableInScope());
6122}
6123
6124Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006125 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006126 if (!Param)
6127 return nullptr;
6128
6129 return CXXDefaultArgExpr::Create(
6130 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
6131}
6132
6133Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6134 QualType T = Importer.Import(E->getType());
6135 if (T.isNull())
6136 return nullptr;
6137
6138 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
6139 if (!TypeInfo)
6140 return nullptr;
6141
6142 return new (Importer.getToContext()) CXXScalarValueInitExpr(
6143 T, TypeInfo, Importer.Import(E->getRParenLoc()));
6144}
6145
6146Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6147 Expr *SubExpr = Importer.Import(E->getSubExpr());
6148 if (!SubExpr)
6149 return nullptr;
6150
6151 auto *Dtor = cast_or_null<CXXDestructorDecl>(
6152 Importer.Import(const_cast<CXXDestructorDecl *>(
6153 E->getTemporary()->getDestructor())));
6154 if (!Dtor)
6155 return nullptr;
6156
6157 ASTContext &ToCtx = Importer.getToContext();
6158 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
6159 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
6160}
6161
6162Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
6163 QualType T = Importer.Import(CE->getType());
6164 if (T.isNull())
6165 return nullptr;
6166
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006167 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
6168 if (!TInfo)
6169 return nullptr;
6170
Aleksei Sidorina693b372016-09-28 10:16:56 +00006171 SmallVector<Expr *, 8> Args(CE->getNumArgs());
6172 if (ImportContainerChecked(CE->arguments(), Args))
6173 return nullptr;
6174
6175 auto *Ctor = cast_or_null<CXXConstructorDecl>(
6176 Importer.Import(CE->getConstructor()));
6177 if (!Ctor)
6178 return nullptr;
6179
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006180 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
6181 Importer.getToContext(), Ctor, T, TInfo, Args,
6182 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
6183 CE->isListInitialization(), CE->isStdInitListInitialization(),
6184 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006185}
6186
6187Expr *
6188ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
6189 QualType T = Importer.Import(E->getType());
6190 if (T.isNull())
6191 return nullptr;
6192
6193 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
6194 if (!TempE)
6195 return nullptr;
6196
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006197 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006198 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
6199 if (!ExtendedBy && E->getExtendingDecl())
6200 return nullptr;
6201
6202 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
6203 T, TempE, E->isBoundToLvalueReference());
6204
6205 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
6206 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
6207 return ToMTE;
6208}
6209
Gabor Horvath7a91c082017-11-14 11:30:38 +00006210Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6211 QualType T = Importer.Import(E->getType());
6212 if (T.isNull())
6213 return nullptr;
6214
6215 Expr *Pattern = Importer.Import(E->getPattern());
6216 if (!Pattern)
6217 return nullptr;
6218
6219 return new (Importer.getToContext()) PackExpansionExpr(
6220 T, Pattern, Importer.Import(E->getEllipsisLoc()),
6221 E->getNumExpansions());
6222}
6223
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006224Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6225 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
6226 if (!Pack)
6227 return nullptr;
6228
6229 Optional<unsigned> Length;
6230
6231 if (!E->isValueDependent())
6232 Length = E->getPackLength();
6233
6234 SmallVector<TemplateArgument, 8> PartialArguments;
6235 if (E->isPartiallySubstituted()) {
6236 if (ImportTemplateArguments(E->getPartialArguments().data(),
6237 E->getPartialArguments().size(),
6238 PartialArguments))
6239 return nullptr;
6240 }
6241
6242 return SizeOfPackExpr::Create(
6243 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
6244 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
6245 Length, PartialArguments);
6246}
6247
Aleksei Sidorina693b372016-09-28 10:16:56 +00006248Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6249 QualType T = Importer.Import(CE->getType());
6250 if (T.isNull())
6251 return nullptr;
6252
6253 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6254 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6255 return nullptr;
6256
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006257 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006258 Importer.Import(CE->getOperatorNew()));
6259 if (!OperatorNewDecl && CE->getOperatorNew())
6260 return nullptr;
6261
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006262 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006263 Importer.Import(CE->getOperatorDelete()));
6264 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6265 return nullptr;
6266
6267 Expr *ToInit = Importer.Import(CE->getInitializer());
6268 if (!ToInit && CE->getInitializer())
6269 return nullptr;
6270
6271 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6272 if (!TInfo)
6273 return nullptr;
6274
6275 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6276 if (!ToArrSize && CE->getArraySize())
6277 return nullptr;
6278
6279 return new (Importer.getToContext()) CXXNewExpr(
6280 Importer.getToContext(),
6281 CE->isGlobalNew(),
6282 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006283 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006284 CE->doesUsualArrayDeleteWantSize(),
6285 PlacementArgs,
6286 Importer.Import(CE->getTypeIdParens()),
6287 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6288 Importer.Import(CE->getSourceRange()),
6289 Importer.Import(CE->getDirectInitRange()));
6290}
6291
6292Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6293 QualType T = Importer.Import(E->getType());
6294 if (T.isNull())
6295 return nullptr;
6296
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006297 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006298 Importer.Import(E->getOperatorDelete()));
6299 if (!OperatorDeleteDecl && E->getOperatorDelete())
6300 return nullptr;
6301
6302 Expr *ToArg = Importer.Import(E->getArgument());
6303 if (!ToArg && E->getArgument())
6304 return nullptr;
6305
6306 return new (Importer.getToContext()) CXXDeleteExpr(
6307 T, E->isGlobalDelete(),
6308 E->isArrayForm(),
6309 E->isArrayFormAsWritten(),
6310 E->doesUsualArrayDeleteWantSize(),
6311 OperatorDeleteDecl,
6312 ToArg,
6313 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006314}
6315
Sean Callanan59721b32015-04-28 18:41:46 +00006316Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6317 QualType T = Importer.Import(E->getType());
6318 if (T.isNull())
6319 return nullptr;
6320
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006321 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006322 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006323 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006324 return nullptr;
6325
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006326 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006327 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006328 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006329
6330 return CXXConstructExpr::Create(Importer.getToContext(), T,
6331 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006332 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006333 ToArgs, E->hadMultipleCandidates(),
6334 E->isListInitialization(),
6335 E->isStdInitListInitialization(),
6336 E->requiresZeroInitialization(),
6337 E->getConstructionKind(),
6338 Importer.Import(E->getParenOrBraceRange()));
6339}
6340
Aleksei Sidorina693b372016-09-28 10:16:56 +00006341Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6342 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6343 if (!SubExpr && EWC->getSubExpr())
6344 return nullptr;
6345
6346 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6347 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6348 if (ExprWithCleanups::CleanupObject Obj =
6349 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6350 Objs[I] = Obj;
6351 else
6352 return nullptr;
6353
6354 return ExprWithCleanups::Create(Importer.getToContext(),
6355 SubExpr, EWC->cleanupsHaveSideEffects(),
6356 Objs);
6357}
6358
Sean Callanan8bca9962016-03-28 21:43:01 +00006359Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6360 QualType T = Importer.Import(E->getType());
6361 if (T.isNull())
6362 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006363
Sean Callanan8bca9962016-03-28 21:43:01 +00006364 Expr *ToFn = Importer.Import(E->getCallee());
6365 if (!ToFn)
6366 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006367
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006368 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006369 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006370 return nullptr;
6371
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006372 return new (Importer.getToContext()) CXXMemberCallExpr(
6373 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6374 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006375}
6376
6377Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6378 QualType T = Importer.Import(E->getType());
6379 if (T.isNull())
6380 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006381
Sean Callanan8bca9962016-03-28 21:43:01 +00006382 return new (Importer.getToContext())
6383 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6384}
6385
6386Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6387 QualType T = Importer.Import(E->getType());
6388 if (T.isNull())
6389 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006390
Sean Callanan8bca9962016-03-28 21:43:01 +00006391 return new (Importer.getToContext())
6392 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6393}
6394
6395
Sean Callanan59721b32015-04-28 18:41:46 +00006396Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6397 QualType T = Importer.Import(E->getType());
6398 if (T.isNull())
6399 return nullptr;
6400
6401 Expr *ToBase = Importer.Import(E->getBase());
6402 if (!ToBase && E->getBase())
6403 return nullptr;
6404
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006405 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006406 if (!ToMember && E->getMemberDecl())
6407 return nullptr;
6408
Peter Szecsief972522018-05-02 11:52:54 +00006409 auto *ToDecl =
6410 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6411 if (!ToDecl && E->getFoundDecl().getDecl())
6412 return nullptr;
6413
6414 DeclAccessPair ToFoundDecl =
6415 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006416
6417 DeclarationNameInfo ToMemberNameInfo(
6418 Importer.Import(E->getMemberNameInfo().getName()),
6419 Importer.Import(E->getMemberNameInfo().getLoc()));
6420
6421 if (E->hasExplicitTemplateArgs()) {
6422 return nullptr; // FIXME: handle template arguments
6423 }
6424
6425 return MemberExpr::Create(Importer.getToContext(), ToBase,
6426 E->isArrow(),
6427 Importer.Import(E->getOperatorLoc()),
6428 Importer.Import(E->getQualifierLoc()),
6429 Importer.Import(E->getTemplateKeywordLoc()),
6430 ToMember, ToFoundDecl, ToMemberNameInfo,
6431 nullptr, T, E->getValueKind(),
6432 E->getObjectKind());
6433}
6434
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006435Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6436 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006437 Expr *BaseE = Importer.Import(E->getBase());
6438 if (!BaseE)
6439 return nullptr;
6440
6441 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6442 if (!ScopeInfo && E->getScopeTypeInfo())
6443 return nullptr;
6444
6445 PseudoDestructorTypeStorage Storage;
6446 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6447 IdentifierInfo *ToII = Importer.Import(FromII);
6448 if (!ToII)
6449 return nullptr;
6450 Storage = PseudoDestructorTypeStorage(
6451 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6452 } else {
6453 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6454 if (!TI)
6455 return nullptr;
6456 Storage = PseudoDestructorTypeStorage(TI);
6457 }
6458
6459 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6460 Importer.getToContext(), BaseE, E->isArrow(),
6461 Importer.Import(E->getOperatorLoc()),
6462 Importer.Import(E->getQualifierLoc()),
6463 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6464 Importer.Import(E->getTildeLoc()), Storage);
6465}
6466
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006467Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6468 CXXDependentScopeMemberExpr *E) {
6469 Expr *Base = nullptr;
6470 if (!E->isImplicitAccess()) {
6471 Base = Importer.Import(E->getBase());
6472 if (!Base)
6473 return nullptr;
6474 }
6475
6476 QualType BaseType = Importer.Import(E->getBaseType());
6477 if (BaseType.isNull())
6478 return nullptr;
6479
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006480 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006481 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006482 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6483 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006484 return nullptr;
6485 ResInfo = &ToTAInfo;
6486 }
6487
6488 DeclarationName Name = Importer.Import(E->getMember());
6489 if (!E->getMember().isEmpty() && Name.isEmpty())
6490 return nullptr;
6491
6492 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6493 // Import additional name location/type info.
6494 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6495 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6496 if (!ToFQ && E->getFirstQualifierFoundInScope())
6497 return nullptr;
6498
6499 return CXXDependentScopeMemberExpr::Create(
6500 Importer.getToContext(), Base, BaseType, E->isArrow(),
6501 Importer.Import(E->getOperatorLoc()),
6502 Importer.Import(E->getQualifierLoc()),
6503 Importer.Import(E->getTemplateKeywordLoc()),
6504 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6505}
6506
Peter Szecsice7f3182018-05-07 12:08:27 +00006507Expr *
6508ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6509 DeclarationName Name = Importer.Import(E->getDeclName());
6510 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6511 return nullptr;
6512
6513 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6514 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6515
6516 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6517 Importer.Import(E->getRAngleLoc()));
6518 TemplateArgumentListInfo *ResInfo = nullptr;
6519 if (E->hasExplicitTemplateArgs()) {
6520 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6521 return nullptr;
6522 ResInfo = &ToTAInfo;
6523 }
6524
6525 return DependentScopeDeclRefExpr::Create(
6526 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6527 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6528}
6529
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006530Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6531 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006532 unsigned NumArgs = CE->arg_size();
6533
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006534 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006535 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6536 return nullptr;
6537
6538 return CXXUnresolvedConstructExpr::Create(
6539 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6540 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6541 Importer.Import(CE->getRParenLoc()));
6542}
6543
6544Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006545 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006546 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6547 if (E->getNamingClass() && !NamingClass)
6548 return nullptr;
6549
6550 DeclarationName Name = Importer.Import(E->getName());
6551 if (E->getName() && !Name)
6552 return nullptr;
6553
6554 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6555 // Import additional name location/type info.
6556 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6557
6558 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006559 for (auto *D : E->decls()) {
6560 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006561 ToDecls.addDecl(To);
6562 else
6563 return nullptr;
6564 }
6565
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006566 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006567 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006568 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6569 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006570 return nullptr;
6571 ResInfo = &ToTAInfo;
6572 }
6573
6574 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6575 return UnresolvedLookupExpr::Create(
6576 Importer.getToContext(), NamingClass,
6577 Importer.Import(E->getQualifierLoc()),
6578 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6579 ResInfo, ToDecls.begin(), ToDecls.end());
6580
6581 return UnresolvedLookupExpr::Create(
6582 Importer.getToContext(), NamingClass,
6583 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6584 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6585}
6586
Peter Szecsice7f3182018-05-07 12:08:27 +00006587Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6588 DeclarationName Name = Importer.Import(E->getName());
6589 if (!E->getName().isEmpty() && Name.isEmpty())
6590 return nullptr;
6591 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6592 // Import additional name location/type info.
6593 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6594
6595 QualType BaseType = Importer.Import(E->getType());
6596 if (!E->getType().isNull() && BaseType.isNull())
6597 return nullptr;
6598
6599 UnresolvedSet<8> ToDecls;
6600 for (Decl *D : E->decls()) {
6601 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6602 ToDecls.addDecl(To);
6603 else
6604 return nullptr;
6605 }
6606
6607 TemplateArgumentListInfo ToTAInfo;
6608 TemplateArgumentListInfo *ResInfo = nullptr;
6609 if (E->hasExplicitTemplateArgs()) {
6610 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6611 return nullptr;
6612 ResInfo = &ToTAInfo;
6613 }
6614
6615 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6616 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6617 return nullptr;
6618 }
6619
6620 return UnresolvedMemberExpr::Create(
6621 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6622 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6623 Importer.Import(E->getQualifierLoc()),
6624 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6625 ToDecls.begin(), ToDecls.end());
6626}
6627
Sean Callanan59721b32015-04-28 18:41:46 +00006628Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6629 QualType T = Importer.Import(E->getType());
6630 if (T.isNull())
6631 return nullptr;
6632
6633 Expr *ToCallee = Importer.Import(E->getCallee());
6634 if (!ToCallee && E->getCallee())
6635 return nullptr;
6636
6637 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006638 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006639 if (ImportContainerChecked(E->arguments(), ToArgs))
6640 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006641
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006642 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006643
6644 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6645 ToArgs_Copied[ai] = ToArgs[ai];
6646
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006647 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6648 return new (Importer.getToContext()) CXXOperatorCallExpr(
6649 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6650 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6651 OCE->getFPFeatures());
6652 }
6653
Sean Callanan59721b32015-04-28 18:41:46 +00006654 return new (Importer.getToContext())
Fangrui Song6907ce22018-07-30 19:24:48 +00006655 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006656 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006657 Importer.Import(E->getRParenLoc()));
6658}
6659
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006660Optional<LambdaCapture>
6661ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6662 VarDecl *Var = nullptr;
6663 if (From.capturesVariable()) {
6664 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6665 if (!Var)
6666 return None;
6667 }
6668
6669 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6670 From.getCaptureKind(), Var,
6671 From.isPackExpansion()
6672 ? Importer.Import(From.getEllipsisLoc())
6673 : SourceLocation());
6674}
6675
6676Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6677 CXXRecordDecl *FromClass = LE->getLambdaClass();
6678 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6679 if (!ToClass)
6680 return nullptr;
6681
6682 // NOTE: lambda classes are created with BeingDefined flag set up.
6683 // It means that ImportDefinition doesn't work for them and we should fill it
6684 // manually.
6685 if (ToClass->isBeingDefined()) {
6686 for (auto FromField : FromClass->fields()) {
6687 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6688 if (!ToField)
6689 return nullptr;
6690 }
6691 }
6692
6693 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6694 Importer.Import(LE->getCallOperator()));
6695 if (!ToCallOp)
6696 return nullptr;
6697
6698 ToClass->completeDefinition();
6699
6700 unsigned NumCaptures = LE->capture_size();
6701 SmallVector<LambdaCapture, 8> Captures;
6702 Captures.reserve(NumCaptures);
6703 for (const auto &FromCapture : LE->captures()) {
6704 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6705 Captures.push_back(*ToCapture);
6706 else
6707 return nullptr;
6708 }
6709
6710 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6711 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6712 return nullptr;
6713
6714 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6715 Importer.Import(LE->getIntroducerRange()),
6716 LE->getCaptureDefault(),
6717 Importer.Import(LE->getCaptureDefaultLoc()),
6718 Captures,
6719 LE->hasExplicitParameters(),
6720 LE->hasExplicitResultType(),
6721 InitCaptures,
6722 Importer.Import(LE->getLocEnd()),
6723 LE->containsUnexpandedParameterPack());
6724}
6725
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006726Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6727 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006728 if (T.isNull())
6729 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006730
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006731 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006732 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006733 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006734
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006735 ASTContext &ToCtx = Importer.getToContext();
6736 InitListExpr *To = new (ToCtx) InitListExpr(
6737 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6738 Exprs, Importer.Import(ILE->getLBraceLoc()));
6739 To->setType(T);
6740
6741 if (ILE->hasArrayFiller()) {
6742 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6743 if (!Filler)
6744 return nullptr;
6745 To->setArrayFiller(Filler);
6746 }
6747
6748 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006749 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006750 if (!ToFD)
6751 return nullptr;
6752 To->setInitializedFieldInUnion(ToFD);
6753 }
6754
6755 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006756 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006757 if (!ToSyntForm)
6758 return nullptr;
6759 To->setSyntacticForm(ToSyntForm);
6760 }
6761
6762 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6763 To->setValueDependent(ILE->isValueDependent());
6764 To->setInstantiationDependent(ILE->isInstantiationDependent());
6765
6766 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006767}
6768
Gabor Marton07b01ff2018-06-29 12:17:34 +00006769Expr *ASTNodeImporter::VisitCXXStdInitializerListExpr(
6770 CXXStdInitializerListExpr *E) {
6771 QualType T = Importer.Import(E->getType());
6772 if (T.isNull())
6773 return nullptr;
6774
6775 Expr *SE = Importer.Import(E->getSubExpr());
6776 if (!SE)
6777 return nullptr;
6778
6779 return new (Importer.getToContext()) CXXStdInitializerListExpr(T, SE);
6780}
6781
Balazs Keri95baa842018-07-25 10:21:06 +00006782Expr *ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
6783 CXXInheritedCtorInitExpr *E) {
6784 QualType T = Importer.Import(E->getType());
6785 if (T.isNull())
6786 return nullptr;
6787
6788 auto *Ctor = cast_or_null<CXXConstructorDecl>(Importer.Import(
6789 E->getConstructor()));
6790 if (!Ctor)
6791 return nullptr;
6792
6793 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
6794 Importer.Import(E->getLocation()), T, Ctor,
6795 E->constructsVBase(), E->inheritedFromVBase());
6796}
6797
Richard Smith30e304e2016-12-14 00:03:17 +00006798Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6799 QualType ToType = Importer.Import(E->getType());
6800 if (ToType.isNull())
6801 return nullptr;
6802
6803 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6804 if (!ToCommon && E->getCommonExpr())
6805 return nullptr;
6806
6807 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6808 if (!ToSubExpr && E->getSubExpr())
6809 return nullptr;
6810
6811 return new (Importer.getToContext())
6812 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6813}
6814
6815Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6816 QualType ToType = Importer.Import(E->getType());
6817 if (ToType.isNull())
6818 return nullptr;
6819 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6820}
6821
Sean Callanandd2c1742016-05-16 20:48:03 +00006822Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006823 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006824 if (!ToField && DIE->getField())
6825 return nullptr;
6826
6827 return CXXDefaultInitExpr::Create(
6828 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6829}
6830
6831Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6832 QualType ToType = Importer.Import(E->getType());
6833 if (ToType.isNull() && !E->getType().isNull())
6834 return nullptr;
6835 ExprValueKind VK = E->getValueKind();
6836 CastKind CK = E->getCastKind();
6837 Expr *ToOp = Importer.Import(E->getSubExpr());
6838 if (!ToOp && E->getSubExpr())
6839 return nullptr;
6840 CXXCastPath BasePath;
6841 if (ImportCastPath(E, BasePath))
6842 return nullptr;
6843 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6844 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6845 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6846 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
Fangrui Song6907ce22018-07-30 19:24:48 +00006847
Sean Callanandd2c1742016-05-16 20:48:03 +00006848 if (isa<CXXStaticCastExpr>(E)) {
6849 return CXXStaticCastExpr::Create(
Fangrui Song6907ce22018-07-30 19:24:48 +00006850 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
Sean Callanandd2c1742016-05-16 20:48:03 +00006851 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6852 } else if (isa<CXXDynamicCastExpr>(E)) {
6853 return CXXDynamicCastExpr::Create(
Fangrui Song6907ce22018-07-30 19:24:48 +00006854 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
Sean Callanandd2c1742016-05-16 20:48:03 +00006855 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6856 } else if (isa<CXXReinterpretCastExpr>(E)) {
6857 return CXXReinterpretCastExpr::Create(
Fangrui Song6907ce22018-07-30 19:24:48 +00006858 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
Sean Callanandd2c1742016-05-16 20:48:03 +00006859 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6860 } else {
6861 return nullptr;
6862 }
6863}
6864
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006865Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6866 SubstNonTypeTemplateParmExpr *E) {
6867 QualType T = Importer.Import(E->getType());
6868 if (T.isNull())
6869 return nullptr;
6870
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006871 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006872 Importer.Import(E->getParameter()));
6873 if (!Param)
6874 return nullptr;
6875
6876 Expr *Replacement = Importer.Import(E->getReplacement());
6877 if (!Replacement)
6878 return nullptr;
6879
6880 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6881 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6882 Replacement);
6883}
6884
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006885Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6886 QualType ToType = Importer.Import(E->getType());
6887 if (ToType.isNull())
6888 return nullptr;
6889
6890 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6891 if (ImportContainerChecked(E->getArgs(), ToArgs))
6892 return nullptr;
6893
6894 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6895 // Value is always false.
6896 bool ToValue = false;
6897 if (!E->isValueDependent())
6898 ToValue = E->getValue();
6899
6900 return TypeTraitExpr::Create(
6901 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6902 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6903}
6904
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006905Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6906 QualType ToType = Importer.Import(E->getType());
6907 if (ToType.isNull())
6908 return nullptr;
6909
6910 if (E->isTypeOperand()) {
6911 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6912 if (!TSI)
6913 return nullptr;
6914
6915 return new (Importer.getToContext())
6916 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6917 }
6918
6919 Expr *Op = Importer.Import(E->getExprOperand());
6920 if (!Op)
6921 return nullptr;
6922
6923 return new (Importer.getToContext())
6924 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6925}
6926
Lang Hames19e07e12017-06-20 21:06:00 +00006927void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6928 CXXMethodDecl *FromMethod) {
6929 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6930 ToMethod->addOverriddenMethod(
6931 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6932 FromOverriddenMethod))));
6933}
6934
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006935ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006936 ASTContext &FromContext, FileManager &FromFileManager,
6937 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006938 : ToContext(ToContext), FromContext(FromContext),
6939 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6940 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006941 ImportedDecls[FromContext.getTranslationUnitDecl()]
6942 = ToContext.getTranslationUnitDecl();
6943}
6944
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006945ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006946
6947QualType ASTImporter::Import(QualType FromT) {
6948 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006949 return {};
John McCall424cec92011-01-19 06:33:43 +00006950
6951 const Type *fromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00006952
6953 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006954 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6955 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006956 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006957 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00006958
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006959 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006960 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006961 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006962 if (ToT.isNull())
6963 return ToT;
Fangrui Song6907ce22018-07-30 19:24:48 +00006964
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006965 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006966 ImportedTypes[fromTy] = ToT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00006967
John McCall424cec92011-01-19 06:33:43 +00006968 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006969}
6970
Douglas Gregor62d311f2010-02-09 19:21:46 +00006971TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006972 if (!FromTSI)
6973 return FromTSI;
6974
6975 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006976 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006977 QualType T = Import(FromTSI->getType());
6978 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006979 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006980
Fangrui Song6907ce22018-07-30 19:24:48 +00006981 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006982 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006983}
6984
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006985Attr *ASTImporter::Import(const Attr *FromAttr) {
6986 Attr *ToAttr = FromAttr->clone(ToContext);
6987 ToAttr->setRange(Import(FromAttr->getRange()));
6988 return ToAttr;
6989}
6990
Sean Callanan59721b32015-04-28 18:41:46 +00006991Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6992 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6993 if (Pos != ImportedDecls.end()) {
6994 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00006995 // FIXME: move this call to ImportDeclParts().
Sean Callanan59721b32015-04-28 18:41:46 +00006996 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6997 return ToD;
6998 } else {
6999 return nullptr;
7000 }
7001}
7002
Douglas Gregor62d311f2010-02-09 19:21:46 +00007003Decl *ASTImporter::Import(Decl *FromD) {
7004 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007005 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007006
Douglas Gregord451ea92011-07-29 23:31:30 +00007007 ASTNodeImporter Importer(*this);
7008
Gabor Marton26f72a92018-07-12 09:42:05 +00007009 // Check whether we've already imported this declaration.
7010 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7011 if (ToD) {
7012 // If FromD has some updated flags after last import, apply it
7013 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007014 return ToD;
7015 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007016
7017 // Import the type.
7018 ToD = Importer.Visit(FromD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007019 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00007020 return nullptr;
7021
Gabor Marton26f72a92018-07-12 09:42:05 +00007022 // Notify subclasses.
7023 Imported(FromD, ToD);
7024
Douglas Gregor62d311f2010-02-09 19:21:46 +00007025 return ToD;
7026}
7027
7028DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
7029 if (!FromDC)
7030 return FromDC;
7031
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007032 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007033 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007034 return nullptr;
7035
Fangrui Song6907ce22018-07-30 19:24:48 +00007036 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007037 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007038 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7039 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007040 if (ToRecord->isCompleteDefinition()) {
7041 // Do nothing.
7042 } else if (FromRecord->isCompleteDefinition()) {
7043 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
7044 ASTNodeImporter::IDK_Basic);
7045 } else {
7046 CompleteDecl(ToRecord);
7047 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007048 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7049 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007050 if (ToEnum->isCompleteDefinition()) {
7051 // Do nothing.
7052 } else if (FromEnum->isCompleteDefinition()) {
7053 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
7054 ASTNodeImporter::IDK_Basic);
7055 } else {
7056 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007057 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007058 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7059 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007060 if (ToClass->getDefinition()) {
7061 // Do nothing.
7062 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
7063 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
7064 ASTNodeImporter::IDK_Basic);
7065 } else {
7066 CompleteDecl(ToClass);
7067 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007068 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7069 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007070 if (ToProto->getDefinition()) {
7071 // Do nothing.
7072 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
7073 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
7074 ASTNodeImporter::IDK_Basic);
7075 } else {
7076 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007077 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007078 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007079
Douglas Gregor95d82832012-01-24 18:36:04 +00007080 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007081}
7082
7083Expr *ASTImporter::Import(Expr *FromE) {
7084 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007085 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007086
7087 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7088}
7089
7090Stmt *ASTImporter::Import(Stmt *FromS) {
7091 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007092 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007093
Fangrui Song6907ce22018-07-30 19:24:48 +00007094 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007095 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7096 if (Pos != ImportedStmts.end())
7097 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007098
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007099 // Import the type
7100 ASTNodeImporter Importer(*this);
7101 Stmt *ToS = Importer.Visit(FromS);
7102 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00007103 return nullptr;
7104
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007105 // Record the imported declaration.
7106 ImportedStmts[FromS] = ToS;
7107 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007108}
7109
7110NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7111 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007112 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007113
Douglas Gregor90ebf252011-04-27 16:48:40 +00007114 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7115
7116 switch (FromNNS->getKind()) {
7117 case NestedNameSpecifier::Identifier:
7118 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7119 return NestedNameSpecifier::Create(ToContext, prefix, II);
7120 }
Craig Topper36250ad2014-05-12 05:36:57 +00007121 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007122
7123 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007124 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007125 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007126 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7127 }
Craig Topper36250ad2014-05-12 05:36:57 +00007128 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007129
7130 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007131 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007132 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007133 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7134 }
Craig Topper36250ad2014-05-12 05:36:57 +00007135 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007136
7137 case NestedNameSpecifier::Global:
7138 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7139
Nikola Smiljanic67860242014-09-26 00:28:20 +00007140 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007141 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007142 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007143 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7144 }
7145 return nullptr;
7146
Douglas Gregor90ebf252011-04-27 16:48:40 +00007147 case NestedNameSpecifier::TypeSpec:
7148 case NestedNameSpecifier::TypeSpecWithTemplate: {
7149 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7150 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007151 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007152 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007153 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007154 bTemplate, T.getTypePtr());
7155 }
7156 }
Craig Topper36250ad2014-05-12 05:36:57 +00007157 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007158 }
7159
7160 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007161}
7162
Douglas Gregor14454802011-02-25 02:25:35 +00007163NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007164 // Copied from NestedNameSpecifier mostly.
7165 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7166 NestedNameSpecifierLoc NNS = FromNNS;
7167
7168 // Push each of the nested-name-specifiers's onto a stack for
7169 // serialization in reverse order.
7170 while (NNS) {
7171 NestedNames.push_back(NNS);
7172 NNS = NNS.getPrefix();
7173 }
7174
7175 NestedNameSpecifierLocBuilder Builder;
7176
7177 while (!NestedNames.empty()) {
7178 NNS = NestedNames.pop_back_val();
7179 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7180 if (!Spec)
7181 return NestedNameSpecifierLoc();
7182
7183 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7184 switch (Kind) {
7185 case NestedNameSpecifier::Identifier:
7186 Builder.Extend(getToContext(),
7187 Spec->getAsIdentifier(),
7188 Import(NNS.getLocalBeginLoc()),
7189 Import(NNS.getLocalEndLoc()));
7190 break;
7191
7192 case NestedNameSpecifier::Namespace:
7193 Builder.Extend(getToContext(),
7194 Spec->getAsNamespace(),
7195 Import(NNS.getLocalBeginLoc()),
7196 Import(NNS.getLocalEndLoc()));
7197 break;
7198
7199 case NestedNameSpecifier::NamespaceAlias:
7200 Builder.Extend(getToContext(),
7201 Spec->getAsNamespaceAlias(),
7202 Import(NNS.getLocalBeginLoc()),
7203 Import(NNS.getLocalEndLoc()));
7204 break;
7205
7206 case NestedNameSpecifier::TypeSpec:
7207 case NestedNameSpecifier::TypeSpecWithTemplate: {
7208 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7209 QualType(Spec->getAsType(), 0));
7210 Builder.Extend(getToContext(),
7211 Import(NNS.getLocalBeginLoc()),
7212 TSI->getTypeLoc(),
7213 Import(NNS.getLocalEndLoc()));
7214 break;
7215 }
7216
7217 case NestedNameSpecifier::Global:
7218 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7219 break;
7220
7221 case NestedNameSpecifier::Super: {
7222 SourceRange ToRange = Import(NNS.getSourceRange());
7223 Builder.MakeSuper(getToContext(),
7224 Spec->getAsRecordDecl(),
7225 ToRange.getBegin(),
7226 ToRange.getEnd());
7227 }
7228 }
7229 }
7230
7231 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007232}
7233
Douglas Gregore2e50d332010-12-01 01:36:18 +00007234TemplateName ASTImporter::Import(TemplateName From) {
7235 switch (From.getKind()) {
7236 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007237 if (auto *ToTemplate =
7238 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007239 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007240
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007241 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007242
Douglas Gregore2e50d332010-12-01 01:36:18 +00007243 case TemplateName::OverloadedTemplate: {
7244 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7245 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007246 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007247 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007248 ToTemplates.addDecl(To);
7249 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007250 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007251 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007252 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00007253 ToTemplates.end());
7254 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007255
Douglas Gregore2e50d332010-12-01 01:36:18 +00007256 case TemplateName::QualifiedTemplate: {
7257 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7258 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7259 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007260 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007261
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007262 if (auto *ToTemplate =
7263 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00007264 return ToContext.getQualifiedTemplateName(Qualifier,
7265 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00007266 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007267
7268 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007269 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007270
Douglas Gregore2e50d332010-12-01 01:36:18 +00007271 case TemplateName::DependentTemplate: {
7272 DependentTemplateName *DTN = From.getAsDependentTemplateName();
7273 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
7274 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007275 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007276
Douglas Gregore2e50d332010-12-01 01:36:18 +00007277 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007278 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00007279 Import(DTN->getIdentifier()));
7280 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007281
Douglas Gregore2e50d332010-12-01 01:36:18 +00007282 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
7283 }
John McCalld9dfe3a2011-06-30 08:33:18 +00007284
7285 case TemplateName::SubstTemplateTemplateParm: {
7286 SubstTemplateTemplateParmStorage *subst
7287 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007288 auto *param =
7289 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00007290 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007291 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007292
7293 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007294 if (replacement.isNull())
7295 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007296
John McCalld9dfe3a2011-06-30 08:33:18 +00007297 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7298 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007299
Douglas Gregor5590be02011-01-15 06:45:20 +00007300 case TemplateName::SubstTemplateTemplateParmPack: {
7301 SubstTemplateTemplateParmPackStorage *SubstPack
7302 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007303 auto *Param =
7304 cast_or_null<TemplateTemplateParmDecl>(
7305 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007306 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007307 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007308
Douglas Gregor5590be02011-01-15 06:45:20 +00007309 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00007310 TemplateArgument ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00007311 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7312 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007313 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007314
Douglas Gregor5590be02011-01-15 06:45:20 +00007315 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7316 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007317 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007318
Douglas Gregore2e50d332010-12-01 01:36:18 +00007319 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007320}
7321
Douglas Gregor62d311f2010-02-09 19:21:46 +00007322SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7323 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007324 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007325
Douglas Gregor811663e2010-02-10 00:15:17 +00007326 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007327
Douglas Gregor811663e2010-02-10 00:15:17 +00007328 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00007329 FileID ToFileID = Import(Decomposed.first);
7330 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007331 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007332 SourceManager &ToSM = ToContext.getSourceManager();
7333 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007334}
7335
7336SourceRange ASTImporter::Import(SourceRange FromRange) {
7337 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7338}
7339
Douglas Gregor811663e2010-02-10 00:15:17 +00007340FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007341 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007342 if (Pos != ImportedFileIDs.end())
7343 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007344
Douglas Gregor811663e2010-02-10 00:15:17 +00007345 SourceManager &FromSM = FromContext.getSourceManager();
7346 SourceManager &ToSM = ToContext.getSourceManager();
7347 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007348
7349 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00007350 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007351 if (FromSLoc.isExpansion()) {
7352 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
7353 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
7354 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
7355 unsigned TokenLen = FromSM.getFileIDSize(FromID);
7356 SourceLocation MLoc;
7357 if (FromEx.isMacroArgExpansion()) {
7358 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
7359 } else {
7360 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
7361 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
7362 FromEx.isExpansionTokenRange());
7363 }
7364 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007365 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007366 // Include location of this file.
7367 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7368
7369 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
7370 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
7371 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7372 // disk again
7373 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7374 // than mmap the files several times.
7375 const FileEntry *Entry =
7376 ToFileManager.getFile(Cache->OrigEntry->getName());
7377 if (!Entry)
7378 return {};
7379 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7380 FromSLoc.getFile().getFileCharacteristic());
7381 } else {
7382 // FIXME: We want to re-use the existing MemoryBuffer!
7383 const llvm::MemoryBuffer *FromBuf =
7384 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
7385 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
7386 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
7387 FromBuf->getBufferIdentifier());
7388 ToID = ToSM.createFileID(std::move(ToBuf),
7389 FromSLoc.getFile().getFileCharacteristic());
7390 }
Douglas Gregor811663e2010-02-10 00:15:17 +00007391 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007392
Sebastian Redl99219f12010-09-30 01:03:06 +00007393 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007394 return ToID;
7395}
7396
Sean Callanandd2c1742016-05-16 20:48:03 +00007397CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7398 Expr *ToExpr = Import(From->getInit());
7399 if (!ToExpr && From->getInit())
7400 return nullptr;
7401
7402 if (From->isBaseInitializer()) {
7403 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7404 if (!ToTInfo && From->getTypeSourceInfo())
7405 return nullptr;
7406
7407 return new (ToContext) CXXCtorInitializer(
7408 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7409 ToExpr, Import(From->getRParenLoc()),
7410 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7411 : SourceLocation());
7412 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007413 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007414 if (!ToField && From->getMember())
7415 return nullptr;
7416
7417 return new (ToContext) CXXCtorInitializer(
7418 ToContext, ToField, Import(From->getMemberLocation()),
7419 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7420 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007421 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007422 Import(From->getIndirectMember()));
7423 if (!ToIField && From->getIndirectMember())
7424 return nullptr;
7425
7426 return new (ToContext) CXXCtorInitializer(
7427 ToContext, ToIField, Import(From->getMemberLocation()),
7428 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7429 } else if (From->isDelegatingInitializer()) {
7430 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7431 if (!ToTInfo && From->getTypeSourceInfo())
7432 return nullptr;
7433
7434 return new (ToContext)
7435 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7436 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007437 } else {
7438 return nullptr;
7439 }
7440}
7441
Aleksei Sidorina693b372016-09-28 10:16:56 +00007442CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7443 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7444 if (Pos != ImportedCXXBaseSpecifiers.end())
7445 return Pos->second;
7446
7447 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7448 Import(BaseSpec->getSourceRange()),
7449 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7450 BaseSpec->getAccessSpecifierAsWritten(),
7451 Import(BaseSpec->getTypeSourceInfo()),
7452 Import(BaseSpec->getEllipsisLoc()));
7453 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7454 return Imported;
7455}
7456
Douglas Gregor0a791672011-01-18 03:11:38 +00007457void ASTImporter::ImportDefinition(Decl *From) {
7458 Decl *To = Import(From);
7459 if (!To)
7460 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00007461
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007462 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007463 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00007464
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007465 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007466 if (!ToRecord->getDefinition()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007467 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007468 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007469 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00007470 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00007471 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007472
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007473 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007474 if (!ToEnum->getDefinition()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007475 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007476 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007477 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00007478 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007479 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007480
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007481 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007482 if (!ToIFace->getDefinition()) {
7483 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007484 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007485 return;
7486 }
7487 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007488
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007489 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007490 if (!ToProto->getDefinition()) {
7491 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007492 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007493 return;
7494 }
7495 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007496
Douglas Gregor0a791672011-01-18 03:11:38 +00007497 Importer.ImportDeclContext(FromDC, true);
7498 }
7499}
7500
Douglas Gregor96e578d2010-02-05 17:54:41 +00007501DeclarationName ASTImporter::Import(DeclarationName FromName) {
7502 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007503 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007504
7505 switch (FromName.getNameKind()) {
7506 case DeclarationName::Identifier:
7507 return Import(FromName.getAsIdentifierInfo());
7508
7509 case DeclarationName::ObjCZeroArgSelector:
7510 case DeclarationName::ObjCOneArgSelector:
7511 case DeclarationName::ObjCMultiArgSelector:
7512 return Import(FromName.getObjCSelector());
7513
7514 case DeclarationName::CXXConstructorName: {
7515 QualType T = Import(FromName.getCXXNameType());
7516 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007517 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007518
7519 return ToContext.DeclarationNames.getCXXConstructorName(
7520 ToContext.getCanonicalType(T));
7521 }
7522
7523 case DeclarationName::CXXDestructorName: {
7524 QualType T = Import(FromName.getCXXNameType());
7525 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007526 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007527
7528 return ToContext.DeclarationNames.getCXXDestructorName(
7529 ToContext.getCanonicalType(T));
7530 }
7531
Richard Smith35845152017-02-07 01:37:30 +00007532 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007533 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007534 Import(FromName.getCXXDeductionGuideTemplate()));
7535 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007536 return {};
Richard Smith35845152017-02-07 01:37:30 +00007537 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7538 }
7539
Douglas Gregor96e578d2010-02-05 17:54:41 +00007540 case DeclarationName::CXXConversionFunctionName: {
7541 QualType T = Import(FromName.getCXXNameType());
7542 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007543 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007544
7545 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7546 ToContext.getCanonicalType(T));
7547 }
7548
7549 case DeclarationName::CXXOperatorName:
7550 return ToContext.DeclarationNames.getCXXOperatorName(
7551 FromName.getCXXOverloadedOperator());
7552
7553 case DeclarationName::CXXLiteralOperatorName:
7554 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7555 Import(FromName.getCXXLiteralIdentifier()));
7556
7557 case DeclarationName::CXXUsingDirective:
7558 // FIXME: STATICS!
7559 return DeclarationName::getUsingDirectiveName();
7560 }
7561
David Blaikiee4d798f2012-01-20 21:50:17 +00007562 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007563}
7564
Douglas Gregore2e50d332010-12-01 01:36:18 +00007565IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007566 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007567 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007568
Sean Callananf94ef1d2016-05-14 06:11:19 +00007569 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7570
7571 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7572 ToId->setBuiltinID(FromId->getBuiltinID());
7573
7574 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007575}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007576
Douglas Gregor43f54792010-02-17 02:12:47 +00007577Selector ASTImporter::Import(Selector FromSel) {
7578 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007579 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007580
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007581 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007582 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7583 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7584 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7585 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7586}
7587
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007588DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7589 DeclContext *DC,
7590 unsigned IDNS,
7591 NamedDecl **Decls,
7592 unsigned NumDecls) {
7593 return Name;
7594}
7595
7596DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007597 if (LastDiagFromFrom)
7598 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7599 FromContext.getDiagnostics());
7600 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007601 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007602}
7603
7604DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007605 if (!LastDiagFromFrom)
7606 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7607 ToContext.getDiagnostics());
7608 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007609 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007610}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007611
Douglas Gregor2e15c842012-02-01 21:00:38 +00007612void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007613 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007614 if (!ID->getDefinition())
7615 ID->startDefinition();
7616 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007617 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007618 if (!PD->getDefinition())
7619 PD->startDefinition();
7620 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007621 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007622 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7623 TD->startDefinition();
7624 TD->setCompleteDefinition(true);
7625 }
7626 }
7627 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007628 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007629 }
7630}
7631
Gabor Marton26f72a92018-07-12 09:42:05 +00007632Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
7633 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
7634 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
7635 "Try to import an already imported Decl");
7636 if (Pos != ImportedDecls.end())
7637 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007638 ImportedDecls[From] = To;
7639 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007640}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007641
Douglas Gregordd6006f2012-07-17 21:16:27 +00007642bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7643 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007644 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007645 = ImportedTypes.find(From.getTypePtr());
7646 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7647 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007648
Douglas Gregordd6006f2012-07-17 21:16:27 +00007649 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00007650 getStructuralEquivalenceKind(*this), false,
7651 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00007652 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007653}