blob: 366c2064d4e1e824d7d22c5964c01355bfdbf4a7 [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
Douglas Gregorbb7930c2010-02-10 19:54:31 +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);
Douglas Gregor2e15c842012-02-01 21:00:38 +0000240
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000241 /// What we should import from the definition.
Douglas Gregor95d82832012-01-24 18:36:04 +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
Douglas Gregord451ea92011-07-29 23:31:30 +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
Douglas Gregordd6006f2012-07-17 21:16:27 +0000298 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
299 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000300 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
301 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000302 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000303 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000304 bool IsStructuralMatch(FunctionTemplateDecl *From,
305 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000306 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000307 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000308 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000309 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000310 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000311 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000312 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000313 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000314 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000315 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000316 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000317 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000318 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000319 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000320 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000321 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000322 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000323 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000324 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000325 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
326 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
327 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
328 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000329 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000330 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000331 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000332 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000333 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000334 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000335 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000336 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000337 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000338 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000339 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000340 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000341 Decl *VisitUsingDecl(UsingDecl *D);
342 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
343 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
344 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
345 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
346
Douglas Gregor85f3f952015-07-07 03:57:15 +0000347 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000348 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000349 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000350 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000351 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000352 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000353 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
354 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
355 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
356 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000357 Decl *VisitClassTemplateSpecializationDecl(
358 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000359 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
360 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000361 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000362
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000363 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000364 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
365
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000366 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000367 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000368 Stmt *VisitDeclStmt(DeclStmt *S);
369 Stmt *VisitNullStmt(NullStmt *S);
370 Stmt *VisitCompoundStmt(CompoundStmt *S);
371 Stmt *VisitCaseStmt(CaseStmt *S);
372 Stmt *VisitDefaultStmt(DefaultStmt *S);
373 Stmt *VisitLabelStmt(LabelStmt *S);
374 Stmt *VisitAttributedStmt(AttributedStmt *S);
375 Stmt *VisitIfStmt(IfStmt *S);
376 Stmt *VisitSwitchStmt(SwitchStmt *S);
377 Stmt *VisitWhileStmt(WhileStmt *S);
378 Stmt *VisitDoStmt(DoStmt *S);
379 Stmt *VisitForStmt(ForStmt *S);
380 Stmt *VisitGotoStmt(GotoStmt *S);
381 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
382 Stmt *VisitContinueStmt(ContinueStmt *S);
383 Stmt *VisitBreakStmt(BreakStmt *S);
384 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000385 // FIXME: MSAsmStmt
386 // FIXME: SEHExceptStmt
387 // FIXME: SEHFinallyStmt
388 // FIXME: SEHTryStmt
389 // FIXME: SEHLeaveStmt
390 // FIXME: CapturedStmt
391 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
392 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
393 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
394 // FIXME: MSDependentExistsStmt
395 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
396 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
397 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
398 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
399 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
400 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
401 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000402
403 // Importing expressions
404 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000405 Expr *VisitVAArgExpr(VAArgExpr *E);
406 Expr *VisitGNUNullExpr(GNUNullExpr *E);
407 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000408 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000409 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
410 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
411 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000412 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000413 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000414 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000415 Expr *VisitStringLiteral(StringLiteral *E);
416 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
417 Expr *VisitAtomicExpr(AtomicExpr *E);
418 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000419 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000420 Expr *VisitParenListExpr(ParenListExpr *E);
421 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000422 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000423 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000424 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000425 Expr *VisitConditionalOperator(ConditionalOperator *E);
426 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
427 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000428 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
429 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
430 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000431 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000432 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000433 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
434 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
435 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
436 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
437 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
438 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
439 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
440 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
441 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000442 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000443 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000444 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
445 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000446 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000447 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000448 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000449 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000450 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
451 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000452 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000453 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000454 Expr *VisitCXXThisExpr(CXXThisExpr *E);
455 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000456 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000457 Expr *VisitMemberExpr(MemberExpr *E);
458 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000459 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000460 Expr *VisitInitListExpr(InitListExpr *E);
Gabor Marton07b01ff2018-06-29 12:17:34 +0000461 Expr *VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000462 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
463 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000464 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
465 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000466 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000467 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000468 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000469
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000470 template<typename IIter, typename OIter>
471 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000472 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
473
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000474 ASTImporter &ImporterRef = Importer;
475 std::transform(Ibegin, Iend, Obegin,
476 [&ImporterRef](ItemT From) -> ItemT {
477 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000478 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000479 }
480
481 template<typename IIter, typename OIter>
482 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000483 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
484
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000485 ASTImporter &ImporterRef = Importer;
486 bool Failed = false;
487 std::transform(Ibegin, Iend, Obegin,
488 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000489 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000490 if (!To && From)
491 Failed = true;
492 return To;
493 });
494 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000495 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000496
497 template<typename InContainerTy, typename OutContainerTy>
498 bool ImportContainerChecked(const InContainerTy &InContainer,
499 OutContainerTy &OutContainer) {
500 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
501 OutContainer.begin());
502 }
503
504 template<typename InContainerTy, typename OIter>
505 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
506 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
507 }
Lang Hames19e07e12017-06-20 21:06:00 +0000508
509 // Importing overrides.
510 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000511
512 FunctionDecl *FindFunctionTemplateSpecialization(FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000513 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000514
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000515template <typename InContainerTy>
516bool ASTNodeImporter::ImportTemplateArgumentListInfo(
517 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
518 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
519 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
520 Importer.Import(FromRAngleLoc));
521 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
522 return true;
523 Result = ToTAInfo;
524 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000525}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000526
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000527template <>
528bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
529 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
530 return ImportTemplateArgumentListInfo(
531 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
532}
533
534template <>
535bool ASTNodeImporter::ImportTemplateArgumentListInfo<
536 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
537 TemplateArgumentListInfo &Result) {
538 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
539 From.arguments(), Result);
540}
541
Gabor Marton5254e642018-06-27 13:32:50 +0000542std::tuple<FunctionTemplateDecl *, ASTNodeImporter::OptionalTemplateArgsTy>
543ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
544 FunctionDecl *FromFD) {
545 assert(FromFD->getTemplatedKind() ==
546 FunctionDecl::TK_FunctionTemplateSpecialization);
547 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
548 auto *Template = cast_or_null<FunctionTemplateDecl>(
549 Importer.Import(FTSInfo->getTemplate()));
550
551 // Import template arguments.
552 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
553 TemplateArgsTy ToTemplArgs;
554 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
555 ToTemplArgs)) // Error during import.
556 return std::make_tuple(Template, OptionalTemplateArgsTy());
557
558 return std::make_tuple(Template, ToTemplArgs);
559}
560
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000561} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000562
Douglas Gregor3996e242010-02-15 22:01:00 +0000563//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000564// Import Types
565//----------------------------------------------------------------------------
566
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000567using namespace clang;
568
John McCall424cec92011-01-19 06:33:43 +0000569QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000570 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
571 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000572 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000573}
574
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000575QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
576 QualType UnderlyingType = Importer.Import(T->getValueType());
577 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000578 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000579
580 return Importer.getToContext().getAtomicType(UnderlyingType);
581}
582
John McCall424cec92011-01-19 06:33:43 +0000583QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000584 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000585#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
586 case BuiltinType::Id: \
587 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000588#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000589#define SHARED_SINGLETON_TYPE(Expansion)
590#define BUILTIN_TYPE(Id, SingletonId) \
591 case BuiltinType::Id: return Importer.getToContext().SingletonId;
592#include "clang/AST/BuiltinTypes.def"
593
594 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
595 // context supports C++.
596
597 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
598 // context supports ObjC.
599
Douglas Gregor96e578d2010-02-05 17:54:41 +0000600 case BuiltinType::Char_U:
601 // The context we're importing from has an unsigned 'char'. If we're
602 // importing into a context with a signed 'char', translate to
603 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000604 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000605 return Importer.getToContext().UnsignedCharTy;
606
607 return Importer.getToContext().CharTy;
608
Douglas Gregor96e578d2010-02-05 17:54:41 +0000609 case BuiltinType::Char_S:
610 // The context we're importing from has an unsigned 'char'. If we're
611 // importing into a context with a signed 'char', translate to
612 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000613 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000614 return Importer.getToContext().SignedCharTy;
615
616 return Importer.getToContext().CharTy;
617
Chris Lattnerad3467e2010-12-25 23:25:43 +0000618 case BuiltinType::WChar_S:
619 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000620 // FIXME: If not in C++, shall we translate to the C equivalent of
621 // wchar_t?
622 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000623 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000624
625 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000626}
627
Aleksei Sidorina693b372016-09-28 10:16:56 +0000628QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
629 QualType OrigT = Importer.Import(T->getOriginalType());
630 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000631 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000632
633 return Importer.getToContext().getDecayedType(OrigT);
634}
635
John McCall424cec92011-01-19 06:33:43 +0000636QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000637 QualType ToElementType = Importer.Import(T->getElementType());
638 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000639 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000640
641 return Importer.getToContext().getComplexType(ToElementType);
642}
643
John McCall424cec92011-01-19 06:33:43 +0000644QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000645 QualType ToPointeeType = Importer.Import(T->getPointeeType());
646 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000647 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000648
649 return Importer.getToContext().getPointerType(ToPointeeType);
650}
651
John McCall424cec92011-01-19 06:33:43 +0000652QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000653 // FIXME: Check for blocks support in "to" context.
654 QualType ToPointeeType = Importer.Import(T->getPointeeType());
655 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000656 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000657
658 return Importer.getToContext().getBlockPointerType(ToPointeeType);
659}
660
John McCall424cec92011-01-19 06:33:43 +0000661QualType
662ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000663 // FIXME: Check for C++ support in "to" context.
664 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
665 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000666 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667
668 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
669}
670
John McCall424cec92011-01-19 06:33:43 +0000671QualType
672ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000673 // FIXME: Check for C++0x support in "to" context.
674 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
675 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000676 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000677
678 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
679}
680
John McCall424cec92011-01-19 06:33:43 +0000681QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000682 // FIXME: Check for C++ support in "to" context.
683 QualType ToPointeeType = Importer.Import(T->getPointeeType());
684 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000685 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000686
687 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
688 return Importer.getToContext().getMemberPointerType(ToPointeeType,
689 ClassType.getTypePtr());
690}
691
John McCall424cec92011-01-19 06:33:43 +0000692QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000693 QualType ToElementType = Importer.Import(T->getElementType());
694 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000695 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000696
697 return Importer.getToContext().getConstantArrayType(ToElementType,
698 T->getSize(),
699 T->getSizeModifier(),
700 T->getIndexTypeCVRQualifiers());
701}
702
John McCall424cec92011-01-19 06:33:43 +0000703QualType
704ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000705 QualType ToElementType = Importer.Import(T->getElementType());
706 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000707 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000708
709 return Importer.getToContext().getIncompleteArrayType(ToElementType,
710 T->getSizeModifier(),
711 T->getIndexTypeCVRQualifiers());
712}
713
John McCall424cec92011-01-19 06:33:43 +0000714QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000715 QualType ToElementType = Importer.Import(T->getElementType());
716 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000717 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000718
719 Expr *Size = Importer.Import(T->getSizeExpr());
720 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000721 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000722
723 SourceRange Brackets = Importer.Import(T->getBracketsRange());
724 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
725 T->getSizeModifier(),
726 T->getIndexTypeCVRQualifiers(),
727 Brackets);
728}
729
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000730QualType ASTNodeImporter::VisitDependentSizedArrayType(
731 const DependentSizedArrayType *T) {
732 QualType ToElementType = Importer.Import(T->getElementType());
733 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000734 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000735
736 // SizeExpr may be null if size is not specified directly.
737 // For example, 'int a[]'.
738 Expr *Size = Importer.Import(T->getSizeExpr());
739 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000740 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000741
742 SourceRange Brackets = Importer.Import(T->getBracketsRange());
743 return Importer.getToContext().getDependentSizedArrayType(
744 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
745 Brackets);
746}
747
John McCall424cec92011-01-19 06:33:43 +0000748QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000749 QualType ToElementType = Importer.Import(T->getElementType());
750 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000751 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000752
753 return Importer.getToContext().getVectorType(ToElementType,
754 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000755 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000756}
757
John McCall424cec92011-01-19 06:33:43 +0000758QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000759 QualType ToElementType = Importer.Import(T->getElementType());
760 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000761 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000762
763 return Importer.getToContext().getExtVectorType(ToElementType,
764 T->getNumElements());
765}
766
John McCall424cec92011-01-19 06:33:43 +0000767QualType
768ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000769 // FIXME: What happens if we're importing a function without a prototype
770 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000771 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000772 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000773 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000774
Douglas Gregor96e578d2010-02-05 17:54:41 +0000775 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000776 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000777}
778
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000779QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000780 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000781 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000782 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000783
784 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000785 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000786 for (const auto &A : T->param_types()) {
787 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000788 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000789 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000790 ArgTypes.push_back(ArgType);
791 }
792
793 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000794 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000795 for (const auto &E : T->exceptions()) {
796 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000797 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000798 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000799 ExceptionTypes.push_back(ExceptionType);
800 }
John McCalldb40c7f2010-12-14 08:05:40 +0000801
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000802 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
803 FunctionProtoType::ExtProtoInfo ToEPI;
804
805 ToEPI.ExtInfo = FromEPI.ExtInfo;
806 ToEPI.Variadic = FromEPI.Variadic;
807 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
808 ToEPI.TypeQuals = FromEPI.TypeQuals;
809 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000810 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
811 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
812 ToEPI.ExceptionSpec.NoexceptExpr =
813 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
814 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
815 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
816 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
817 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000818
Jordan Rose5c382722013-03-08 21:51:21 +0000819 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000820}
821
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000822QualType ASTNodeImporter::VisitUnresolvedUsingType(
823 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000824 const auto *ToD =
825 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000826 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000827 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000828
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000829 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000830 cast_or_null<UnresolvedUsingTypenameDecl>(
831 Importer.Import(T->getDecl()->getPreviousDecl()));
832 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000833 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000834
835 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
836}
837
Sean Callananda6df8a2011-08-11 16:56:07 +0000838QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
839 QualType ToInnerType = Importer.Import(T->getInnerType());
840 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000841 return {};
Sean Callananda6df8a2011-08-11 16:56:07 +0000842
843 return Importer.getToContext().getParenType(ToInnerType);
844}
845
John McCall424cec92011-01-19 06:33:43 +0000846QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000847 auto *ToDecl =
848 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000849 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000850 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000851
852 return Importer.getToContext().getTypeDeclType(ToDecl);
853}
854
John McCall424cec92011-01-19 06:33:43 +0000855QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000856 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
857 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000858 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000859
860 return Importer.getToContext().getTypeOfExprType(ToExpr);
861}
862
John McCall424cec92011-01-19 06:33:43 +0000863QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000864 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
865 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000866 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000867
868 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
869}
870
John McCall424cec92011-01-19 06:33:43 +0000871QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000872 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000873 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
874 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000875 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000876
Douglas Gregor81495f32012-02-12 18:42:33 +0000877 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
878 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000879 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000880
881 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000882}
883
Alexis Hunte852b102011-05-24 22:41:36 +0000884QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
885 QualType ToBaseType = Importer.Import(T->getBaseType());
886 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
887 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000888 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000889
890 return Importer.getToContext().getUnaryTransformType(ToBaseType,
891 ToUnderlyingType,
892 T->getUTTKind());
893}
894
Richard Smith30482bc2011-02-20 03:19:35 +0000895QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000896 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000897 QualType FromDeduced = T->getDeducedType();
898 QualType ToDeduced;
899 if (!FromDeduced.isNull()) {
900 ToDeduced = Importer.Import(FromDeduced);
901 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000902 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000903 }
904
Richard Smithe301ba22015-11-11 02:02:15 +0000905 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000906 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000907}
908
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000909QualType ASTNodeImporter::VisitInjectedClassNameType(
910 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000911 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000912 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000913 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000914
915 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
916 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000917 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000918
919 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
920 // See comments in InjectedClassNameType definition for details
921 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
922 enum {
923 TypeAlignmentInBits = 4,
924 TypeAlignment = 1 << TypeAlignmentInBits
925 };
926
927 return QualType(new (Importer.getToContext(), TypeAlignment)
928 InjectedClassNameType(D, InjType), 0);
929}
930
John McCall424cec92011-01-19 06:33:43 +0000931QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000932 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000933 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000934 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000935
936 return Importer.getToContext().getTagDeclType(ToDecl);
937}
938
John McCall424cec92011-01-19 06:33:43 +0000939QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000940 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000941 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000942 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000943
944 return Importer.getToContext().getTagDeclType(ToDecl);
945}
946
Sean Callanan72fe0852015-04-02 23:50:08 +0000947QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
948 QualType FromModifiedType = T->getModifiedType();
949 QualType FromEquivalentType = T->getEquivalentType();
950 QualType ToModifiedType;
951 QualType ToEquivalentType;
952
953 if (!FromModifiedType.isNull()) {
954 ToModifiedType = Importer.Import(FromModifiedType);
955 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000956 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000957 }
958 if (!FromEquivalentType.isNull()) {
959 ToEquivalentType = Importer.Import(FromEquivalentType);
960 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000961 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000962 }
963
964 return Importer.getToContext().getAttributedType(T->getAttrKind(),
965 ToModifiedType, ToEquivalentType);
966}
967
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000968QualType ASTNodeImporter::VisitTemplateTypeParmType(
969 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000970 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000971 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
972 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000973 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000974
975 return Importer.getToContext().getTemplateTypeParmType(
976 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
977}
978
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000979QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
980 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000981 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000982 cast_or_null<TemplateTypeParmType>(Importer.Import(
983 QualType(T->getReplacedParameter(), 0)).getTypePtr());
984 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000985 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000986
987 QualType Replacement = Importer.Import(T->getReplacementType());
988 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000989 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000990 Replacement = Replacement.getCanonicalType();
991
992 return Importer.getToContext().getSubstTemplateTypeParmType(
993 Replaced, Replacement);
994}
995
Douglas Gregore2e50d332010-12-01 01:36:18 +0000996QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000997 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000998 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
999 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001000 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001001
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001002 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001003 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001004 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001005
1006 QualType ToCanonType;
1007 if (!QualType(T, 0).isCanonical()) {
1008 QualType FromCanonType
1009 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1010 ToCanonType =Importer.Import(FromCanonType);
1011 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001012 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001013 }
1014 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00001015 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001016 ToCanonType);
1017}
1018
John McCall424cec92011-01-19 06:33:43 +00001019QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001020 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001021 // Note: the qualifier in an ElaboratedType is optional.
1022 if (T->getQualifier()) {
1023 ToQualifier = Importer.Import(T->getQualifier());
1024 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001025 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +00001026 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001027
1028 QualType ToNamedType = Importer.Import(T->getNamedType());
1029 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001030 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001031
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001032 TagDecl *OwnedTagDecl =
1033 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl()));
1034 if (!OwnedTagDecl && T->getOwnedTagDecl())
1035 return {};
1036
Abramo Bagnara6150c882010-05-11 21:36:43 +00001037 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001038 ToQualifier, ToNamedType,
1039 OwnedTagDecl);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001040}
1041
Gabor Horvath7a91c082017-11-14 11:30:38 +00001042QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1043 QualType Pattern = Importer.Import(T->getPattern());
1044 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001045 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +00001046
1047 return Importer.getToContext().getPackExpansionType(Pattern,
1048 T->getNumExpansions());
1049}
1050
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001051QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
1052 const DependentTemplateSpecializationType *T) {
1053 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
1054 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001055 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001056
1057 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1058 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001059 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001060
1061 SmallVector<TemplateArgument, 2> ToPack;
1062 ToPack.reserve(T->getNumArgs());
1063 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001064 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001065
1066 return Importer.getToContext().getDependentTemplateSpecializationType(
1067 T->getKeyword(), Qualifier, Name, ToPack);
1068}
1069
Peter Szecsice7f3182018-05-07 12:08:27 +00001070QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1071 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
1072 if (!NNS && T->getQualifier())
1073 return QualType();
1074
1075 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1076 if (!Name && T->getIdentifier())
1077 return QualType();
1078
1079 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
1080 ? QualType()
1081 : Importer.Import(T->getCanonicalTypeInternal());
1082 if (!Canon.isNull())
1083 Canon = Canon.getCanonicalType();
1084
1085 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
1086 Name, Canon);
1087}
1088
John McCall424cec92011-01-19 06:33:43 +00001089QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001090 auto *Class =
1091 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001092 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001093 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001094
John McCall8b07ec22010-05-15 11:32:37 +00001095 return Importer.getToContext().getObjCInterfaceType(Class);
1096}
1097
John McCall424cec92011-01-19 06:33:43 +00001098QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001099 QualType ToBaseType = Importer.Import(T->getBaseType());
1100 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001101 return {};
John McCall8b07ec22010-05-15 11:32:37 +00001102
Douglas Gregore9d95f12015-07-07 03:57:35 +00001103 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001104 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001105 QualType ImportedTypeArg = Importer.Import(TypeArg);
1106 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001107 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +00001108
1109 TypeArgs.push_back(ImportedTypeArg);
1110 }
1111
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001112 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001113 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001114 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001115 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001116 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001117 Protocols.push_back(Protocol);
1118 }
1119
Douglas Gregore9d95f12015-07-07 03:57:35 +00001120 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001121 Protocols,
1122 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001123}
1124
John McCall424cec92011-01-19 06:33:43 +00001125QualType
1126ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001127 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1128 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001129 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001130
John McCall8b07ec22010-05-15 11:32:37 +00001131 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001132}
1133
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001134//----------------------------------------------------------------------------
1135// Import Declarations
1136//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001137bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1138 DeclContext *&LexicalDC,
1139 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001140 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001141 SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001142 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1143 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1144 DeclContext *OrigDC = D->getDeclContext();
1145 FunctionDecl *FunDecl;
1146 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1147 FunDecl->hasBody()) {
1148 SourceRange RecR = D->getSourceRange();
1149 SourceRange BodyR = FunDecl->getBody()->getSourceRange();
1150 // If RecordDecl is not in Body (it is a param), we bail out.
1151 if (RecR.isValid() && BodyR.isValid() &&
1152 (RecR.getBegin() < BodyR.getBegin() ||
1153 BodyR.getEnd() < RecR.getEnd())) {
1154 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1155 << D->getDeclKindName();
1156 return true;
1157 }
1158 }
1159
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001160 // Import the context of this declaration.
Gabor Marton6e1510c2018-07-12 11:50:21 +00001161 DC = Importer.ImportContext(OrigDC);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001162 if (!DC)
1163 return true;
1164
1165 LexicalDC = DC;
1166 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1167 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1168 if (!LexicalDC)
1169 return true;
1170 }
1171
1172 // Import the name of this declaration.
1173 Name = Importer.Import(D->getDeclName());
1174 if (D->getDeclName() && !Name)
1175 return true;
1176
1177 // Import the location of this declaration.
1178 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001179 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001180 return false;
1181}
1182
Douglas Gregord451ea92011-07-29 23:31:30 +00001183void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1184 if (!FromD)
1185 return;
1186
1187 if (!ToD) {
1188 ToD = Importer.Import(FromD);
1189 if (!ToD)
1190 return;
1191 }
1192
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001193 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1194 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001195 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001196 ImportDefinition(FromRecord, ToRecord);
1197 }
1198 }
1199 return;
1200 }
1201
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001202 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1203 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001204 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1205 ImportDefinition(FromEnum, ToEnum);
1206 }
1207 }
1208 return;
1209 }
1210}
1211
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001212void
1213ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1214 DeclarationNameInfo& To) {
1215 // NOTE: To.Name and To.Loc are already imported.
1216 // We only have to import To.LocInfo.
1217 switch (To.getName().getNameKind()) {
1218 case DeclarationName::Identifier:
1219 case DeclarationName::ObjCZeroArgSelector:
1220 case DeclarationName::ObjCOneArgSelector:
1221 case DeclarationName::ObjCMultiArgSelector:
1222 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001223 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001224 return;
1225
1226 case DeclarationName::CXXOperatorName: {
1227 SourceRange Range = From.getCXXOperatorNameRange();
1228 To.setCXXOperatorNameRange(Importer.Import(Range));
1229 return;
1230 }
1231 case DeclarationName::CXXLiteralOperatorName: {
1232 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1233 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1234 return;
1235 }
1236 case DeclarationName::CXXConstructorName:
1237 case DeclarationName::CXXDestructorName:
1238 case DeclarationName::CXXConversionFunctionName: {
1239 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1240 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1241 return;
1242 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001243 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001244 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001245}
1246
Douglas Gregor2e15c842012-02-01 21:00:38 +00001247void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001248 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001249 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001250 return;
1251 }
1252
Aaron Ballman629afae2014-03-07 19:56:05 +00001253 for (auto *From : FromDC->decls())
1254 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001255}
1256
Balazs Keri1d20cc22018-07-16 12:16:39 +00001257void ASTNodeImporter::ImportImplicitMethods(
1258 const CXXRecordDecl *From, CXXRecordDecl *To) {
1259 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1260 "Import implicit methods to or from non-definition");
1261
1262 for (CXXMethodDecl *FromM : From->methods())
1263 if (FromM->isImplicit())
1264 Importer.Import(FromM);
1265}
1266
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001267static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1268 ASTImporter &Importer) {
1269 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1270 auto *ToTypedef =
1271 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1272 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1273
1274 To->setTypedefNameForAnonDecl(ToTypedef);
1275 }
1276}
1277
Douglas Gregord451ea92011-07-29 23:31:30 +00001278bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001279 ImportDefinitionKind Kind) {
1280 if (To->getDefinition() || To->isBeingDefined()) {
1281 if (Kind == IDK_Everything)
1282 ImportDeclContext(From, /*ForceImport=*/true);
1283
Douglas Gregore2e50d332010-12-01 01:36:18 +00001284 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001285 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001286
1287 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001288
1289 setTypedefNameForAnonDecl(From, To, Importer);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001290
1291 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001292 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1293 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001294
1295 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1296 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1297 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001298 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001299 ToData.Aggregate = FromData.Aggregate;
1300 ToData.PlainOldData = FromData.PlainOldData;
1301 ToData.Empty = FromData.Empty;
1302 ToData.Polymorphic = FromData.Polymorphic;
1303 ToData.Abstract = FromData.Abstract;
1304 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001305 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1306 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1307 ToData.HasBasesWithNonStaticDataMembers =
1308 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001309 ToData.HasPrivateFields = FromData.HasPrivateFields;
1310 ToData.HasProtectedFields = FromData.HasProtectedFields;
1311 ToData.HasPublicFields = FromData.HasPublicFields;
1312 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001313 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001314 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001315 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001316 ToData.HasUninitializedReferenceMember
1317 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001318 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001319 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1320 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001321 ToData.NeedOverloadResolutionForCopyConstructor
1322 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001323 ToData.NeedOverloadResolutionForMoveConstructor
1324 = FromData.NeedOverloadResolutionForMoveConstructor;
1325 ToData.NeedOverloadResolutionForMoveAssignment
1326 = FromData.NeedOverloadResolutionForMoveAssignment;
1327 ToData.NeedOverloadResolutionForDestructor
1328 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001329 ToData.DefaultedCopyConstructorIsDeleted
1330 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001331 ToData.DefaultedMoveConstructorIsDeleted
1332 = FromData.DefaultedMoveConstructorIsDeleted;
1333 ToData.DefaultedMoveAssignmentIsDeleted
1334 = FromData.DefaultedMoveAssignmentIsDeleted;
1335 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001336 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1337 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001338 ToData.HasConstexprNonCopyMoveConstructor
1339 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001340 ToData.HasDefaultedDefaultConstructor
1341 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001342 ToData.DefaultedDefaultConstructorIsConstexpr
1343 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001344 ToData.HasConstexprDefaultConstructor
1345 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001346 ToData.HasNonLiteralTypeFieldsOrBases
1347 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001348 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001349 ToData.UserProvidedDefaultConstructor
1350 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001351 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001352 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1353 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1354 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1355 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001356 ToData.ImplicitCopyAssignmentHasConstParam
1357 = FromData.ImplicitCopyAssignmentHasConstParam;
1358 ToData.HasDeclaredCopyConstructorWithConstParam
1359 = FromData.HasDeclaredCopyConstructorWithConstParam;
1360 ToData.HasDeclaredCopyAssignmentWithConstParam
1361 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001362
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001363 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001364 for (const auto &Base1 : FromCXX->bases()) {
1365 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001366 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001367 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001368
1369 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001370 if (Base1.isPackExpansion())
1371 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001372
1373 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001374 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001375
Douglas Gregore2e50d332010-12-01 01:36:18 +00001376 Bases.push_back(
1377 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001378 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1379 Base1.isVirtual(),
1380 Base1.isBaseOfClass(),
1381 Base1.getAccessSpecifierAsWritten(),
1382 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001383 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001384 }
1385 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001386 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001387 }
1388
Douglas Gregor2e15c842012-02-01 21:00:38 +00001389 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001390 ImportDeclContext(From, /*ForceImport=*/true);
1391
Douglas Gregore2e50d332010-12-01 01:36:18 +00001392 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001393 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001394}
1395
Larisse Voufo39a1e502013-08-06 01:03:05 +00001396bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1397 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001398 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001399 return false;
1400
1401 // FIXME: Can we really import any initializer? Alternatively, we could force
1402 // ourselves to import every declaration of a variable and then only use
1403 // getInit() here.
1404 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1405
1406 // FIXME: Other bits to merge?
1407
1408 return false;
1409}
1410
Douglas Gregord451ea92011-07-29 23:31:30 +00001411bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001412 ImportDefinitionKind Kind) {
1413 if (To->getDefinition() || To->isBeingDefined()) {
1414 if (Kind == IDK_Everything)
1415 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001416 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001417 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001418
1419 To->startDefinition();
1420
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001421 setTypedefNameForAnonDecl(From, To, Importer);
1422
Douglas Gregord451ea92011-07-29 23:31:30 +00001423 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1424 if (T.isNull())
1425 return true;
1426
1427 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1428 if (ToPromotionType.isNull())
1429 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001430
1431 if (shouldForceImportDeclContext(Kind))
1432 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001433
1434 // FIXME: we might need to merge the number of positive or negative bits
1435 // if the enumerator lists don't match.
1436 To->completeDefinition(T, ToPromotionType,
1437 From->getNumPositiveBits(),
1438 From->getNumNegativeBits());
1439 return false;
1440}
1441
Douglas Gregora082a492010-11-30 19:14:50 +00001442TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1443 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001444 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1445 if (ImportContainerChecked(*Params, ToParams))
1446 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001447
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001448 Expr *ToRequiresClause;
1449 if (Expr *const R = Params->getRequiresClause()) {
1450 ToRequiresClause = Importer.Import(R);
1451 if (!ToRequiresClause)
1452 return nullptr;
1453 } else {
1454 ToRequiresClause = nullptr;
1455 }
1456
Douglas Gregora082a492010-11-30 19:14:50 +00001457 return TemplateParameterList::Create(Importer.getToContext(),
1458 Importer.Import(Params->getTemplateLoc()),
1459 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001460 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001461 Importer.Import(Params->getRAngleLoc()),
1462 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001463}
1464
Douglas Gregore2e50d332010-12-01 01:36:18 +00001465TemplateArgument
1466ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1467 switch (From.getKind()) {
1468 case TemplateArgument::Null:
1469 return TemplateArgument();
1470
1471 case TemplateArgument::Type: {
1472 QualType ToType = Importer.Import(From.getAsType());
1473 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001474 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001475 return TemplateArgument(ToType);
1476 }
1477
1478 case TemplateArgument::Integral: {
1479 QualType ToType = Importer.Import(From.getIntegralType());
1480 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001481 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001482 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001483 }
1484
Eli Friedmanb826a002012-09-26 02:36:12 +00001485 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001486 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001487 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1488 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001489 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001490 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001491 }
1492
1493 case TemplateArgument::NullPtr: {
1494 QualType ToType = Importer.Import(From.getNullPtrType());
1495 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001496 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001497 return TemplateArgument(ToType, /*isNullPtr*/true);
1498 }
1499
Douglas Gregore2e50d332010-12-01 01:36:18 +00001500 case TemplateArgument::Template: {
1501 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1502 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001503 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001504
1505 return TemplateArgument(ToTemplate);
1506 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001507
1508 case TemplateArgument::TemplateExpansion: {
1509 TemplateName ToTemplate
1510 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1511 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001512 return {};
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001513
Douglas Gregore1d60df2011-01-14 23:41:42 +00001514 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001515 }
1516
Douglas Gregore2e50d332010-12-01 01:36:18 +00001517 case TemplateArgument::Expression:
1518 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1519 return TemplateArgument(ToExpr);
1520 return TemplateArgument();
1521
1522 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001523 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001524 ToPack.reserve(From.pack_size());
1525 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001526 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001527
1528 return TemplateArgument(
1529 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001530 }
1531 }
1532
1533 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001534}
1535
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001536Optional<TemplateArgumentLoc>
1537ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001538 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1539 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1540 TemplateArgumentLocInfo ToInfo;
1541 if (Arg.getKind() == TemplateArgument::Expression) {
1542 Expr *E = Importer.Import(FromInfo.getAsExpr());
1543 ToInfo = TemplateArgumentLocInfo(E);
1544 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001545 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001546 } else if (Arg.getKind() == TemplateArgument::Type) {
1547 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1548 ToInfo = TemplateArgumentLocInfo(TSI);
1549 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001550 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001551 } else {
1552 ToInfo = TemplateArgumentLocInfo(
1553 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1554 Importer.Import(FromInfo.getTemplateNameLoc()),
1555 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1556 }
1557 return TemplateArgumentLoc(Arg, ToInfo);
1558}
1559
Douglas Gregore2e50d332010-12-01 01:36:18 +00001560bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1561 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001562 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001563 for (unsigned I = 0; I != NumFromArgs; ++I) {
1564 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1565 if (To.isNull() && !FromArgs[I].isNull())
1566 return true;
1567
1568 ToArgs.push_back(To);
1569 }
1570
1571 return false;
1572}
1573
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001574// We cannot use Optional<> pattern here and below because
1575// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1576// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001577template <typename InContainerTy>
1578bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1579 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1580 for (const auto &FromLoc : Container) {
1581 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1582 ToTAInfo.addArgument(*ToLoc);
1583 else
1584 return true;
1585 }
1586 return false;
1587}
1588
Gabor Marton26f72a92018-07-12 09:42:05 +00001589static StructuralEquivalenceKind
1590getStructuralEquivalenceKind(const ASTImporter &Importer) {
1591 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1592 : StructuralEquivalenceKind::Default;
1593}
1594
Douglas Gregor5c73e912010-02-11 00:48:18 +00001595bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001596 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001597 // Eliminate a potential failure point where we attempt to re-import
1598 // something we're trying to import while completing ToRecord.
1599 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1600 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001601 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001602 if (ToOriginRecord)
1603 ToRecord = ToOriginRecord;
1604 }
1605
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001606 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001607 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001608 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001609 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001610 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001611 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001612}
1613
Larisse Voufo39a1e502013-08-06 01:03:05 +00001614bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1615 bool Complain) {
1616 StructuralEquivalenceContext Ctx(
1617 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001618 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1619 false, Complain);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001620 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1621}
1622
Douglas Gregor98c10182010-02-12 22:17:39 +00001623bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001624 StructuralEquivalenceContext Ctx(
1625 Importer.getFromContext(), Importer.getToContext(),
1626 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001627 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001628}
1629
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001630bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1631 FunctionTemplateDecl *To) {
1632 StructuralEquivalenceContext Ctx(
1633 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001634 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1635 false, false);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001636 return Ctx.IsStructurallyEquivalent(From, To);
1637}
1638
Balazs Keric7797c42018-07-11 09:37:24 +00001639bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1640 StructuralEquivalenceContext Ctx(
1641 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001642 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1643 false, false);
Balazs Keric7797c42018-07-11 09:37:24 +00001644 return Ctx.IsStructurallyEquivalent(From, To);
1645}
1646
Douglas Gregor91155082012-11-14 22:29:20 +00001647bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001648 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001649 const llvm::APSInt &FromVal = FromEC->getInitVal();
1650 const llvm::APSInt &ToVal = ToEC->getInitVal();
1651
1652 return FromVal.isSigned() == ToVal.isSigned() &&
1653 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1654 FromVal == ToVal;
1655}
1656
1657bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001658 ClassTemplateDecl *To) {
1659 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1660 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001661 Importer.getNonEquivalentDecls(),
1662 getStructuralEquivalenceKind(Importer));
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001663 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001664}
1665
Larisse Voufo39a1e502013-08-06 01:03:05 +00001666bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1667 VarTemplateDecl *To) {
1668 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1669 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001670 Importer.getNonEquivalentDecls(),
1671 getStructuralEquivalenceKind(Importer));
Larisse Voufo39a1e502013-08-06 01:03:05 +00001672 return Ctx.IsStructurallyEquivalent(From, To);
1673}
1674
Douglas Gregore4c83e42010-02-09 22:48:33 +00001675Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001676 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001677 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001678 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001679}
1680
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001681Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1682 // Import the context of this declaration.
1683 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1684 if (!DC)
1685 return nullptr;
1686
1687 DeclContext *LexicalDC = DC;
1688 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1689 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1690 if (!LexicalDC)
1691 return nullptr;
1692 }
1693
1694 // Import the location of this declaration.
1695 SourceLocation Loc = Importer.Import(D->getLocation());
1696
Gabor Marton26f72a92018-07-12 09:42:05 +00001697 EmptyDecl *ToD;
1698 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc))
1699 return ToD;
1700
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001701 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001702 LexicalDC->addDeclInternal(ToD);
1703 return ToD;
1704}
1705
Sean Callanan65198272011-11-17 23:20:56 +00001706Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1707 TranslationUnitDecl *ToD =
1708 Importer.getToContext().getTranslationUnitDecl();
1709
Gabor Marton26f72a92018-07-12 09:42:05 +00001710 Importer.MapImported(D, ToD);
Sean Callanan65198272011-11-17 23:20:56 +00001711
1712 return ToD;
1713}
1714
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001715Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001716 SourceLocation Loc = Importer.Import(D->getLocation());
1717 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1718
1719 // Import the context of this declaration.
1720 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1721 if (!DC)
1722 return nullptr;
1723
Gabor Marton26f72a92018-07-12 09:42:05 +00001724 AccessSpecDecl *ToD;
1725 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
1726 DC, Loc, ColonLoc))
1727 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001728
1729 // Lexical DeclContext and Semantic DeclContext
1730 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00001731 ToD->setLexicalDeclContext(DC);
1732 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001733
Gabor Marton26f72a92018-07-12 09:42:05 +00001734 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001735}
1736
Aleksei Sidorina693b372016-09-28 10:16:56 +00001737Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1738 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1739 if (!DC)
1740 return nullptr;
1741
1742 DeclContext *LexicalDC = DC;
1743
1744 // Import the location of this declaration.
1745 SourceLocation Loc = Importer.Import(D->getLocation());
1746
1747 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1748 if (!AssertExpr)
1749 return nullptr;
1750
1751 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001752 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001753 if (!ToMsg && FromMsg)
1754 return nullptr;
1755
Gabor Marton26f72a92018-07-12 09:42:05 +00001756 StaticAssertDecl *ToD;
1757 if (GetImportedOrCreateDecl(
1758 ToD, D, Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1759 Importer.Import(D->getRParenLoc()), D->isFailed()))
1760 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001761
1762 ToD->setLexicalDeclContext(LexicalDC);
1763 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001764 return ToD;
1765}
1766
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001767Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1768 // Import the major distinguishing characteristics of this namespace.
1769 DeclContext *DC, *LexicalDC;
1770 DeclarationName Name;
1771 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001772 NamedDecl *ToD;
1773 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001774 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001775 if (ToD)
1776 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001777
1778 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001779 if (!Name) {
1780 // This is an anonymous namespace. Adopt an existing anonymous
1781 // namespace if we can.
1782 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001783 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001784 MergeWithNamespace = TU->getAnonymousNamespace();
1785 else
1786 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1787 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001788 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001789 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001790 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001791 for (auto *FoundDecl : FoundDecls) {
1792 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001793 continue;
1794
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001795 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001796 MergeWithNamespace = FoundNS;
1797 ConflictingDecls.clear();
1798 break;
1799 }
1800
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001801 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001802 }
1803
1804 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001805 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001806 ConflictingDecls.data(),
1807 ConflictingDecls.size());
1808 }
1809 }
1810
1811 // Create the "to" namespace, if needed.
1812 NamespaceDecl *ToNamespace = MergeWithNamespace;
1813 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001814 if (GetImportedOrCreateDecl(
1815 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
1816 Importer.Import(D->getLocStart()), Loc, Name.getAsIdentifierInfo(),
1817 /*PrevDecl=*/nullptr))
1818 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001819 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001820 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001821
1822 // If this is an anonymous namespace, register it as the anonymous
1823 // namespace within its context.
1824 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001825 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001826 TU->setAnonymousNamespace(ToNamespace);
1827 else
1828 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1829 }
1830 }
Gabor Marton26f72a92018-07-12 09:42:05 +00001831 Importer.MapImported(D, ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001832
1833 ImportDeclContext(D);
1834
1835 return ToNamespace;
1836}
1837
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001838Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1839 // Import the major distinguishing characteristics of this namespace.
1840 DeclContext *DC, *LexicalDC;
1841 DeclarationName Name;
1842 SourceLocation Loc;
1843 NamedDecl *LookupD;
1844 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1845 return nullptr;
1846 if (LookupD)
1847 return LookupD;
1848
1849 // NOTE: No conflict resolution is done for namespace aliases now.
1850
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001851 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1852 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001853 if (!TargetDecl)
1854 return nullptr;
1855
1856 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1857 if (!ToII)
1858 return nullptr;
1859
1860 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1861 if (D->getQualifierLoc() && !ToQLoc)
1862 return nullptr;
1863
Gabor Marton26f72a92018-07-12 09:42:05 +00001864 NamespaceAliasDecl *ToD;
1865 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC,
1866 Importer.Import(D->getNamespaceLoc()),
1867 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1868 Importer.Import(D->getTargetNameLoc()),
1869 TargetDecl))
1870 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001871
1872 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001873 LexicalDC->addDeclInternal(ToD);
1874
1875 return ToD;
1876}
1877
Richard Smithdda56e42011-04-15 14:24:37 +00001878Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001879 // Import the major distinguishing characteristics of this typedef.
1880 DeclContext *DC, *LexicalDC;
1881 DeclarationName Name;
1882 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001883 NamedDecl *ToD;
1884 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001885 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001886 if (ToD)
1887 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001888
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001889 // If this typedef is not in block scope, determine whether we've
1890 // seen a typedef with the same name (that we can merge with) or any
1891 // other entity by that name (which name lookup could conflict with).
1892 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001893 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001894 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001895 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001896 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001897 for (auto *FoundDecl : FoundDecls) {
1898 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001899 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001900 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001901 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1902 FoundTypedef->getUnderlyingType()))
Gabor Marton26f72a92018-07-12 09:42:05 +00001903 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001904 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001905
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001906 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001907 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001908
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001909 if (!ConflictingDecls.empty()) {
1910 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1911 ConflictingDecls.data(),
1912 ConflictingDecls.size());
1913 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001914 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001915 }
1916 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001917
Douglas Gregorb4964f72010-02-15 23:54:17 +00001918 // Import the underlying type of this typedef;
1919 QualType T = Importer.Import(D->getUnderlyingType());
1920 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001921 return nullptr;
1922
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001923 // Create the new typedef node.
1924 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001925 SourceLocation StartL = Importer.Import(D->getLocStart());
Gabor Marton26f72a92018-07-12 09:42:05 +00001926
Richard Smithdda56e42011-04-15 14:24:37 +00001927 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00001928 if (IsAlias) {
1929 if (GetImportedOrCreateDecl<TypeAliasDecl>(
1930 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1931 Name.getAsIdentifierInfo(), TInfo))
1932 return ToTypedef;
1933 } else if (GetImportedOrCreateDecl<TypedefDecl>(
1934 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1935 Name.getAsIdentifierInfo(), TInfo))
1936 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001937
Douglas Gregordd483172010-02-22 17:42:47 +00001938 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001939 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001940
1941 // Templated declarations should not appear in DeclContext.
1942 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1943 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1944 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001945
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001946 return ToTypedef;
1947}
1948
Richard Smithdda56e42011-04-15 14:24:37 +00001949Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1950 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1951}
1952
1953Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1954 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1955}
1956
Gabor Horvath7a91c082017-11-14 11:30:38 +00001957Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1958 // Import the major distinguishing characteristics of this typedef.
1959 DeclContext *DC, *LexicalDC;
1960 DeclarationName Name;
1961 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001962 NamedDecl *FoundD;
1963 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001964 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001965 if (FoundD)
1966 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001967
1968 // If this typedef is not in block scope, determine whether we've
1969 // seen a typedef with the same name (that we can merge with) or any
1970 // other entity by that name (which name lookup could conflict with).
1971 if (!DC->isFunctionOrMethod()) {
1972 SmallVector<NamedDecl *, 4> ConflictingDecls;
1973 unsigned IDNS = Decl::IDNS_Ordinary;
1974 SmallVector<NamedDecl *, 2> FoundDecls;
1975 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001976 for (auto *FoundDecl : FoundDecls) {
1977 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001978 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001979 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00001980 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001981 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001982 }
1983
1984 if (!ConflictingDecls.empty()) {
1985 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1986 ConflictingDecls.data(),
1987 ConflictingDecls.size());
1988 if (!Name)
1989 return nullptr;
1990 }
1991 }
1992
1993 TemplateParameterList *Params = ImportTemplateParameterList(
1994 D->getTemplateParameters());
1995 if (!Params)
1996 return nullptr;
1997
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001998 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001999 Importer.Import(D->getTemplatedDecl()));
2000 if (!TemplDecl)
2001 return nullptr;
2002
Gabor Marton26f72a92018-07-12 09:42:05 +00002003 TypeAliasTemplateDecl *ToAlias;
2004 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
2005 Name, Params, TemplDecl))
2006 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002007
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002008 TemplDecl->setDescribedAliasTemplate(ToAlias);
2009
Gabor Horvath7a91c082017-11-14 11:30:38 +00002010 ToAlias->setAccess(D->getAccess());
2011 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002012 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002013 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002014}
2015
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002016Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2017 // Import the major distinguishing characteristics of this label.
2018 DeclContext *DC, *LexicalDC;
2019 DeclarationName Name;
2020 SourceLocation Loc;
2021 NamedDecl *ToD;
2022 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2023 return nullptr;
2024 if (ToD)
2025 return ToD;
2026
2027 assert(LexicalDC->isFunctionOrMethod());
2028
Gabor Marton26f72a92018-07-12 09:42:05 +00002029 LabelDecl *ToLabel;
2030 if (D->isGnuLocal()
2031 ? GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2032 Importer.Import(D->getLocation()),
2033 Name.getAsIdentifierInfo(),
2034 Importer.Import(D->getLocStart()))
2035 : GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2036 Importer.Import(D->getLocation()),
2037 Name.getAsIdentifierInfo()))
2038 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002039
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002040 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002041 if (!Label)
2042 return nullptr;
2043
2044 ToLabel->setStmt(Label);
2045 ToLabel->setLexicalDeclContext(LexicalDC);
2046 LexicalDC->addDeclInternal(ToLabel);
2047 return ToLabel;
2048}
2049
Douglas Gregor98c10182010-02-12 22:17:39 +00002050Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2051 // Import the major distinguishing characteristics of this enum.
2052 DeclContext *DC, *LexicalDC;
2053 DeclarationName Name;
2054 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002055 NamedDecl *ToD;
2056 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002057 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002058 if (ToD)
2059 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002060
Douglas Gregor98c10182010-02-12 22:17:39 +00002061 // Figure out what enum name we're looking for.
2062 unsigned IDNS = Decl::IDNS_Tag;
2063 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002064 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2065 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002066 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002067 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002068 IDNS |= Decl::IDNS_Ordinary;
2069
2070 // We may already have an enum of the same name; try to find and match it.
2071 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002072 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002073 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002074 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002075 for (auto *FoundDecl : FoundDecls) {
2076 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002077 continue;
2078
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002079 Decl *Found = FoundDecl;
2080 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2081 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00002082 Found = Tag->getDecl();
2083 }
2084
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002085 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002086 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002087 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002088 }
2089
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002090 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002091 }
2092
2093 if (!ConflictingDecls.empty()) {
2094 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2095 ConflictingDecls.data(),
2096 ConflictingDecls.size());
2097 }
2098 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002099
Douglas Gregor98c10182010-02-12 22:17:39 +00002100 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002101 EnumDecl *D2;
2102 if (GetImportedOrCreateDecl(
2103 D2, D, Importer.getToContext(), DC, Importer.Import(D->getLocStart()),
2104 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2105 D->isScopedUsingClassTag(), D->isFixed()))
2106 return D2;
2107
John McCall3e11ebe2010-03-15 10:12:16 +00002108 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002109 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002110 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002111 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002112 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002113
2114 // Import the integer type.
2115 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2116 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002117 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002118 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002119
2120 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002121 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002122 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002123
Douglas Gregor3996e242010-02-15 22:01:00 +00002124 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002125}
2126
Douglas Gregor5c73e912010-02-11 00:48:18 +00002127Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2128 // If this record has a definition in the translation unit we're coming from,
2129 // but this particular declaration is not that definition, import the
2130 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002131 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002132 if (Definition && Definition != D &&
2133 // In contrast to a normal CXXRecordDecl, the implicit
2134 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2135 // The definition of the implicit CXXRecordDecl in this case is the
2136 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2137 // condition in order to be able to import the implict Decl.
2138 !D->isImplicit()) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002139 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002140 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002141 return nullptr;
2142
Gabor Marton26f72a92018-07-12 09:42:05 +00002143 return Importer.MapImported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002144 }
Gabor Martona3af5672018-05-23 14:24:02 +00002145
Douglas Gregor5c73e912010-02-11 00:48:18 +00002146 // Import the major distinguishing characteristics of this record.
2147 DeclContext *DC, *LexicalDC;
2148 DeclarationName Name;
2149 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002150 NamedDecl *ToD;
2151 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002152 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002153 if (ToD)
2154 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002155
Douglas Gregor5c73e912010-02-11 00:48:18 +00002156 // Figure out what structure name we're looking for.
2157 unsigned IDNS = Decl::IDNS_Tag;
2158 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002159 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2160 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002161 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002162 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002163 IDNS |= Decl::IDNS_Ordinary;
2164
2165 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002166 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002167 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002168 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002169 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002170 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002171 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002172
2173 if (!FoundDecls.empty()) {
2174 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2175 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2176 D->getASTContext().getExternalSource()->CompleteType(D);
2177 }
2178
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002179 for (auto *FoundDecl : FoundDecls) {
2180 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002181 continue;
2182
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002183 Decl *Found = FoundDecl;
2184 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2185 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002186 Found = Tag->getDecl();
2187 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002188
2189 if (D->getDescribedTemplate()) {
2190 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found))
2191 Found = Template->getTemplatedDecl();
2192 else
2193 continue;
2194 }
2195
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002196 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002197 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002198 if (!IsStructuralMatch(D, FoundRecord, false))
2199 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002200 }
2201
Sean Callanan9092d472017-05-13 00:46:33 +00002202 PrevDecl = FoundRecord;
2203
Douglas Gregor25791052010-02-12 00:09:27 +00002204 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002205 if ((SearchName && !D->isCompleteDefinition())
2206 || (D->isCompleteDefinition() &&
2207 D->isAnonymousStructOrUnion()
2208 == FoundDef->isAnonymousStructOrUnion() &&
2209 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002210 // The record types structurally match, or the "from" translation
2211 // unit only had a forward declaration anyway; call it the same
2212 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002213 // FIXME: Structural equivalence check should check for same
2214 // user-defined methods.
2215 Importer.MapImported(D, FoundDef);
2216 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2217 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2218 assert(FoundCXX && "Record type mismatch");
2219
2220 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2221 // FoundDef may not have every implicit method that D has
2222 // because implicit methods are created only if they are used.
2223 ImportImplicitMethods(DCXX, FoundCXX);
2224 }
2225 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002226 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002227 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002228 // We have a forward declaration of this type, so adopt that forward
2229 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002230
2231 // If one or both can be completed from external storage then try one
2232 // last time to complete and compare them before doing this.
2233
2234 if (FoundRecord->hasExternalLexicalStorage() &&
2235 !FoundRecord->isCompleteDefinition())
2236 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2237 if (D->hasExternalLexicalStorage())
2238 D->getASTContext().getExternalSource()->CompleteType(D);
2239
2240 if (FoundRecord->isCompleteDefinition() &&
2241 D->isCompleteDefinition() &&
2242 !IsStructuralMatch(D, FoundRecord))
2243 continue;
2244
Douglas Gregor25791052010-02-12 00:09:27 +00002245 AdoptDecl = FoundRecord;
2246 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002247 } else if (!SearchName) {
2248 continue;
2249 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002250 }
2251
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002252 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002253 }
2254
Douglas Gregordd6006f2012-07-17 21:16:27 +00002255 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002256 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2257 ConflictingDecls.data(),
2258 ConflictingDecls.size());
2259 }
2260 }
2261
2262 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002263 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002264 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002265 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002266 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002267 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002268 if (DCXX->isLambda()) {
2269 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00002270 if (GetImportedOrCreateSpecialDecl(
2271 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2272 DC, TInfo, Loc, DCXX->isDependentLambda(),
2273 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2274 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002275 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2276 if (DCXX->getLambdaContextDecl() && !CDecl)
2277 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002278 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002279 } else if (DCXX->isInjectedClassName()) {
2280 // We have to be careful to do a similar dance to the one in
2281 // Sema::ActOnStartCXXMemberDeclarations
2282 CXXRecordDecl *const PrevDecl = nullptr;
2283 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002284 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2285 D->getTagKind(), DC, StartLoc, Loc,
2286 Name.getAsIdentifierInfo(), PrevDecl,
2287 DelayTypeCreation))
2288 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002289 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002290 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002291 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002292 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2293 D->getTagKind(), DC, StartLoc, Loc,
2294 Name.getAsIdentifierInfo(),
2295 cast_or_null<CXXRecordDecl>(PrevDecl)))
2296 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002297 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002298
Douglas Gregor3996e242010-02-15 22:01:00 +00002299 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002300 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002301 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002302 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002303 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002304
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002305 if (ClassTemplateDecl *FromDescribed =
2306 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002307 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2308 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002309 if (!ToDescribed)
2310 return nullptr;
2311 D2CXX->setDescribedClassTemplate(ToDescribed);
Gabor Marton5915777e2018-06-26 13:44:24 +00002312 if (!DCXX->isInjectedClassName()) {
2313 // In a record describing a template the type should be an
2314 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2315 // previously set type to the correct value here (ToDescribed is not
2316 // available at record create).
2317 // FIXME: The previous type is cleared but not removed from
2318 // ASTContext's internal storage.
2319 CXXRecordDecl *Injected = nullptr;
2320 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2321 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2322 if (Record && Record->isInjectedClassName()) {
2323 Injected = Record;
2324 break;
2325 }
2326 }
2327 D2CXX->setTypeForDecl(nullptr);
2328 Importer.getToContext().getInjectedClassNameType(D2CXX,
2329 ToDescribed->getInjectedClassNameSpecialization());
2330 if (Injected) {
2331 Injected->setTypeForDecl(nullptr);
2332 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2333 }
2334 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002335 } else if (MemberSpecializationInfo *MemberInfo =
2336 DCXX->getMemberSpecializationInfo()) {
2337 TemplateSpecializationKind SK =
2338 MemberInfo->getTemplateSpecializationKind();
2339 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002340 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002341 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2342 if (FromInst && !ToInst)
2343 return nullptr;
2344 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2345 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2346 Importer.Import(MemberInfo->getPointOfInstantiation()));
2347 }
Douglas Gregor25791052010-02-12 00:09:27 +00002348 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002349 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2350 D->getTagKind(), DC, StartLoc, Loc,
2351 Name.getAsIdentifierInfo(), PrevDecl))
2352 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002353 D2->setLexicalDeclContext(LexicalDC);
2354 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002355 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002356
Douglas Gregor14454802011-02-25 02:25:35 +00002357 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002358 if (D->isAnonymousStructOrUnion())
2359 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002360 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002361
2362 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002363
Douglas Gregor95d82832012-01-24 18:36:04 +00002364 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002365 return nullptr;
2366
Douglas Gregor3996e242010-02-15 22:01:00 +00002367 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002368}
2369
Douglas Gregor98c10182010-02-12 22:17:39 +00002370Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2371 // Import the major distinguishing characteristics of this enumerator.
2372 DeclContext *DC, *LexicalDC;
2373 DeclarationName Name;
2374 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002375 NamedDecl *ToD;
2376 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002377 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002378 if (ToD)
2379 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002380
2381 QualType T = Importer.Import(D->getType());
2382 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002383 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002384
Douglas Gregor98c10182010-02-12 22:17:39 +00002385 // Determine whether there are any other declarations with the same name and
2386 // in the same context.
2387 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002388 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002389 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002390 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002391 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002392 for (auto *FoundDecl : FoundDecls) {
2393 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002394 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002395
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002396 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002397 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002398 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002399 }
2400
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002401 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002402 }
2403
2404 if (!ConflictingDecls.empty()) {
2405 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2406 ConflictingDecls.data(),
2407 ConflictingDecls.size());
2408 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002409 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002410 }
2411 }
2412
2413 Expr *Init = Importer.Import(D->getInitExpr());
2414 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002415 return nullptr;
2416
Gabor Marton26f72a92018-07-12 09:42:05 +00002417 EnumConstantDecl *ToEnumerator;
2418 if (GetImportedOrCreateDecl(
2419 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2420 Name.getAsIdentifierInfo(), T, Init, D->getInitVal()))
2421 return ToEnumerator;
2422
Douglas Gregordd483172010-02-22 17:42:47 +00002423 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002424 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002425 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002426 return ToEnumerator;
2427}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002428
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002429bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2430 FunctionDecl *ToFD) {
2431 switch (FromFD->getTemplatedKind()) {
2432 case FunctionDecl::TK_NonTemplate:
2433 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002434 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002435
2436 case FunctionDecl::TK_MemberSpecialization: {
2437 auto *InstFD = cast_or_null<FunctionDecl>(
2438 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2439 if (!InstFD)
2440 return true;
2441
2442 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2443 SourceLocation POI = Importer.Import(
2444 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2445 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2446 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002447 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002448 }
2449
2450 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Gabor Marton5254e642018-06-27 13:32:50 +00002451 FunctionTemplateDecl* Template;
2452 OptionalTemplateArgsTy ToTemplArgs;
2453 std::tie(Template, ToTemplArgs) =
2454 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2455 if (!Template || !ToTemplArgs)
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002456 return true;
2457
2458 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Gabor Marton5254e642018-06-27 13:32:50 +00002459 Importer.getToContext(), *ToTemplArgs);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002460
Gabor Marton5254e642018-06-27 13:32:50 +00002461 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002462 TemplateArgumentListInfo ToTAInfo;
2463 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002464 if (FromTAArgsAsWritten)
2465 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002466 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002467
2468 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2469
Gabor Marton5254e642018-06-27 13:32:50 +00002470 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002471 ToFD->setFunctionTemplateSpecialization(
2472 Template, ToTAList, /* InsertPos= */ nullptr,
2473 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002474 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002475 }
2476
2477 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2478 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2479 UnresolvedSet<8> TemplDecls;
2480 unsigned NumTemplates = FromInfo->getNumTemplates();
2481 for (unsigned I = 0; I < NumTemplates; I++) {
2482 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2483 Importer.Import(FromInfo->getTemplate(I))))
2484 TemplDecls.addDecl(ToFTD);
2485 else
2486 return true;
2487 }
2488
2489 // Import TemplateArgumentListInfo.
2490 TemplateArgumentListInfo ToTAInfo;
2491 if (ImportTemplateArgumentListInfo(
2492 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2493 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2494 FromInfo->getNumTemplateArgs()),
2495 ToTAInfo))
2496 return true;
2497
2498 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2499 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002500 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002501 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002502 }
Sam McCallfdc32072018-01-26 12:06:44 +00002503 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002504}
2505
Gabor Marton5254e642018-06-27 13:32:50 +00002506FunctionDecl *
2507ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
2508 FunctionTemplateDecl* Template;
2509 OptionalTemplateArgsTy ToTemplArgs;
2510 std::tie(Template, ToTemplArgs) =
2511 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2512 if (!Template || !ToTemplArgs)
2513 return nullptr;
2514
2515 void *InsertPos = nullptr;
2516 auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos);
2517 return FoundSpec;
2518}
2519
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002520Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002521
2522 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
2523 auto RedeclIt = Redecls.begin();
2524 // Import the first part of the decl chain. I.e. import all previous
2525 // declarations starting from the canonical decl.
2526 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt)
2527 if (!Importer.Import(*RedeclIt))
2528 return nullptr;
2529 assert(*RedeclIt == D);
2530
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002531 // Import the major distinguishing characteristics of this function.
2532 DeclContext *DC, *LexicalDC;
2533 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002534 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002535 NamedDecl *ToD;
2536 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002537 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002538 if (ToD)
2539 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002540
Gabor Marton5254e642018-06-27 13:32:50 +00002541 const FunctionDecl *FoundByLookup = nullptr;
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002542
Gabor Marton5254e642018-06-27 13:32:50 +00002543 // If this is a function template specialization, then try to find the same
2544 // existing specialization in the "to" context. The localUncachedLookup
2545 // below will not find any specialization, but would find the primary
2546 // template; thus, we have to skip normal lookup in case of specializations.
2547 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2548 if (D->getTemplatedKind() ==
2549 FunctionDecl::TK_FunctionTemplateSpecialization) {
2550 if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) {
2551 if (D->doesThisDeclarationHaveABody() &&
2552 FoundFunction->hasBody())
2553 return Importer.Imported(D, FoundFunction);
2554 FoundByLookup = FoundFunction;
2555 }
2556 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002557 // Try to find a function in our own ("to") context with the same name, same
2558 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002559 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002560 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002561 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002562 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002563 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002564 for (auto *FoundDecl : FoundDecls) {
2565 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002566 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002567
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002568 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002569 if (FoundFunction->hasExternalFormalLinkage() &&
2570 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00002571 if (IsStructuralMatch(D, FoundFunction)) {
2572 const FunctionDecl *Definition = nullptr;
2573 if (D->doesThisDeclarationHaveABody() &&
2574 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002575 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00002576 D, const_cast<FunctionDecl *>(Definition));
2577 }
2578 FoundByLookup = FoundFunction;
2579 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002580 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002581
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002582 // FIXME: Check for overloading more carefully, e.g., by boosting
2583 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002584
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002585 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002586 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002587 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002588
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002589 // Complain about inconsistent function types.
2590 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002591 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002592 Importer.ToDiag(FoundFunction->getLocation(),
2593 diag::note_odr_value_here)
2594 << FoundFunction->getType();
2595 }
2596 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002597
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002598 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002599 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002600
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002601 if (!ConflictingDecls.empty()) {
2602 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2603 ConflictingDecls.data(),
2604 ConflictingDecls.size());
2605 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002606 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002607 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002608 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002609
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002610 DeclarationNameInfo NameInfo(Name, Loc);
2611 // Import additional name location/type info.
2612 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2613
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002614 QualType FromTy = D->getType();
2615 bool usedDifferentExceptionSpec = false;
2616
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002617 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002618 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2619 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2620 // FunctionDecl that we are importing the FunctionProtoType for.
2621 // To avoid an infinite recursion when importing, create the FunctionDecl
2622 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002623 if (FromEPI.ExceptionSpec.SourceDecl ||
2624 FromEPI.ExceptionSpec.SourceTemplate ||
2625 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002626 FunctionProtoType::ExtProtoInfo DefaultEPI;
2627 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002628 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002629 usedDifferentExceptionSpec = true;
2630 }
2631 }
2632
Douglas Gregorb4964f72010-02-15 23:54:17 +00002633 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002634 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002635 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002636 return nullptr;
2637
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002638 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002639 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002640 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002641 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002642 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002643 return nullptr;
2644
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002645 Parameters.push_back(ToP);
2646 }
2647
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002648 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002649 if (D->getTypeSourceInfo() && !TInfo)
2650 return nullptr;
2651
2652 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002653 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002654 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002655 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002656 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
2657 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2658 InnerLocStart, NameInfo, T, TInfo, FromConstructor->isExplicit(),
2659 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
2660 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00002661 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2662 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002663 for (auto *I : FromConstructor->inits()) {
2664 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002665 if (!ToI && I)
2666 return nullptr;
2667 CtorInitializers.push_back(ToI);
2668 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002669 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002670 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2671 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002672 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002673 ToCtor->setCtorInitializers(Memory);
2674 ToCtor->setNumCtorInitializers(NumInitializers);
2675 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002676 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002677 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
2678 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2679 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2680 D->isImplicit()))
2681 return ToFunction;
2682 } else if (CXXConversionDecl *FromConversion =
2683 dyn_cast<CXXConversionDecl>(D)) {
2684 if (GetImportedOrCreateDecl<CXXConversionDecl>(
2685 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2686 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2687 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
2688 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002689 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002690 if (GetImportedOrCreateDecl<CXXMethodDecl>(
2691 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2692 InnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
2693 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
2694 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002695 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002696 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
2697 InnerLocStart, NameInfo, T, TInfo,
2698 D->getStorageClass(), D->isInlineSpecified(),
2699 D->hasWrittenPrototype(), D->isConstexpr()))
2700 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002701 }
John McCall3e11ebe2010-03-15 10:12:16 +00002702
2703 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002704 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002705 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002706 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002707 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2708 ToFunction->setTrivial(D->isTrivial());
2709 ToFunction->setPure(D->isPure());
Rafael Stahla0010472018-07-09 08:40:17 +00002710 ToFunction->setRangeEnd(Importer.Import(D->getLocEnd()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00002711
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002712 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002713 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002714 Param->setOwningFunction(ToFunction);
2715 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002716 }
David Blaikie9c70e042011-09-21 18:16:56 +00002717 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002718
Gabor Marton5254e642018-06-27 13:32:50 +00002719 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002720 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00002721 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002722 ToFunction->setPreviousDecl(Recent);
2723 }
2724
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002725 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2726 // params it refers to.
2727 if (TInfo) {
2728 if (auto ProtoLoc =
2729 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2730 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2731 ProtoLoc.setParam(I, Parameters[I]);
2732 }
2733 }
2734
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002735 if (usedDifferentExceptionSpec) {
2736 // Update FunctionProtoType::ExtProtoInfo.
2737 QualType T = Importer.Import(D->getType());
2738 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002739 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002740 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002741 }
2742
Gabor Marton5254e642018-06-27 13:32:50 +00002743 if (D->doesThisDeclarationHaveABody()) {
2744 if (Stmt *FromBody = D->getBody()) {
2745 if (Stmt *ToBody = Importer.Import(FromBody)) {
2746 ToFunction->setBody(ToBody);
2747 }
Sean Callanan59721b32015-04-28 18:41:46 +00002748 }
2749 }
2750
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002751 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002752
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002753 // If it is a template, import all related things.
2754 if (ImportTemplateInformation(D, ToFunction))
2755 return nullptr;
2756
Gabor Marton5254e642018-06-27 13:32:50 +00002757 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
2758
2759 // TODO Can we generalize this approach to other AST nodes as well?
2760 if (D->getDeclContext()->containsDeclAndLoad(D))
2761 DC->addDeclInternal(ToFunction);
2762 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002763 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002764
Gabor Marton5254e642018-06-27 13:32:50 +00002765 // Friend declaration's lexical context is the befriending class, but the
2766 // semantic context is the enclosing scope of the befriending class.
2767 // We want the friend functions to be found in the semantic context by lookup.
2768 // FIXME should we handle this generically in VisitFriendDecl?
2769 // In Other cases when LexicalDC != DC we don't want it to be added,
2770 // e.g out-of-class definitions like void B::f() {} .
2771 if (LexicalDC != DC && IsFriend) {
2772 DC->makeDeclVisibleInContext(ToFunction);
2773 }
2774
2775 // Import the rest of the chain. I.e. import all subsequent declarations.
2776 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt)
2777 if (!Importer.Import(*RedeclIt))
2778 return nullptr;
2779
Lang Hames19e07e12017-06-20 21:06:00 +00002780 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2781 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2782
Douglas Gregor43f54792010-02-17 02:12:47 +00002783 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002784}
2785
Douglas Gregor00eace12010-02-21 18:29:16 +00002786Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2787 return VisitFunctionDecl(D);
2788}
2789
2790Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2791 return VisitCXXMethodDecl(D);
2792}
2793
2794Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2795 return VisitCXXMethodDecl(D);
2796}
2797
2798Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2799 return VisitCXXMethodDecl(D);
2800}
2801
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002802static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002803 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002804 if (!Owner)
2805 return 0;
2806
2807 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002808 for (const auto *D : Owner->noload_decls()) {
2809 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002810 return Index;
2811
2812 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2813 ++Index;
2814 }
2815
2816 return Index;
2817}
2818
Douglas Gregor5c73e912010-02-11 00:48:18 +00002819Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2820 // Import the major distinguishing characteristics of a variable.
2821 DeclContext *DC, *LexicalDC;
2822 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002823 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002824 NamedDecl *ToD;
2825 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002826 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002827 if (ToD)
2828 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002829
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002830 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002831 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002832 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002833 for (auto *FoundDecl : FoundDecls) {
2834 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002835 // For anonymous fields, match up by index.
2836 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2837 continue;
2838
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002839 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002840 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002841 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002842 return FoundField;
2843 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002844
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002845 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2846 << Name << D->getType() << FoundField->getType();
2847 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2848 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002849 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002850 }
2851 }
2852
Douglas Gregorb4964f72010-02-15 23:54:17 +00002853 // Import the type.
2854 QualType T = Importer.Import(D->getType());
2855 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002856 return nullptr;
2857
Douglas Gregor5c73e912010-02-11 00:48:18 +00002858 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2859 Expr *BitWidth = Importer.Import(D->getBitWidth());
2860 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002861 return nullptr;
2862
Gabor Marton26f72a92018-07-12 09:42:05 +00002863 FieldDecl *ToField;
2864 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
2865 Importer.Import(D->getInnerLocStart()), Loc,
2866 Name.getAsIdentifierInfo(), T, TInfo, BitWidth,
2867 D->isMutable(), D->getInClassInitStyle()))
2868 return ToField;
2869
Douglas Gregordd483172010-02-22 17:42:47 +00002870 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002871 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002872 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002873 Expr *ToInitializer = Importer.Import(FromInitializer);
2874 if (ToInitializer)
2875 ToField->setInClassInitializer(ToInitializer);
2876 else
2877 return nullptr;
2878 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002879 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00002880 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002881 return ToField;
2882}
2883
Francois Pichet783dd6e2010-11-21 06:08:52 +00002884Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2885 // Import the major distinguishing characteristics of a variable.
2886 DeclContext *DC, *LexicalDC;
2887 DeclarationName Name;
2888 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002889 NamedDecl *ToD;
2890 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002891 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002892 if (ToD)
2893 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002894
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002895 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002896 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002897 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002898 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002899 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002900 // For anonymous indirect fields, match up by index.
2901 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2902 continue;
2903
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002904 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002905 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002906 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002907 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002908 return FoundField;
2909 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002910
2911 // If there are more anonymous fields to check, continue.
2912 if (!Name && I < N-1)
2913 continue;
2914
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002915 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2916 << Name << D->getType() << FoundField->getType();
2917 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2918 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002919 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002920 }
2921 }
2922
Francois Pichet783dd6e2010-11-21 06:08:52 +00002923 // Import the type.
2924 QualType T = Importer.Import(D->getType());
2925 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002926 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002927
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002928 auto **NamedChain =
2929 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002930
2931 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002932 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002933 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002934 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002935 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002936 NamedChain[i++] = cast<NamedDecl>(D);
2937 }
2938
Gabor Marton26f72a92018-07-12 09:42:05 +00002939 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
2940 IndirectFieldDecl *ToIndirectField;
2941 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
2942 Loc, Name.getAsIdentifierInfo(), T, CH))
2943 // FIXME here we leak `NamedChain` which is allocated before
2944 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00002945
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002946 for (const auto *A : D->attrs())
2947 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00002948
Francois Pichet783dd6e2010-11-21 06:08:52 +00002949 ToIndirectField->setAccess(D->getAccess());
2950 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002951 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002952 return ToIndirectField;
2953}
2954
Aleksei Sidorina693b372016-09-28 10:16:56 +00002955Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2956 // Import the major distinguishing characteristics of a declaration.
2957 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2958 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2959 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2960 if (!DC || !LexicalDC)
2961 return nullptr;
2962
2963 // Determine whether we've already imported this decl.
2964 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2965 auto *RD = cast<CXXRecordDecl>(DC);
2966 FriendDecl *ImportedFriend = RD->getFirstFriend();
2967 StructuralEquivalenceContext Context(
2968 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002969 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2970 false, false);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002971
2972 while (ImportedFriend) {
2973 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2974 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2975 ImportedFriend->getFriendDecl()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002976 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002977
2978 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2979 if (Importer.IsStructurallyEquivalent(
2980 D->getFriendType()->getType(),
2981 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00002982 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002983 }
2984 ImportedFriend = ImportedFriend->getNextFriend();
2985 }
2986
2987 // Not found. Create it.
2988 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002989 if (NamedDecl *FriendD = D->getFriendDecl()) {
2990 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2991 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2992 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2993 ToFriendD->setObjectOfFriendDecl(false);
2994
2995 ToFU = ToFriendD;
2996 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002997 ToFU = Importer.Import(D->getFriendType());
2998 if (!ToFU)
2999 return nullptr;
3000
3001 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003002 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003003 for (unsigned I = 0; I < D->NumTPLists; I++) {
3004 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
3005 if (!List)
3006 return nullptr;
3007 ToTPLists[I] = List;
3008 }
3009
Gabor Marton26f72a92018-07-12 09:42:05 +00003010 FriendDecl *FrD;
3011 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
3012 Importer.Import(D->getLocation()), ToFU,
3013 Importer.Import(D->getFriendLoc()), ToTPLists))
3014 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003015
3016 FrD->setAccess(D->getAccess());
3017 FrD->setLexicalDeclContext(LexicalDC);
3018 LexicalDC->addDeclInternal(FrD);
3019 return FrD;
3020}
3021
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003022Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3023 // Import the major distinguishing characteristics of an ivar.
3024 DeclContext *DC, *LexicalDC;
3025 DeclarationName Name;
3026 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003027 NamedDecl *ToD;
3028 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003029 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003030 if (ToD)
3031 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003032
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003033 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003034 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003035 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003036 for (auto *FoundDecl : FoundDecls) {
3037 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003038 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003039 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003040 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003041 return FoundIvar;
3042 }
3043
3044 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3045 << Name << D->getType() << FoundIvar->getType();
3046 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3047 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003048 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003049 }
3050 }
3051
3052 // Import the type.
3053 QualType T = Importer.Import(D->getType());
3054 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003055 return nullptr;
3056
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003057 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3058 Expr *BitWidth = Importer.Import(D->getBitWidth());
3059 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003060 return nullptr;
3061
Gabor Marton26f72a92018-07-12 09:42:05 +00003062 ObjCIvarDecl *ToIvar;
3063 if (GetImportedOrCreateDecl(
3064 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
3065 Importer.Import(D->getInnerLocStart()), Loc,
3066 Name.getAsIdentifierInfo(), T, TInfo, D->getAccessControl(), BitWidth,
3067 D->getSynthesize()))
3068 return ToIvar;
3069
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003070 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003071 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003072 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003073}
3074
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003075Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3076 // Import the major distinguishing characteristics of a variable.
3077 DeclContext *DC, *LexicalDC;
3078 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003079 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003080 NamedDecl *ToD;
3081 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003082 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003083 if (ToD)
3084 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003085
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003086 // Try to find a variable in our own ("to") context with the same name and
3087 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003088 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003089 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003090 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003091 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003092 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003093 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003094 for (auto *FoundDecl : FoundDecls) {
3095 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003096 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003097
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003098 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003099 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003100 if (FoundVar->hasExternalFormalLinkage() &&
3101 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003102 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003103 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003104 MergeWithVar = FoundVar;
3105 break;
3106 }
3107
Douglas Gregor56521c52010-02-12 17:23:39 +00003108 const ArrayType *FoundArray
3109 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3110 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003111 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003112 if (FoundArray && TArray) {
3113 if (isa<IncompleteArrayType>(FoundArray) &&
3114 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003115 // Import the type.
3116 QualType T = Importer.Import(D->getType());
3117 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003118 return nullptr;
3119
Douglas Gregor56521c52010-02-12 17:23:39 +00003120 FoundVar->setType(T);
3121 MergeWithVar = FoundVar;
3122 break;
3123 } else if (isa<IncompleteArrayType>(TArray) &&
3124 isa<ConstantArrayType>(FoundArray)) {
3125 MergeWithVar = FoundVar;
3126 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003127 }
3128 }
3129
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003130 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003131 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003132 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3133 << FoundVar->getType();
3134 }
3135 }
3136
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003137 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003138 }
3139
3140 if (MergeWithVar) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003141 // An equivalent variable with external linkage has been found. Link
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003142 // the two declarations, then merge them.
Gabor Marton26f72a92018-07-12 09:42:05 +00003143 Importer.MapImported(D, MergeWithVar);
3144 updateFlags(D, MergeWithVar);
3145
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003146 if (VarDecl *DDef = D->getDefinition()) {
3147 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3148 Importer.ToDiag(ExistingDef->getLocation(),
3149 diag::err_odr_variable_multiple_def)
3150 << Name;
3151 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3152 } else {
3153 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003154 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003155 if (DDef->isInitKnownICE()) {
3156 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3157 Eval->CheckedICE = true;
3158 Eval->IsICE = DDef->isInitICE();
3159 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003160 }
3161 }
3162
3163 return MergeWithVar;
3164 }
3165
3166 if (!ConflictingDecls.empty()) {
3167 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3168 ConflictingDecls.data(),
3169 ConflictingDecls.size());
3170 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003171 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003172 }
3173 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003174
Douglas Gregorb4964f72010-02-15 23:54:17 +00003175 // Import the type.
3176 QualType T = Importer.Import(D->getType());
3177 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003178 return nullptr;
3179
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003180 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003181 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003182 VarDecl *ToVar;
3183 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
3184 Importer.Import(D->getInnerLocStart()), Loc,
3185 Name.getAsIdentifierInfo(), T, TInfo,
3186 D->getStorageClass()))
3187 return ToVar;
3188
Douglas Gregor14454802011-02-25 02:25:35 +00003189 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003190 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003191 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003192
3193 // Templated declarations should never appear in the enclosing DeclContext.
3194 if (!D->getDescribedVarTemplate())
3195 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003196
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003197 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003198 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003199 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003200
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003201 if (D->isConstexpr())
3202 ToVar->setConstexpr(true);
3203
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003204 return ToVar;
3205}
3206
Douglas Gregor8b228d72010-02-17 21:22:52 +00003207Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3208 // Parameters are created in the translation unit's context, then moved
3209 // into the function declaration's context afterward.
3210 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3211
3212 // Import the name of this declaration.
3213 DeclarationName Name = Importer.Import(D->getDeclName());
3214 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003215 return nullptr;
3216
Douglas Gregor8b228d72010-02-17 21:22:52 +00003217 // Import the location of this declaration.
3218 SourceLocation Loc = Importer.Import(D->getLocation());
3219
3220 // Import the parameter's type.
3221 QualType T = Importer.Import(D->getType());
3222 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003223 return nullptr;
3224
Douglas Gregor8b228d72010-02-17 21:22:52 +00003225 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003226 ImplicitParamDecl *ToParm = nullptr;
3227 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, Loc,
3228 Name.getAsIdentifierInfo(), T,
3229 D->getParameterKind()))
3230 return ToParm;
3231 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003232}
3233
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003234Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3235 // Parameters are created in the translation unit's context, then moved
3236 // into the function declaration's context afterward.
3237 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3238
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003239 // Import the name of this declaration.
3240 DeclarationName Name = Importer.Import(D->getDeclName());
3241 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003242 return nullptr;
3243
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003244 // Import the location of this declaration.
3245 SourceLocation Loc = Importer.Import(D->getLocation());
3246
3247 // Import the parameter's type.
3248 QualType T = Importer.Import(D->getType());
3249 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003250 return nullptr;
3251
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003252 // Create the imported parameter.
3253 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003254 ParmVarDecl *ToParm;
3255 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3256 Importer.Import(D->getInnerLocStart()), Loc,
3257 Name.getAsIdentifierInfo(), T, TInfo,
3258 D->getStorageClass(),
3259 /*DefaultArg*/ nullptr))
3260 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003261
3262 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003263 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003264 ToParm->setKNRPromoted(D->isKNRPromoted());
3265
3266 Expr *ToDefArg = nullptr;
3267 Expr *FromDefArg = nullptr;
3268 if (D->hasUninstantiatedDefaultArg()) {
3269 FromDefArg = D->getUninstantiatedDefaultArg();
3270 ToDefArg = Importer.Import(FromDefArg);
3271 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3272 } else if (D->hasUnparsedDefaultArg()) {
3273 ToParm->setUnparsedDefaultArg();
3274 } else if (D->hasDefaultArg()) {
3275 FromDefArg = D->getDefaultArg();
3276 ToDefArg = Importer.Import(FromDefArg);
3277 ToParm->setDefaultArg(ToDefArg);
3278 }
3279 if (FromDefArg && !ToDefArg)
3280 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003281
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003282 if (D->isObjCMethodParameter()) {
3283 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3284 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3285 } else {
3286 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3287 D->getFunctionScopeIndex());
3288 }
3289
Gabor Marton26f72a92018-07-12 09:42:05 +00003290 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003291}
3292
Douglas Gregor43f54792010-02-17 02:12:47 +00003293Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3294 // Import the major distinguishing characteristics of a method.
3295 DeclContext *DC, *LexicalDC;
3296 DeclarationName Name;
3297 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003298 NamedDecl *ToD;
3299 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003300 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003301 if (ToD)
3302 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003303
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003304 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003305 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003306 for (auto *FoundDecl : FoundDecls) {
3307 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003308 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3309 continue;
3310
3311 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003312 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3313 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003314 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003315 << D->isInstanceMethod() << Name << D->getReturnType()
3316 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003317 Importer.ToDiag(FoundMethod->getLocation(),
3318 diag::note_odr_objc_method_here)
3319 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003320 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003321 }
3322
3323 // Check the number of parameters.
3324 if (D->param_size() != FoundMethod->param_size()) {
3325 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3326 << D->isInstanceMethod() << Name
3327 << D->param_size() << FoundMethod->param_size();
3328 Importer.ToDiag(FoundMethod->getLocation(),
3329 diag::note_odr_objc_method_here)
3330 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003331 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003332 }
3333
3334 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003335 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003336 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3337 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003338 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003339 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003340 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003341 diag::err_odr_objc_method_param_type_inconsistent)
3342 << D->isInstanceMethod() << Name
3343 << (*P)->getType() << (*FoundP)->getType();
3344 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3345 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003346 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003347 }
3348 }
3349
3350 // Check variadic/non-variadic.
3351 // Check the number of parameters.
3352 if (D->isVariadic() != FoundMethod->isVariadic()) {
3353 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3354 << D->isInstanceMethod() << Name;
3355 Importer.ToDiag(FoundMethod->getLocation(),
3356 diag::note_odr_objc_method_here)
3357 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003358 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003359 }
3360
3361 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003362 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003363 }
3364 }
3365
3366 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003367 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003368 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003369 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003370
Alp Toker314cc812014-01-25 16:55:45 +00003371 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003372
Gabor Marton26f72a92018-07-12 09:42:05 +00003373 ObjCMethodDecl *ToMethod;
3374 if (GetImportedOrCreateDecl(
3375 ToMethod, D, Importer.getToContext(), Loc,
3376 Importer.Import(D->getLocEnd()), Name.getObjCSelector(), ResultTy,
3377 ReturnTInfo, DC, D->isInstanceMethod(), D->isVariadic(),
3378 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3379 D->getImplementationControl(), D->hasRelatedResultType()))
3380 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003381
3382 // FIXME: When we decide to merge method definitions, we'll need to
3383 // deal with implicit parameters.
3384
3385 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003386 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003387 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003388 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003389 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003390 return nullptr;
3391
Douglas Gregor43f54792010-02-17 02:12:47 +00003392 ToParams.push_back(ToP);
3393 }
3394
3395 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003396 for (auto *ToParam : ToParams) {
3397 ToParam->setOwningFunction(ToMethod);
3398 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003399 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003400
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003401 SmallVector<SourceLocation, 12> SelLocs;
3402 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003403 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003404 Loc = Importer.Import(Loc);
3405
3406 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003407
3408 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003409 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003410 return ToMethod;
3411}
3412
Douglas Gregor85f3f952015-07-07 03:57:15 +00003413Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3414 // Import the major distinguishing characteristics of a category.
3415 DeclContext *DC, *LexicalDC;
3416 DeclarationName Name;
3417 SourceLocation Loc;
3418 NamedDecl *ToD;
3419 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3420 return nullptr;
3421 if (ToD)
3422 return ToD;
3423
3424 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3425 if (!BoundInfo)
3426 return nullptr;
3427
Gabor Marton26f72a92018-07-12 09:42:05 +00003428 ObjCTypeParamDecl *Result;
3429 if (GetImportedOrCreateDecl(
3430 Result, D, Importer.getToContext(), DC, D->getVariance(),
3431 Importer.Import(D->getVarianceLoc()), D->getIndex(),
3432 Importer.Import(D->getLocation()), Name.getAsIdentifierInfo(),
3433 Importer.Import(D->getColonLoc()), BoundInfo))
3434 return Result;
3435
Douglas Gregor85f3f952015-07-07 03:57:15 +00003436 Result->setLexicalDeclContext(LexicalDC);
3437 return Result;
3438}
3439
Douglas Gregor84c51c32010-02-18 01:47:50 +00003440Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3441 // Import the major distinguishing characteristics of a category.
3442 DeclContext *DC, *LexicalDC;
3443 DeclarationName Name;
3444 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003445 NamedDecl *ToD;
3446 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003447 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003448 if (ToD)
3449 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003450
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003451 auto *ToInterface =
3452 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003453 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003454 return nullptr;
3455
Douglas Gregor84c51c32010-02-18 01:47:50 +00003456 // Determine if we've already encountered this category.
3457 ObjCCategoryDecl *MergeWithCategory
3458 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3459 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3460 if (!ToCategory) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003461
3462 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
3463 Importer.Import(D->getAtStartLoc()), Loc,
3464 Importer.Import(D->getCategoryNameLoc()),
3465 Name.getAsIdentifierInfo(), ToInterface,
3466 /*TypeParamList=*/nullptr,
3467 Importer.Import(D->getIvarLBraceLoc()),
3468 Importer.Import(D->getIvarRBraceLoc())))
3469 return ToCategory;
3470
Douglas Gregor84c51c32010-02-18 01:47:50 +00003471 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003472 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003473 // Import the type parameter list after calling Imported, to avoid
3474 // loops when bringing in their DeclContext.
3475 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3476 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003477
Douglas Gregor84c51c32010-02-18 01:47:50 +00003478 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003479 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3480 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003481 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3482 = D->protocol_loc_begin();
3483 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3484 FromProtoEnd = D->protocol_end();
3485 FromProto != FromProtoEnd;
3486 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003487 auto *ToProto =
3488 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003489 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003490 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003491 Protocols.push_back(ToProto);
3492 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3493 }
3494
3495 // FIXME: If we're merging, make sure that the protocol list is the same.
3496 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3497 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003498 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003499 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003500 }
3501
3502 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003503 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003504
3505 // If we have an implementation, import it as well.
3506 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003507 auto *Impl =
3508 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003509 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003510 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003511 return nullptr;
3512
Douglas Gregor84c51c32010-02-18 01:47:50 +00003513 ToCategory->setImplementation(Impl);
3514 }
3515
3516 return ToCategory;
3517}
3518
Douglas Gregor2aa53772012-01-24 17:42:07 +00003519bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3520 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003521 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003522 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003523 if (shouldForceImportDeclContext(Kind))
3524 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003525 return false;
3526 }
3527
3528 // Start the protocol definition
3529 To->startDefinition();
3530
3531 // Import protocols
3532 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3533 SmallVector<SourceLocation, 4> ProtocolLocs;
3534 ObjCProtocolDecl::protocol_loc_iterator
3535 FromProtoLoc = From->protocol_loc_begin();
3536 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3537 FromProtoEnd = From->protocol_end();
3538 FromProto != FromProtoEnd;
3539 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003540 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003541 if (!ToProto)
3542 return true;
3543 Protocols.push_back(ToProto);
3544 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3545 }
3546
3547 // FIXME: If we're merging, make sure that the protocol list is the same.
3548 To->setProtocolList(Protocols.data(), Protocols.size(),
3549 ProtocolLocs.data(), Importer.getToContext());
3550
Douglas Gregor2e15c842012-02-01 21:00:38 +00003551 if (shouldForceImportDeclContext(Kind)) {
3552 // Import all of the members of this protocol.
3553 ImportDeclContext(From, /*ForceImport=*/true);
3554 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003555 return false;
3556}
3557
Douglas Gregor98d156a2010-02-17 16:12:00 +00003558Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003559 // If this protocol has a definition in the translation unit we're coming
3560 // from, but this particular declaration is not that definition, import the
3561 // definition and map to that.
3562 ObjCProtocolDecl *Definition = D->getDefinition();
3563 if (Definition && Definition != D) {
3564 Decl *ImportedDef = Importer.Import(Definition);
3565 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003566 return nullptr;
3567
Gabor Marton26f72a92018-07-12 09:42:05 +00003568 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003569 }
3570
Douglas Gregor84c51c32010-02-18 01:47:50 +00003571 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003572 DeclContext *DC, *LexicalDC;
3573 DeclarationName Name;
3574 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003575 NamedDecl *ToD;
3576 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003577 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003578 if (ToD)
3579 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003580
Craig Topper36250ad2014-05-12 05:36:57 +00003581 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003582 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003583 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003584 for (auto *FoundDecl : FoundDecls) {
3585 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003586 continue;
3587
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003588 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003589 break;
3590 }
3591
3592 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003593 if (!ToProto) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003594 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
3595 Name.getAsIdentifierInfo(), Loc,
3596 Importer.Import(D->getAtStartLoc()),
3597 /*PrevDecl=*/nullptr))
3598 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003599 ToProto->setLexicalDeclContext(LexicalDC);
3600 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003601 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003602
3603 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003604
Douglas Gregor2aa53772012-01-24 17:42:07 +00003605 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003606 return nullptr;
3607
Douglas Gregor98d156a2010-02-17 16:12:00 +00003608 return ToProto;
3609}
3610
Sean Callanan0aae0412014-12-10 00:00:37 +00003611Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3612 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3613 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3614
3615 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3616 SourceLocation LangLoc = Importer.Import(D->getLocation());
3617
3618 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00003619
3620 LinkageSpecDecl *ToLinkageSpec;
3621 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
3622 ExternLoc, LangLoc, D->getLanguage(), HasBraces))
3623 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00003624
3625 if (HasBraces) {
3626 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3627 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3628 }
3629
3630 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3631 LexicalDC->addDeclInternal(ToLinkageSpec);
3632
Sean Callanan0aae0412014-12-10 00:00:37 +00003633 return ToLinkageSpec;
3634}
3635
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003636Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3637 DeclContext *DC, *LexicalDC;
3638 DeclarationName Name;
3639 SourceLocation Loc;
3640 NamedDecl *ToD = nullptr;
3641 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3642 return nullptr;
3643 if (ToD)
3644 return ToD;
3645
3646 DeclarationNameInfo NameInfo(Name,
3647 Importer.Import(D->getNameInfo().getLoc()));
3648 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3649
Gabor Marton26f72a92018-07-12 09:42:05 +00003650 UsingDecl *ToUsing;
3651 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3652 Importer.Import(D->getUsingLoc()),
3653 Importer.Import(D->getQualifierLoc()), NameInfo,
3654 D->hasTypename()))
3655 return ToUsing;
3656
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003657 ToUsing->setLexicalDeclContext(LexicalDC);
3658 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003659
3660 if (NamedDecl *FromPattern =
3661 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003662 if (auto *ToPattern =
3663 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003664 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3665 else
3666 return nullptr;
3667 }
3668
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003669 for (auto *FromShadow : D->shadows()) {
3670 if (auto *ToShadow =
3671 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003672 ToUsing->addShadowDecl(ToShadow);
3673 else
3674 // FIXME: We return a nullptr here but the definition is already created
3675 // and available with lookups. How to fix this?..
3676 return nullptr;
3677 }
3678 return ToUsing;
3679}
3680
3681Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3682 DeclContext *DC, *LexicalDC;
3683 DeclarationName Name;
3684 SourceLocation Loc;
3685 NamedDecl *ToD = nullptr;
3686 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3687 return nullptr;
3688 if (ToD)
3689 return ToD;
3690
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003691 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3692 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003693 if (!ToUsing)
3694 return nullptr;
3695
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003696 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3697 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003698 if (!ToTarget)
3699 return nullptr;
3700
Gabor Marton26f72a92018-07-12 09:42:05 +00003701 UsingShadowDecl *ToShadow;
3702 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
3703 ToUsing, ToTarget))
3704 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003705
3706 ToShadow->setLexicalDeclContext(LexicalDC);
3707 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003708
3709 if (UsingShadowDecl *FromPattern =
3710 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003711 if (auto *ToPattern =
3712 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003713 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3714 ToPattern);
3715 else
3716 // FIXME: We return a nullptr here but the definition is already created
3717 // and available with lookups. How to fix this?..
3718 return nullptr;
3719 }
3720
3721 LexicalDC->addDeclInternal(ToShadow);
3722
3723 return ToShadow;
3724}
3725
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003726Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3727 DeclContext *DC, *LexicalDC;
3728 DeclarationName Name;
3729 SourceLocation Loc;
3730 NamedDecl *ToD = nullptr;
3731 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3732 return nullptr;
3733 if (ToD)
3734 return ToD;
3735
3736 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3737 if (!ToComAncestor)
3738 return nullptr;
3739
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003740 auto *ToNominated = cast_or_null<NamespaceDecl>(
3741 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003742 if (!ToNominated)
3743 return nullptr;
3744
Gabor Marton26f72a92018-07-12 09:42:05 +00003745 UsingDirectiveDecl *ToUsingDir;
3746 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
3747 Importer.Import(D->getUsingLoc()),
3748 Importer.Import(D->getNamespaceKeyLocation()),
3749 Importer.Import(D->getQualifierLoc()),
3750 Importer.Import(D->getIdentLocation()),
3751 ToNominated, ToComAncestor))
3752 return ToUsingDir;
3753
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003754 ToUsingDir->setLexicalDeclContext(LexicalDC);
3755 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003756
3757 return ToUsingDir;
3758}
3759
3760Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3761 UnresolvedUsingValueDecl *D) {
3762 DeclContext *DC, *LexicalDC;
3763 DeclarationName Name;
3764 SourceLocation Loc;
3765 NamedDecl *ToD = nullptr;
3766 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3767 return nullptr;
3768 if (ToD)
3769 return ToD;
3770
3771 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3772 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3773
Gabor Marton26f72a92018-07-12 09:42:05 +00003774 UnresolvedUsingValueDecl *ToUsingValue;
3775 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
3776 Importer.Import(D->getUsingLoc()),
3777 Importer.Import(D->getQualifierLoc()), NameInfo,
3778 Importer.Import(D->getEllipsisLoc())))
3779 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003780
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003781 ToUsingValue->setAccess(D->getAccess());
3782 ToUsingValue->setLexicalDeclContext(LexicalDC);
3783 LexicalDC->addDeclInternal(ToUsingValue);
3784
3785 return ToUsingValue;
3786}
3787
3788Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3789 UnresolvedUsingTypenameDecl *D) {
3790 DeclContext *DC, *LexicalDC;
3791 DeclarationName Name;
3792 SourceLocation Loc;
3793 NamedDecl *ToD = nullptr;
3794 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3795 return nullptr;
3796 if (ToD)
3797 return ToD;
3798
Gabor Marton26f72a92018-07-12 09:42:05 +00003799 UnresolvedUsingTypenameDecl *ToUsing;
3800 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3801 Importer.Import(D->getUsingLoc()),
3802 Importer.Import(D->getTypenameLoc()),
3803 Importer.Import(D->getQualifierLoc()), Loc, Name,
3804 Importer.Import(D->getEllipsisLoc())))
3805 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003806
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003807 ToUsing->setAccess(D->getAccess());
3808 ToUsing->setLexicalDeclContext(LexicalDC);
3809 LexicalDC->addDeclInternal(ToUsing);
3810
3811 return ToUsing;
3812}
3813
Douglas Gregor2aa53772012-01-24 17:42:07 +00003814bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3815 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003816 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003817 if (To->getDefinition()) {
3818 // Check consistency of superclass.
3819 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3820 if (FromSuper) {
3821 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3822 if (!FromSuper)
3823 return true;
3824 }
3825
3826 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3827 if ((bool)FromSuper != (bool)ToSuper ||
3828 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3829 Importer.ToDiag(To->getLocation(),
3830 diag::err_odr_objc_superclass_inconsistent)
3831 << To->getDeclName();
3832 if (ToSuper)
3833 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3834 << To->getSuperClass()->getDeclName();
3835 else
3836 Importer.ToDiag(To->getLocation(),
3837 diag::note_odr_objc_missing_superclass);
3838 if (From->getSuperClass())
3839 Importer.FromDiag(From->getSuperClassLoc(),
3840 diag::note_odr_objc_superclass)
3841 << From->getSuperClass()->getDeclName();
3842 else
3843 Importer.FromDiag(From->getLocation(),
3844 diag::note_odr_objc_missing_superclass);
3845 }
3846
Douglas Gregor2e15c842012-02-01 21:00:38 +00003847 if (shouldForceImportDeclContext(Kind))
3848 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003849 return false;
3850 }
3851
3852 // Start the definition.
3853 To->startDefinition();
3854
3855 // If this class has a superclass, import it.
3856 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003857 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3858 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003859 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003860
3861 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003862 }
3863
3864 // Import protocols
3865 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3866 SmallVector<SourceLocation, 4> ProtocolLocs;
3867 ObjCInterfaceDecl::protocol_loc_iterator
3868 FromProtoLoc = From->protocol_loc_begin();
3869
3870 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3871 FromProtoEnd = From->protocol_end();
3872 FromProto != FromProtoEnd;
3873 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003874 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003875 if (!ToProto)
3876 return true;
3877 Protocols.push_back(ToProto);
3878 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3879 }
3880
3881 // FIXME: If we're merging, make sure that the protocol list is the same.
3882 To->setProtocolList(Protocols.data(), Protocols.size(),
3883 ProtocolLocs.data(), Importer.getToContext());
3884
3885 // Import categories. When the categories themselves are imported, they'll
3886 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003887 for (auto *Cat : From->known_categories())
3888 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003889
Douglas Gregor2aa53772012-01-24 17:42:07 +00003890 // If we have an @implementation, import it as well.
3891 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003892 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3893 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003894 if (!Impl)
3895 return true;
3896
3897 To->setImplementation(Impl);
3898 }
3899
Douglas Gregor2e15c842012-02-01 21:00:38 +00003900 if (shouldForceImportDeclContext(Kind)) {
3901 // Import all of the members of this class.
3902 ImportDeclContext(From, /*ForceImport=*/true);
3903 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003904 return false;
3905}
3906
Douglas Gregor85f3f952015-07-07 03:57:15 +00003907ObjCTypeParamList *
3908ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3909 if (!list)
3910 return nullptr;
3911
3912 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3913 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003914 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3915 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003916 if (!toTypeParam)
3917 return nullptr;
3918
3919 toTypeParams.push_back(toTypeParam);
3920 }
3921
3922 return ObjCTypeParamList::create(Importer.getToContext(),
3923 Importer.Import(list->getLAngleLoc()),
3924 toTypeParams,
3925 Importer.Import(list->getRAngleLoc()));
3926}
3927
Douglas Gregor45635322010-02-16 01:20:57 +00003928Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003929 // If this class has a definition in the translation unit we're coming from,
3930 // but this particular declaration is not that definition, import the
3931 // definition and map to that.
3932 ObjCInterfaceDecl *Definition = D->getDefinition();
3933 if (Definition && Definition != D) {
3934 Decl *ImportedDef = Importer.Import(Definition);
3935 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003936 return nullptr;
3937
Gabor Marton26f72a92018-07-12 09:42:05 +00003938 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003939 }
3940
Douglas Gregor45635322010-02-16 01:20:57 +00003941 // Import the major distinguishing characteristics of an @interface.
3942 DeclContext *DC, *LexicalDC;
3943 DeclarationName Name;
3944 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003945 NamedDecl *ToD;
3946 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003947 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003948 if (ToD)
3949 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003950
Douglas Gregor2aa53772012-01-24 17:42:07 +00003951 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003952 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003953 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003954 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003955 for (auto *FoundDecl : FoundDecls) {
3956 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003957 continue;
3958
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003959 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003960 break;
3961 }
3962
Douglas Gregor2aa53772012-01-24 17:42:07 +00003963 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003964 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003965 if (!ToIface) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003966 if (GetImportedOrCreateDecl(
3967 ToIface, D, Importer.getToContext(), DC,
3968 Importer.Import(D->getAtStartLoc()), Name.getAsIdentifierInfo(),
3969 /*TypeParamList=*/nullptr,
3970 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
3971 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003972 ToIface->setLexicalDeclContext(LexicalDC);
3973 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003974 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003975 Importer.MapImported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003976 // Import the type parameter list after calling Imported, to avoid
3977 // loops when bringing in their DeclContext.
3978 ToIface->setTypeParamList(ImportObjCTypeParamList(
3979 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003980
Douglas Gregor2aa53772012-01-24 17:42:07 +00003981 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003982 return nullptr;
3983
Douglas Gregor98d156a2010-02-17 16:12:00 +00003984 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003985}
3986
Douglas Gregor4da9d682010-12-07 15:32:12 +00003987Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003988 auto *Category = cast_or_null<ObjCCategoryDecl>(
3989 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003990 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003991 return nullptr;
3992
Douglas Gregor4da9d682010-12-07 15:32:12 +00003993 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3994 if (!ToImpl) {
3995 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3996 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003997 return nullptr;
3998
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003999 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Gabor Marton26f72a92018-07-12 09:42:05 +00004000 if (GetImportedOrCreateDecl(
4001 ToImpl, D, Importer.getToContext(), DC,
4002 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
4003 Importer.Import(D->getLocation()),
4004 Importer.Import(D->getAtStartLoc()), CategoryNameLoc))
4005 return ToImpl;
4006
Douglas Gregor4da9d682010-12-07 15:32:12 +00004007 DeclContext *LexicalDC = DC;
4008 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4009 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4010 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004011 return nullptr;
4012
Douglas Gregor4da9d682010-12-07 15:32:12 +00004013 ToImpl->setLexicalDeclContext(LexicalDC);
4014 }
4015
Sean Callanan95e74be2011-10-21 02:57:43 +00004016 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004017 Category->setImplementation(ToImpl);
4018 }
4019
Gabor Marton26f72a92018-07-12 09:42:05 +00004020 Importer.MapImported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004021 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004022 return ToImpl;
4023}
4024
Douglas Gregorda8025c2010-12-07 01:26:03 +00004025Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4026 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004027 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
4028 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004029 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004030 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004031
4032 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004033 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004034 if (D->getSuperClass()) {
4035 Super = cast_or_null<ObjCInterfaceDecl>(
4036 Importer.Import(D->getSuperClass()));
4037 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004038 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004039 }
4040
4041 ObjCImplementationDecl *Impl = Iface->getImplementation();
4042 if (!Impl) {
4043 // We haven't imported an implementation yet. Create a new @implementation
4044 // now.
Gabor Marton26f72a92018-07-12 09:42:05 +00004045 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
4046 Importer.ImportContext(D->getDeclContext()),
4047 Iface, Super, Importer.Import(D->getLocation()),
4048 Importer.Import(D->getAtStartLoc()),
4049 Importer.Import(D->getSuperClassLoc()),
4050 Importer.Import(D->getIvarLBraceLoc()),
4051 Importer.Import(D->getIvarRBraceLoc())))
4052 return Impl;
4053
Douglas Gregorda8025c2010-12-07 01:26:03 +00004054 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4055 DeclContext *LexicalDC
4056 = Importer.ImportContext(D->getLexicalDeclContext());
4057 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004058 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004059 Impl->setLexicalDeclContext(LexicalDC);
4060 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004061
Douglas Gregorda8025c2010-12-07 01:26:03 +00004062 // Associate the implementation with the class it implements.
4063 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004064 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004065 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004066 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004067
4068 // Verify that the existing @implementation has the same superclass.
4069 if ((Super && !Impl->getSuperClass()) ||
4070 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004071 (Super && Impl->getSuperClass() &&
4072 !declaresSameEntity(Super->getCanonicalDecl(),
4073 Impl->getSuperClass()))) {
4074 Importer.ToDiag(Impl->getLocation(),
4075 diag::err_odr_objc_superclass_inconsistent)
4076 << Iface->getDeclName();
4077 // FIXME: It would be nice to have the location of the superclass
4078 // below.
4079 if (Impl->getSuperClass())
4080 Importer.ToDiag(Impl->getLocation(),
4081 diag::note_odr_objc_superclass)
4082 << Impl->getSuperClass()->getDeclName();
4083 else
4084 Importer.ToDiag(Impl->getLocation(),
4085 diag::note_odr_objc_missing_superclass);
4086 if (D->getSuperClass())
4087 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004088 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004089 << D->getSuperClass()->getDeclName();
4090 else
4091 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004092 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004093 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004094 }
4095 }
4096
4097 // Import all of the members of this @implementation.
4098 ImportDeclContext(D);
4099
4100 return Impl;
4101}
4102
Douglas Gregora11c4582010-02-17 18:02:10 +00004103Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4104 // Import the major distinguishing characteristics of an @property.
4105 DeclContext *DC, *LexicalDC;
4106 DeclarationName Name;
4107 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004108 NamedDecl *ToD;
4109 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004110 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004111 if (ToD)
4112 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004113
4114 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004115 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004116 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004117 for (auto *FoundDecl : FoundDecls) {
4118 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004119 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004120 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004121 FoundProp->getType())) {
4122 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4123 << Name << D->getType() << FoundProp->getType();
4124 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4125 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004126 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004127 }
4128
4129 // FIXME: Check property attributes, getters, setters, etc.?
4130
4131 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004132 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004133 return FoundProp;
4134 }
4135 }
4136
4137 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004138 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4139 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004140 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004141
4142 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004143 ObjCPropertyDecl *ToProperty;
4144 if (GetImportedOrCreateDecl(
4145 ToProperty, D, Importer.getToContext(), DC, Loc,
4146 Name.getAsIdentifierInfo(), Importer.Import(D->getAtLoc()),
4147 Importer.Import(D->getLParenLoc()), Importer.Import(D->getType()),
4148 TSI, D->getPropertyImplementation()))
4149 return ToProperty;
4150
Douglas Gregora11c4582010-02-17 18:02:10 +00004151 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004152 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004153
4154 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004155 ToProperty->setPropertyAttributesAsWritten(
4156 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00004157 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
4158 Importer.Import(D->getGetterNameLoc()));
4159 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
4160 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00004161 ToProperty->setGetterMethodDecl(
4162 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4163 ToProperty->setSetterMethodDecl(
4164 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4165 ToProperty->setPropertyIvarDecl(
4166 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4167 return ToProperty;
4168}
4169
Douglas Gregor14a49e22010-12-07 18:32:03 +00004170Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004171 auto *Property = cast_or_null<ObjCPropertyDecl>(
4172 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00004173 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004174 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004175
4176 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4177 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004178 return nullptr;
4179
Douglas Gregor14a49e22010-12-07 18:32:03 +00004180 // Import the lexical declaration context.
4181 DeclContext *LexicalDC = DC;
4182 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4183 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4184 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004185 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004186 }
4187
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004188 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004189 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004190 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004191
4192 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004193 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004194 if (D->getPropertyIvarDecl()) {
4195 Ivar = cast_or_null<ObjCIvarDecl>(
4196 Importer.Import(D->getPropertyIvarDecl()));
4197 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004198 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004199 }
4200
4201 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004202 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4203 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004204 if (!ToImpl) {
4205 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
4206 Importer.Import(D->getLocStart()),
4207 Importer.Import(D->getLocation()), Property,
4208 D->getPropertyImplementation(), Ivar,
4209 Importer.Import(D->getPropertyIvarDeclLoc())))
4210 return ToImpl;
4211
Douglas Gregor14a49e22010-12-07 18:32:03 +00004212 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004213 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004214 } else {
4215 // Check that we have the same kind of property implementation (@synthesize
4216 // vs. @dynamic).
4217 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4218 Importer.ToDiag(ToImpl->getLocation(),
4219 diag::err_odr_objc_property_impl_kind_inconsistent)
4220 << Property->getDeclName()
4221 << (ToImpl->getPropertyImplementation()
4222 == ObjCPropertyImplDecl::Dynamic);
4223 Importer.FromDiag(D->getLocation(),
4224 diag::note_odr_objc_property_impl_kind)
4225 << D->getPropertyDecl()->getDeclName()
4226 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004227 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004228 }
4229
4230 // For @synthesize, check that we have the same
4231 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4232 Ivar != ToImpl->getPropertyIvarDecl()) {
4233 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4234 diag::err_odr_objc_synthesize_ivar_inconsistent)
4235 << Property->getDeclName()
4236 << ToImpl->getPropertyIvarDecl()->getDeclName()
4237 << Ivar->getDeclName();
4238 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4239 diag::note_odr_objc_synthesize_ivar_here)
4240 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004241 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004242 }
4243
4244 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004245 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004246 }
4247
4248 return ToImpl;
4249}
4250
Douglas Gregora082a492010-11-30 19:14:50 +00004251Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4252 // For template arguments, we adopt the translation unit as our declaration
4253 // context. This context will be fixed when the actual template declaration
4254 // is created.
4255
4256 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004257 TemplateTypeParmDecl *ToD = nullptr;
4258 (void)GetImportedOrCreateDecl(
4259 ToD, D, Importer.getToContext(),
4260 Importer.getToContext().getTranslationUnitDecl(),
4261 Importer.Import(D->getLocStart()), Importer.Import(D->getLocation()),
4262 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4263 D->wasDeclaredWithTypename(), D->isParameterPack());
4264 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004265}
4266
4267Decl *
4268ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4269 // Import the name of this declaration.
4270 DeclarationName Name = Importer.Import(D->getDeclName());
4271 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004272 return nullptr;
4273
Douglas Gregora082a492010-11-30 19:14:50 +00004274 // Import the location of this declaration.
4275 SourceLocation Loc = Importer.Import(D->getLocation());
4276
4277 // Import the type of this declaration.
4278 QualType T = Importer.Import(D->getType());
4279 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004280 return nullptr;
4281
Douglas Gregora082a492010-11-30 19:14:50 +00004282 // Import type-source information.
4283 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4284 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004285 return nullptr;
4286
Douglas Gregora082a492010-11-30 19:14:50 +00004287 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004288
4289 NonTypeTemplateParmDecl *ToD = nullptr;
4290 (void)GetImportedOrCreateDecl(
4291 ToD, D, Importer.getToContext(),
4292 Importer.getToContext().getTranslationUnitDecl(),
4293 Importer.Import(D->getInnerLocStart()), Loc, D->getDepth(),
4294 D->getPosition(), Name.getAsIdentifierInfo(), T, D->isParameterPack(),
4295 TInfo);
4296 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004297}
4298
4299Decl *
4300ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4301 // Import the name of this declaration.
4302 DeclarationName Name = Importer.Import(D->getDeclName());
4303 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004304 return nullptr;
4305
Douglas Gregora082a492010-11-30 19:14:50 +00004306 // Import the location of this declaration.
4307 SourceLocation Loc = Importer.Import(D->getLocation());
Gabor Marton26f72a92018-07-12 09:42:05 +00004308
Douglas Gregora082a492010-11-30 19:14:50 +00004309 // Import template parameters.
4310 TemplateParameterList *TemplateParams
4311 = ImportTemplateParameterList(D->getTemplateParameters());
4312 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004313 return nullptr;
4314
Douglas Gregora082a492010-11-30 19:14:50 +00004315 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004316
4317 TemplateTemplateParmDecl *ToD = nullptr;
4318 (void)GetImportedOrCreateDecl(
4319 ToD, D, Importer.getToContext(),
4320 Importer.getToContext().getTranslationUnitDecl(), Loc, D->getDepth(),
4321 D->getPosition(), D->isParameterPack(), Name.getAsIdentifierInfo(),
4322 TemplateParams);
4323 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004324}
4325
Gabor Marton9581c332018-05-23 13:53:36 +00004326// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4327// it has any definition in the redecl chain.
4328static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4329 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4330 if (!ToTemplatedDef)
4331 return nullptr;
4332 ClassTemplateDecl *TemplateWithDef =
4333 ToTemplatedDef->getDescribedClassTemplate();
4334 return TemplateWithDef;
4335}
4336
Douglas Gregora082a492010-11-30 19:14:50 +00004337Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4338 // If this record has a definition in the translation unit we're coming from,
4339 // but this particular declaration is not that definition, import the
4340 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004341 auto *Definition =
4342 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004343 if (Definition && Definition != D->getTemplatedDecl()) {
4344 Decl *ImportedDef
4345 = Importer.Import(Definition->getDescribedClassTemplate());
4346 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004347 return nullptr;
4348
Gabor Marton26f72a92018-07-12 09:42:05 +00004349 return Importer.MapImported(D, ImportedDef);
Douglas Gregora082a492010-11-30 19:14:50 +00004350 }
Gabor Marton9581c332018-05-23 13:53:36 +00004351
Douglas Gregora082a492010-11-30 19:14:50 +00004352 // Import the major distinguishing characteristics of this class template.
4353 DeclContext *DC, *LexicalDC;
4354 DeclarationName Name;
4355 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004356 NamedDecl *ToD;
4357 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004358 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004359 if (ToD)
4360 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004361
Douglas Gregora082a492010-11-30 19:14:50 +00004362 // We may already have a template of the same name; try to find and match it.
4363 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004364 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004365 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004366 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004367 for (auto *FoundDecl : FoundDecls) {
4368 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004369 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004370
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004371 Decl *Found = FoundDecl;
4372 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00004373
4374 // The class to be imported is a definition.
4375 if (D->isThisDeclarationADefinition()) {
4376 // Lookup will find the fwd decl only if that is more recent than the
4377 // definition. So, try to get the definition if that is available in
4378 // the redecl chain.
4379 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
4380 if (!TemplateWithDef)
4381 continue;
4382 FoundTemplate = TemplateWithDef; // Continue with the definition.
4383 }
4384
Douglas Gregora082a492010-11-30 19:14:50 +00004385 if (IsStructuralMatch(D, FoundTemplate)) {
4386 // The class templates structurally match; call it the same template.
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004387
Gabor Marton26f72a92018-07-12 09:42:05 +00004388 Importer.MapImported(D->getTemplatedDecl(),
4389 FoundTemplate->getTemplatedDecl());
4390 return Importer.MapImported(D, FoundTemplate);
Gabor Marton9581c332018-05-23 13:53:36 +00004391 }
Douglas Gregora082a492010-11-30 19:14:50 +00004392 }
Gabor Marton9581c332018-05-23 13:53:36 +00004393
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004394 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004395 }
Gabor Marton9581c332018-05-23 13:53:36 +00004396
Douglas Gregora082a492010-11-30 19:14:50 +00004397 if (!ConflictingDecls.empty()) {
4398 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004399 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004400 ConflictingDecls.size());
4401 }
Gabor Marton9581c332018-05-23 13:53:36 +00004402
Douglas Gregora082a492010-11-30 19:14:50 +00004403 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004404 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004405 }
4406
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004407 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4408
Douglas Gregora082a492010-11-30 19:14:50 +00004409 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004410 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4411 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004412 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004413 return nullptr;
4414
Douglas Gregora082a492010-11-30 19:14:50 +00004415 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004416 TemplateParameterList *TemplateParams =
4417 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004418 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004419 return nullptr;
4420
Gabor Marton26f72a92018-07-12 09:42:05 +00004421 ClassTemplateDecl *D2;
4422 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
4423 TemplateParams, ToTemplated))
4424 return D2;
4425
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004426 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004427
4428 D2->setAccess(D->getAccess());
4429 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004430 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004431
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004432 if (FromTemplated->isCompleteDefinition() &&
4433 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004434 // FIXME: Import definition!
4435 }
4436
4437 return D2;
4438}
4439
Douglas Gregore2e50d332010-12-01 01:36:18 +00004440Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4441 ClassTemplateSpecializationDecl *D) {
4442 // If this record has a definition in the translation unit we're coming from,
4443 // but this particular declaration is not that definition, import the
4444 // definition and map to that.
4445 TagDecl *Definition = D->getDefinition();
4446 if (Definition && Definition != D) {
4447 Decl *ImportedDef = Importer.Import(Definition);
4448 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004449 return nullptr;
4450
Gabor Marton26f72a92018-07-12 09:42:05 +00004451 return Importer.MapImported(D, ImportedDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004452 }
4453
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004454 auto *ClassTemplate =
4455 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004456 D->getSpecializedTemplate()));
4457 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004458 return nullptr;
4459
Douglas Gregore2e50d332010-12-01 01:36:18 +00004460 // Import the context of this declaration.
4461 DeclContext *DC = ClassTemplate->getDeclContext();
4462 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004463 return nullptr;
4464
Douglas Gregore2e50d332010-12-01 01:36:18 +00004465 DeclContext *LexicalDC = DC;
4466 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4467 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4468 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004469 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004470 }
4471
4472 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004473 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4474 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004475
4476 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004477 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004478 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4479 D->getTemplateArgs().size(),
4480 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004481 return nullptr;
4482
Douglas Gregore2e50d332010-12-01 01:36:18 +00004483 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004484 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004485 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004486 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004487 if (D2) {
4488 // We already have a class template specialization with these template
4489 // arguments.
4490
4491 // FIXME: Check for specialization vs. instantiation errors.
4492
4493 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004494 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004495 // The record types structurally match, or the "from" translation
4496 // unit only had a forward declaration anyway; call it the same
4497 // function.
Gabor Marton26f72a92018-07-12 09:42:05 +00004498 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004499 }
4500 }
4501 } else {
4502 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004503 if (auto *PartialSpec =
4504 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004505 // Import TemplateArgumentListInfo
4506 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004507 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4508 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004509 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004510
4511 QualType CanonInjType = Importer.Import(
4512 PartialSpec->getInjectedSpecializationType());
4513 if (CanonInjType.isNull())
4514 return nullptr;
4515 CanonInjType = CanonInjType.getCanonicalType();
4516
4517 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4518 PartialSpec->getTemplateParameters());
4519 if (!ToTPList && PartialSpec->getTemplateParameters())
4520 return nullptr;
4521
Gabor Marton26f72a92018-07-12 09:42:05 +00004522 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
4523 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4524 IdLoc, ToTPList, ClassTemplate,
4525 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4526 ToTAInfo, CanonInjType, nullptr))
4527 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004528
4529 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004530 if (GetImportedOrCreateDecl(
4531 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4532 IdLoc, ClassTemplate, TemplateArgs, /*PrevDecl=*/nullptr))
4533 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004534 }
4535
Douglas Gregore2e50d332010-12-01 01:36:18 +00004536 D2->setSpecializationKind(D->getSpecializationKind());
4537
4538 // Add this specialization to the class template.
4539 ClassTemplate->AddSpecialization(D2, InsertPos);
4540
4541 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004542 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004543
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004544 if (auto *TSI = D->getTypeAsWritten()) {
4545 TypeSourceInfo *TInfo = Importer.Import(TSI);
4546 if (!TInfo)
4547 return nullptr;
4548 D2->setTypeAsWritten(TInfo);
4549 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4550 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4551 }
4552
4553 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4554 if (POI.isValid())
4555 D2->setPointOfInstantiation(POI);
4556 else if (D->getPointOfInstantiation().isValid())
4557 return nullptr;
4558
4559 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4560
Gabor Martonb14056b2018-05-25 11:21:24 +00004561 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00004562 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00004563
4564 // Add to the DC only if it was an explicit specialization/instantiation.
4565 if (D2->isExplicitInstantiationOrSpecialization()) {
4566 LexicalDC->addDeclInternal(D2);
4567 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004568 }
John McCallf937c022011-10-07 06:10:15 +00004569 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004570 return nullptr;
4571
Douglas Gregore2e50d332010-12-01 01:36:18 +00004572 return D2;
4573}
4574
Larisse Voufo39a1e502013-08-06 01:03:05 +00004575Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4576 // If this variable has a definition in the translation unit we're coming
4577 // from,
4578 // but this particular declaration is not that definition, import the
4579 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004580 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004581 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4582 if (Definition && Definition != D->getTemplatedDecl()) {
4583 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4584 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004585 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004586
Gabor Marton26f72a92018-07-12 09:42:05 +00004587 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004588 }
4589
4590 // Import the major distinguishing characteristics of this variable template.
4591 DeclContext *DC, *LexicalDC;
4592 DeclarationName Name;
4593 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004594 NamedDecl *ToD;
4595 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004596 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004597 if (ToD)
4598 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004599
4600 // We may already have a template of the same name; try to find and match it.
4601 assert(!DC->isFunctionOrMethod() &&
4602 "Variable templates cannot be declared at function scope");
4603 SmallVector<NamedDecl *, 4> ConflictingDecls;
4604 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004605 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004606 for (auto *FoundDecl : FoundDecls) {
4607 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004608 continue;
4609
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004610 Decl *Found = FoundDecl;
4611 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004612 if (IsStructuralMatch(D, FoundTemplate)) {
4613 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00004614 Importer.MapImported(D->getTemplatedDecl(),
4615 FoundTemplate->getTemplatedDecl());
4616 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004617 }
4618 }
4619
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004620 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004621 }
4622
4623 if (!ConflictingDecls.empty()) {
4624 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4625 ConflictingDecls.data(),
4626 ConflictingDecls.size());
4627 }
4628
4629 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004630 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004631
4632 VarDecl *DTemplated = D->getTemplatedDecl();
4633
4634 // Import the type.
4635 QualType T = Importer.Import(DTemplated->getType());
4636 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004637 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004638
4639 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004640 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4641 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004642 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004643
4644 // Create the variable template declaration itself.
4645 TemplateParameterList *TemplateParams =
4646 ImportTemplateParameterList(D->getTemplateParameters());
4647 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004648 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004649
Gabor Marton26f72a92018-07-12 09:42:05 +00004650 VarTemplateDecl *ToVarTD;
4651 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
4652 Name, TemplateParams, ToTemplated))
4653 return ToVarTD;
4654
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004655 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004656
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004657 ToVarTD->setAccess(D->getAccess());
4658 ToVarTD->setLexicalDeclContext(LexicalDC);
4659 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004660
Larisse Voufo39a1e502013-08-06 01:03:05 +00004661 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004662 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004663 // FIXME: Import definition!
4664 }
4665
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004666 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004667}
4668
4669Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4670 VarTemplateSpecializationDecl *D) {
4671 // If this record has a definition in the translation unit we're coming from,
4672 // but this particular declaration is not that definition, import the
4673 // definition and map to that.
4674 VarDecl *Definition = D->getDefinition();
4675 if (Definition && Definition != D) {
4676 Decl *ImportedDef = Importer.Import(Definition);
4677 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004678 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004679
Gabor Marton26f72a92018-07-12 09:42:05 +00004680 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004681 }
4682
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004683 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004684 Importer.Import(D->getSpecializedTemplate()));
4685 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004686 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004687
4688 // Import the context of this declaration.
4689 DeclContext *DC = VarTemplate->getDeclContext();
4690 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004691 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004692
4693 DeclContext *LexicalDC = DC;
4694 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4695 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4696 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004697 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004698 }
4699
4700 // Import the location of this declaration.
4701 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4702 SourceLocation IdLoc = Importer.Import(D->getLocation());
4703
4704 // Import template arguments.
4705 SmallVector<TemplateArgument, 2> TemplateArgs;
4706 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4707 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004708 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004709
4710 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004711 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004712 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004713 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004714 if (D2) {
4715 // We already have a variable template specialization with these template
4716 // arguments.
4717
4718 // FIXME: Check for specialization vs. instantiation errors.
4719
4720 if (VarDecl *FoundDef = D2->getDefinition()) {
4721 if (!D->isThisDeclarationADefinition() ||
4722 IsStructuralMatch(D, FoundDef)) {
4723 // The record types structurally match, or the "from" translation
4724 // unit only had a forward declaration anyway; call it the same
4725 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00004726 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004727 }
4728 }
4729 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004730 // Import the type.
4731 QualType T = Importer.Import(D->getType());
4732 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004733 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004734
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004735 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4736 if (D->getTypeSourceInfo() && !TInfo)
4737 return nullptr;
4738
4739 TemplateArgumentListInfo ToTAInfo;
4740 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4741 return nullptr;
4742
4743 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004744 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004745 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4746 // Import TemplateArgumentListInfo
4747 TemplateArgumentListInfo ArgInfos;
4748 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4749 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4750 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4751 return nullptr;
4752
4753 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4754 FromPartial->getTemplateParameters());
4755 if (!ToTPList)
4756 return nullptr;
4757
Gabor Marton26f72a92018-07-12 09:42:05 +00004758 PartVarSpecDecl *ToPartial;
4759 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
4760 StartLoc, IdLoc, ToTPList, VarTemplate, T,
4761 TInfo, D->getStorageClass(), TemplateArgs,
4762 ArgInfos))
4763 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004764
4765 auto *FromInst = FromPartial->getInstantiatedFromMember();
4766 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4767 if (FromInst && !ToInst)
4768 return nullptr;
4769
4770 ToPartial->setInstantiatedFromMember(ToInst);
4771 if (FromPartial->isMemberSpecialization())
4772 ToPartial->setMemberSpecialization();
4773
4774 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004775 } else { // Full specialization
Gabor Marton26f72a92018-07-12 09:42:05 +00004776 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, StartLoc,
4777 IdLoc, VarTemplate, T, TInfo,
4778 D->getStorageClass(), TemplateArgs))
4779 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004780 }
4781
4782 SourceLocation POI = D->getPointOfInstantiation();
4783 if (POI.isValid())
4784 D2->setPointOfInstantiation(Importer.Import(POI));
4785
Larisse Voufo39a1e502013-08-06 01:03:05 +00004786 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004787 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004788
4789 // Add this specialization to the class template.
4790 VarTemplate->AddSpecialization(D2, InsertPos);
4791
4792 // Import the qualifier, if any.
4793 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4794
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004795 if (D->isConstexpr())
4796 D2->setConstexpr(true);
4797
Larisse Voufo39a1e502013-08-06 01:03:05 +00004798 // Add the specialization to this context.
4799 D2->setLexicalDeclContext(LexicalDC);
4800 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004801
4802 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004803 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004804
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004805 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4806 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4807 // we have to check the declaration for initializer - otherwise, it won't be
4808 // imported.
4809 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4810 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004811 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004812
4813 return D2;
4814}
4815
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004816Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4817 DeclContext *DC, *LexicalDC;
4818 DeclarationName Name;
4819 SourceLocation Loc;
4820 NamedDecl *ToD;
4821
4822 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4823 return nullptr;
4824
4825 if (ToD)
4826 return ToD;
4827
4828 // Try to find a function in our own ("to") context with the same name, same
4829 // type, and in the same context as the function we're importing.
4830 if (!LexicalDC->isFunctionOrMethod()) {
4831 unsigned IDNS = Decl::IDNS_Ordinary;
4832 SmallVector<NamedDecl *, 2> FoundDecls;
4833 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004834 for (auto *FoundDecl : FoundDecls) {
4835 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004836 continue;
4837
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004838 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004839 if (FoundFunction->hasExternalFormalLinkage() &&
4840 D->hasExternalFormalLinkage()) {
4841 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00004842 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004843 // FIXME: Actually try to merge the body and other attributes.
4844 return FoundFunction;
4845 }
4846 }
4847 }
4848 }
4849 }
4850
4851 TemplateParameterList *Params =
4852 ImportTemplateParameterList(D->getTemplateParameters());
4853 if (!Params)
4854 return nullptr;
4855
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004856 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004857 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4858 if (!TemplatedFD)
4859 return nullptr;
4860
Gabor Marton26f72a92018-07-12 09:42:05 +00004861 FunctionTemplateDecl *ToFunc;
4862 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
4863 Params, TemplatedFD))
4864 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004865
4866 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4867 ToFunc->setAccess(D->getAccess());
4868 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004869
4870 LexicalDC->addDeclInternal(ToFunc);
4871 return ToFunc;
4872}
4873
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004874//----------------------------------------------------------------------------
4875// Import Statements
4876//----------------------------------------------------------------------------
4877
Sean Callanan59721b32015-04-28 18:41:46 +00004878DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4879 if (DG.isNull())
4880 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4881 size_t NumDecls = DG.end() - DG.begin();
4882 SmallVector<Decl *, 1> ToDecls(NumDecls);
4883 auto &_Importer = this->Importer;
4884 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4885 [&_Importer](Decl *D) -> Decl * {
4886 return _Importer.Import(D);
4887 });
4888 return DeclGroupRef::Create(Importer.getToContext(),
4889 ToDecls.begin(),
4890 NumDecls);
4891}
4892
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004893Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4894 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4895 << S->getStmtClassName();
4896 return nullptr;
4897}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004898
4899Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4900 SmallVector<IdentifierInfo *, 4> Names;
4901 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4902 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004903 // ToII is nullptr when no symbolic name is given for output operand
4904 // see ParseStmtAsm::ParseAsmOperandsOpt
4905 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004906 return nullptr;
4907 Names.push_back(ToII);
4908 }
4909 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4910 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004911 // ToII is nullptr when no symbolic name is given for input operand
4912 // see ParseStmtAsm::ParseAsmOperandsOpt
4913 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004914 return nullptr;
4915 Names.push_back(ToII);
4916 }
4917
4918 SmallVector<StringLiteral *, 4> Clobbers;
4919 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004920 auto *Clobber = cast_or_null<StringLiteral>(
4921 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004922 if (!Clobber)
4923 return nullptr;
4924 Clobbers.push_back(Clobber);
4925 }
4926
4927 SmallVector<StringLiteral *, 4> Constraints;
4928 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004929 auto *Output = cast_or_null<StringLiteral>(
4930 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004931 if (!Output)
4932 return nullptr;
4933 Constraints.push_back(Output);
4934 }
4935
4936 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004937 auto *Input = cast_or_null<StringLiteral>(
4938 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004939 if (!Input)
4940 return nullptr;
4941 Constraints.push_back(Input);
4942 }
4943
4944 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004945 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004946 return nullptr;
4947
Aleksei Sidorina693b372016-09-28 10:16:56 +00004948 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004949 return nullptr;
4950
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004951 auto *AsmStr = cast_or_null<StringLiteral>(
4952 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004953 if (!AsmStr)
4954 return nullptr;
4955
4956 return new (Importer.getToContext()) GCCAsmStmt(
4957 Importer.getToContext(),
4958 Importer.Import(S->getAsmLoc()),
4959 S->isSimple(),
4960 S->isVolatile(),
4961 S->getNumOutputs(),
4962 S->getNumInputs(),
4963 Names.data(),
4964 Constraints.data(),
4965 Exprs.data(),
4966 AsmStr,
4967 S->getNumClobbers(),
4968 Clobbers.data(),
4969 Importer.Import(S->getRParenLoc()));
4970}
4971
Sean Callanan59721b32015-04-28 18:41:46 +00004972Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4973 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004974 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004975 if (!ToD)
4976 return nullptr;
4977 }
4978 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4979 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4980 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4981}
4982
4983Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4984 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4985 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4986 S->hasLeadingEmptyMacro());
4987}
4988
4989Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004990 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004991
4992 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004993 return nullptr;
4994
Sean Callanan59721b32015-04-28 18:41:46 +00004995 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4996 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004997 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4998 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004999}
5000
5001Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5002 Expr *ToLHS = Importer.Import(S->getLHS());
5003 if (!ToLHS)
5004 return nullptr;
5005 Expr *ToRHS = Importer.Import(S->getRHS());
5006 if (!ToRHS && S->getRHS())
5007 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00005008 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5009 if (!ToSubStmt && S->getSubStmt())
5010 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005011 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
5012 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
5013 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005014 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00005015 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
5016 ToStmt->setSubStmt(ToSubStmt);
5017 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005018}
5019
5020Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5021 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
5022 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5023 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5024 if (!ToSubStmt && S->getSubStmt())
5025 return nullptr;
5026 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
5027 ToSubStmt);
5028}
5029
5030Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5031 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005032 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00005033 if (!ToLabelDecl && S->getDecl())
5034 return nullptr;
5035 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5036 if (!ToSubStmt && S->getSubStmt())
5037 return nullptr;
5038 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
5039 ToSubStmt);
5040}
5041
5042Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5043 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
5044 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5045 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00005046 if (ImportContainerChecked(FromAttrs, ToAttrs))
5047 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005048 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5049 if (!ToSubStmt && S->getSubStmt())
5050 return nullptr;
5051 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
5052 ToAttrs, ToSubStmt);
5053}
5054
5055Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5056 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00005057 Stmt *ToInit = Importer.Import(S->getInit());
5058 if (!ToInit && S->getInit())
5059 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005060 VarDecl *ToConditionVariable = nullptr;
5061 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5062 ToConditionVariable =
5063 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5064 if (!ToConditionVariable)
5065 return nullptr;
5066 }
5067 Expr *ToCondition = Importer.Import(S->getCond());
5068 if (!ToCondition && S->getCond())
5069 return nullptr;
5070 Stmt *ToThenStmt = Importer.Import(S->getThen());
5071 if (!ToThenStmt && S->getThen())
5072 return nullptr;
5073 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
5074 Stmt *ToElseStmt = Importer.Import(S->getElse());
5075 if (!ToElseStmt && S->getElse())
5076 return nullptr;
5077 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00005078 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00005079 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00005080 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00005081 ToCondition, ToThenStmt,
5082 ToElseLoc, ToElseStmt);
5083}
5084
5085Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00005086 Stmt *ToInit = Importer.Import(S->getInit());
5087 if (!ToInit && S->getInit())
5088 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005089 VarDecl *ToConditionVariable = nullptr;
5090 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5091 ToConditionVariable =
5092 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5093 if (!ToConditionVariable)
5094 return nullptr;
5095 }
5096 Expr *ToCondition = Importer.Import(S->getCond());
5097 if (!ToCondition && S->getCond())
5098 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005099 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00005100 Importer.getToContext(), ToInit,
5101 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00005102 Stmt *ToBody = Importer.Import(S->getBody());
5103 if (!ToBody && S->getBody())
5104 return nullptr;
5105 ToStmt->setBody(ToBody);
5106 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5107 // Now we have to re-chain the cases.
5108 SwitchCase *LastChainedSwitchCase = nullptr;
5109 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5110 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005111 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00005112 if (!ToSC)
5113 return nullptr;
5114 if (LastChainedSwitchCase)
5115 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5116 else
5117 ToStmt->setSwitchCaseList(ToSC);
5118 LastChainedSwitchCase = ToSC;
5119 }
5120 return ToStmt;
5121}
5122
5123Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5124 VarDecl *ToConditionVariable = nullptr;
5125 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5126 ToConditionVariable =
5127 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5128 if (!ToConditionVariable)
5129 return nullptr;
5130 }
5131 Expr *ToCondition = Importer.Import(S->getCond());
5132 if (!ToCondition && S->getCond())
5133 return nullptr;
5134 Stmt *ToBody = Importer.Import(S->getBody());
5135 if (!ToBody && S->getBody())
5136 return nullptr;
5137 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5138 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5139 ToConditionVariable,
5140 ToCondition, ToBody,
5141 ToWhileLoc);
5142}
5143
5144Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5145 Stmt *ToBody = Importer.Import(S->getBody());
5146 if (!ToBody && S->getBody())
5147 return nullptr;
5148 Expr *ToCondition = Importer.Import(S->getCond());
5149 if (!ToCondition && S->getCond())
5150 return nullptr;
5151 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5152 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5153 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5154 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5155 ToDoLoc, ToWhileLoc,
5156 ToRParenLoc);
5157}
5158
5159Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5160 Stmt *ToInit = Importer.Import(S->getInit());
5161 if (!ToInit && S->getInit())
5162 return nullptr;
5163 Expr *ToCondition = Importer.Import(S->getCond());
5164 if (!ToCondition && S->getCond())
5165 return nullptr;
5166 VarDecl *ToConditionVariable = nullptr;
5167 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5168 ToConditionVariable =
5169 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5170 if (!ToConditionVariable)
5171 return nullptr;
5172 }
5173 Expr *ToInc = Importer.Import(S->getInc());
5174 if (!ToInc && S->getInc())
5175 return nullptr;
5176 Stmt *ToBody = Importer.Import(S->getBody());
5177 if (!ToBody && S->getBody())
5178 return nullptr;
5179 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5180 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5181 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5182 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5183 ToInit, ToCondition,
5184 ToConditionVariable,
5185 ToInc, ToBody,
5186 ToForLoc, ToLParenLoc,
5187 ToRParenLoc);
5188}
5189
5190Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5191 LabelDecl *ToLabel = nullptr;
5192 if (LabelDecl *FromLabel = S->getLabel()) {
5193 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5194 if (!ToLabel)
5195 return nullptr;
5196 }
5197 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5198 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5199 return new (Importer.getToContext()) GotoStmt(ToLabel,
5200 ToGotoLoc, ToLabelLoc);
5201}
5202
5203Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5204 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5205 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5206 Expr *ToTarget = Importer.Import(S->getTarget());
5207 if (!ToTarget && S->getTarget())
5208 return nullptr;
5209 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5210 ToTarget);
5211}
5212
5213Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5214 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5215 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5216}
5217
5218Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5219 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5220 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5221}
5222
5223Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5224 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5225 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5226 if (!ToRetExpr && S->getRetValue())
5227 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005228 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
5229 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00005230 if (!ToNRVOCandidate && NRVOCandidate)
5231 return nullptr;
5232 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5233 ToNRVOCandidate);
5234}
5235
5236Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5237 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5238 VarDecl *ToExceptionDecl = nullptr;
5239 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5240 ToExceptionDecl =
5241 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5242 if (!ToExceptionDecl)
5243 return nullptr;
5244 }
5245 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5246 if (!ToHandlerBlock && S->getHandlerBlock())
5247 return nullptr;
5248 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5249 ToExceptionDecl,
5250 ToHandlerBlock);
5251}
5252
5253Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5254 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5255 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5256 if (!ToTryBlock && S->getTryBlock())
5257 return nullptr;
5258 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5259 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5260 CXXCatchStmt *FromHandler = S->getHandler(HI);
5261 if (Stmt *ToHandler = Importer.Import(FromHandler))
5262 ToHandlers[HI] = ToHandler;
5263 else
5264 return nullptr;
5265 }
5266 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5267 ToHandlers);
5268}
5269
5270Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005271 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005272 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5273 if (!ToRange && S->getRangeStmt())
5274 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005275 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005276 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5277 if (!ToBegin && S->getBeginStmt())
5278 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005279 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005280 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5281 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005282 return nullptr;
5283 Expr *ToCond = Importer.Import(S->getCond());
5284 if (!ToCond && S->getCond())
5285 return nullptr;
5286 Expr *ToInc = Importer.Import(S->getInc());
5287 if (!ToInc && S->getInc())
5288 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005289 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005290 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5291 if (!ToLoopVar && S->getLoopVarStmt())
5292 return nullptr;
5293 Stmt *ToBody = Importer.Import(S->getBody());
5294 if (!ToBody && S->getBody())
5295 return nullptr;
5296 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005297 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005298 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5299 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005300 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005301 ToCond, ToInc,
5302 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005303 ToForLoc, ToCoawaitLoc,
5304 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005305}
5306
5307Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5308 Stmt *ToElem = Importer.Import(S->getElement());
5309 if (!ToElem && S->getElement())
5310 return nullptr;
5311 Expr *ToCollect = Importer.Import(S->getCollection());
5312 if (!ToCollect && S->getCollection())
5313 return nullptr;
5314 Stmt *ToBody = Importer.Import(S->getBody());
5315 if (!ToBody && S->getBody())
5316 return nullptr;
5317 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5318 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5319 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5320 ToCollect,
5321 ToBody, ToForLoc,
5322 ToRParenLoc);
5323}
5324
5325Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5326 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5327 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5328 VarDecl *ToExceptionDecl = nullptr;
5329 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5330 ToExceptionDecl =
5331 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5332 if (!ToExceptionDecl)
5333 return nullptr;
5334 }
5335 Stmt *ToBody = Importer.Import(S->getCatchBody());
5336 if (!ToBody && S->getCatchBody())
5337 return nullptr;
5338 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5339 ToRParenLoc,
5340 ToExceptionDecl,
5341 ToBody);
5342}
5343
5344Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5345 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5346 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5347 if (!ToAtFinallyStmt && S->getFinallyBody())
5348 return nullptr;
5349 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5350 ToAtFinallyStmt);
5351}
5352
5353Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5354 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5355 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5356 if (!ToAtTryStmt && S->getTryBody())
5357 return nullptr;
5358 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5359 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5360 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5361 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5362 ToCatchStmts[CI] = ToCatchStmt;
5363 else
5364 return nullptr;
5365 }
5366 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5367 if (!ToAtFinallyStmt && S->getFinallyStmt())
5368 return nullptr;
5369 return ObjCAtTryStmt::Create(Importer.getToContext(),
5370 ToAtTryLoc, ToAtTryStmt,
5371 ToCatchStmts.begin(), ToCatchStmts.size(),
5372 ToAtFinallyStmt);
5373}
5374
5375Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5376 (ObjCAtSynchronizedStmt *S) {
5377 SourceLocation ToAtSynchronizedLoc =
5378 Importer.Import(S->getAtSynchronizedLoc());
5379 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5380 if (!ToSynchExpr && S->getSynchExpr())
5381 return nullptr;
5382 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5383 if (!ToSynchBody && S->getSynchBody())
5384 return nullptr;
5385 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5386 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5387}
5388
5389Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5390 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5391 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5392 if (!ToThrow && S->getThrowExpr())
5393 return nullptr;
5394 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5395}
5396
5397Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5398 (ObjCAutoreleasePoolStmt *S) {
5399 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5400 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5401 if (!ToSubStmt && S->getSubStmt())
5402 return nullptr;
5403 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5404 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005405}
5406
5407//----------------------------------------------------------------------------
5408// Import Expressions
5409//----------------------------------------------------------------------------
5410Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5411 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5412 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005413 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005414}
5415
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005416Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5417 QualType T = Importer.Import(E->getType());
5418 if (T.isNull())
5419 return nullptr;
5420
5421 Expr *SubExpr = Importer.Import(E->getSubExpr());
5422 if (!SubExpr && E->getSubExpr())
5423 return nullptr;
5424
5425 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5426 if (!TInfo)
5427 return nullptr;
5428
5429 return new (Importer.getToContext()) VAArgExpr(
5430 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5431 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5432}
5433
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005434Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5435 QualType T = Importer.Import(E->getType());
5436 if (T.isNull())
5437 return nullptr;
5438
5439 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005440 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005441}
5442
5443Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5444 QualType T = Importer.Import(E->getType());
5445 if (T.isNull())
5446 return nullptr;
5447
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005448 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005449 if (!SL && E->getFunctionName())
5450 return nullptr;
5451
5452 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005453 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005454}
5455
Douglas Gregor52f820e2010-02-19 01:17:02 +00005456Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005457 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005458 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005459 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005460
Craig Topper36250ad2014-05-12 05:36:57 +00005461 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005462 if (E->getDecl() != E->getFoundDecl()) {
5463 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5464 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005465 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005466 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005467
5468 QualType T = Importer.Import(E->getType());
5469 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005470 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005471
Aleksei Sidorina693b372016-09-28 10:16:56 +00005472 TemplateArgumentListInfo ToTAInfo;
5473 TemplateArgumentListInfo *ResInfo = nullptr;
5474 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005475 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5476 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005477 ResInfo = &ToTAInfo;
5478 }
5479
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005480 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5481 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005482 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005483 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005484 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005485 Importer.Import(E->getLocation()),
5486 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005487 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005488 if (E->hadMultipleCandidates())
5489 DRE->setHadMultipleCandidates(true);
5490 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005491}
5492
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005493Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5494 QualType T = Importer.Import(E->getType());
5495 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005496 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005497
5498 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5499}
5500
5501ASTNodeImporter::Designator
5502ASTNodeImporter::ImportDesignator(const Designator &D) {
5503 if (D.isFieldDesignator()) {
5504 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5505 // Caller checks for import error
5506 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5507 Importer.Import(D.getFieldLoc()));
5508 }
5509 if (D.isArrayDesignator())
5510 return Designator(D.getFirstExprIndex(),
5511 Importer.Import(D.getLBracketLoc()),
5512 Importer.Import(D.getRBracketLoc()));
5513
5514 assert(D.isArrayRangeDesignator());
5515 return Designator(D.getFirstExprIndex(),
5516 Importer.Import(D.getLBracketLoc()),
5517 Importer.Import(D.getEllipsisLoc()),
5518 Importer.Import(D.getRBracketLoc()));
5519}
5520
5521
5522Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005523 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005524 if (!Init)
5525 return nullptr;
5526
5527 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5528 // List elements from the second, the first is Init itself
5529 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005530 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005531 IndexExprs[I - 1] = Arg;
5532 else
5533 return nullptr;
5534 }
5535
5536 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005537 llvm::transform(DIE->designators(), Designators.begin(),
5538 [this](const Designator &D) -> Designator {
5539 return ImportDesignator(D);
5540 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005541
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005542 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005543 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005544 return nullptr;
5545
5546 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005547 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005548 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5549 DIE->usesGNUSyntax(), Init);
5550}
5551
5552Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5553 QualType T = Importer.Import(E->getType());
5554 if (T.isNull())
5555 return nullptr;
5556
5557 return new (Importer.getToContext())
5558 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5559}
5560
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005561Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5562 QualType T = Importer.Import(E->getType());
5563 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005564 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005565
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005566 return IntegerLiteral::Create(Importer.getToContext(),
5567 E->getValue(), T,
5568 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005569}
5570
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005571Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5572 QualType T = Importer.Import(E->getType());
5573 if (T.isNull())
5574 return nullptr;
5575
5576 return FloatingLiteral::Create(Importer.getToContext(),
5577 E->getValue(), E->isExact(), T,
5578 Importer.Import(E->getLocation()));
5579}
5580
Douglas Gregor623421d2010-02-18 02:21:22 +00005581Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5582 QualType T = Importer.Import(E->getType());
5583 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005584 return nullptr;
5585
Douglas Gregorfb65e592011-07-27 05:40:30 +00005586 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5587 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005588 Importer.Import(E->getLocation()));
5589}
5590
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005591Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5592 QualType T = Importer.Import(E->getType());
5593 if (T.isNull())
5594 return nullptr;
5595
5596 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5597 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5598
5599 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5600 E->getKind(), E->isPascal(), T,
5601 Locations.data(), Locations.size());
5602}
5603
5604Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5605 QualType T = Importer.Import(E->getType());
5606 if (T.isNull())
5607 return nullptr;
5608
5609 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5610 if (!TInfo)
5611 return nullptr;
5612
5613 Expr *Init = Importer.Import(E->getInitializer());
5614 if (!Init)
5615 return nullptr;
5616
5617 return new (Importer.getToContext()) CompoundLiteralExpr(
5618 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5619 Init, E->isFileScope());
5620}
5621
5622Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5623 QualType T = Importer.Import(E->getType());
5624 if (T.isNull())
5625 return nullptr;
5626
5627 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5628 if (ImportArrayChecked(
5629 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5630 Exprs.begin()))
5631 return nullptr;
5632
5633 return new (Importer.getToContext()) AtomicExpr(
5634 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5635 Importer.Import(E->getRParenLoc()));
5636}
5637
5638Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5639 QualType T = Importer.Import(E->getType());
5640 if (T.isNull())
5641 return nullptr;
5642
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005643 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005644 if (!ToLabel)
5645 return nullptr;
5646
5647 return new (Importer.getToContext()) AddrLabelExpr(
5648 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5649 ToLabel, T);
5650}
5651
Douglas Gregorc74247e2010-02-19 01:07:06 +00005652Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5653 Expr *SubExpr = Importer.Import(E->getSubExpr());
5654 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005655 return nullptr;
5656
Douglas Gregorc74247e2010-02-19 01:07:06 +00005657 return new (Importer.getToContext())
5658 ParenExpr(Importer.Import(E->getLParen()),
5659 Importer.Import(E->getRParen()),
5660 SubExpr);
5661}
5662
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005663Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5664 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005665 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005666 return nullptr;
5667
5668 return new (Importer.getToContext()) ParenListExpr(
5669 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5670 Exprs, Importer.Import(E->getLParenLoc()));
5671}
5672
5673Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5674 QualType T = Importer.Import(E->getType());
5675 if (T.isNull())
5676 return nullptr;
5677
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005678 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5679 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005680 if (!ToSubStmt && E->getSubStmt())
5681 return nullptr;
5682
5683 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5684 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5685}
5686
Douglas Gregorc74247e2010-02-19 01:07:06 +00005687Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5688 QualType T = Importer.Import(E->getType());
5689 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005690 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005691
5692 Expr *SubExpr = Importer.Import(E->getSubExpr());
5693 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005694 return nullptr;
5695
Aaron Ballmana5038552018-01-09 13:07:03 +00005696 return new (Importer.getToContext()) UnaryOperator(
5697 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5698 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005699}
5700
Aaron Ballmana5038552018-01-09 13:07:03 +00005701Expr *
5702ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005703 QualType ResultType = Importer.Import(E->getType());
5704
5705 if (E->isArgumentType()) {
5706 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5707 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005708 return nullptr;
5709
Peter Collingbournee190dee2011-03-11 19:24:49 +00005710 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5711 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005712 Importer.Import(E->getOperatorLoc()),
5713 Importer.Import(E->getRParenLoc()));
5714 }
5715
5716 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5717 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005718 return nullptr;
5719
Peter Collingbournee190dee2011-03-11 19:24:49 +00005720 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5721 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005722 Importer.Import(E->getOperatorLoc()),
5723 Importer.Import(E->getRParenLoc()));
5724}
5725
Douglas Gregorc74247e2010-02-19 01:07:06 +00005726Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5727 QualType T = Importer.Import(E->getType());
5728 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005729 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005730
5731 Expr *LHS = Importer.Import(E->getLHS());
5732 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005733 return nullptr;
5734
Douglas Gregorc74247e2010-02-19 01:07:06 +00005735 Expr *RHS = Importer.Import(E->getRHS());
5736 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005737 return nullptr;
5738
Douglas Gregorc74247e2010-02-19 01:07:06 +00005739 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005740 T, E->getValueKind(),
5741 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005742 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005743 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005744}
5745
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005746Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5747 QualType T = Importer.Import(E->getType());
5748 if (T.isNull())
5749 return nullptr;
5750
5751 Expr *ToLHS = Importer.Import(E->getLHS());
5752 if (!ToLHS)
5753 return nullptr;
5754
5755 Expr *ToRHS = Importer.Import(E->getRHS());
5756 if (!ToRHS)
5757 return nullptr;
5758
5759 Expr *ToCond = Importer.Import(E->getCond());
5760 if (!ToCond)
5761 return nullptr;
5762
5763 return new (Importer.getToContext()) ConditionalOperator(
5764 ToCond, Importer.Import(E->getQuestionLoc()),
5765 ToLHS, Importer.Import(E->getColonLoc()),
5766 ToRHS, T, E->getValueKind(), E->getObjectKind());
5767}
5768
5769Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5770 BinaryConditionalOperator *E) {
5771 QualType T = Importer.Import(E->getType());
5772 if (T.isNull())
5773 return nullptr;
5774
5775 Expr *Common = Importer.Import(E->getCommon());
5776 if (!Common)
5777 return nullptr;
5778
5779 Expr *Cond = Importer.Import(E->getCond());
5780 if (!Cond)
5781 return nullptr;
5782
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005783 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5784 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005785 if (!OpaqueValue)
5786 return nullptr;
5787
5788 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5789 if (!TrueExpr)
5790 return nullptr;
5791
5792 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5793 if (!FalseExpr)
5794 return nullptr;
5795
5796 return new (Importer.getToContext()) BinaryConditionalOperator(
5797 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5798 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5799 T, E->getValueKind(), E->getObjectKind());
5800}
5801
Aleksei Sidorina693b372016-09-28 10:16:56 +00005802Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5803 QualType T = Importer.Import(E->getType());
5804 if (T.isNull())
5805 return nullptr;
5806
5807 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5808 if (!ToQueried)
5809 return nullptr;
5810
5811 Expr *Dim = Importer.Import(E->getDimensionExpression());
5812 if (!Dim && E->getDimensionExpression())
5813 return nullptr;
5814
5815 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5816 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5817 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5818}
5819
5820Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5821 QualType T = Importer.Import(E->getType());
5822 if (T.isNull())
5823 return nullptr;
5824
5825 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5826 if (!ToQueried)
5827 return nullptr;
5828
5829 return new (Importer.getToContext()) ExpressionTraitExpr(
5830 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5831 E->getValue(), Importer.Import(E->getLocEnd()), T);
5832}
5833
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005834Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5835 QualType T = Importer.Import(E->getType());
5836 if (T.isNull())
5837 return nullptr;
5838
5839 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5840 if (!SourceExpr && E->getSourceExpr())
5841 return nullptr;
5842
5843 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005844 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005845 E->getObjectKind(), SourceExpr);
5846}
5847
Aleksei Sidorina693b372016-09-28 10:16:56 +00005848Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5849 QualType T = Importer.Import(E->getType());
5850 if (T.isNull())
5851 return nullptr;
5852
5853 Expr *ToLHS = Importer.Import(E->getLHS());
5854 if (!ToLHS)
5855 return nullptr;
5856
5857 Expr *ToRHS = Importer.Import(E->getRHS());
5858 if (!ToRHS)
5859 return nullptr;
5860
5861 return new (Importer.getToContext()) ArraySubscriptExpr(
5862 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5863 Importer.Import(E->getRBracketLoc()));
5864}
5865
Douglas Gregorc74247e2010-02-19 01:07:06 +00005866Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5867 QualType T = Importer.Import(E->getType());
5868 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005869 return nullptr;
5870
Douglas Gregorc74247e2010-02-19 01:07:06 +00005871 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5872 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005873 return nullptr;
5874
Douglas Gregorc74247e2010-02-19 01:07:06 +00005875 QualType CompResultType = Importer.Import(E->getComputationResultType());
5876 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005877 return nullptr;
5878
Douglas Gregorc74247e2010-02-19 01:07:06 +00005879 Expr *LHS = Importer.Import(E->getLHS());
5880 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005881 return nullptr;
5882
Douglas Gregorc74247e2010-02-19 01:07:06 +00005883 Expr *RHS = Importer.Import(E->getRHS());
5884 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005885 return nullptr;
5886
Douglas Gregorc74247e2010-02-19 01:07:06 +00005887 return new (Importer.getToContext())
5888 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005889 T, E->getValueKind(),
5890 E->getObjectKind(),
5891 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005892 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005893 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005894}
5895
Aleksei Sidorina693b372016-09-28 10:16:56 +00005896bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5897 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5898 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5899 Path.push_back(Spec);
5900 else
5901 return true;
5902 }
5903 return false;
John McCallcf142162010-08-07 06:22:56 +00005904}
5905
Douglas Gregor98c10182010-02-12 22:17:39 +00005906Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5907 QualType T = Importer.Import(E->getType());
5908 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005909 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005910
5911 Expr *SubExpr = Importer.Import(E->getSubExpr());
5912 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005913 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005914
5915 CXXCastPath BasePath;
5916 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005917 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005918
5919 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005920 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005921}
5922
Aleksei Sidorina693b372016-09-28 10:16:56 +00005923Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005924 QualType T = Importer.Import(E->getType());
5925 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005926 return nullptr;
5927
Douglas Gregor5481d322010-02-19 01:32:14 +00005928 Expr *SubExpr = Importer.Import(E->getSubExpr());
5929 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005930 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005931
5932 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5933 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005934 return nullptr;
5935
John McCallcf142162010-08-07 06:22:56 +00005936 CXXCastPath BasePath;
5937 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005938 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005939
Aleksei Sidorina693b372016-09-28 10:16:56 +00005940 switch (E->getStmtClass()) {
5941 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005942 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005943 return CStyleCastExpr::Create(Importer.getToContext(), T,
5944 E->getValueKind(), E->getCastKind(),
5945 SubExpr, &BasePath, TInfo,
5946 Importer.Import(CCE->getLParenLoc()),
5947 Importer.Import(CCE->getRParenLoc()));
5948 }
5949
5950 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005951 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005952 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5953 E->getValueKind(), TInfo,
5954 E->getCastKind(), SubExpr, &BasePath,
5955 Importer.Import(FCE->getLParenLoc()),
5956 Importer.Import(FCE->getRParenLoc()));
5957 }
5958
5959 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005960 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005961 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5962 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5963 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5964 TInfo, SubExpr);
5965 }
5966 default:
5967 break; // just fall through
5968 }
5969
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005970 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005971 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5972 RParenLoc = Importer.Import(Named->getRParenLoc());
5973 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5974
5975 switch (E->getStmtClass()) {
5976 case Stmt::CXXStaticCastExprClass:
5977 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5978 E->getValueKind(), E->getCastKind(),
5979 SubExpr, &BasePath, TInfo,
5980 ExprLoc, RParenLoc, Brackets);
5981
5982 case Stmt::CXXDynamicCastExprClass:
5983 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5984 E->getValueKind(), E->getCastKind(),
5985 SubExpr, &BasePath, TInfo,
5986 ExprLoc, RParenLoc, Brackets);
5987
5988 case Stmt::CXXReinterpretCastExprClass:
5989 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5990 E->getValueKind(), E->getCastKind(),
5991 SubExpr, &BasePath, TInfo,
5992 ExprLoc, RParenLoc, Brackets);
5993
5994 case Stmt::CXXConstCastExprClass:
5995 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5996 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5997 RParenLoc, Brackets);
5998 default:
5999 llvm_unreachable("Cast expression of unsupported type!");
6000 return nullptr;
6001 }
6002}
6003
6004Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
6005 QualType T = Importer.Import(OE->getType());
6006 if (T.isNull())
6007 return nullptr;
6008
6009 SmallVector<OffsetOfNode, 4> Nodes;
6010 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
6011 const OffsetOfNode &Node = OE->getComponent(I);
6012
6013 switch (Node.getKind()) {
6014 case OffsetOfNode::Array:
6015 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
6016 Node.getArrayExprIndex(),
6017 Importer.Import(Node.getLocEnd())));
6018 break;
6019
6020 case OffsetOfNode::Base: {
6021 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
6022 if (!BS && Node.getBase())
6023 return nullptr;
6024 Nodes.push_back(OffsetOfNode(BS));
6025 break;
6026 }
6027 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006028 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006029 if (!FD)
6030 return nullptr;
6031 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
6032 Importer.Import(Node.getLocEnd())));
6033 break;
6034 }
6035 case OffsetOfNode::Identifier: {
6036 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
6037 if (!ToII)
6038 return nullptr;
6039 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
6040 Importer.Import(Node.getLocEnd())));
6041 break;
6042 }
6043 }
6044 }
6045
6046 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
6047 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
6048 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
6049 if (!ToIndexExpr)
6050 return nullptr;
6051 Exprs[I] = ToIndexExpr;
6052 }
6053
6054 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
6055 if (!TInfo && OE->getTypeSourceInfo())
6056 return nullptr;
6057
6058 return OffsetOfExpr::Create(Importer.getToContext(), T,
6059 Importer.Import(OE->getOperatorLoc()),
6060 TInfo, Nodes, Exprs,
6061 Importer.Import(OE->getRParenLoc()));
6062}
6063
6064Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6065 QualType T = Importer.Import(E->getType());
6066 if (T.isNull())
6067 return nullptr;
6068
6069 Expr *Operand = Importer.Import(E->getOperand());
6070 if (!Operand)
6071 return nullptr;
6072
6073 CanThrowResult CanThrow;
6074 if (E->isValueDependent())
6075 CanThrow = CT_Dependent;
6076 else
6077 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
6078
6079 return new (Importer.getToContext()) CXXNoexceptExpr(
6080 T, Operand, CanThrow,
6081 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
6082}
6083
6084Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6085 QualType T = Importer.Import(E->getType());
6086 if (T.isNull())
6087 return nullptr;
6088
6089 Expr *SubExpr = Importer.Import(E->getSubExpr());
6090 if (!SubExpr && E->getSubExpr())
6091 return nullptr;
6092
6093 return new (Importer.getToContext()) CXXThrowExpr(
6094 SubExpr, T, Importer.Import(E->getThrowLoc()),
6095 E->isThrownVariableInScope());
6096}
6097
6098Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006099 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006100 if (!Param)
6101 return nullptr;
6102
6103 return CXXDefaultArgExpr::Create(
6104 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
6105}
6106
6107Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6108 QualType T = Importer.Import(E->getType());
6109 if (T.isNull())
6110 return nullptr;
6111
6112 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
6113 if (!TypeInfo)
6114 return nullptr;
6115
6116 return new (Importer.getToContext()) CXXScalarValueInitExpr(
6117 T, TypeInfo, Importer.Import(E->getRParenLoc()));
6118}
6119
6120Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6121 Expr *SubExpr = Importer.Import(E->getSubExpr());
6122 if (!SubExpr)
6123 return nullptr;
6124
6125 auto *Dtor = cast_or_null<CXXDestructorDecl>(
6126 Importer.Import(const_cast<CXXDestructorDecl *>(
6127 E->getTemporary()->getDestructor())));
6128 if (!Dtor)
6129 return nullptr;
6130
6131 ASTContext &ToCtx = Importer.getToContext();
6132 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
6133 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
6134}
6135
6136Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
6137 QualType T = Importer.Import(CE->getType());
6138 if (T.isNull())
6139 return nullptr;
6140
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006141 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
6142 if (!TInfo)
6143 return nullptr;
6144
Aleksei Sidorina693b372016-09-28 10:16:56 +00006145 SmallVector<Expr *, 8> Args(CE->getNumArgs());
6146 if (ImportContainerChecked(CE->arguments(), Args))
6147 return nullptr;
6148
6149 auto *Ctor = cast_or_null<CXXConstructorDecl>(
6150 Importer.Import(CE->getConstructor()));
6151 if (!Ctor)
6152 return nullptr;
6153
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006154 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
6155 Importer.getToContext(), Ctor, T, TInfo, Args,
6156 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
6157 CE->isListInitialization(), CE->isStdInitListInitialization(),
6158 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006159}
6160
6161Expr *
6162ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
6163 QualType T = Importer.Import(E->getType());
6164 if (T.isNull())
6165 return nullptr;
6166
6167 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
6168 if (!TempE)
6169 return nullptr;
6170
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006171 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006172 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
6173 if (!ExtendedBy && E->getExtendingDecl())
6174 return nullptr;
6175
6176 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
6177 T, TempE, E->isBoundToLvalueReference());
6178
6179 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
6180 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
6181 return ToMTE;
6182}
6183
Gabor Horvath7a91c082017-11-14 11:30:38 +00006184Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6185 QualType T = Importer.Import(E->getType());
6186 if (T.isNull())
6187 return nullptr;
6188
6189 Expr *Pattern = Importer.Import(E->getPattern());
6190 if (!Pattern)
6191 return nullptr;
6192
6193 return new (Importer.getToContext()) PackExpansionExpr(
6194 T, Pattern, Importer.Import(E->getEllipsisLoc()),
6195 E->getNumExpansions());
6196}
6197
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006198Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6199 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
6200 if (!Pack)
6201 return nullptr;
6202
6203 Optional<unsigned> Length;
6204
6205 if (!E->isValueDependent())
6206 Length = E->getPackLength();
6207
6208 SmallVector<TemplateArgument, 8> PartialArguments;
6209 if (E->isPartiallySubstituted()) {
6210 if (ImportTemplateArguments(E->getPartialArguments().data(),
6211 E->getPartialArguments().size(),
6212 PartialArguments))
6213 return nullptr;
6214 }
6215
6216 return SizeOfPackExpr::Create(
6217 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
6218 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
6219 Length, PartialArguments);
6220}
6221
Aleksei Sidorina693b372016-09-28 10:16:56 +00006222Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6223 QualType T = Importer.Import(CE->getType());
6224 if (T.isNull())
6225 return nullptr;
6226
6227 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6228 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6229 return nullptr;
6230
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006231 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006232 Importer.Import(CE->getOperatorNew()));
6233 if (!OperatorNewDecl && CE->getOperatorNew())
6234 return nullptr;
6235
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006236 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006237 Importer.Import(CE->getOperatorDelete()));
6238 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6239 return nullptr;
6240
6241 Expr *ToInit = Importer.Import(CE->getInitializer());
6242 if (!ToInit && CE->getInitializer())
6243 return nullptr;
6244
6245 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6246 if (!TInfo)
6247 return nullptr;
6248
6249 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6250 if (!ToArrSize && CE->getArraySize())
6251 return nullptr;
6252
6253 return new (Importer.getToContext()) CXXNewExpr(
6254 Importer.getToContext(),
6255 CE->isGlobalNew(),
6256 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006257 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006258 CE->doesUsualArrayDeleteWantSize(),
6259 PlacementArgs,
6260 Importer.Import(CE->getTypeIdParens()),
6261 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6262 Importer.Import(CE->getSourceRange()),
6263 Importer.Import(CE->getDirectInitRange()));
6264}
6265
6266Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6267 QualType T = Importer.Import(E->getType());
6268 if (T.isNull())
6269 return nullptr;
6270
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006271 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006272 Importer.Import(E->getOperatorDelete()));
6273 if (!OperatorDeleteDecl && E->getOperatorDelete())
6274 return nullptr;
6275
6276 Expr *ToArg = Importer.Import(E->getArgument());
6277 if (!ToArg && E->getArgument())
6278 return nullptr;
6279
6280 return new (Importer.getToContext()) CXXDeleteExpr(
6281 T, E->isGlobalDelete(),
6282 E->isArrayForm(),
6283 E->isArrayFormAsWritten(),
6284 E->doesUsualArrayDeleteWantSize(),
6285 OperatorDeleteDecl,
6286 ToArg,
6287 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006288}
6289
Sean Callanan59721b32015-04-28 18:41:46 +00006290Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6291 QualType T = Importer.Import(E->getType());
6292 if (T.isNull())
6293 return nullptr;
6294
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006295 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006296 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006297 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006298 return nullptr;
6299
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006300 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006301 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006302 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006303
6304 return CXXConstructExpr::Create(Importer.getToContext(), T,
6305 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006306 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006307 ToArgs, E->hadMultipleCandidates(),
6308 E->isListInitialization(),
6309 E->isStdInitListInitialization(),
6310 E->requiresZeroInitialization(),
6311 E->getConstructionKind(),
6312 Importer.Import(E->getParenOrBraceRange()));
6313}
6314
Aleksei Sidorina693b372016-09-28 10:16:56 +00006315Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6316 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6317 if (!SubExpr && EWC->getSubExpr())
6318 return nullptr;
6319
6320 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6321 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6322 if (ExprWithCleanups::CleanupObject Obj =
6323 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6324 Objs[I] = Obj;
6325 else
6326 return nullptr;
6327
6328 return ExprWithCleanups::Create(Importer.getToContext(),
6329 SubExpr, EWC->cleanupsHaveSideEffects(),
6330 Objs);
6331}
6332
Sean Callanan8bca9962016-03-28 21:43:01 +00006333Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6334 QualType T = Importer.Import(E->getType());
6335 if (T.isNull())
6336 return nullptr;
6337
6338 Expr *ToFn = Importer.Import(E->getCallee());
6339 if (!ToFn)
6340 return nullptr;
6341
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006342 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006343 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006344 return nullptr;
6345
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006346 return new (Importer.getToContext()) CXXMemberCallExpr(
6347 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6348 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006349}
6350
6351Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6352 QualType T = Importer.Import(E->getType());
6353 if (T.isNull())
6354 return nullptr;
6355
6356 return new (Importer.getToContext())
6357 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6358}
6359
6360Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6361 QualType T = Importer.Import(E->getType());
6362 if (T.isNull())
6363 return nullptr;
6364
6365 return new (Importer.getToContext())
6366 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6367}
6368
6369
Sean Callanan59721b32015-04-28 18:41:46 +00006370Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6371 QualType T = Importer.Import(E->getType());
6372 if (T.isNull())
6373 return nullptr;
6374
6375 Expr *ToBase = Importer.Import(E->getBase());
6376 if (!ToBase && E->getBase())
6377 return nullptr;
6378
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006379 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006380 if (!ToMember && E->getMemberDecl())
6381 return nullptr;
6382
Peter Szecsief972522018-05-02 11:52:54 +00006383 auto *ToDecl =
6384 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6385 if (!ToDecl && E->getFoundDecl().getDecl())
6386 return nullptr;
6387
6388 DeclAccessPair ToFoundDecl =
6389 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006390
6391 DeclarationNameInfo ToMemberNameInfo(
6392 Importer.Import(E->getMemberNameInfo().getName()),
6393 Importer.Import(E->getMemberNameInfo().getLoc()));
6394
6395 if (E->hasExplicitTemplateArgs()) {
6396 return nullptr; // FIXME: handle template arguments
6397 }
6398
6399 return MemberExpr::Create(Importer.getToContext(), ToBase,
6400 E->isArrow(),
6401 Importer.Import(E->getOperatorLoc()),
6402 Importer.Import(E->getQualifierLoc()),
6403 Importer.Import(E->getTemplateKeywordLoc()),
6404 ToMember, ToFoundDecl, ToMemberNameInfo,
6405 nullptr, T, E->getValueKind(),
6406 E->getObjectKind());
6407}
6408
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006409Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6410 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006411 Expr *BaseE = Importer.Import(E->getBase());
6412 if (!BaseE)
6413 return nullptr;
6414
6415 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6416 if (!ScopeInfo && E->getScopeTypeInfo())
6417 return nullptr;
6418
6419 PseudoDestructorTypeStorage Storage;
6420 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6421 IdentifierInfo *ToII = Importer.Import(FromII);
6422 if (!ToII)
6423 return nullptr;
6424 Storage = PseudoDestructorTypeStorage(
6425 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6426 } else {
6427 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6428 if (!TI)
6429 return nullptr;
6430 Storage = PseudoDestructorTypeStorage(TI);
6431 }
6432
6433 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6434 Importer.getToContext(), BaseE, E->isArrow(),
6435 Importer.Import(E->getOperatorLoc()),
6436 Importer.Import(E->getQualifierLoc()),
6437 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6438 Importer.Import(E->getTildeLoc()), Storage);
6439}
6440
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006441Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6442 CXXDependentScopeMemberExpr *E) {
6443 Expr *Base = nullptr;
6444 if (!E->isImplicitAccess()) {
6445 Base = Importer.Import(E->getBase());
6446 if (!Base)
6447 return nullptr;
6448 }
6449
6450 QualType BaseType = Importer.Import(E->getBaseType());
6451 if (BaseType.isNull())
6452 return nullptr;
6453
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006454 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006455 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006456 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6457 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006458 return nullptr;
6459 ResInfo = &ToTAInfo;
6460 }
6461
6462 DeclarationName Name = Importer.Import(E->getMember());
6463 if (!E->getMember().isEmpty() && Name.isEmpty())
6464 return nullptr;
6465
6466 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6467 // Import additional name location/type info.
6468 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6469 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6470 if (!ToFQ && E->getFirstQualifierFoundInScope())
6471 return nullptr;
6472
6473 return CXXDependentScopeMemberExpr::Create(
6474 Importer.getToContext(), Base, BaseType, E->isArrow(),
6475 Importer.Import(E->getOperatorLoc()),
6476 Importer.Import(E->getQualifierLoc()),
6477 Importer.Import(E->getTemplateKeywordLoc()),
6478 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6479}
6480
Peter Szecsice7f3182018-05-07 12:08:27 +00006481Expr *
6482ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6483 DeclarationName Name = Importer.Import(E->getDeclName());
6484 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6485 return nullptr;
6486
6487 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6488 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6489
6490 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6491 Importer.Import(E->getRAngleLoc()));
6492 TemplateArgumentListInfo *ResInfo = nullptr;
6493 if (E->hasExplicitTemplateArgs()) {
6494 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6495 return nullptr;
6496 ResInfo = &ToTAInfo;
6497 }
6498
6499 return DependentScopeDeclRefExpr::Create(
6500 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6501 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6502}
6503
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006504Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6505 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006506 unsigned NumArgs = CE->arg_size();
6507
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006508 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006509 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6510 return nullptr;
6511
6512 return CXXUnresolvedConstructExpr::Create(
6513 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6514 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6515 Importer.Import(CE->getRParenLoc()));
6516}
6517
6518Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006519 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006520 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6521 if (E->getNamingClass() && !NamingClass)
6522 return nullptr;
6523
6524 DeclarationName Name = Importer.Import(E->getName());
6525 if (E->getName() && !Name)
6526 return nullptr;
6527
6528 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6529 // Import additional name location/type info.
6530 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6531
6532 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006533 for (auto *D : E->decls()) {
6534 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006535 ToDecls.addDecl(To);
6536 else
6537 return nullptr;
6538 }
6539
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006540 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006541 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006542 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6543 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006544 return nullptr;
6545 ResInfo = &ToTAInfo;
6546 }
6547
6548 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6549 return UnresolvedLookupExpr::Create(
6550 Importer.getToContext(), NamingClass,
6551 Importer.Import(E->getQualifierLoc()),
6552 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6553 ResInfo, ToDecls.begin(), ToDecls.end());
6554
6555 return UnresolvedLookupExpr::Create(
6556 Importer.getToContext(), NamingClass,
6557 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6558 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6559}
6560
Peter Szecsice7f3182018-05-07 12:08:27 +00006561Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6562 DeclarationName Name = Importer.Import(E->getName());
6563 if (!E->getName().isEmpty() && Name.isEmpty())
6564 return nullptr;
6565 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6566 // Import additional name location/type info.
6567 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6568
6569 QualType BaseType = Importer.Import(E->getType());
6570 if (!E->getType().isNull() && BaseType.isNull())
6571 return nullptr;
6572
6573 UnresolvedSet<8> ToDecls;
6574 for (Decl *D : E->decls()) {
6575 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6576 ToDecls.addDecl(To);
6577 else
6578 return nullptr;
6579 }
6580
6581 TemplateArgumentListInfo ToTAInfo;
6582 TemplateArgumentListInfo *ResInfo = nullptr;
6583 if (E->hasExplicitTemplateArgs()) {
6584 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6585 return nullptr;
6586 ResInfo = &ToTAInfo;
6587 }
6588
6589 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6590 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6591 return nullptr;
6592 }
6593
6594 return UnresolvedMemberExpr::Create(
6595 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6596 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6597 Importer.Import(E->getQualifierLoc()),
6598 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6599 ToDecls.begin(), ToDecls.end());
6600}
6601
Sean Callanan59721b32015-04-28 18:41:46 +00006602Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6603 QualType T = Importer.Import(E->getType());
6604 if (T.isNull())
6605 return nullptr;
6606
6607 Expr *ToCallee = Importer.Import(E->getCallee());
6608 if (!ToCallee && E->getCallee())
6609 return nullptr;
6610
6611 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006612 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006613 if (ImportContainerChecked(E->arguments(), ToArgs))
6614 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006615
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006616 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006617
6618 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6619 ToArgs_Copied[ai] = ToArgs[ai];
6620
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006621 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6622 return new (Importer.getToContext()) CXXOperatorCallExpr(
6623 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6624 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6625 OCE->getFPFeatures());
6626 }
6627
Sean Callanan59721b32015-04-28 18:41:46 +00006628 return new (Importer.getToContext())
6629 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006630 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006631 Importer.Import(E->getRParenLoc()));
6632}
6633
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006634Optional<LambdaCapture>
6635ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6636 VarDecl *Var = nullptr;
6637 if (From.capturesVariable()) {
6638 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6639 if (!Var)
6640 return None;
6641 }
6642
6643 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6644 From.getCaptureKind(), Var,
6645 From.isPackExpansion()
6646 ? Importer.Import(From.getEllipsisLoc())
6647 : SourceLocation());
6648}
6649
6650Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6651 CXXRecordDecl *FromClass = LE->getLambdaClass();
6652 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6653 if (!ToClass)
6654 return nullptr;
6655
6656 // NOTE: lambda classes are created with BeingDefined flag set up.
6657 // It means that ImportDefinition doesn't work for them and we should fill it
6658 // manually.
6659 if (ToClass->isBeingDefined()) {
6660 for (auto FromField : FromClass->fields()) {
6661 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6662 if (!ToField)
6663 return nullptr;
6664 }
6665 }
6666
6667 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6668 Importer.Import(LE->getCallOperator()));
6669 if (!ToCallOp)
6670 return nullptr;
6671
6672 ToClass->completeDefinition();
6673
6674 unsigned NumCaptures = LE->capture_size();
6675 SmallVector<LambdaCapture, 8> Captures;
6676 Captures.reserve(NumCaptures);
6677 for (const auto &FromCapture : LE->captures()) {
6678 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6679 Captures.push_back(*ToCapture);
6680 else
6681 return nullptr;
6682 }
6683
6684 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6685 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6686 return nullptr;
6687
6688 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6689 Importer.Import(LE->getIntroducerRange()),
6690 LE->getCaptureDefault(),
6691 Importer.Import(LE->getCaptureDefaultLoc()),
6692 Captures,
6693 LE->hasExplicitParameters(),
6694 LE->hasExplicitResultType(),
6695 InitCaptures,
6696 Importer.Import(LE->getLocEnd()),
6697 LE->containsUnexpandedParameterPack());
6698}
6699
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006700Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6701 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006702 if (T.isNull())
6703 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006704
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006705 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006706 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006707 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006708
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006709 ASTContext &ToCtx = Importer.getToContext();
6710 InitListExpr *To = new (ToCtx) InitListExpr(
6711 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6712 Exprs, Importer.Import(ILE->getLBraceLoc()));
6713 To->setType(T);
6714
6715 if (ILE->hasArrayFiller()) {
6716 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6717 if (!Filler)
6718 return nullptr;
6719 To->setArrayFiller(Filler);
6720 }
6721
6722 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006723 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006724 if (!ToFD)
6725 return nullptr;
6726 To->setInitializedFieldInUnion(ToFD);
6727 }
6728
6729 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006730 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006731 if (!ToSyntForm)
6732 return nullptr;
6733 To->setSyntacticForm(ToSyntForm);
6734 }
6735
6736 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6737 To->setValueDependent(ILE->isValueDependent());
6738 To->setInstantiationDependent(ILE->isInstantiationDependent());
6739
6740 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006741}
6742
Gabor Marton07b01ff2018-06-29 12:17:34 +00006743Expr *ASTNodeImporter::VisitCXXStdInitializerListExpr(
6744 CXXStdInitializerListExpr *E) {
6745 QualType T = Importer.Import(E->getType());
6746 if (T.isNull())
6747 return nullptr;
6748
6749 Expr *SE = Importer.Import(E->getSubExpr());
6750 if (!SE)
6751 return nullptr;
6752
6753 return new (Importer.getToContext()) CXXStdInitializerListExpr(T, SE);
6754}
6755
Richard Smith30e304e2016-12-14 00:03:17 +00006756Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6757 QualType ToType = Importer.Import(E->getType());
6758 if (ToType.isNull())
6759 return nullptr;
6760
6761 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6762 if (!ToCommon && E->getCommonExpr())
6763 return nullptr;
6764
6765 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6766 if (!ToSubExpr && E->getSubExpr())
6767 return nullptr;
6768
6769 return new (Importer.getToContext())
6770 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6771}
6772
6773Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6774 QualType ToType = Importer.Import(E->getType());
6775 if (ToType.isNull())
6776 return nullptr;
6777 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6778}
6779
Sean Callanandd2c1742016-05-16 20:48:03 +00006780Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006781 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006782 if (!ToField && DIE->getField())
6783 return nullptr;
6784
6785 return CXXDefaultInitExpr::Create(
6786 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6787}
6788
6789Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6790 QualType ToType = Importer.Import(E->getType());
6791 if (ToType.isNull() && !E->getType().isNull())
6792 return nullptr;
6793 ExprValueKind VK = E->getValueKind();
6794 CastKind CK = E->getCastKind();
6795 Expr *ToOp = Importer.Import(E->getSubExpr());
6796 if (!ToOp && E->getSubExpr())
6797 return nullptr;
6798 CXXCastPath BasePath;
6799 if (ImportCastPath(E, BasePath))
6800 return nullptr;
6801 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6802 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6803 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6804 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6805
6806 if (isa<CXXStaticCastExpr>(E)) {
6807 return CXXStaticCastExpr::Create(
6808 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6809 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6810 } else if (isa<CXXDynamicCastExpr>(E)) {
6811 return CXXDynamicCastExpr::Create(
6812 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6813 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6814 } else if (isa<CXXReinterpretCastExpr>(E)) {
6815 return CXXReinterpretCastExpr::Create(
6816 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6817 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6818 } else {
6819 return nullptr;
6820 }
6821}
6822
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006823Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6824 SubstNonTypeTemplateParmExpr *E) {
6825 QualType T = Importer.Import(E->getType());
6826 if (T.isNull())
6827 return nullptr;
6828
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006829 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006830 Importer.Import(E->getParameter()));
6831 if (!Param)
6832 return nullptr;
6833
6834 Expr *Replacement = Importer.Import(E->getReplacement());
6835 if (!Replacement)
6836 return nullptr;
6837
6838 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6839 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6840 Replacement);
6841}
6842
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006843Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6844 QualType ToType = Importer.Import(E->getType());
6845 if (ToType.isNull())
6846 return nullptr;
6847
6848 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6849 if (ImportContainerChecked(E->getArgs(), ToArgs))
6850 return nullptr;
6851
6852 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6853 // Value is always false.
6854 bool ToValue = false;
6855 if (!E->isValueDependent())
6856 ToValue = E->getValue();
6857
6858 return TypeTraitExpr::Create(
6859 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6860 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6861}
6862
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006863Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6864 QualType ToType = Importer.Import(E->getType());
6865 if (ToType.isNull())
6866 return nullptr;
6867
6868 if (E->isTypeOperand()) {
6869 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6870 if (!TSI)
6871 return nullptr;
6872
6873 return new (Importer.getToContext())
6874 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6875 }
6876
6877 Expr *Op = Importer.Import(E->getExprOperand());
6878 if (!Op)
6879 return nullptr;
6880
6881 return new (Importer.getToContext())
6882 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6883}
6884
Lang Hames19e07e12017-06-20 21:06:00 +00006885void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6886 CXXMethodDecl *FromMethod) {
6887 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6888 ToMethod->addOverriddenMethod(
6889 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6890 FromOverriddenMethod))));
6891}
6892
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006893ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006894 ASTContext &FromContext, FileManager &FromFileManager,
6895 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006896 : ToContext(ToContext), FromContext(FromContext),
6897 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6898 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006899 ImportedDecls[FromContext.getTranslationUnitDecl()]
6900 = ToContext.getTranslationUnitDecl();
6901}
6902
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006903ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006904
6905QualType ASTImporter::Import(QualType FromT) {
6906 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006907 return {};
John McCall424cec92011-01-19 06:33:43 +00006908
6909 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006910
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006911 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006912 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6913 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006914 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006915 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006916
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006917 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006918 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006919 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006920 if (ToT.isNull())
6921 return ToT;
6922
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006923 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006924 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006925
John McCall424cec92011-01-19 06:33:43 +00006926 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006927}
6928
Douglas Gregor62d311f2010-02-09 19:21:46 +00006929TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006930 if (!FromTSI)
6931 return FromTSI;
6932
6933 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006934 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006935 QualType T = Import(FromTSI->getType());
6936 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006937 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006938
6939 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006940 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006941}
6942
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006943Attr *ASTImporter::Import(const Attr *FromAttr) {
6944 Attr *ToAttr = FromAttr->clone(ToContext);
6945 ToAttr->setRange(Import(FromAttr->getRange()));
6946 return ToAttr;
6947}
6948
Sean Callanan59721b32015-04-28 18:41:46 +00006949Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6950 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6951 if (Pos != ImportedDecls.end()) {
6952 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00006953 // FIXME: move this call to ImportDeclParts().
Sean Callanan59721b32015-04-28 18:41:46 +00006954 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6955 return ToD;
6956 } else {
6957 return nullptr;
6958 }
6959}
6960
Douglas Gregor62d311f2010-02-09 19:21:46 +00006961Decl *ASTImporter::Import(Decl *FromD) {
6962 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006963 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006964
Douglas Gregord451ea92011-07-29 23:31:30 +00006965 ASTNodeImporter Importer(*this);
6966
Gabor Marton26f72a92018-07-12 09:42:05 +00006967 // Check whether we've already imported this declaration.
6968 Decl *ToD = GetAlreadyImportedOrNull(FromD);
6969 if (ToD) {
6970 // If FromD has some updated flags after last import, apply it
6971 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006972 return ToD;
6973 }
Gabor Marton26f72a92018-07-12 09:42:05 +00006974
6975 // Import the type.
6976 ToD = Importer.Visit(FromD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00006977 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006978 return nullptr;
6979
Gabor Marton26f72a92018-07-12 09:42:05 +00006980 // Notify subclasses.
6981 Imported(FromD, ToD);
6982
Douglas Gregor62d311f2010-02-09 19:21:46 +00006983 return ToD;
6984}
6985
6986DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6987 if (!FromDC)
6988 return FromDC;
6989
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006990 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006991 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006992 return nullptr;
6993
Douglas Gregor2e15c842012-02-01 21:00:38 +00006994 // When we're using a record/enum/Objective-C class/protocol as a context, we
6995 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006996 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6997 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006998 if (ToRecord->isCompleteDefinition()) {
6999 // Do nothing.
7000 } else if (FromRecord->isCompleteDefinition()) {
7001 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
7002 ASTNodeImporter::IDK_Basic);
7003 } else {
7004 CompleteDecl(ToRecord);
7005 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007006 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7007 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007008 if (ToEnum->isCompleteDefinition()) {
7009 // Do nothing.
7010 } else if (FromEnum->isCompleteDefinition()) {
7011 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
7012 ASTNodeImporter::IDK_Basic);
7013 } else {
7014 CompleteDecl(ToEnum);
7015 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007016 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7017 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007018 if (ToClass->getDefinition()) {
7019 // Do nothing.
7020 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
7021 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
7022 ASTNodeImporter::IDK_Basic);
7023 } else {
7024 CompleteDecl(ToClass);
7025 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007026 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7027 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007028 if (ToProto->getDefinition()) {
7029 // Do nothing.
7030 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
7031 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
7032 ASTNodeImporter::IDK_Basic);
7033 } else {
7034 CompleteDecl(ToProto);
7035 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007036 }
7037
7038 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007039}
7040
7041Expr *ASTImporter::Import(Expr *FromE) {
7042 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007043 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007044
7045 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7046}
7047
7048Stmt *ASTImporter::Import(Stmt *FromS) {
7049 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007050 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007051
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007052 // Check whether we've already imported this declaration.
7053 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7054 if (Pos != ImportedStmts.end())
7055 return Pos->second;
7056
7057 // Import the type
7058 ASTNodeImporter Importer(*this);
7059 Stmt *ToS = Importer.Visit(FromS);
7060 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00007061 return nullptr;
7062
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007063 // Record the imported declaration.
7064 ImportedStmts[FromS] = ToS;
7065 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007066}
7067
7068NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7069 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007070 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007071
Douglas Gregor90ebf252011-04-27 16:48:40 +00007072 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7073
7074 switch (FromNNS->getKind()) {
7075 case NestedNameSpecifier::Identifier:
7076 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7077 return NestedNameSpecifier::Create(ToContext, prefix, II);
7078 }
Craig Topper36250ad2014-05-12 05:36:57 +00007079 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007080
7081 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007082 if (auto *NS =
7083 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007084 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7085 }
Craig Topper36250ad2014-05-12 05:36:57 +00007086 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007087
7088 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007089 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007090 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007091 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7092 }
Craig Topper36250ad2014-05-12 05:36:57 +00007093 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007094
7095 case NestedNameSpecifier::Global:
7096 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7097
Nikola Smiljanic67860242014-09-26 00:28:20 +00007098 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007099 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007100 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007101 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7102 }
7103 return nullptr;
7104
Douglas Gregor90ebf252011-04-27 16:48:40 +00007105 case NestedNameSpecifier::TypeSpec:
7106 case NestedNameSpecifier::TypeSpecWithTemplate: {
7107 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7108 if (!T.isNull()) {
7109 bool bTemplate = FromNNS->getKind() ==
7110 NestedNameSpecifier::TypeSpecWithTemplate;
7111 return NestedNameSpecifier::Create(ToContext, prefix,
7112 bTemplate, T.getTypePtr());
7113 }
7114 }
Craig Topper36250ad2014-05-12 05:36:57 +00007115 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007116 }
7117
7118 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007119}
7120
Douglas Gregor14454802011-02-25 02:25:35 +00007121NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007122 // Copied from NestedNameSpecifier mostly.
7123 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7124 NestedNameSpecifierLoc NNS = FromNNS;
7125
7126 // Push each of the nested-name-specifiers's onto a stack for
7127 // serialization in reverse order.
7128 while (NNS) {
7129 NestedNames.push_back(NNS);
7130 NNS = NNS.getPrefix();
7131 }
7132
7133 NestedNameSpecifierLocBuilder Builder;
7134
7135 while (!NestedNames.empty()) {
7136 NNS = NestedNames.pop_back_val();
7137 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7138 if (!Spec)
7139 return NestedNameSpecifierLoc();
7140
7141 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7142 switch (Kind) {
7143 case NestedNameSpecifier::Identifier:
7144 Builder.Extend(getToContext(),
7145 Spec->getAsIdentifier(),
7146 Import(NNS.getLocalBeginLoc()),
7147 Import(NNS.getLocalEndLoc()));
7148 break;
7149
7150 case NestedNameSpecifier::Namespace:
7151 Builder.Extend(getToContext(),
7152 Spec->getAsNamespace(),
7153 Import(NNS.getLocalBeginLoc()),
7154 Import(NNS.getLocalEndLoc()));
7155 break;
7156
7157 case NestedNameSpecifier::NamespaceAlias:
7158 Builder.Extend(getToContext(),
7159 Spec->getAsNamespaceAlias(),
7160 Import(NNS.getLocalBeginLoc()),
7161 Import(NNS.getLocalEndLoc()));
7162 break;
7163
7164 case NestedNameSpecifier::TypeSpec:
7165 case NestedNameSpecifier::TypeSpecWithTemplate: {
7166 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7167 QualType(Spec->getAsType(), 0));
7168 Builder.Extend(getToContext(),
7169 Import(NNS.getLocalBeginLoc()),
7170 TSI->getTypeLoc(),
7171 Import(NNS.getLocalEndLoc()));
7172 break;
7173 }
7174
7175 case NestedNameSpecifier::Global:
7176 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7177 break;
7178
7179 case NestedNameSpecifier::Super: {
7180 SourceRange ToRange = Import(NNS.getSourceRange());
7181 Builder.MakeSuper(getToContext(),
7182 Spec->getAsRecordDecl(),
7183 ToRange.getBegin(),
7184 ToRange.getEnd());
7185 }
7186 }
7187 }
7188
7189 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007190}
7191
Douglas Gregore2e50d332010-12-01 01:36:18 +00007192TemplateName ASTImporter::Import(TemplateName From) {
7193 switch (From.getKind()) {
7194 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007195 if (auto *ToTemplate =
7196 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007197 return TemplateName(ToTemplate);
7198
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007199 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007200
7201 case TemplateName::OverloadedTemplate: {
7202 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7203 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007204 for (auto *I : *FromStorage) {
7205 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007206 ToTemplates.addDecl(To);
7207 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007208 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007209 }
7210 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
7211 ToTemplates.end());
7212 }
7213
7214 case TemplateName::QualifiedTemplate: {
7215 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7216 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7217 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007218 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007219
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007220 if (auto *ToTemplate =
7221 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007222 return ToContext.getQualifiedTemplateName(Qualifier,
7223 QTN->hasTemplateKeyword(),
7224 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007225
7226 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007227 }
7228
7229 case TemplateName::DependentTemplate: {
7230 DependentTemplateName *DTN = From.getAsDependentTemplateName();
7231 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
7232 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007233 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007234
7235 if (DTN->isIdentifier()) {
7236 return ToContext.getDependentTemplateName(Qualifier,
7237 Import(DTN->getIdentifier()));
7238 }
7239
7240 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
7241 }
John McCalld9dfe3a2011-06-30 08:33:18 +00007242
7243 case TemplateName::SubstTemplateTemplateParm: {
7244 SubstTemplateTemplateParmStorage *subst
7245 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007246 auto *param =
7247 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00007248 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007249 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007250
7251 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007252 if (replacement.isNull())
7253 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007254
7255 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7256 }
Douglas Gregor5590be02011-01-15 06:45:20 +00007257
7258 case TemplateName::SubstTemplateTemplateParmPack: {
7259 SubstTemplateTemplateParmPackStorage *SubstPack
7260 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007261 auto *Param =
7262 cast_or_null<TemplateTemplateParmDecl>(
7263 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007264 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007265 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007266
7267 ASTNodeImporter Importer(*this);
7268 TemplateArgument ArgPack
7269 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7270 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007271 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007272
7273 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7274 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007275 }
7276
7277 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007278}
7279
Douglas Gregor62d311f2010-02-09 19:21:46 +00007280SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7281 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007282 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007283
Douglas Gregor811663e2010-02-10 00:15:17 +00007284 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007285
Douglas Gregor811663e2010-02-10 00:15:17 +00007286 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00007287 FileID ToFileID = Import(Decomposed.first);
7288 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007289 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007290 SourceManager &ToSM = ToContext.getSourceManager();
7291 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007292}
7293
7294SourceRange ASTImporter::Import(SourceRange FromRange) {
7295 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7296}
7297
Douglas Gregor811663e2010-02-10 00:15:17 +00007298FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007299 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007300 if (Pos != ImportedFileIDs.end())
7301 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007302
Douglas Gregor811663e2010-02-10 00:15:17 +00007303 SourceManager &FromSM = FromContext.getSourceManager();
7304 SourceManager &ToSM = ToContext.getSourceManager();
7305 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007306
7307 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00007308 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007309 if (FromSLoc.isExpansion()) {
7310 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
7311 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
7312 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
7313 unsigned TokenLen = FromSM.getFileIDSize(FromID);
7314 SourceLocation MLoc;
7315 if (FromEx.isMacroArgExpansion()) {
7316 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
7317 } else {
7318 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
7319 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
7320 FromEx.isExpansionTokenRange());
7321 }
7322 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007323 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007324 // Include location of this file.
7325 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7326
7327 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
7328 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
7329 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7330 // disk again
7331 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7332 // than mmap the files several times.
7333 const FileEntry *Entry =
7334 ToFileManager.getFile(Cache->OrigEntry->getName());
7335 if (!Entry)
7336 return {};
7337 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7338 FromSLoc.getFile().getFileCharacteristic());
7339 } else {
7340 // FIXME: We want to re-use the existing MemoryBuffer!
7341 const llvm::MemoryBuffer *FromBuf =
7342 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
7343 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
7344 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
7345 FromBuf->getBufferIdentifier());
7346 ToID = ToSM.createFileID(std::move(ToBuf),
7347 FromSLoc.getFile().getFileCharacteristic());
7348 }
Douglas Gregor811663e2010-02-10 00:15:17 +00007349 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007350
Sebastian Redl99219f12010-09-30 01:03:06 +00007351 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007352 return ToID;
7353}
7354
Sean Callanandd2c1742016-05-16 20:48:03 +00007355CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7356 Expr *ToExpr = Import(From->getInit());
7357 if (!ToExpr && From->getInit())
7358 return nullptr;
7359
7360 if (From->isBaseInitializer()) {
7361 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7362 if (!ToTInfo && From->getTypeSourceInfo())
7363 return nullptr;
7364
7365 return new (ToContext) CXXCtorInitializer(
7366 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7367 ToExpr, Import(From->getRParenLoc()),
7368 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7369 : SourceLocation());
7370 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007371 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007372 if (!ToField && From->getMember())
7373 return nullptr;
7374
7375 return new (ToContext) CXXCtorInitializer(
7376 ToContext, ToField, Import(From->getMemberLocation()),
7377 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7378 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007379 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007380 Import(From->getIndirectMember()));
7381 if (!ToIField && From->getIndirectMember())
7382 return nullptr;
7383
7384 return new (ToContext) CXXCtorInitializer(
7385 ToContext, ToIField, Import(From->getMemberLocation()),
7386 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7387 } else if (From->isDelegatingInitializer()) {
7388 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7389 if (!ToTInfo && From->getTypeSourceInfo())
7390 return nullptr;
7391
7392 return new (ToContext)
7393 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7394 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007395 } else {
7396 return nullptr;
7397 }
7398}
7399
Aleksei Sidorina693b372016-09-28 10:16:56 +00007400CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7401 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7402 if (Pos != ImportedCXXBaseSpecifiers.end())
7403 return Pos->second;
7404
7405 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7406 Import(BaseSpec->getSourceRange()),
7407 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7408 BaseSpec->getAccessSpecifierAsWritten(),
7409 Import(BaseSpec->getTypeSourceInfo()),
7410 Import(BaseSpec->getEllipsisLoc()));
7411 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7412 return Imported;
7413}
7414
Douglas Gregor0a791672011-01-18 03:11:38 +00007415void ASTImporter::ImportDefinition(Decl *From) {
7416 Decl *To = Import(From);
7417 if (!To)
7418 return;
7419
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007420 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007421 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007422
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007423 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007424 if (!ToRecord->getDefinition()) {
7425 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007426 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007427 return;
7428 }
7429 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007430
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007431 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007432 if (!ToEnum->getDefinition()) {
7433 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007434 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007435 return;
7436 }
7437 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007438
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007439 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007440 if (!ToIFace->getDefinition()) {
7441 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007442 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007443 return;
7444 }
7445 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007446
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007447 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007448 if (!ToProto->getDefinition()) {
7449 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007450 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007451 return;
7452 }
7453 }
7454
Douglas Gregor0a791672011-01-18 03:11:38 +00007455 Importer.ImportDeclContext(FromDC, true);
7456 }
7457}
7458
Douglas Gregor96e578d2010-02-05 17:54:41 +00007459DeclarationName ASTImporter::Import(DeclarationName FromName) {
7460 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007461 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007462
7463 switch (FromName.getNameKind()) {
7464 case DeclarationName::Identifier:
7465 return Import(FromName.getAsIdentifierInfo());
7466
7467 case DeclarationName::ObjCZeroArgSelector:
7468 case DeclarationName::ObjCOneArgSelector:
7469 case DeclarationName::ObjCMultiArgSelector:
7470 return Import(FromName.getObjCSelector());
7471
7472 case DeclarationName::CXXConstructorName: {
7473 QualType T = Import(FromName.getCXXNameType());
7474 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007475 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007476
7477 return ToContext.DeclarationNames.getCXXConstructorName(
7478 ToContext.getCanonicalType(T));
7479 }
7480
7481 case DeclarationName::CXXDestructorName: {
7482 QualType T = Import(FromName.getCXXNameType());
7483 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007484 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007485
7486 return ToContext.DeclarationNames.getCXXDestructorName(
7487 ToContext.getCanonicalType(T));
7488 }
7489
Richard Smith35845152017-02-07 01:37:30 +00007490 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007491 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007492 Import(FromName.getCXXDeductionGuideTemplate()));
7493 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007494 return {};
Richard Smith35845152017-02-07 01:37:30 +00007495 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7496 }
7497
Douglas Gregor96e578d2010-02-05 17:54:41 +00007498 case DeclarationName::CXXConversionFunctionName: {
7499 QualType T = Import(FromName.getCXXNameType());
7500 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007501 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007502
7503 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7504 ToContext.getCanonicalType(T));
7505 }
7506
7507 case DeclarationName::CXXOperatorName:
7508 return ToContext.DeclarationNames.getCXXOperatorName(
7509 FromName.getCXXOverloadedOperator());
7510
7511 case DeclarationName::CXXLiteralOperatorName:
7512 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7513 Import(FromName.getCXXLiteralIdentifier()));
7514
7515 case DeclarationName::CXXUsingDirective:
7516 // FIXME: STATICS!
7517 return DeclarationName::getUsingDirectiveName();
7518 }
7519
David Blaikiee4d798f2012-01-20 21:50:17 +00007520 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007521}
7522
Douglas Gregore2e50d332010-12-01 01:36:18 +00007523IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007524 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007525 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007526
Sean Callananf94ef1d2016-05-14 06:11:19 +00007527 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7528
7529 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7530 ToId->setBuiltinID(FromId->getBuiltinID());
7531
7532 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007533}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007534
Douglas Gregor43f54792010-02-17 02:12:47 +00007535Selector ASTImporter::Import(Selector FromSel) {
7536 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007537 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007538
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007539 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007540 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7541 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7542 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7543 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7544}
7545
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007546DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7547 DeclContext *DC,
7548 unsigned IDNS,
7549 NamedDecl **Decls,
7550 unsigned NumDecls) {
7551 return Name;
7552}
7553
7554DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007555 if (LastDiagFromFrom)
7556 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7557 FromContext.getDiagnostics());
7558 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007559 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007560}
7561
7562DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007563 if (!LastDiagFromFrom)
7564 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7565 ToContext.getDiagnostics());
7566 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007567 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007568}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007569
Douglas Gregor2e15c842012-02-01 21:00:38 +00007570void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007571 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007572 if (!ID->getDefinition())
7573 ID->startDefinition();
7574 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007575 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007576 if (!PD->getDefinition())
7577 PD->startDefinition();
7578 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007579 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007580 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7581 TD->startDefinition();
7582 TD->setCompleteDefinition(true);
7583 }
7584 }
7585 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007586 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007587 }
7588}
7589
Gabor Marton26f72a92018-07-12 09:42:05 +00007590Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
7591 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
7592 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
7593 "Try to import an already imported Decl");
7594 if (Pos != ImportedDecls.end())
7595 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007596 ImportedDecls[From] = To;
7597 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007598}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007599
Douglas Gregordd6006f2012-07-17 21:16:27 +00007600bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7601 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007602 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007603 = ImportedTypes.find(From.getTypePtr());
7604 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7605 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007606
Douglas Gregordd6006f2012-07-17 21:16:27 +00007607 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00007608 getStructuralEquivalenceKind(*this), false,
7609 Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007610 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007611}